Home > Storage > PowerFlex > White Papers > PowerFlex REST API Introduction > Create a snapshot policy
This use case demonstrates the steps required to create a snapshot policy and add a volume to the policy.
We need to POST to the /api/types/Volume/instances/action/queryIdByKey endpoint and include the volume name in the payload. A successful response returns the volume id. Here we have defined a function called getVolID. The input to the function is the name of the volume. The output returned by the function is the volume ID.
import requests
import getpass
#Import our PFxM Authentication function
from pfauth import authenticate
import json
import urllib3
#Get volume id
def getVolID(pfxm, accessToken, volname):
#Get volume id from volume name
url = f'https://{pfxm} /api/types/Volume/instances/action/queryIdByKey'
payload = json.dumps({ "name": f"{volname}"
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
}
response = requests.post(url, headers=headers, data=payload, verify=False)
volid = response.text
volid = volid.replace('"','')
print(f'The id for volume {volname} is {volid}')
return volid
The create snapshot policy function takes the snapshot policy name as input and returns the snapshot policy id. The snapshot policy id is required when adding the volume.
#POST REST API call. Create an hourly snapshot policy. Retain 24 snapshots in a day,
# 7 daily snapshots per week, and 4 weekly snapshots per month.
def createPolicy(pfxm, accessToken, policyName):
url = f'https://{pfxm}/api/types/SnapshotPolicy/instances'
payload = json.dumps({
"name": f"{policyName}",
"numOfRetainedSnapshotsPerLevel": ['24','7','4'],
"autoSnapshotCreationCadenceInMin": "60",
"secureSnapshots": "FALSE",
"snapshotAccessMode": "ReadOnly"
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
}
response = requests.post(url, headers=headers, data=payload, verify=False)
print(f'API response status code: {response.status_code}')
policyID = response.text
json_object = json.loads(response.text)
policyID = str(json_object["id"])
return policyID
The last function takes the volume ID and the policy ID as input and returns the HTTP code.
def addVoltoPolicy(pfxm, accesssToken, volid,policyID):
url = f'https://{pfxm}/api/instances/SnapshotPolicy::{policyID}/action/addSourceVolumeToSnapshotPolicy'
payload = json.dumps({
'sourceVolumeId': f'{volid}'
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
}
response = requests.post(url, headers=headers, data=payload, verify=False)
print(f'API response status code: {response.status_code}')
return response
The main section contains the input variables for the volume name and policy name, followed by the calls to each function.
#Prompt user for PowerFlex Manager username and password.
username = input('Enter PowerFlex Manager username:')
password = getpass.getpass('Enter password:')
#Pass variables to the authenticate() function.
accessToken = authenticate(username, password)
#PFxM host
pfxm = 'pfmp.powerflex.lab'
#Name of volume and policy
volname = 'apivol01'
policyName = 'Hourly1'
#Call functions
volid = getVolID(volname)
policyID = createPolicy(policyName)
addvol = addVoltoPolicy(volid, policyID)
#Print HTTP response code
print(f'{addvol}')