skd——server

举报
玩个球球 发表于 2024/03/06 08:36:55 2024/03/06
【摘要】 # coding: utf-8from huaweicloudsdkcore.auth.credentials import BasicCredentialsfrom huaweicloudsdkecs.v2.region.ecs_region import EcsRegionfrom huaweicloudsdkcore.exceptions import exceptionsfrom h...
# coding: utf-8

from huaweicloudsdkcore.auth.credentials import BasicCredentials
from huaweicloudsdkecs.v2.region.ecs_region import EcsRegion
from huaweicloudsdkcore.exceptions import exceptions
from huaweicloudsdkecs.v2 import *
import json,yaml
import argparse
import time

def get_token():
    ak = ""
    sk = ""

    credentials = BasicCredentials(ak, sk) \

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

class server:
    def __init__(self,client):
        self.client=client
    def get__name_id(self,name):
        try:
            request = ListServersDetailsRequest()
            response = self.client.list_servers_details(request)
            for i in response.servers:
                if i.name == name:
                    return  i.id
        except:
            pass
           
    def get(self,name):
        while True:
            try:
                id=self.get__name_id(name)
                request = ShowServerRequest()
                request.server_id = id
                response = self.client.show_server(request)
                if response.server.status == "ACTIVE":
                    return response
            except:
                pass
    def get_list(self,name):
        request = ListServersDetailsRequest()
        response = self.client.list_servers_details(request)
        for i in response.servers:
            if i.name == name:
                return  str(i)
    def create(self,name_service,image_id):
        client=self.client
        request = CreateServersRequest()
   
        listMetadataServer = {
            "op_svc_userid": "91e09f7da8534301b31783d65d5127ef",
            "admin_pass": "",
            "__support_agent_list": "hss,ces",
        }
        extendparamServer = PrePaidServerExtendParam(
            charging_mode="0",
            region_id="cn-north-4"
        )
        listSecurityGroupsServer = [
            PrePaidServerSecurityGroup(
                id="f9303b1a-cc14-447b-addf-9f3aa5a843dc"
            )
        ]
        extendparamRootVolume = PrePaidServerRootVolumeExtendParam(
            resource_spec_code="GPSSD",
            resource_type="3"
        )
        rootVolumeServer = PrePaidServerRootVolume(
            volumetype="GPSSD",
            size=50,
            extendparam=extendparamRootVolume,
            hwpassthrough=True
        )
        extendparamEip = PrePaidServerEipExtendParam(
            charging_mode="postPaid"
        )
        bandwidthEip = PrePaidServerEipBandwidth(
            size=5,
            sharetype="PER",
            chargemode="traffic"
        )
        eipPublicip = PrePaidServerEip(
            iptype="5_bgp",
            bandwidth=bandwidthEip,
            extendparam=extendparamEip
        )
        publicipServer = PrePaidServerPublicip(
            eip=eipPublicip
        )
        listNicsServer = [
            PrePaidServerNic(
                subnet_id="180379a2-a7ed-485f-8fae-0088642381e7",
                ip_address=""
            )
        ]
        serverbody = PrePaidServer(
            image_ref= image_id,
            flavor_ref="c7.large.2",
            name=name_service,
            user_data="IyEvYmluL2Jhc2gKZWNobyAncm9vdDokNiQyQUF2SUhPayRjV2NZL3RlZTRlRGZaaFQwa0hrQnQ5NFpMb2o5L0dCLlUxU1hzcHh3WFAzNTRmSGpxM2xZYTU0a2N5enhwbk1BSUlta3lxeWR5QWZFeC9hU2Nnb3ZMLicgfCBjaHBhc3N3ZCAtZTs=",
            vpcid="59affc5d-aa13-4828-af32-f00c59a1bfd6",
            nics=listNicsServer,
            publicip=publicipServer,
            count=1,
            is_auto_rename=False,
            root_volume=rootVolumeServer,
            security_groups=listSecurityGroupsServer,
            availability_zone="",
            batch_create_in_multi_az=False,
            extendparam=extendparamServer,
            metadata=listMetadataServer,
            description=""
        )
        request.body = CreateServersRequestBody(
            server=serverbody
        )
        response = client.create_servers(request)
        time.sleep(4)
        g=self.get(name=name_service)
        print(g)

           
    def gets(self):
        request = ListServersDetailsRequest()
        response = self.client.list_servers_details(request)
        return response
       

   
    def delete(self,name):
        id=self.get__name_id(name)
        request = DeleteServersRequest()
        listServersbody = [
            ServerId(
                id=id
            )
        ]
        request.body = DeleteServersRequestBody(
            servers=listServersbody
        )
        response = self.client.delete_servers(request)
        print(response)
       
def parses(parse):
    client=get_token()
    a=server(client)
   
    parse.add_argument('command',type=str)
    parse.add_argument('-n','--name',type=str)
    parse.add_argument('-o','--output',type=str)
    parse.add_argument('-i','--input',type=str)
    args=parse.parse_args()
    if args.command :
        if args.command == 'getall':
            gs=a.gets()
            with open(args.output,'w') as f:
                yaml.dump(gs,f)
        elif args.command == 'get':
            g=a.get_list(args.name)
            print(type(g))
            with open(args.output,'w') as f:
                f.write(g)
        elif args.command == 'create':
            i_json=json.loads(args.input)
            a.create(i_json['name'],i_json['imagename'])
        elif args.command == "delete":
            a.delete(args.name)
           
if __name__ == "__main__":
    parse=argparse.ArgumentParser()
    parses(parse)
    "02a17486-1214-4e42-8da7-7d200cac585e"
    "02a17486-1214-4e42-8da7-7d200cac585e"
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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