当前位置: 首页>>代码示例>>Python>>正文


Python VApp.get_all_networks方法代码示例

本文整理汇总了Python中pyvcloud.vcd.vapp.VApp.get_all_networks方法的典型用法代码示例。如果您正苦于以下问题:Python VApp.get_all_networks方法的具体用法?Python VApp.get_all_networks怎么用?Python VApp.get_all_networks使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在pyvcloud.vcd.vapp.VApp的用法示例。


在下文中一共展示了VApp.get_all_networks方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: list_networks

# 需要导入模块: from pyvcloud.vcd.vapp import VApp [as 别名]
# 或者: from pyvcloud.vcd.vapp.VApp import get_all_networks [as 别名]
 def list_networks(self):
     vapp_name = self.params.get('vapp_name')
     vapp_resource = self.vdc.get_vapp(vapp_name)
     vapp = VApp(self.client, name=vapp_name, resource=vapp_resource)
     response = dict()
     response['msg'] = []
     for network in vapp.get_all_networks():
         response['msg'].append(network.get('{'+NSMAP['ovf']+'}name'))
     return response
开发者ID:equinix-ms,项目名称:ansible-module-vcloud-director,代码行数:11,代码来源:vcd_vapp.py

示例2: VappNetwork

# 需要导入模块: from pyvcloud.vcd.vapp import VApp [as 别名]
# 或者: from pyvcloud.vcd.vapp.VApp import get_all_networks [as 别名]
class VappNetwork(VcdAnsibleModule):
    def __init__(self, **kwargs):
        super(VappNetwork, self).__init__(**kwargs)
        vapp_resource = self.get_resource()
        self.vapp = VApp(self.client, resource=vapp_resource)

    def manage_states(self):
        state = self.params.get('state')
        if state == "present":
            return self.add_network()

        if state == "absent":
            return self.delete_network()

    def get_resource(self):
        vapp = self.params.get('vapp')
        vdc = self.params.get('vdc')
        org_resource = Org(self.client, resource=self.client.get_org())
        vdc_resource = VDC(self.client, resource=org_resource.get_vdc(vdc))
        vapp_resource_href = vdc_resource.get_resource_href(name=vapp, entity_type=EntityType.VAPP)
        vapp_resource = self.client.get_resource(vapp_resource_href)

        return vapp_resource

    def get_network(self):
        network_name = self.params.get('network')
        networks = self.vapp.get_all_networks()
        for network in networks:
            if network.get('{'+NSMAP['ovf']+'}name') == network_name:
                return network
        raise EntityNotFoundException('Can\'t find the specified vApp network')

    def delete_network(self):
        network_name = self.params.get('network')
        response = dict()
        response['changed'] = False

        try:
            self.get_network()
        except EntityNotFoundException:
            response['warnings'] = 'Vapp Network {} is not present.'.format(network_name)
        else:
            network_config_section = self.vapp.resource.NetworkConfigSection
            for network_config in network_config_section.NetworkConfig:
                if network_config.get('networkName') == network_name:
                    network_config_section.remove(network_config)
            delete_network_task = self.client.put_linked_resource(
                self.vapp.resource.NetworkConfigSection, RelationType.EDIT,
                EntityType.NETWORK_CONFIG_SECTION.value,
                network_config_section)
            self.execute_task(delete_network_task)
            response['msg'] = 'Vapp Network {} has been deleted.'.format(network_name)
            response['changed'] = True
            
        return response

    def add_network(self):
        network_name = self.params.get('network')
        fence_mode = self.params.get('fence_mode')
        parent_network = self.params.get('parent_network')
        ip_scope = self.params.get('ip_scope')

        response = dict()
        response['changed'] = False

        try:
            self.get_network()
        except EntityNotFoundException:
            network_config_section = self.vapp.resource.NetworkConfigSection
            config = E.Configuration()
            if parent_network:
                vdc = self.params.get('vdc')
                org_resource = Org(self.client, resource=self.client.get_org())
                vdc_resource = VDC(self.client, resource=org_resource.get_vdc(vdc))
                orgvdc_networks = vdc_resource.list_orgvdc_network_resources(parent_network)
                parent = next((network for network in orgvdc_networks if network.get('name') == parent_network), None)
                if parent:
                    config.append(E.ParentNetwork(href=parent.get('href')))
                else:
                    raise EntityNotFoundException('Parent network \'%s\' does not exist'.format(parent_network))
            elif ip_scope:
                scope = E.IpScope(
                    E.IsInherited('false'),
                    E.Gateway(str(ip_network(ip_scope, strict=False).network_address+1)),
                    E.Netmask(str(ip_network(ip_scope, strict=False).netmask)))
                config.append(E.IpScopes(scope))
            else:
                raise VappNetworkCreateError('Either parent_network or ip_scope must be set')
            config.append(E.FenceMode(fence_mode))

            network_config = E.NetworkConfig(config, networkName=network_name)
            network_config_section.append(network_config)

            add_network_task = self.client.put_linked_resource(
                self.vapp.resource.NetworkConfigSection, RelationType.EDIT,
                EntityType.NETWORK_CONFIG_SECTION.value,
                network_config_section)
            self.execute_task(add_network_task)
            response['msg'] = 'Vapp Network {} has been added'.format(network_name)
            response['changed'] = True
#.........这里部分代码省略.........
开发者ID:equinix-ms,项目名称:ansible-module-vcloud-director,代码行数:103,代码来源:vcd_vapp_network.py


注:本文中的pyvcloud.vcd.vapp.VApp.get_all_networks方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。