yuyuyuyu3

举报
yd_219475889 发表于 2024/12/13 18:58:29 2024/12/13
【摘要】 def createsecuritygroup(name): try: request = CreateSecurityGroupRequest() securityGroupbody = CreateSecurityGroupOption( name=name ) request.body = Create...
def createsecuritygroup(name):
    try:
        request = CreateSecurityGroupRequest()
        securityGroupbody = CreateSecurityGroupOption(
            name=name
        )
        request.body = CreateSecurityGroupRequestBody(
            security_group=securityGroupbody
        )
        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)


def getAllSecurityGroups():
    try:
        request = ListSecurityGroupsRequest()
        response = client.list_security_groups(request)
        print(response)
        return response
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)


def getSecurityGroupIdByName(name):
    allgroup = getAllSecurityGroups()
    dictdata = allgroup.to_dict()["security_groups"]
    for data in dictdata:
        if data['name'] == name:
            return data['id']
    else:
        return 'None'


def deleteSecureGroup(name):
    try:
        request = DeleteSecurityGroupRequest()
        security_group_id = getSecurityGroupIdByName(name)
        if security_group_id != 'None':
            request.security_group_id = security_group_id
            response = client.delete_security_group(request)
            print(response)
        else:
            print('there is no ',name)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
def getSecureGroupByName(name):
    security_group_id = getSecurityGroupIdByName(name)
    if security_group_id != 'None':
        try:
            request = ShowSecurityGroupRequest()
            request.security_group_id = id
            response = client.show_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)

def getAllSecurityGroups():
    try:
        request = ListSecurityGroupsRequest()
        response = client.list_security_groups(request)
        print(response)
        return response
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

def getSecurityGroupIdByName(name):
    allgroup = getAllSecurityGroups()
    dictdata = allgroup.to_dict()["security_groups"]
    for data in dictdata:
        if data['name'] == name:
            return data['id']
    else:
        return 'None'

def addEgressAllRules(SecurityGroupName):
    try:
        request = CreateSecurityGroupRuleRequest()
        id = getSecurityGroupIdByName(SecurityGroupName)
        securityGroupRulebody = CreateSecurityGroupRuleOption(
            security_group_id=id,
            direction="egress"
        )
        request.body = CreateSecurityGroupRuleRequestBody(
            security_group_rule=securityGroupRulebody
        )
        response = client.create_security_group_rule(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
def addTCP22(SecurityGroupName):
    try:
        request = CreateSecurityGroupRuleRequest()
        id = getSecurityGroupIdByName(SecurityGroupName)
        securityGroupRulebody = CreateSecurityGroupRuleOption(
            security_group_id=id,
            direction="ingress",
            protocol="tcp",
            port_range_min=22,
            port_range_max=22
        )
        request.body = CreateSecurityGroupRuleRequestBody(
            security_group_rule=securityGroupRulebody
        )
        response = client.create_security_group_rule(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)
def addICMPAll(SecurityGroupName):
    try:
        request = CreateSecurityGroupRuleRequest()
        id = getSecurityGroupIdByName(SecurityGroupName)
        securityGroupRulebody = CreateSecurityGroupRuleOption(
            security_group_id=id,
            direction="ingress",
            ethertype="IPv4",
            protocol="icmp"
        )
        request.body = CreateSecurityGroupRuleRequestBody(
            security_group_rule=securityGroupRulebody
        )
        response = client.create_security_group_rule(request)
        print(response)
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)

def createservergroup(name):
    try:
        request = CreateServerGroupRequest()
        listPoliciesServerGroup = [
            "anti-affinity"
        ]
        serverGroupbody = CreateServerGroupOption(
            name=name,
            policies=listPoliciesServerGroup
        )
        request.body = CreateServerGroupRequestBody(
            server_group=serverGroupbody
        )
        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)


def getAllServerGroup():
    try:
        request = ListServerGroupsRequest()
        response = client.list_server_groups(request)
        print(response)
        return response
    except exceptions.ClientRequestException as e:
        print(e.status_code)
        print(e.request_id)
        print(e.error_code)
        print(e.error_msg)


def getServerGroupIdByName(name):
    serversGroup = getAllServerGroup()
    dicdata = serversGroup.to_dict()['server_groups']
    for server in dicdata:
        if server['name'] == name:
            return server['id']
    else:
        return 'None'


def deleteServerGroup(name):
    serverid = getServerGroupIdByName(name)
    if serverid != 'None':
        try:
            request = DeleteServerGroupRequest()
            request.server_group_id = serverid
            response = client.delete_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)
    else:
        print('there is no ',name)

def getServerGroupInfo(name):
    serverid = getServerGroupIdByName(name)
    if serverid != 'None':
        try:
            request = ShowServerGroupRequest()
            request.server_group_id = serverid
            response = client.show_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)
    else:
        print('there is no ',name)
【版权声明】本文为华为云社区用户原创内容,未经允许不得转载,如需转载请自行联系原作者进行授权。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

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

全部回复

上滑加载中

设置昵称

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

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

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