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 create_external_network(self, name, vim_server_name, port_group_names, gateway_ip, netmask, ip_ranges, description=None, primary_dns_ip=None, secondary_dns_ip=None, dns_suffix=None): """Create an external network. :param str name: name of external network to be created. :param str vim_server_name: VIM server_name (VC name). :param list port_group_names: list of port group names. :param str gateway_ip: IP address of the gateway of the new network. :param str netmask: Netmask of the gateway. :param list ip_ranges: list of IP ranges used for static pool allocation in the network. For example, [192.168.1.2-192.168.1.49, 192.168.1.100-192.168.1.149]. :param str description: description of external network. :param str primary_dns_ip: IP address of primary DNS server. :param str secondary_dns_ip: IP address of secondary DNS Server. :param str dns_suffix: DNS suffix. :return: an object containing vmext:VMWExternalNetwork XML element that represents the new external network. :rtype: lxml.objectify.ObjectifiedElement """ vc_record = self.get_vcenter(vim_server_name) vc_href = vc_record.get('href') pg_morefs = self.get_port_group_morefs(port_group_names) vmw_external_network = E_VMEXT.VMWExternalNetwork(name=name) if description is not None: vmw_external_network.append(E.Description(description)) config = E.Configuration() ip_scopes = E.IpScopes() ip_scope = E.IpScope() ip_scope.append(E.IsInherited(False)) ip_scope.append(E.Gateway(gateway_ip)) ip_scope.append(E.Netmask(netmask)) if primary_dns_ip is not None: ip_scope.append(E.Dns1(primary_dns_ip)) if secondary_dns_ip is not None: ip_scope.append(E.Dns2(secondary_dns_ip)) if dns_suffix is not None: ip_scope.append(E.DnsSuffix(dns_suffix)) e_ip_ranges = E.IpRanges() for ip_range in ip_ranges: e_ip_range = E.IpRange() ip_range_token = ip_range.split('-') e_ip_range.append(E.StartAddress(ip_range_token[0])) e_ip_range.append(E.EndAddress(ip_range_token[1])) e_ip_ranges.append(e_ip_range) ip_scope.append(e_ip_ranges) ip_scopes.append(ip_scope) config.append(ip_scopes) config.append(E.FenceMode(FenceMode.ISOLATED.value)) vmw_external_network.append(config) vim_port_group_refs = E_VMEXT.VimPortGroupRefs() for pg_moref in pg_morefs: vim_object_ref = E_VMEXT.VimObjectRef() vim_object_ref.append(E_VMEXT.VimServerRef(href=vc_href)) vim_object_ref.append(E_VMEXT.MoRef(pg_moref[0])) vim_object_ref.append(E_VMEXT.VimObjectType(pg_moref[1])) vim_port_group_refs.append(vim_object_ref) vmw_external_network.append(vim_port_group_refs) return self.client.post_linked_resource( self.extension.get_resource(), rel=RelationType.ADD, media_type=EntityType.EXTERNAL_NETWORK.value, contents=vmw_external_network)
def create_isolated_vdc_network(self, network_name, gateway_ip, netmask, description=None, primary_dns_ip=None, secondary_dns_ip=None, dns_suffix=None, ip_range_start=None, ip_range_end=None, is_dhcp_enabled=None, default_lease_time=None, max_lease_time=None, dhcp_ip_range_start=None, dhcp_ip_range_end=None, is_shared=None): """Create a new isolated OrgVdc network in this vdc. :param network_name: (str): Name of the new network. :param gateway_ip: (str): IP address of the gateway of the new network. :param netmask: (str): Network mask. :param description: (str): Description of the new network. :param primary_dns_ip: (str): IP address of primary DNS server. :param secondary_dns_ip: (str): IP address of secondary DNS Server. :param dns_suffix: (str): DNS suffix. :param ip_range_start: (str): Start address of the IP ranges used for static pool allocation in the network. :param ip_range_end: (str): End address of the IP ranges used for static pool allocation in the network. :param is_dhcp_enabled: (bool): Is DHCP service enabled on the new network. :param default_lease_time: (int): Default lease in seconds for DHCP addresses. :param max_lease_time: (int): Max lease in seconds for DHCP addresses. :param dhcp_ip_range_start: (str): Start address of the IP range used for DHCP addresses. :param dhcp_ip_range_end: (str): End address of the IP range used for DHCP addresses. :param is_shared: (bool): True, if the network is shared with other vdc(s) in the organization, else False. :return: A :class:`lxml.objectify.StringElement` object representing a sparsely populated OrgVdcNetwork element. """ if self.resource is None: self.resource = self.client.get_resource(self.href) request_payload = E.OrgVdcNetwork(name=network_name) if description is not None: request_payload.append(E.Description(description)) vdc_network_configuration = E.Configuration() ip_scope = E.IpScope() ip_scope.append(E.IsInherited('false')) ip_scope.append(E.Gateway(gateway_ip)) ip_scope.append(E.Netmask(netmask)) if primary_dns_ip is not None: ip_scope.append(E.Dns1(primary_dns_ip)) if secondary_dns_ip is not None: ip_scope.append(E.Dns2(secondary_dns_ip)) if dns_suffix is not None: ip_scope.append(E.DnsSuffix(dns_suffix)) if ip_range_start is not None and ip_range_end is not None: ip_range = E.IpRange() ip_range.append(E.StartAddress(ip_range_start)) ip_range.append(E.EndAddress(ip_range_end)) ip_scope.append(E.IpRanges(ip_range)) vdc_network_configuration.append(E.IpScopes(ip_scope)) vdc_network_configuration.append(E.FenceMode(FenceMode.ISOLATED.value)) request_payload.append(vdc_network_configuration) dhcp_service = E.DhcpService() if is_dhcp_enabled is not None: dhcp_service.append(E.IsEnabled(is_dhcp_enabled)) if default_lease_time is not None: dhcp_service.append(E.DefaultLeaseTime(str(default_lease_time))) if max_lease_time is not None: dhcp_service.append(E.MaxLeaseTime(str(max_lease_time))) if dhcp_ip_range_start is not None and dhcp_ip_range_end is not None: dhcp_ip_range = E.IpRange() dhcp_ip_range.append(E.StartAddress(dhcp_ip_range_start)) dhcp_ip_range.append(E.EndAddress(dhcp_ip_range_end)) dhcp_service.append(dhcp_ip_range) request_payload.append(E.ServiceConfig(dhcp_service)) if is_shared is not None: request_payload.append(E.IsShared(is_shared)) return self.client.post_linked_resource( self.resource, RelationType.ADD, EntityType.ORG_VDC_NETWORK.value, request_payload)
def add_subnet(self, name, gateway_ip, netmask, ip_ranges, primary_dns_ip=None, secondary_dns_ip=None, dns_suffix=None): """Add subnet to an external network. :param str name: Name of external network. :param str gateway_ip: IP address of the gateway of the new network. :param str netmask: Netmask of the gateway. :param list ip_ranges: list of IP ranges used for static pool allocation in the network. For example, [192.168.1.2-192.168.1.49, 192.168.1.100-192.168.1.149]. :param str primary_dns_ip: IP address of primary DNS server. :param str secondary_dns_ip: IP address of secondary DNS Server. :param str dns_suffix: DNS suffix. :rtype: lxml.objectify.ObjectifiedElement """ if self.resource is None: self.reload() platform = Platform(self.client) ext_net = platform.get_external_network(name) config = ext_net['{' + NSMAP['vcloud'] + '}Configuration'] ip_scopes = config.IpScopes ip_scope = E.IpScope() ip_scope.append(E.IsInherited(False)) ip_scope.append(E.Gateway(gateway_ip)) ip_scope.append(E.Netmask(netmask)) if primary_dns_ip is not None: ip_scope.append(E.Dns1(primary_dns_ip)) if secondary_dns_ip is not None: ip_scope.append(E.Dns2(secondary_dns_ip)) if dns_suffix is not None: ip_scope.append(E.DnsSuffix(dns_suffix)) e_ip_ranges = E.IpRanges() for ip_range in ip_ranges: e_ip_range = E.IpRange() ip_range_token = ip_range.split('-') e_ip_range.append(E.StartAddress(ip_range_token[0])) e_ip_range.append(E.EndAddress(ip_range_token[1])) e_ip_ranges.append(e_ip_range) ip_scope.append(e_ip_ranges) ip_scopes.append(ip_scope) return self.client.put_linked_resource( ext_net, rel=RelationType.EDIT, media_type=EntityType.EXTERNAL_NETWORK.value, contents=ext_net)
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
def add_external_network(self, network_name, ip_configuration): """Add the given external network to the gateway. :param str network_name: external network name. :param list ip_configuration: list of tuples that contain subnet in CIDR format and allocated ip address. Example [(10.10.10.1/24, Auto), (10.10.20.1/24, 10.10.20.3)] :return: object containing EntityType.TASK XML data representing the asynchronous task. :rtype: lxml.objectify.ObjectifiedElement """ if self.resource is None: self.reload() gateway = self.resource for inf in gateway.Configuration.GatewayInterfaces.GatewayInterface: if inf.Network.get('name') == network_name: raise AlreadyExistsException('External network ' + network_name + 'already added to the gateway.') ext_nw = self._get_external_network(network_name) gw_interface = self._create_gateway_interface(ext_nw, 'uplink') # Add subnet participation ip_scopes = ext_nw.xpath( 'vcloud:Configuration/vcloud:IpScopes/vcloud:IpScope', namespaces=NSMAP) for ip_scope in ip_scopes: subnet_participation_param = E.SubnetParticipation() subnet = None ext_nw_subnet = ip_scope.Gateway.text + '/' + \ str(netmask_to_cidr_prefix_len(ip_scope.Gateway.text, ip_scope.Netmask.text)) for sn in ip_configuration: if len(sn) != 2: raise InvalidParameterException( 'IP Configuration should have both subnet and IP.') if sn[0] == ext_nw_subnet: subnet = sn break if subnet is None: continue ip_assigned = subnet[1].strip() # Configure Ip Settings subnet_participation_param.append(E.Gateway(ip_scope.Gateway.text)) subnet_participation_param.append(E.Netmask(ip_scope.Netmask.text)) if not ip_assigned and ip_assigned.lower() != 'auto': subnet_participation_param.append(E.IpAddress(ip_assigned)) gw_interface.append(subnet_participation_param) gateway.Configuration.GatewayInterfaces.append(gw_interface) return self.client.put_linked_resource( self.resource, RelationType.GATEWAY_UPDATE_PROPERTIES, EntityType.EDGE_GATEWAY.value, gateway)
def create_vapp_network(self, name, network_cidr, description=None, primary_dns_ip=None, secondary_dns_ip=None, dns_suffix=None, ip_ranges=None, is_guest_vlan_allowed=False): """Create a vApp network. :param str network_name: name of vApp network to be created. :param str network_cidr: CIDR in the format of 192.168.1.1/24. :param str description: description of vApp network. :param str primary_dns_ip: IP address of primary DNS server. :param str secondary_dns_ip: IP address of secondary DNS Server. :param str dns_suffix: DNS suffix. :params list ip_ranges: list of IP ranges used for static pool allocation in the network. For example, [192.168.1.2-192.168.1.49, 192.168.1.100-192.168.1.149]. :return: an object containing EntityType.TASK XML data which represents the asynchronous task that is creating the vApp network. :rtype: lxml.objectify.ObjectifiedElement """ network_config_section = \ deepcopy(self.resource.NetworkConfigSection) network_config = E.NetworkConfig(networkName=name) if description is not None: network_config.append(E.Description(description)) config = E.Configuration() ip_scopes = E.IpScopes() ip_scope = E.IpScope() ip_scope.append(E.IsInherited(False)) gateway_ip, netmask = cidr_to_netmask(network_cidr) ip_scope.append(E.Gateway(gateway_ip)) ip_scope.append(E.Netmask(netmask)) if primary_dns_ip is not None: ip_scope.append(E.Dns1(primary_dns_ip)) if secondary_dns_ip is not None: ip_scope.append(E.Dns2(secondary_dns_ip)) if dns_suffix is not None: ip_scope.append(E.DnsSuffix(dns_suffix)) e_ip_ranges = E.IpRanges() for ip_range in ip_ranges: e_ip_range = E.IpRange() ip_range_token = ip_range.split('-') e_ip_range.append(E.StartAddress(ip_range_token[0])) e_ip_range.append(E.EndAddress(ip_range_token[1])) e_ip_ranges.append(e_ip_range) ip_scope.append(e_ip_ranges) ip_scopes.append(ip_scope) config.append(ip_scopes) config.append(E.FenceMode(FenceMode.ISOLATED.value)) config.append(E.GuestVlanAllowed(is_guest_vlan_allowed)) network_config.append(config) network_config_section.append(network_config) return self.client.put_linked_resource( self.resource.NetworkConfigSection, RelationType.EDIT, EntityType.NETWORK_CONFIG_SECTION.value, network_config_section)