def create_gateway(ctx, name, external_networks_name, description, default_gateway_external_network, default_gw_ip, is_dns_relay, is_ha, is_advanced, is_distributed_routing, configure_ip_settings, sub_allocated_ext_net_name, sub_allocated_subnet, ip_ranges, configure_rate_limits, is_flip_flop, gateway_config): try: restore_session(ctx, vdc_required=True) client = ctx.obj['client'] vdc_href = ctx.obj['profiles'].get('vdc_href') api_version = ctx.obj['profiles'].get('api_version') vdc = VDC(client, href=vdc_href) is_configured_default_gw = False if default_gateway_external_network is not None and len( default_gateway_external_network) > 0: is_configured_default_gw = True is_ip_settings_configured = False ext_net_to_participated_subnet_with_ip_settings = {} if configure_ip_settings is not None and len(configure_ip_settings) \ > 0: is_ip_settings_configured = True ext_net_to_participated_subnet_with_ip_settings = tuple_to_dict( configure_ip_settings) is_sub_allocate_ip_pools_enabled = False ext_net_to_subnet_with_ip_range = {} if sub_allocated_ext_net_name is not None and len( sub_allocated_ext_net_name) > 0 and sub_allocated_subnet is \ not None and len(sub_allocated_subnet) > 0 and ip_ranges is \ not None and len(sub_allocated_subnet) > 0: is_sub_allocate_ip_pools_enabled = True ext_net_to_subnet_with_ip_range = { sub_allocated_ext_net_name: { sub_allocated_subnet: list(ip_ranges) } } ext_net_to_rate_limit = {} if configure_rate_limits is not None and len(configure_rate_limits) \ > 0: ext_net_to_rate_limit = tuple_to_dict(configure_rate_limits) if float(api_version) <= float(ApiVersion.VERSION_30.value): result = vdc.create_gateway_api_version_30( name, external_networks_name, gateway_config, description, is_configured_default_gw, default_gateway_external_network, default_gw_ip, is_dns_relay, is_ha, is_advanced, is_distributed_routing, is_ip_settings_configured, ext_net_to_participated_subnet_with_ip_settings, is_sub_allocate_ip_pools_enabled, ext_net_to_subnet_with_ip_range, ext_net_to_rate_limit) else: result = vdc.create_gateway( name, external_networks_name, gateway_config, description, is_configured_default_gw, default_gateway_external_network, default_gw_ip, is_dns_relay, is_ha, is_advanced, is_distributed_routing, is_ip_settings_configured, ext_net_to_participated_subnet_with_ip_settings, is_sub_allocate_ip_pools_enabled, ext_net_to_subnet_with_ip_range, ext_net_to_rate_limit, is_flip_flop) stdout(result.Tasks.Task[0], ctx) except Exception as e: stderr(e, ctx)
print("VDC {0} does not exist, exiting".format(cfg.org['vdc_name'])) sys.exit() # Ensure the edge doesn't exist. ------------------------------------------------------------------ print("\nFetching Edges...") try: network_resource = vdc.get_gateway(cfg.org.edge_gateway.name) print("Edge gateway already exists: {0}".format(cfg.org.edge_gateway.name)) except Exception: print("Edge gateway {0} does not exist, creating".format( cfg.org.edge_gateway.name)) network_resource = vdc.create_gateway( cfg.org.edge_gateway.name, external_networks=[cfg.system.ext_network.name, cfg.org.mng_nw], gateway_backing_config=GatewayBackingConfigType.XLARGE.value, is_default_gateway=True, selected_extnw_for_default_gw=cfg.system.ext_network.name, default_gateway_ip=cfg.system.ext_network.gateway, is_ha_enabled=True, should_create_as_advanced=True, is_dr_enabled=True) handle_task(client, network_resource.Tasks.Task[0]) # Status report ----------------------------------------------------------------------------------- new_edge_gateway = vdc.get_gateway(cfg.org.edge_gateway.name) print( '\nNew Edge gateway is created -------------------------------------------------' ) print('name: {}'.format(new_edge_gateway.attrib['name'])) print('href: {}'.format(new_edge_gateway.attrib['href']))
class VdcGW(VcdAnsibleModule): def __init__(self, **kwargs): super(VdcGW, self).__init__(**kwargs) self.vdc_name = self.params.get('vdc_name') self.gateway_name = self.params.get('gateway_name') self.description = self.params.get('description') self.external_networks = self.params.get('external_networks') self.gateway_backing_config = self.params.get('gateway_backing_config') self.default_gateway = self.params.get('default_gateway') self.extnw_for_default_gw = self.params.get('extnw_for_default_gw') self.default_gateway_ip = self.params.get('default_gateway_ip') self.default_gw_for_dns_relay = self.params.get( 'default_gw_for_dns_relay') self.ha_enabled = self.params.get('ha_enabled') self.create_as_advanced_gw = self.params.get('create_as_advanced_gw') self.dr_enabled = self.params.get('dr_enabled') self.configure_ip_settings = self.params.get('configure_ip_settings') self.ext_net_to_participated_subnet_with_ip_settings = self.params.get( 'ext_net_to_participated_subnet_with_ip_settings') self.sub_allocate_ip_pools = self.params.get('sub_allocate_ip_pools') self.ext_net_to_subnet_with_ip_range = self.params.get( 'ext_net_to_subnet_with_ip_range') self.ext_net_to_rate_limit = self.params.get('ext_net_to_rate_limit') self.flips_mode = self.params.get('flips_mode') self.org_name = self.params.get('org_name') org_resource = self.client.get_org_by_name(self.org_name) self.org = Org(self.client, resource=org_resource) vdc_resource = self.org.get_vdc(self.vdc_name) self.vdc = VDC(self.client, name=self.vdc_name, resource=vdc_resource) def manage_states(self): state = self.params.get('state') if state == "present": return self.create_gw() if state == "absent": return self.delete_gw() def create_gw(self): response = dict() try: self.vdc.get_gateway(self.gateway_name) except EntityNotFoundException: create = self.vdc.create_gateway( name=self.gateway_name, external_networks=self.external_networks, gateway_backing_config=self.gateway_backing_config, desc=self.description, is_default_gateway=self.default_gateway, selected_extnw_for_default_gw=self.extnw_for_default_gw, default_gateway_ip=self.default_gateway_ip, is_default_gw_for_dns_relay_selected=self. default_gw_for_dns_relay, is_ha_enabled=self.ha_enabled, should_create_as_advanced=self.create_as_advanced_gw, is_dr_enabled=self.dr_enabled, is_ip_settings_configured=self.configure_ip_settings, ext_net_to_participated_subnet_with_ip_settings=self. ext_net_to_participated_subnet_with_ip_settings, is_sub_allocate_ip_pools_enabled=self.sub_allocate_ip_pools, ext_net_to_subnet_with_ip_range=self. ext_net_to_subnet_with_ip_range, ext_net_to_rate_limit=self.ext_net_to_rate_limit, is_flips_mode_enabled=self.flips_mode) self.execute_task(create.Tasks.Task[0]) response['msg'] = 'Edge Gateway {0} created'.format( self.gateway_name) response['changed'] = True else: response[ 'warnings'] = 'Edge Gateway {0} is already present.'.format( self.gateway_name) return response def delete_gw(self): response = dict() try: self.vdc.get_gateway(self.gateway_name) except EntityNotFoundException: response['warnings'] = 'Edge Gateway {0} is not present.'.format( self.gateway_name) else: delete = self.vdc.delete_gateway(self.gateway_name) self.execute_task(delete) response['changed'] = True return response
def create_gateway(ctx, name, external_networks_name, description, default_gateway_external_network, default_gw_ip, is_dns_relay, is_ha, is_advanced, is_distributed_routing, configure_ip_settings, sub_allocated_ext_net_name, sub_allocated_subnet, ip_ranges, configure_rate_limits, is_flip_flop, gateway_config): """Create a gateway. \b Note System Administrators can create gateway. \b Examples vcd gateway create <gateway-name> -e <External-Network-Name1> Create gateway by providing new gateway name, external networks. It will create Compact by default without Default gateway configuration \b vcd gateway create <gateway-name> -e <External-Network-Name1> -e <External-Network-Name1> Create gateway by providing multiple external networks \b vcd gateway create <gateway-name> -e <External-Network-Name1> --default-gateway <External Network> --default-gw-ip <default gateway IP> --dns-relay-enabled/--dns-relay-disabled Create gateway by providing Default gateway configuration \b vcd gateway create <gateway-name> -e <External-Network-Name1> --c <compact/full/full4/x-large> Create gateway by providing gateway config. \b vcd gateway create <gateway-name> -e <External-Network-Name1> --ha-enabled/--ha-disabled Create gateway with HA enabled \b vcd gateway create <gateway-name> -e <External-Network-Name1> --advanced-enabled/--advanced-disabled Create advanced gateway \b vcd gateway create <gateway-name> -e <External-Network-Name1> --distributed-routing-enabled/--distributed-routing-disabled Create gateway with enable distributed routing for networks connected to this gateway \b vcd gateway create <gateway-name> -e <External-Network-Name1> --distributed-routing-enabled/--distributed-routing-disabled \b vcd gateway create <gateway-name> -e <External-Network-Name1> --configure-ip-setting <External Network Name1> <subnet1> <IP> --configure-ip-setting <External Network Name2> <subnet1> <IP> ...(One or more ip settings) For ex: --configure-ip-setting ext_net 10.3.2.1/24 Auto or --configure-ip-setting ext_net 10.3.2.1/24 10.3.2.3 Create gateway with Configure IP settings \b vcd gateway create <gateway-name> -e <External-Network-Name1> --sub-allocate-ip <External Network Name> --subnet <subnet> --ip-range <IP Range> --ip-range <IP Range> ...(one or more IP Ranges) For ex: --sub-allocate-ip <External Network Name> --subnet 10.3.2.1/20 --ip-range 10.3.2.3-10.3.2.4 \b vcd gateway create <gateway-name> -e <External-Network-Name1> --configure-rate-limit <External_Network_Name1> <incoming rate limit> <outgoing rate limit> --configure-rate-limit <External_Network_Name2> <incoming rate limit> <outgoing rate limit> ... (One or more rate limits) Create gateway with Rate Limits Configured \b \b vcd gateway create <gateway-name> -e <External-Network-Name1> --flips-mode-enabled/--flips-mode-disabled flips flop mode """ try: restore_session(ctx, vdc_required=True) client = ctx.obj['client'] vdc_href = ctx.obj['profiles'].get('vdc_href') vdc = VDC(client, href=vdc_href) is_configured_default_gw = False if default_gateway_external_network is not None and len( default_gateway_external_network) > 0: is_configured_default_gw = True is_ip_settings_configured = False ext_net_to_participated_subnet_with_ip_settings = {} if configure_ip_settings is not None and len(configure_ip_settings) \ > 0: is_ip_settings_configured = True ext_net_to_participated_subnet_with_ip_settings = tuple_to_dict \ (configure_ip_settings) is_sub_allocate_ip_pools_enabled = False ext_net_to_subnet_with_ip_range = {} if sub_allocated_ext_net_name is not None and len( sub_allocated_ext_net_name) > 0 and sub_allocated_subnet is \ not None and len(sub_allocated_subnet) > 0 and ip_ranges is \ not None and len(sub_allocated_subnet) > 0: is_sub_allocate_ip_pools_enabled = True ext_net_to_subnet_with_ip_range = {sub_allocated_ext_net_name : {sub_allocated_subnet : list(ip_ranges)}} ext_net_to_rate_limit = {} if configure_rate_limits is not None and len(configure_rate_limits) \ > 0: ext_net_to_rate_limit = tuple_to_dict(configure_rate_limits) result = vdc.create_gateway(name, external_networks_name, gateway_config, description, is_configured_default_gw, default_gateway_external_network, default_gw_ip, is_dns_relay, is_ha, is_advanced, is_distributed_routing, is_ip_settings_configured, ext_net_to_participated_subnet_with_ip_settings, is_sub_allocate_ip_pools_enabled, ext_net_to_subnet_with_ip_range, ext_net_to_rate_limit, is_flip_flop) stdout(result.Tasks.Task[0], ctx) except Exception as e: stderr(e, ctx)