def test_get_public_ip_ondemand(self): """ check allocate public ip / ondemand """ # ondemand fake_ctx = self.generate_node_context() fake_client = self.generate_client() fake_client._vdc_gateway.get_public_ips = mock.MagicMock( return_value=[] ) fake_client._vdc_gateway.allocate_public_ip = mock.MagicMock( return_value=self.generate_task( vcloud_plugin_common.TASK_STATUS_SUCCESS ) ) new_gateway = self.generate_gateway() new_gateway.get_public_ips = mock.MagicMock( return_value=['10.18.1.21'] ) fake_client.get_gateways = mock.MagicMock( return_value=[new_gateway] ) with mock.patch('vcloud_plugin_common.ctx', fake_ctx): self.assertEqual( network_plugin.get_public_ip( fake_client, fake_client._vdc_gateway, vcloud_plugin_common.ONDEMAND_SERVICE_TYPE, fake_ctx ), '10.18.1.21' )
def _floatingip_operation(operation, vca_client, ctx): """ create/release floating ip by nat rules for this ip with relation to internal ip for current node, save selected public_ip in runtime properties """ service_type = get_vcloud_config().get('service_type') gateway = get_gateway( vca_client, ctx.target.node.properties['floatingip']['edge_gateway']) internal_ip = get_vm_ip(vca_client, ctx, gateway) nat_operation = None public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP) or ctx.target.node.properties['floatingip'].get(PUBLIC_IP)) if operation == CREATE: CheckAssignedInternalIp(internal_ip, gateway) if public_ip: CheckAssignedExternalIp(public_ip, gateway) else: public_ip = get_public_ip(vca_client, gateway, service_type, ctx) nat_operation = _add_nat_rule elif operation == DELETE: if not public_ip: ctx.logger.info("Can't get external IP".format(public_ip)) return nat_operation = _del_nat_rule else: raise cfy_exc.NonRecoverableError( "Unknown operation {0}".format(operation) ) external_ip = check_ip(public_ip) nat_operation(gateway, "SNAT", internal_ip, external_ip) nat_operation(gateway, "DNAT", external_ip, internal_ip) if not save_gateway_configuration(gateway, vca_client): return ctx.operation.retry(message='Waiting for gateway.', retry_after=10) if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip else: if is_ondemand(service_type): if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) del ctx.target.instance.runtime_properties[PUBLIC_IP]
def _floatingip_operation(operation, vca_client, ctx): """ create/release floating ip by nat rules for this ip with relation to internal ip for current node, save selected public_ip in runtime properties """ service_type = get_vcloud_config().get('service_type') gateway = get_gateway( vca_client, ctx.target.node.properties['floatingip']['edge_gateway']) if gateway.is_busy(): return False internal_ip = get_vm_ip(vca_client, ctx, gateway) nat_operation = None public_ip = (ctx.target.instance.runtime_properties.get(PUBLIC_IP) or ctx.target.node.properties['floatingip'].get(PUBLIC_IP)) if operation == CREATE: CheckAssignedInternalIp(internal_ip, gateway) if public_ip: CheckAssignedExternalIp(public_ip, gateway) else: public_ip = get_public_ip(vca_client, gateway, service_type, ctx) nat_operation = _add_nat_rule elif operation == DELETE: if not public_ip: ctx.logger.info("Can't get external IP".format(public_ip)) return nat_operation = _del_nat_rule else: raise cfy_exc.NonRecoverableError( "Unknown operation {0}".format(operation)) external_ip = check_ip(public_ip) nat_operation(gateway, "SNAT", internal_ip, external_ip) nat_operation(gateway, "DNAT", external_ip, internal_ip) success = save_gateway_configuration(gateway, vca_client) if not success: return False if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip else: if is_ondemand(service_type): if not ctx.target.node.properties['floatingip'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) del ctx.target.instance.runtime_properties[PUBLIC_IP] return True
def test_get_public_ip_subscription(self): """ check allocate public ip / subscription """ gateway = self.generate_gateway() gateway.get_public_ips = mock.MagicMock( return_value=['10.18.1.1', '10.18.1.2']) rule_inlist = self.generate_nat_rule('DNAT', '10.18.1.1', 'any', 'internal', '11', 'TCP') gateway.get_nat_rules = mock.MagicMock(return_value=[rule_inlist]) fake_ctx = self.generate_node_context() # for subscription we dont use client self.assertEqual( network_plugin.get_public_ip( None, gateway, vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE, fake_ctx), '10.18.1.2')
def test_get_public_ip(self): gateway = self.generate_gateway() gateway.get_public_ips = mock.MagicMock(return_value=[ '10.18.1.1', '10.18.1.2' ]) rule_inlist = self.generate_nat_rule( 'DNAT', '10.18.1.1', 'any', 'internal', '11', 'TCP' ) gateway.get_nat_rules = mock.MagicMock( return_value=[rule_inlist] ) fake_ctx = self.generate_node_context() # for subscription we dont use client self.assertEqual( network_plugin.get_public_ip( None, gateway, vcloud_plugin_common.SUBSCRIPTION_SERVICE_TYPE, fake_ctx ), '10.18.1.2' )
def _obtain_public_ip(vca_client, ctx, gateway, operation): """ return public ip for rules, in delete case - returned already used in create case - return new free ip """ public_ip = None if operation == CREATE: public_ip = ctx.target.node.properties['nat'].get(PUBLIC_IP) if not public_ip: service_type = get_vcloud_config().get('service_type') public_ip = get_public_ip(vca_client, gateway, service_type, ctx) elif operation == DELETE: if PUBLIC_IP in ctx.target.instance.runtime_properties: public_ip = ctx.target.instance.runtime_properties[PUBLIC_IP] else: raise cfy_exc.NonRecoverableError("Can't obtain public IP from runtime properties") else: raise cfy_exc.NonRecoverableError("Unknown operation") return public_ip
def _obtain_public_ip(vca_client, ctx, gateway, operation): """ return public ip for rules, in delete case - returned already used in create case - return new free ip """ public_ip = None if operation == CREATE: public_ip = ctx.target.node.properties['nat'].get(PUBLIC_IP) if not public_ip: service_type = get_vcloud_config().get('service_type') public_ip = get_public_ip(vca_client, gateway, service_type, ctx) elif operation == DELETE: if PUBLIC_IP in ctx.target.instance.runtime_properties: public_ip = ctx.target.instance.runtime_properties[PUBLIC_IP] else: raise cfy_exc.NonRecoverableError( "Can't obtain public IP from runtime properties") else: raise cfy_exc.NonRecoverableError("Unknown operation") return public_ip