def delete(vca_client, **kwargs): """ delete vcloud air network """ if ctx.node.properties['use_external_resource'] is True: del ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] ctx.logger.info("Network was not deleted - external resource has" " been used") return network_name = get_network_name(ctx.node.properties) if not _dhcp_operation(vca_client, network_name, DELETE_POOL): return set_retry(ctx) ctx.logger.info("Delete network '{0}'".format(network_name)) success, task = vca_client.delete_vdc_network( get_vcloud_config()['vdc'], network_name) if success: wait_for_task(vca_client, task) ctx.logger.info( "Network '{0}' has been successful deleted.".format(network_name)) else: if task and CANT_DELETE in task: ctx.logger.info("Network {} in use. Deleting the network skipped.". format(network_name)) return raise cfy_exc.NonRecoverableError( "Could not delete network '{0}': {1}".format(network_name, task))
def creation_validation(vca_client, **kwargs): """ check network description from node description """ network_name = get_network_name(ctx.node.properties) ctx.logger.info("Validation cloudify.vcloud.nodes.Network node: {0}" .format(network_name)) if is_network_exists(vca_client, network_name): if ctx.node.properties.get('use_external_resource'): # TODO: check: default gateway must exists return else: raise cfy_exc.NonRecoverableError( "Network already exsists: {0}".format(network_name)) net_prop = get_mandatory(ctx.node.properties, "network") gateway_name = get_mandatory(net_prop, 'edge_gateway') if not vca_client.get_gateway(get_vcloud_config()['vdc'], gateway_name): raise cfy_exc.NonRecoverableError( "Gateway {0} not found".format(gateway_name)) static_ip = _split_adresses(get_mandatory(net_prop, 'static_range')) check_ip(static_ip.start) check_ip(static_ip.end) dns_list = net_prop.get("dns") if dns_list: for ip in dns_list: check_ip(ip) gateway_ip = check_ip(get_mandatory(net_prop, "gateway_ip")) netmask = check_ip(get_mandatory(net_prop, "netmask")) ips = [gateway_ip, static_ip.start, static_ip.end] dhcp = net_prop.get("dhcp") if dhcp: dhcp_range = get_mandatory(net_prop["dhcp"], "dhcp_range") dhcp_ip = _split_adresses(dhcp_range) if not is_separate_ranges(static_ip, dhcp_ip): raise cfy_exc.NonRecoverableError( "Static_range and dhcp_range is overlapped.") ips.extend([dhcp_ip.start, dhcp_ip.end]) if not is_ips_in_same_subnet(ips, netmask): raise cfy_exc.NonRecoverableError( "IP addresses in different subnets.")
def test_get_network_name(self): """ check get network name """ # external without resource_id with self.assertRaises(cfy_exc.NonRecoverableError): vcloud_network_plugin.get_network_name({ 'use_external_resource': True }) # exteranal with resource_id self.assertEqual( vcloud_network_plugin.get_network_name({ 'use_external_resource': True, 'resource_id': 'some_text' }), 'some_text' ) # internal, without network with self.assertRaises(cfy_exc.NonRecoverableError): vcloud_network_plugin.get_network_name({}) # network without name with self.assertRaises(cfy_exc.NonRecoverableError): vcloud_network_plugin.get_network_name({ 'network': { 'name': None } }) # good case self.assertEqual( vcloud_network_plugin.get_network_name({ 'network': { 'name': 'good_text' } }), 'good_text' )
def create(vca_client, **kwargs): """ create new vcloud air network, e.g.: { 'use_external_resource': False, 'resource_id': 'secret_network', 'network': { 'dhcp': { 'dhcp_range': "10.1.1.128-10.1.1.255" }, 'static_range': "10.1.1.2-10.1.1.127", 'gateway_ip': "10.1.1.1", 'edge_gateway': 'gateway', 'name': 'secret_network', "netmask": '255.255.255.0', "dns": ["8.8.8.8", "4.4.4.4"] } } """ vdc_name = get_vcloud_config()['vdc'] if ctx.node.properties['use_external_resource']: network_name = ctx.node.properties['resource_id'] if not is_network_exists(vca_client, network_name): raise cfy_exc.NonRecoverableError( "Can't find external resource: {0}".format(network_name)) ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name ctx.logger.info( "External resource {0} has been used".format(network_name)) return network_name = get_network_name(ctx.node.properties) if not ctx.instance.runtime_properties.get(SKIP_CREATE_NETWORK): net_prop = ctx.node.properties["network"] if network_name in _get_network_list(vca_client, get_vcloud_config()['vdc']): raise cfy_exc.NonRecoverableError( "Network {0} already exists, but parameter " "'use_external_resource' is 'false' or absent" .format(network_name)) ip = _split_adresses(net_prop['static_range']) gateway_name = net_prop['edge_gateway'] get_gateway(vca_client, gateway_name) start_address = ip.start end_address = ip.end gateway_ip = net_prop["gateway_ip"] netmask = net_prop["netmask"] dns1 = "" dns2 = "" dns_list = net_prop.get("dns") if dns_list: dns1 = dns_list[0] if len(dns_list) > 1: dns2 = dns_list[1] dns_suffix = net_prop.get("dns_suffix") ctx.logger.info("Create network {0}." .format(network_name)) success, result = vca_client.create_vdc_network( vdc_name, network_name, gateway_name, start_address, end_address, gateway_ip, netmask, dns1, dns2, dns_suffix) if success: wait_for_task(vca_client, result) ctx.logger.info("Network {0} has been successfully created." .format(network_name)) else: raise cfy_exc.NonRecoverableError( "Could not create network {0}: {1}". format(network_name, result)) ctx.instance.runtime_properties[VCLOUD_NETWORK_NAME] = network_name if not _dhcp_operation(vca_client, network_name, ADD_POOL): ctx.instance.runtime_properties[SKIP_CREATE_NETWORK] = True return set_retry(ctx)
def _create_connections_list(vca_client): """ return full list connections for node """ connections = [] ports = _get_connected(ctx.instance, 'port') networks = _get_connected(ctx.instance, 'network') management_network_name = ctx.node.properties.get('management_network') for port in ports: port_properties = port.node.properties['port'] connections.append( _create_connection(port_properties['network'], port_properties.get('ip_address'), port_properties.get('mac_address'), port_properties.get('ip_allocation_mode', 'POOL').upper(), port_properties.get('primary_interface', False)) ) for net in networks: connections.append( _create_connection(get_network_name(net.node.properties), None, None, 'POOL')) if management_network_name and not any( [conn['network'] == management_network_name for conn in connections]): connections.append(_create_connection(management_network_name, None, None, 'POOL')) for conn in connections: if not is_network_exists(vca_client, conn['network']): raise cfy_exc.NonRecoverableError( "Network '{0}' could not be found".format(conn['network'])) primary_iface_set = len(filter(lambda conn: conn.get('primary_interface', False), connections)) > 0 if not primary_iface_set: if management_network_name: primary_name = management_network_name elif connections: primary_name = connections[0]['network'] else: raise cfy_exc.NonRecoverableError( "Can't setup primary interface") # check list of connections and set managment network as primary # in case when we dont have any primary networks for conn in connections: network_name = conn['network'] if (conn['ip_allocation_mode'] == 'DHCP' and not _isDhcpAvailable(vca_client, network_name)): raise cfy_exc.NonRecoverableError( "DHCP for network {0} is not available" .format(network_name)) if not primary_iface_set: conn['primary_interface'] = \ (network_name == primary_name) if conn['primary_interface']: ctx.logger.info( "The primary interface has been set to {}".format( network_name)) return connections