Hello all,
The following case is an example of using Python to invoke OceanStor DJ/DME REST APIs to automatically divide resources.
Environment preparation:
OceanStor DME 1.0.RC1
Python 3.8.5
The requests library must be installed for Python.
Python script is as follows:
import http
import requests
import json
import ssl
import time
import urllib3
#Ignore the certificate warning
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
#the Oceanstor DME IP and port info
host="192.168.40.24:26335"
#the base of Oceanstor DME REST API
url="https://"+host+"/rest"
#the Third-Party user and password defined on Oceanstor DME
username="root_rest"
password="Huawei@123"
#the X-Auth-Token value
token=""
sla_id=""
# the volume name going to create
volume_name="api_vol_0"
volume_id=""
# the host name on OceanStor DME.
host_name="esxi_host_1"
host_id=""
header={
"Accept":"application/json",
"Accept-Charset":"utf8",
"Content-Type":"application/json",
"Host":host,
"X-Auth-Token":token
}
#USER LOGIN
def auth():
payload = {"grantType":"password","userName":username,"value":password}
resp = requests.put(url=url+"/plat/smapp/v1/sessions",headers=header,data=json.dumps(payload),verify=False)
global token
token = resp.json()["accessSession"]
header["X-Auth-Token"]=token
def getSLA():
payload = {}
resp = requests.get(url=url+"/service-policy/v1/service-levels",headers=header,data=json.dumps(payload),verify=False)
global sla_id
sla_id = resp.json()["service-levels"][1]["id"]
print("sla id is "+sla_id)
def createVolume():
payload = {"volumes":[{"name":volume_name,"capacity":10,"count":1}],"service_level_id":sla_id}
resp = requests.post(url=url+"/blockservice/v1/volumes",headers=header,data=json.dumps(payload),verify=False)
task_id = resp.json()["task_id"]
print("volume creation task id is "+task_id)
time.sleep(5)
def getVolume():
payload = {}
resp = requests.get(url=url+"/blockservice/v1/volumes?name="+volume_name,headers=header,data=json.dumps(payload),verify=False)
global volume_id
#print(resp.text)
volume_id = resp.json()["volumes"][0]["id"]
print("volume id is "+volume_id)
def getHost():
payload = {"name":host_name}
resp = requests.post(url=url+"/hostmgmt/v1/hosts/summary",headers=header,data=json.dumps(payload),verify=False)
global host_id
#print(resp.text)
host_id = resp.json()["hosts"][0]["id"]
print("host id is "+host_id)
def createMap():
payload = {"volume_ids":[volume_id],"host_id":host_id}
resp = requests.post(url=url+"/blockservice/v1/volumes/host-mapping",headers=header,data=json.dumps(payload),verify=False)
print(resp.text)
task_id = resp.json()["task_id"]
print("mapping_view creation task id is "+task_id)
if __name__ == "__main__":
auth()
getSLA()
createVolume()
getVolume()
getHost()
createMap()
Thank you.
