openstack api 操作 flavor

1585364631
2023-02-09 / 0 评论 / 163 阅读 / 正在检测是否收录...

通过 openstack api 的方式操作 flavor

获取flavor列表

import json, requests

url = "http://172.16.1.151:5000/v3/auth/tokens"
body = {
  "auth": {
    "identity": {
      "methods": [
        "password"
      ],
      "password": {
        "user": {
          "domain": {
            "name": "demo"
          },
          "name": "admin",
          "password": "000000"
        }
      }
    },
    "scope": {
      "project": {
        "domain": {
          "name": "demo"
        },
        "name": "admin"
      }
    }
  }
}

headers = {
    "Content-Type": "application/json",
}

token = requests.post(url, data=json.dumps(body), headers=headers).headers['X-Subject-Token']

flavors = requests.get('http://controller:8774/v2.1/flavors', headers={'X-Auth-Token': token})
json_flavors = json.loads(flavors.text)
for i in json_flavors['flavors']:
    print(i)

创建新的flavor

import json
import requests

url = "http://172.16.1.151:5000/v3/auth/tokens"

body = {
  "auth": {
    "identity": {
      "methods": [
        "password"
      ],
      "password": {
        "user": {
          "domain": {
            "name": "demo"
          },
          "name": "admin",
          "password": "000000"
        }
      }
    },
    "scope": {
      "project": {
        "domain": {
          "name": "demo"
        },
        "name": "admin"
      }
    }
  }
}
headers = {
    "Content-Type": "application/json",
}

token = requests.post(url, data=json.dumps(body), headers=headers).headers['X-Subject-Token']


# 创建云主机类型
def create_flavor(id, vcpus, ram, disk, name):
    headers = {
        "Content-Type": "application/json",
        "X-Auth-Token": token
    }
    body = {
        "flavor": {
            "name": name,
            "ram": ram,
            "vcpus": vcpus,
            "disk": disk,
            "id": id
        }
    }
    return requests.post('http://172.16.1.151:8774/v2.1/flavors', data=json.dumps(body), headers=headers)


flavor = create_flavor(id=100, vcpus=1, ram=1024, disk=10, name='api-create')
print(flavor.text)

删除flavor

import requests,json
url = r'http://172.16.1.151:5000/v3/auth/tokens'
headers = {
    "context-type": "application/json"
}
body = {
  "auth": {
    "identity": {
      "methods": [
        "password"
      ],
      "password": {
        "user": {
          "domain": {
            "name": "demo"
          },
          "name": "admin",
          "password": "000000"
        }
      }
    },
    "scope": {
      "project": {
        "domain": {
          "name": "demo"
        },
        "name": "admin"
      }
    }
  }
}

token = requests.post(url, data=json.dumps(body), headers=headers).headers['X-Subject-Token']

headers = {
    "context-type": "application/json",
    "x-auth-token": token
}

#删除云主机类型
flavor = requests.delete('http://172.16.1.151:8774/v2.1/flavors/' + '100',headers=headers)
print(flavor.text)
0

评论 (0)

取消