Got it

Use Python to invoke OceanStor DJ/DME REST APIs to automatically allocate resources

Latest reply: Sep 4, 2020 12:52:03 337 2 3 0 0

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.

Very interesting
View more
  • x
  • convention:

Thank you for sharing, interesting
View more
  • x
  • convention:

Comment

You need to log in to comment to the post Login | Register
Comment

Notice: To protect the legitimate rights and interests of you, the community, and third parties, do not release content that may bring legal risks to all parties, including but are not limited to the following:
  • Politically sensitive content
  • Content concerning pornography, gambling, and drug abuse
  • Content that may disclose or infringe upon others ' commercial secrets, intellectual properties, including trade marks, copyrights, and patents, and personal privacy
Do not share your account and password with others. All operations performed using your account will be regarded as your own actions and all consequences arising therefrom will be borne by you. For details, see " User Agreement."

My Followers

Login and enjoy all the member benefits

Login

Block
Are you sure to block this user?
Users on your blacklist cannot comment on your post,cannot mention you, cannot send you private messages.
Reminder
Please bind your phone number to obtain invitation bonus.