笔记2

举报
幻想乡的幻想 发表于 2021/10/09 17:50:13 2021/10/09
【摘要】 ECS# coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkcore.exceptions import exceptionsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfro...

ECS

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
from huaweicloudsdkecs.v2 import *

if __name__ == "__main__":
    ak = "WFFOEG4DGTIPPHRTOKHS"
    sk = "IiLAlVSsB8nWKJC1cAmaUn3oZ4CtXZMxHQA31189"

    credentials = BasicCredentials(ak, sk) \

    client = EcsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(EcsRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = CreatePostPaidServersRequest()
        rootVolumePostPaidServerRootVolume = PostPaidServerRootVolume(
            volumetype="SAS",
            size=40
        )
        listPostPaidServerNicNicsServer = [
            PostPaidServerNic(
                subnet_id="ce9470b4-7ebc-4707-8be3-827556be668f"
            )
        ]
        serverPostPaidServer = PostPaidServer(
            admin_pass="16Wl123456",
            availability_zone="cn-north-4a",
            count=1,
            flavor_ref="t6.small.1",
            image_ref="cf58a404-7c6f-4cee-991a-62f6632148e0",
            name="test",
            nics=listPostPaidServerNicNicsServer,
            root_volume=rootVolumePostPaidServerRootVolume,
            vpcid="8db66f0d-e5e9-4e4d-a953-5ffca2396ea0"
        )
        request.body = CreatePostPaidServersRequestBody(
            server=serverPostPaidServer
        )
        response = client.create_post_paid_servers(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

安全组

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkvpc.v3.region.vpc_region import VpcRegion
from huaweicloudsdkvpc.v3 import *
import json
if __name__ == "__main__":
    ak = "WFFOEG4DGTIPPHRTOKHS"
    sk = "IiLAlVSsB8nWKJC1cAmaUn3oZ4CtXZMxHQA31189"

    credentials = BasicCredentials(ak, sk) \

    client = VpcClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(VpcRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = ListSecurityGroupsRequest()
        response = client.list_security_groups(request)
        security_groups = json.loads(response.__str__())['security_groups']
        for security_group in security_groups :
            if security_group['name']  == 'sd_security_group' :
                request = DeleteSecurityGroupRequest()
                request.security_group_id = security_group['id']
                response = client.delete_security_group(request)
                print(security_group)
        request = CreateSecurityGroupRequest()
        securityGroupCreateSecurityGroupOption = CreateSecurityGroupOption(
            name="sd_security_group"
        )
        request.body = CreateSecurityGroupRequestBody(
            security_group=securityGroupCreateSecurityGroupOption
        )
        response = client.create_security_group(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

可用区

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
from huaweicloudsdkecs.v2 import *
import json

if __name__ == "__main__":
    ak = "WFFOEG4DGTIPPHRTOKHS"
    sk = "IiLAlVSsB8nWKJC1cAmaUn3oZ4CtXZMxHQA31189"

    credentials = BasicCredentials(ak, sk) \

    client = EcsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(EcsRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = NovaListAvailabilityZonesRequest()
        response = client.nova_list_availability_zones(request)
        az = json.loads(response.__str__())['availabilityZoneInfo']
        for azname in az :
            print(azname['zoneName'])
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg

服务器组

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
from huaweicloudsdkecs.v2 import *
import json
if __name__ == "__main__":
    ak = "WFFOEG4DGTIPPHRTOKHS"
    sk = "IiLAlVSsB8nWKJC1cAmaUn3oZ4CtXZMxHQA31189"

    credentials = BasicCredentials(ak, sk) \

    client = EcsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(EcsRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = ListServerGroupsRequest()
        response = client.list_server_groups(request)
        server_groups = json.loads(response.__str__())['server_groups']
        for server_group in server_groups :
            if server_group['name'] == 'sd_server_group' :
                print(server_group)
                request = DeleteServerGroupRequest()
                request.server_group_id = server_group['id']
                response = client.delete_server_group(request)
        request = CreateServerGroupRequest()
        listCreateServerGroupOptionPoliciesServerGroup = [
            "anti-affinity"
        ]
        serverGroupCreateServerGroupOption = CreateServerGroupOption(
            name="sd_server_group",
            policies=listCreateServerGroupOptionPoliciesServerGroup
        )
        request.body = CreateServerGroupRequestBody(
            server_group=serverGroupCreateServerGroupOption
        )
        response = client.create_server_group(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

镜像

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkims.v2.region.ims_region import ImsRegion
from huaweicloudsdkims.v2 import *
import json

if __name__ == "__main__":
    ak = "WFFOEG4DGTIPPHRTOKHS"
    sk = "IiLAlVSsB8nWKJC1cAmaUn3oZ4CtXZMxHQA31189"

    credentials = BasicCredentials(ak, sk) \

    client = ImsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(ImsRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = ListImagesRequest()
        request.imagetype = "gold"
        request.os_bit = "64"
        request.os_type = "Linux"
        request.platform = "CentOS"
        request.architecture = "x86"
        request.os_version = "CentOS 7.5 64bit"
        response = client.list_images(request)
        images = json.loads(response.__str__())['images']
        for image in images :
            if image['name'] == 'CentOS 7.5 64bit' :
                print(image['id'])
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

实例类型

# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
from huaweicloudsdkecs.v2 import *
import json
if __name__ == "__main__":
    ak = "WFFOEG4DGTIPPHRTOKHS"
    sk = "IiLAlVSsB8nWKJC1cAmaUn3oZ4CtXZMxHQA31189"
    
    credentials = BasicCredentials(ak, sk) \

    client = EcsClient.new_builder() \
        .with_credentials(credentials) \
        .with_region(EcsRegion.value_of("cn-north-4")) \
        .build()

    try:
        request = ListFlavorsRequest()
        response = client.list_flavors(request)
        flavors = json.loads(response.__str__())['flavors']
        for flavor in flavors :
            if flavor['id'] == 'c6.large.2' :
                print('Name: '+flavor['name'])
                print('Ram: '+flavor['ram'].__str__())
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。