AI开发平台MODELARTS-切换Lite Server服务器操作系统:Python封装API方式切换操作系统

时间:2024-11-08 16:58:59

Python封装API方式切换操作系统

以下为BMS使用Python语言通过API方式切换操作系统的示例代码。

# -*- coding: UTF-8 -*-

import requests
import json
import time
import requests.packages.urllib3.exceptions
from urllib3.exceptions import InsecureRequestWarning

requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
class ServerOperation(object):

    ################################  IAM 认证API#################################################

    def __init__(self, account, password, region_name, username=None, project_id=None):
        """
        :param username:  if IAM user,here is small user, else  big user
        :param account: account big  big user
        :param password: account
        :param region_name:
        """
        self.account = account
        self.username = username
        self.password = password
        self.region_name = region_name
        self.project_id = project_id
        self.ma_endpoint = "https://modelarts.{}.myhuaweicloud.com".format(region_name)
        self.service_endpoint = "https://bms.{}.myhuaweicloud.com".format(region_name)
        self.iam_endpoint = "https://iam.{}.myhuaweicloud.com".format(region_name)
        self.headers = {"Content-Type": "application/json",
                        "X-Auth-Token": self.get_project_token_by_account(self.iam_endpoint)}

    def get_project_token_by_account(self, iam_endpoint):
        body = {
            "auth": {
                "identity": {
                    "methods": [
                        "password"
                    ],
                    "password": {
                        "user": {
                            "name": self.username if self.username else self.account,
                            "password": self.password,
                            "domain": {
                                "name": self.account
                            }
                        }
                    }
                },
                "scope": {
                    "project": {
                        "name": self.region_name
                    }
                }
            }
        }
        headers = {
            "Content-Type": "application/json"
        }
        import json
        url = iam_endpoint + "/v3/auth/tokens"
        response = requests.post(url, headers=headers, data=json.dumps(body), verify=True)
        token = (response.headers['X-Subject-Token'])
        return token
    def change_os(self, server_id):
        url = "{}/v1/{}/baremetalservers/{}/changeos".format(self.service_endpoint, self.project_id, server_id)
        print(url)
        body = {
            "os-change": {
                "adminpass": "@Server",
                "imageid": "40d88eea-6e41-418a-ad6c-c177fe1876b8"
            }
        }
        response = requests.post(url, headers=self.headers, data=json.dumps(body), verify=False)
        print(json.dumps(response.json(), indent=1))
        return response.json()

if __name__ == '__main__':
    # 调用API前置准备,初始化认证鉴权信息
    server = ServerOperation(username="xxx",
                             account="xxx",
                             password="xxx",
                             project_id="xxx",
                             region_name="cn-north-4")

    server.change_os(server_id="0c84bb62-35bd-4e1c-ba08-a3a686bc5097")
support.huaweicloud.com/usermanual-server-modelarts/usermanual-server-0020.html