PowerFlex PowerAPI
(PowerFlex REST API Introduction)
Mar 2023
1
Table Of Contents
Use cases
Add file system tree quota
File system snapshot rule and protection policy
PowerFlex PowerAPI
Use cases
The following are examples of using PowerFlex PowerAPI.
Add file system tree quota
Create a snapshot rule and protection policy
Add file system tree quota
This use case demonstrates the steps required to add a tree quota to a NAS file
system.
Get file system id
We can obtain the file system id using the /rest/v1/file-systems endpoint. We need to
include a select statement and select the file system id. This example assumes that
there is a single file system and that quotas have already been enabled on the file
system.
#Import modules
import json
import requests
import urllib3
from pfauth import authenticate
def getFsId(pfxm, accessToken):
#Get Sfile system id
url = f'https://{pfxm}/rest/v1/file-systems?select=id'
payload = {}
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
PowerFlex PowerAPI
'Authorization': f'Bearer {accessToken}'
response = requests.get(url, headers=headers, data=payload, verify=False)
responseJson = response.json()
responseStr = json.dumps(responseJson, indent=2)
jsonObject = json.loads(responseStr)
fsid = (jsonObject[0]['id'])
return fsid
Create tree quota
This function creates the file system tree quota. We need the following parameters
as input to our function.
The identifier of the target file system
The file system path
Enforcement of user quotas
The quota hard limit
The quota soft limit
The grace period
def createTreequota(pfxm, accessToken, fsid, path, userQuota, treeHardlimit,
treeSoftlimit, gracePeriod):
url = f'https://{pfxm}/rest/v1/file-tree-quotas'
payload = json.dumps({
"file_system_id": f"{fsid}",
"path": f"{path}",
"is_user_quotas_enforced": userQuota,
"hard_limit": treeHardlimit ,
"soft_limit": treeSoftlimit,
"grace_period": gracePeriod
PowerFlex PowerAPI
})
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
response = requests.post(url, headers = headers, data = payload, verify = False)
return response
Main program
The main program sets the variables and calls each function.
#PFxM host
pfxm = 'pfmp.powerflex.lab'
#PFxM user
username = 'admin'
password = 'P@ssword123'
#Pass variables to the authenticate() function.
accessToken = authenticate(username, password)
#Get the id of the file system
fsid = getFsId(pfxm, accessToken)
path = '/share1'
userQuota = False
treeHardlimit = 314572800
treeSoftlimit = 209715200
PowerFlex PowerAPI
gracePeriod = 604800
treeQuota = createTreequota(pfxm, accessToken, fsid, path, userQuota,
treeHardlimit, treeSoftlimit, gracePeriod)
File system snapshot rule and protection policy
This use case demonstrates how to create a snapshot rule and a snapshot policy
consisting of multiple rules. For example, we can create a rule that generates a
snapshot once per day, and a second that creates a snapshot every week. When
creating the snapshot policy, we include the snapshot rule identifiers for the two
snapshots.
We need the following information to create each snapshot rule:
Snapshot rule name
Days to create a snapshot
Frequency/Start Time
Retention
File snapshot access type
Create file system snapshot rule
The parameters required for the json payload will vary based on how the snapshot
rule is defined. This example shows the creation of two snapshot rules. The first rule
definition is for daily snapshots; the second rule definition is for weekly snapshots.
#Import modules
import json
import requests
import urllib3
from pfauth import authenticate
#Create snapshot rule code block
def createSnapshotrule(pfxm, accessToken, payload):
#Create snapshot rule
url = f'https://{pfxm}/rest/v1/nas-snapshot-rules'
PowerFlex PowerAPI
payload
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
response = requests.post(url, headers = headers, data = payload, verify = False)
responseJson = response.json()
ruleid = (responseJson['id'])
return ruleid
#PFxM host
pfxm = 'pfmp.powerflex.lab'
#PFxM user
username = 'admin'
password = 'P@ssword123'
#Pass variables to the authenticate() function.
accessToken = authenticate(username, password)
#Daily file system snapshot
ruleName = "Daily"
interval = "ONE_DAY"
retention = 168
ruleType = "PROTOCOL_READ_ONLY"
payload = json.dumps({
"name": f"{ruleName}",
PowerFlex PowerAPI
"interval": f"{interval}",
"retention": retention,
"type": f"{ruleType}"
})
#Create Daily snapshot rule
ruleid1 = createSnapshotrule(pfxm, accessToken, payload)
#Weekly file system snapshot
ruleName = "Weekly"
timeDay = "05:00"
daysWeek = "SATURDAY"
retention = 168
ruleType = "PROTOCOL_READ_ONLY"
payload = json.dumps({
"name": f"{ruleName}",
"interval": None,
"time_of_day": f"{timeDay}",
"days_of_week": [f"{daysWeek}"],
"retention": retention,
"type": f"{ruleType}"
})
#Create Weekly snapshot rule
ruleid2 = createSnapshotrule(pfxm, accessToken, payload)
Create snapshot policy
PowerFlex PowerAPI
We now need to create the snapshot policy and add the two rules we created in the
previous step.
#File System snapshot policy code block
def createPolicy(pfxm, accessToken, payload):
#Create snapshot rule
url = f'https://{pfxm}/rest/v1/nas-protection-policies'
payload
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
response = requests.post(url, headers = headers, data = payload, verify = False)
responseJson = response.json()
policyid = (responseJson['id'])
return policyid
policyName = "ProdFS"
#Snapshot policy payload
payload = json.dumps({
"name": f"{policyName}",
"snapshot_rule_ids": [
f"{ruleid1}",
f"{ruleid2}"
PowerFlex PowerAPI
})
Assign policy to file system
Now we can assign the file system snapshot policy to a NAS file system.
#Assign protection policy to file system code block
def setPolicy(pfxm, accessToken, payload):
#Set protection policy
url = f'https://{pfxm}/rest/v1/file-systems/63b499a8-598a-e997-d311-
ea32dfd5becd/set-protection-policy'
payload
headers = {
'Content-Type': 'application/json',
'Accept': 'application/json',
'Authorization': f'Bearer {accessToken}'
response = requests.post(url, headers = headers, data = payload, verify = False)
print (f"The response is {response.text}")
responseJson = response.json()
policyid = (responseJson['id'])
return assignPolicy
#Payload for assigning protection policy to file system
payload = json.dumps({
"protection_policy_id": f"{prodfsPolicy}"
})
#Assign the protection policy
10
PowerFlex PowerAPI
assignPolicy = setPolicy(pfxm, accessToken, payload)
11
PowerFlex PowerAPI