def _save_configuration(gateway, vca_client, operation, public_ip): """ save/refresh nat rules on gateway """ ctx.logger.info("Save NAT configuration.") success = save_gateway_configuration(gateway, vca_client, ctx) if not success: return False ctx.logger.info("NAT configuration has been saved.") if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip else: service_type = get_vcloud_config().get('service_type') if is_ondemand(service_type): if not ctx.target.node.properties['nat'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx ) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if PORT_REPLACEMENT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PORT_REPLACEMENT] if SSH_PORT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PORT] if SSH_PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PUBLIC_IP] return True
def _save_configuration(gateway, vca_client, operation, public_ip): """ save/refresh nat rules on gateway """ ctx.logger.info("Save NAT configuration.") success = save_gateway_configuration(gateway, vca_client, ctx) if not success: return False ctx.logger.info("NAT configuration has been saved.") if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = public_ip else: service_type = get_vcloud_config().get('service_type') if is_ondemand(service_type): if not ctx.target.node.properties['nat'].get(PUBLIC_IP): del_ondemand_public_ip( vca_client, gateway, ctx.target.instance.runtime_properties[PUBLIC_IP], ctx) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if PORT_REPLACEMENT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PORT_REPLACEMENT] if SSH_PORT in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PORT] if SSH_PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[SSH_PUBLIC_IP] return True
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 True 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, ctx) if not success: return False if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip save_ssh_parameters(ctx, '22', 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) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if SSH_PUBLIC_IP in ctx.source.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PUBLIC_IP] if SSH_PORT in ctx.target.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PORT] return True
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 True 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, ctx) if not success: return False if operation == CREATE: ctx.target.instance.runtime_properties[PUBLIC_IP] = external_ip save_ssh_parameters(ctx, '22', 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) if PUBLIC_IP in ctx.target.instance.runtime_properties: del ctx.target.instance.runtime_properties[PUBLIC_IP] if SSH_PUBLIC_IP in ctx.source.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PUBLIC_IP] if SSH_PORT in ctx.source.instance.runtime_properties: del ctx.source.instance.runtime_properties[SSH_PORT] return True
def test_del_ondemand_public_ip(self): """ test release public ip """ fake_client = self.generate_client() gateway = self.generate_gateway() fake_ctx = self.generate_node_context() # can't deallocate ip gateway.deallocate_public_ip = mock.MagicMock(return_value=None) with mock.patch('vcloud_network_plugin.wait_for_gateway', mock.MagicMock()): with self.assertRaises(cfy_exc.NonRecoverableError): vcloud_network_plugin.del_ondemand_public_ip( fake_client, gateway, '127.0.0.1', fake_ctx) gateway.deallocate_public_ip.assert_called_with('127.0.0.1') # successfully dropped public ip gateway.deallocate_public_ip = mock.MagicMock( return_value=self.generate_task( vcloud_plugin_common.TASK_STATUS_SUCCESS ) ) with mock.patch('vcloud_plugin_common.ctx', fake_ctx): with mock.patch('vcloud_network_plugin.wait_for_gateway', mock.MagicMock()): vcloud_network_plugin.del_ondemand_public_ip( fake_client, gateway, '127.0.0.1', fake_ctx)