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 else: response[ 'warnings'] = 'Vapp Network {} is already present.'.format( network_name) return response
def connect_org_vdc_network(self, orgvdc_network_name, retain_ip=None, is_deployed=None, fence_mode=FenceMode.BRIDGED.value): """Connect the vapp to an orgvdc network. :param orgvdc_network_name: (str): name of the orgvdc network to be connected :param retain_ip: (bool): True if the network resources such as IP/MAC of router will be retained across deployments. :param is_deployed: (bool): True if this orgvdc network has been deployed. :param fence_mode: (str): Controls connectivity to the parent network. One of bridged, isolated or natRouted. bridged by default. :return: A :class:`lxml.objectify.StringElement` object representing the asynchronous task that is connecting the network. :raises: Exception: If orgvdc network does not exist in the vdc or if it is already connected to the vapp. """ vdc = VDC(self.client, href=find_link(self.resource, RelationType.UP, EntityType.VDC.value).href) orgvdc_networks = \ vdc.list_orgvdc_network_resources(orgvdc_network_name) if len(orgvdc_networks) == 0: raise EntityNotFoundException( "Orgvdc network \'%s\' does not exist in vdc " "\'%s\'" % (orgvdc_network_name, vdc.get_resource().get('name'))) orgvdc_network_href = orgvdc_networks[0].get('href') network_configuration_section = \ deepcopy(self.resource.NetworkConfigSection) matched_orgvdc_network_config = \ self._search_for_network_config_by_name( orgvdc_network_name, network_configuration_section) if matched_orgvdc_network_config is not None: raise InvalidStateException( "Orgvdc network \'%s\' is already connected to " "vapp." % orgvdc_network_name) configuration = E.Configuration( E.ParentNetwork(href=orgvdc_network_href), E.FenceMode(fence_mode)) if retain_ip is not None: configuration.append(E.RetainNetInfoAcrossDeployments(retain_ip)) network_config = E.NetworkConfig(configuration, networkName=orgvdc_network_name) if is_deployed is not None: network_config.append(E.IsDeployed(is_deployed)) network_configuration_section.append(network_config) return self.client.put_linked_resource( self.resource.NetworkConfigSection, RelationType.EDIT, EntityType.NETWORK_CONFIG_SECTION.value, network_configuration_section)
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 else: response['warnings'] = 'Vapp Network {} is already present.'.format(network_name) return response
# state we need to wait. if item_dict['status'] != "RESOLVED": print("Template unresolved: {0} {1} {2}".format( item_dict['catalogName'], item_dict['name'], item_dict['status'])) find_unresolved = True time.sleep(3) print("No unresolved templates found") # Check for desired networks and create them if they don't exist. # (We might add other kinds of networks later.) if cfg.networks.get("isolated"): print("Checking isolated networks...") for network in cfg.networks['isolated']: isolated_network_list = vdc.list_orgvdc_network_resources( name=network['network_name'], type=FenceMode.ISOLATED.value) if len(isolated_network_list) > 0: print("Isolated network exists: {0}".format( network['network_name'])) else: # Create the network in the vDC. print("Network does not exist, creating: {0}".format( network['network_name'])) network_resource = vdc.create_isolated_vdc_network(**network) handle_task(client, network_resource.Tasks.Task[0]) print("Network created") # Ensure the network is visible in the VDC. network_exists = False while not network_exists: new_network_list = vdc.list_orgvdc_network_resources(
cprint("\nList of all Tenant Edges in the VDC {}".format(vdc['name']), 'yellow') edgelist = vdc_instance.list_edge_gateways() edgetable = PrettyTable( ["Organization", "VDC name", "Edge name", "Edge href"]) for j in range(len(edgelist)): edgetable.add_row([ o.get('name'), vdc['name'], edgelist[j]['name'], edgelist[j]['href'] ]) print(edgetable) # Retrieve VDC Org Networks --------------------------------------------------------------- cprint("\nList of VDC Org Networks from VDC {}".format(vdc['name']), 'yellow') orgnets = vdc_instance.list_orgvdc_network_resources() orgnettable = PrettyTable( ["Organization", "VDC name", "Org Nw name", "Org Nw href"]) for k in range(len(orgnets)): orgnettable.add_row([ o.get('name'), vdc['name'], orgnets[k].attrib['name'], orgnets[k].attrib['href'] ]) print(orgnettable) # Retrieve all vApps from vCD ------------------------------------------------------------- vapps_list = vdc_instance.list_resources() for vapp in vapps_list: # Exclude VM Templates from Catalogs # There're two types vApp+xml or vAppTemplate+xml if vapp.get('type').split('.')[-1] == 'vApp+xml':
def connect_org_vdc_network(self, orgvdc_network_name, retain_ip=None, is_deployed=None, fence_mode=FenceMode.BRIDGED.value): """Connect the vApp to an org vdc network. :param str orgvdc_network_name: name of the org vdc network to be connected to. :param bool retain_ip: True, if the network resources such as IP/MAC of router will be retained across deployments. :param bool is_deployed: True, if this org vdc network has been deployed. :param str fence_mode: mode of connectivity to the parent network. Acceptable values are 'bridged', 'isolated' or 'natRouted'. Default value is 'bridged'. :return: an object containing EntityType.TASK XML data which represents the asynchronous task that is connecting the vApp to the network. :rtype: lxml.objectify.ObjectifiedElement :raises: EntityNotFoundException: if named org vdc network does not exist. :raises: InvalidStateException: if the vApp is already connected to the org vdc network. """ vdc = VDC(self.client, href=find_link(self.resource, RelationType.UP, EntityType.VDC.value).href) orgvdc_networks = \ vdc.list_orgvdc_network_resources(orgvdc_network_name) if len(orgvdc_networks) == 0: raise EntityNotFoundException( "Org vdc network \'%s\' does not exist in vdc " "\'%s\'" % (orgvdc_network_name, vdc.get_resource().get('name'))) orgvdc_network_href = orgvdc_networks[0].get('href') network_configuration_section = \ deepcopy(self.resource.NetworkConfigSection) matched_orgvdc_network_config = \ self._search_for_network_config_by_name( orgvdc_network_name, network_configuration_section) if matched_orgvdc_network_config is not None: raise InvalidStateException( "Org vdc network \'%s\' is already connected to " "vApp." % orgvdc_network_name) configuration = E.Configuration( E.ParentNetwork(href=orgvdc_network_href), E.FenceMode(fence_mode)) if retain_ip is not None: configuration.append(E.RetainNetInfoAcrossDeployments(retain_ip)) network_config = E.NetworkConfig(configuration, networkName=orgvdc_network_name) if is_deployed is not None: network_config.append(E.IsDeployed(is_deployed)) network_configuration_section.append(network_config) return self.client.put_linked_resource( self.resource.NetworkConfigSection, RelationType.EDIT, EntityType.NETWORK_CONFIG_SECTION.value, network_configuration_section)
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') ip_range_start = self.params.get('ip_range_start') ip_range_end = self.params.get('ip_range_end') dns1 = self.params.get('dns1') dns2 = self.params.get('dns2') dns_suffix = self.params.get('dns_suffix') nat_state = self.params.get('nat_state') fw_state = self.params.get('fw_state') dhcp_enabled = self.params.get('dhcp_enabled') 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: if 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)), E.Dns1(dns1), E.Dns2(dns2)) if ip_range_start: if not ip_range_end: ip_range_end = ip_range_start ip_range = E.IpRange( E.StartAddress(ip_range_start), E.EndAddress(ip_range_end)) scope.append(E.IpRanges(ip_range)) config.append(E.IpScopes(scope)) 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)), E.Dns1(dns1), E.Dns2(dns2), E.DnsSuffix(dns_suffix)) if ip_range_start: if not ip_range_end: ip_range_end = ip_range_start ip_range = E.IpRange(E.StartAddress(ip_range_start), E.EndAddress(ip_range_end)) scope.append(E.IpRanges(ip_range)) config.append(E.IpScopes(scope)) else: raise VappNetworkCreateError( 'Either parent_network or ip_scope must be set') config.append(E.FenceMode(fence_mode)) features = E.Features() if fw_state == 'disabled': features.append(E.FirewallService(E.IsEnabled('false'))) if nat_state == 'disabled': features.append(E.NatService(E.IsEnabled('false'))) config.append(features) 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 else: response[ 'warnings'] = 'Vapp Network {} is already present.'.format( network_name) return response