def delete_customer_gateway(context, customer_gateway_id): customer_gateway = ec2utils.get_db_item(context, customer_gateway_id) vpn_connections = db_api.get_items(context, "vpn") if any(vpn["customer_gateway_id"] == customer_gateway["id"] for vpn in vpn_connections): raise exception.IncorrectState(reason=_("The customer gateway is in use.")) db_api.delete_item(context, customer_gateway["id"]) return True
def delete_vpn_connection(context, vpn_connection_id): vpn_connection = ec2utils.get_db_item(context, vpn_connection_id) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, vpn_connection['id']) cleaner.addCleanup(db_api.restore_item, context, 'vpn', vpn_connection) neutron = clients.neutron(context) _stop_vpn_connection(neutron, vpn_connection) try: neutron.delete_ipsecpolicy(vpn_connection['os_ipsecpolicy_id']) except neutron_exception.Conflict as ex: LOG.warning( _('Failed to delete ipsecoplicy %(os_id)s during deleting ' 'VPN connection %(id)s. Reason: %(reason)s'), {'id': vpn_connection['id'], 'os_id': vpn_connection['os_ipsecpolicy_id'], 'reason': ex.message}) except neutron_exception.NotFound: pass try: neutron.delete_ikepolicy(vpn_connection['os_ikepolicy_id']) except neutron_exception.Conflict as ex: LOG.warning( _('Failed to delete ikepolicy %(os_id)s during deleting ' 'VPN connection %(id)s. Reason: %(reason)s'), {'id': vpn_connection['id'], 'os_id': vpn_connection['os_ikepolicy_id'], 'reason': ex.message}) except neutron_exception.NotFound: pass return True
def delete_internet_gateway(context, internet_gateway_id): igw = ec2utils.get_db_item(context, internet_gateway_id) if igw.get("vpc_id"): msg = _("The internetGateway '%(igw_id)s' has dependencies and " "cannot be deleted.") % {"igw_id": igw["id"]} raise exception.DependencyViolation(msg) db_api.delete_item(context, igw["id"]) return True
def delete_vpn_gateway(context, vpn_gateway_id): vpn_gateway = ec2utils.get_db_item(context, vpn_gateway_id) vpn_connections = db_api.get_items(context, "vpn") if vpn_gateway["vpc_id"] or any(vpn["vpn_gateway_id"] == vpn_gateway["id"] for vpn in vpn_connections): raise exception.IncorrectState(reason=_("The VPN gateway is in use.")) db_api.delete_item(context, vpn_gateway["id"]) return True
def delete_vpn_connection(context, vpn_connection_id): vpn_connection = ec2utils.get_db_item(context, vpn_connection_id) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, vpn_connection['id']) cleaner.addCleanup(db_api.restore_item, context, 'vpn', vpn_connection) neutron = clients.neutron(context) _stop_vpn_connection(neutron, vpn_connection) try: neutron.delete_ipsecpolicy(vpn_connection['os_ipsecpolicy_id']) except neutron_exception.Conflict as ex: LOG.warning( _('Failed to delete ipsecoplicy %(os_id)s during deleting ' 'VPN connection %(id)s. Reason: %(reason)s'), { 'id': vpn_connection['id'], 'os_id': vpn_connection['os_ipsecpolicy_id'], 'reason': ex.message }) except neutron_exception.NotFound: pass try: neutron.delete_ikepolicy(vpn_connection['os_ikepolicy_id']) except neutron_exception.Conflict as ex: LOG.warning( _('Failed to delete ikepolicy %(os_id)s during deleting ' 'VPN connection %(id)s. Reason: %(reason)s'), { 'id': vpn_connection['id'], 'os_id': vpn_connection['os_ikepolicy_id'], 'reason': ex.message }) except neutron_exception.NotFound: pass return True
def delete_group(self, context, group_name=None, group_id=None, delete_default=False): neutron = clients.neutron(context) if group_id is None or not group_id.startswith('sg-'): return SecurityGroupEngineNova().delete_group(context, group_name, group_id) security_group = ec2utils.get_db_item(context, group_id) try: if not delete_default: os_security_group = neutron.show_security_group( security_group['os_id']) if (os_security_group and os_security_group['security_group']['name'] == security_group['vpc_id']): raise exception.CannotDelete() neutron.delete_security_group(security_group['os_id']) except neutron_exception.Conflict as ex: # TODO(Alex): Instance ID is unknown here, report exception message # in its place - looks readable. raise exception.DependencyViolation( obj1_id=group_id, obj2_id=ex.message) except neutron_exception.NeutronClientException as ex: # TODO(Alex): do log error # TODO(Alex): adjust caught exception classes to catch: # the port doesn't exist pass db_api.delete_item(context, group_id)
def delete_group(self, context, group_name=None, group_id=None, delete_default=False): neutron = clients.neutron(context) if group_name: sg = describe_security_groups( context, group_name=[group_name])['securityGroupInfo'][0] group_id = sg['groupId'] group_name = None security_group = ec2utils.get_db_item(context, group_id) try: if not delete_default: os_security_group = neutron.show_security_group( security_group['os_id']) if (os_security_group and os_security_group['security_group']['name'] == security_group['vpc_id']): raise exception.CannotDelete() neutron.delete_security_group(security_group['os_id']) except neutron_exception.Conflict as ex: # TODO(Alex): Instance ID is unknown here, report exception message # in its place - looks readable. raise exception.DependencyViolation( obj1_id=group_id, obj2_id=ex.message) except neutron_exception.NeutronClientException as ex: # TODO(Alex): do log error # TODO(Alex): adjust caught exception classes to catch: # the port doesn't exist pass db_api.delete_item(context, group_id)
def release_address(self, context, public_ip, allocation_id): neutron = clients.neutron(context) if public_ip: # TODO(ft): implement search in DB layer address = next((addr for addr in db_api.get_items(context, 'eipalloc') if addr['public_ip'] == public_ip), None) if address and _is_address_valid(context, neutron, address): msg = _('You must specify an allocation id when releasing a ' 'VPC elastic IP address') raise exception.InvalidParameterValue(msg) return AddressEngineNova().release_address(context, public_ip, None) address = ec2utils.get_db_item(context, allocation_id) if not _is_address_valid(context, neutron, address): raise exception.InvalidAllocationIDNotFound( id=allocation_id) if 'network_interface_id' in address: raise exception.InvalidIPAddressInUse( ip_address=address['public_ip']) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, address['id']) cleaner.addCleanup(db_api.restore_item, context, 'eipalloc', address) try: neutron.delete_floatingip(address['os_id']) except neutron_exception.NotFound: pass
def release_address(self, context, public_ip, allocation_id): neutron = clients.neutron(context) if public_ip: # TODO(ft): implement search in DB layer address = next((addr for addr in db_api.get_items(context, 'eipalloc') if addr['public_ip'] == public_ip), None) if address and _is_address_valid(context, neutron, address): msg = _('You must specify an allocation id when releasing a ' 'VPC elastic IP address') raise exception.InvalidParameterValue(msg) return AddressEngineNova().release_address(context, public_ip, None) address = ec2utils.get_db_item(context, allocation_id) if not _is_address_valid(context, neutron, address): raise exception.InvalidAllocationIDNotFound(id=allocation_id) if 'network_interface_id' in address: raise exception.InvalidIPAddressInUse( ip_address=address['public_ip']) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, address['id']) cleaner.addCleanup(db_api.restore_item, context, 'eipalloc', address) try: neutron.delete_floatingip(address['os_id']) except neutron_exception.NotFound: pass
def delete_internet_gateway(context, internet_gateway_id): igw = ec2utils.get_db_item(context, internet_gateway_id) if igw.get('vpc_id'): msg = _("The internetGateway '%(igw_id)s' has dependencies and " "cannot be deleted.") % {'igw_id': igw['id']} raise exception.DependencyViolation(msg) db_api.delete_item(context, igw['id']) return True
def delete_vpn_gateway(context, vpn_gateway_id): vpn_gateway = ec2utils.get_db_item(context, vpn_gateway_id) vpn_connections = db_api.get_items(context, 'vpn') if vpn_gateway['vpc_id'] or any(vpn['vpn_gateway_id'] == vpn_gateway['id'] for vpn in vpn_connections): raise exception.IncorrectState(reason=_('The VPN gateway is in use.')) db_api.delete_item(context, vpn_gateway['id']) return True
def delete_vpc(context, vpc_id): vpc = ec2utils.get_db_item(context, vpc_id) subnets = subnet_api.describe_subnets(context, filter=[{ 'name': 'vpc-id', 'value': [vpc_id] }])['subnetSet'] internet_gateways = internet_gateway_api.describe_internet_gateways( context, filter=[{ 'name': 'attachment.vpc-id', 'value': [vpc['id']] }])['internetGatewaySet'] route_tables = route_table_api.describe_route_tables(context, filter=[{ 'name': 'vpc-id', 'value': [vpc['id']] }])['routeTableSet'] security_groups = security_group_api.describe_security_groups( context, filter=[{ 'name': 'vpc-id', 'value': [vpc['id']] }])['securityGroupInfo'] if (subnets or internet_gateways or len(route_tables) > 1 or len(security_groups) > 1): msg = _("The vpc '%(vpc_id)s' has dependencies and " "cannot be deleted.") msg = msg % {'vpc_id': vpc['id']} raise exception.DependencyViolation(msg) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, vpc['id']) cleaner.addCleanup(db_api.restore_item, context, 'vpc', vpc) route_table_api._delete_route_table(context, vpc['route_table_id'], cleaner=cleaner) if len(security_groups) > 0: security_group_api.delete_security_group( context, group_id=security_groups[0]['groupId'], delete_default=True) try: neutron.delete_router(vpc['os_id']) except neutron_exception.Conflict as ex: LOG.warning( _('Failed to delete router %(os_id)s during deleting ' 'VPC %(id)s. Reason: %(reason)s'), { 'id': vpc['id'], 'os_id': vpc['os_id'], 'reason': ex.message }) except neutron_exception.NotFound: pass return True
def delete_customer_gateway(context, customer_gateway_id): customer_gateway = ec2utils.get_db_item(context, customer_gateway_id) vpn_connections = db_api.get_items(context, 'vpn') if any(vpn['customer_gateway_id'] == customer_gateway['id'] for vpn in vpn_connections): raise exception.IncorrectState( reason=_('The customer gateway is in use.')) db_api.delete_item(context, customer_gateway['id']) return True
def deregister_image(context, image_id): os_image = ec2utils.get_os_image(context, image_id) _check_owner(context, os_image) glance = clients.glance(context) try: glance.images.delete(os_image.id) except glance_exception.HTTPNotFound: pass db_api.delete_item(context, image_id) return True
def delete_dhcp_options(context, dhcp_options_id): if not dhcp_options_id: raise exception.MissingParameter( _('DHCP options ID must be specified')) dhcp_options = ec2utils.get_db_item(context, dhcp_options_id) vpcs = db_api.get_items(context, 'vpc') for vpc in vpcs: if dhcp_options['id'] == vpc.get('dhcp_options_id'): raise exception.DependencyViolation(obj1_id=dhcp_options['id'], obj2_id=vpc['id']) db_api.delete_item(context, dhcp_options['id']) return True
def release_address(self, context, public_ip, allocation_id): neutron = clients.neutron(context) if public_ip: # TODO(ft): implement search in DB layer address = next((addr for addr in db_api.get_items(context, 'eipalloc') if addr['public_ip'] == public_ip), None) if address and _is_address_valid(context, neutron, address): msg = _('You must specify an allocation id when releasing a ' 'VPC elastic IP address') raise exception.InvalidParameterValue(msg) os_floating_ip = self.get_os_floating_ip_by_public_ip(context, public_ip) try: neutron.delete_floatingip(os_floating_ip['id']) except neutron_exception.NotFound: pass return address = ec2utils.get_db_item(context, allocation_id) if not _is_address_valid(context, neutron, address): raise exception.InvalidAllocationIDNotFound( id=allocation_id) if 'network_interface_id' in address: if CONF.disable_ec2_classic: network_interface_id = address['network_interface_id'] network_interface = db_api.get_item_by_id(context, network_interface_id) default_vpc = ec2utils.check_and_create_default_vpc(context) if default_vpc: default_vpc_id = default_vpc['id'] if (network_interface and network_interface['vpc_id'] == default_vpc_id): association_id = ec2utils.change_ec2_id_kind(address['id'], 'eipassoc') self.disassociate_address( context, association_id=association_id) else: raise exception.InvalidIPAddressInUse( ip_address=address['public_ip']) else: raise exception.InvalidIPAddressInUse( ip_address=address['public_ip']) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, address['id']) cleaner.addCleanup(db_api.restore_item, context, 'eipalloc', address) try: neutron.delete_floatingip(address['os_id']) except neutron_exception.NotFound: pass
def delete_dhcp_options(context, dhcp_options_id): if not dhcp_options_id: raise exception.MissingParameter( _('DHCP options ID must be specified')) dhcp_options = ec2utils.get_db_item(context, dhcp_options_id) vpcs = db_api.get_items(context, 'vpc') for vpc in vpcs: if dhcp_options['id'] == vpc.get('dhcp_options_id'): raise exception.DependencyViolation( obj1_id=dhcp_options['id'], obj2_id=vpc['id']) db_api.delete_item(context, dhcp_options['id']) return True
def delete_vpc(context, vpc_id): vpc = ec2utils.get_db_item(context, vpc_id) subnets = subnet_api.describe_subnets( context, filter=[{'name': 'vpc-id', 'value': [vpc_id]}])['subnetSet'] internet_gateways = internet_gateway_api.describe_internet_gateways( context, filter=[{'name': 'attachment.vpc-id', 'value': [vpc['id']]}])['internetGatewaySet'] route_tables = route_table_api.describe_route_tables( context, filter=[{'name': 'vpc-id', 'value': [vpc['id']]}])['routeTableSet'] security_groups = security_group_api.describe_security_groups( context, filter=[{'name': 'vpc-id', 'value': [vpc['id']]}])['securityGroupInfo'] vpn_gateways = vpn_gateway_api.describe_vpn_gateways( context, filter=[{'name': 'attachment.vpc-id', 'value': [vpc['id']]}])['vpnGatewaySet'] if (subnets or internet_gateways or len(route_tables) > 1 or len(security_groups) > 1 or vpn_gateways): msg = _("The vpc '%(vpc_id)s' has dependencies and " "cannot be deleted.") msg = msg % {'vpc_id': vpc['id']} raise exception.DependencyViolation(msg) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, vpc['id']) cleaner.addCleanup(db_api.restore_item, context, 'vpc', vpc) route_table_api._delete_route_table(context, vpc['route_table_id'], cleaner=cleaner) if len(security_groups) > 0: security_group_api.delete_security_group( context, group_id=security_groups[0]['groupId'], delete_default=True) try: neutron.delete_router(vpc['os_id']) except neutron_exception.Conflict as ex: LOG.warning('Failed to delete router %(os_id)s during deleting ' 'VPC %(id)s. Reason: %(reason)s', {'id': vpc['id'], 'os_id': vpc['os_id'], 'reason': ex.message}) except neutron_exception.NotFound: pass return True
def _delete_route_table(context, route_table_id, vpc=None, cleaner=None): def get_associated_subnets(): return [s for s in db_api.get_items(context, 'subnet') if s.get('route_table_id') == route_table_id] if (vpc and route_table_id == vpc['route_table_id'] or len(get_associated_subnets()) > 0): msg = _("The routeTable '%(rtb_id)s' has dependencies and cannot " "be deleted.") % {'rtb_id': route_table_id} raise exception.DependencyViolation(msg) if cleaner: route_table = db_api.get_item_by_id(context, route_table_id) db_api.delete_item(context, route_table_id) if cleaner and route_table: cleaner.addCleanup(db_api.restore_item, context, 'rtb', route_table)
def deregister_image(context, image_id): os_image = ec2utils.get_os_image(context, image_id) if not os_image: image = db_api.get_item_by_id(context, image_id) if image.get('state') != 'failed': # TODO(ft): figure out corresponding AWS error raise exception.IncorrectState( reason='Image is still being created') else: _check_owner(context, os_image) glance = clients.glance(context) try: glance.images.delete(os_image.id) except glance_exception.HTTPNotFound: pass db_api.delete_item(context, image_id) return True
def delete_subnet(context, subnet_id): subnet = ec2utils.get_db_item(context, subnet_id) vpc = db_api.get_item_by_id(context, subnet['vpc_id']) network_interfaces = network_interface_api.describe_network_interfaces( context, filter=[{ 'name': 'subnet-id', 'value': [subnet_id] }])['networkInterfaceSet'] if network_interfaces: msg = _("The subnet '%(subnet_id)s' has dependencies and " "cannot be deleted.") % { 'subnet_id': subnet_id } raise exception.DependencyViolation(msg) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, subnet['id']) cleaner.addCleanup(db_api.restore_item, context, 'subnet', subnet) vpn_gateway_api._stop_vpn_in_subnet(context, neutron, cleaner, subnet) try: neutron.remove_interface_router(vpc['os_id'], {'subnet_id': subnet['os_id']}) except neutron_exception.NotFound: pass cleaner.addCleanup(neutron.add_interface_router, vpc['os_id'], {'subnet_id': subnet['os_id']}) try: os_subnet = neutron.show_subnet(subnet['os_id'])['subnet'] except neutron_exception.NotFound: pass else: try: neutron.delete_network(os_subnet['network_id']) except neutron_exception.NetworkInUseClient as ex: LOG.warning( _('Failed to delete network %(os_id)s during ' 'deleting Subnet %(id)s. Reason: %(reason)s'), { 'id': subnet['id'], 'os_id': os_subnet['network_id'], 'reason': ex.message }) return True
def delete_network_interface(context, network_interface_id): network_interface = ec2utils.get_db_item(context, network_interface_id) if 'instance_id' in network_interface: msg = _("Network interface '%(eni_id)s' is currently in use.") msg = msg % {'eni_id': network_interface_id} raise exception.InvalidParameterValue(msg) for address in db_api.get_items(context, 'eipalloc'): if address.get('network_interface_id') == network_interface['id']: address_api._disassociate_address_item(context, address) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, network_interface['id']) cleaner.addCleanup(db_api.restore_item, context, 'eni', network_interface) try: neutron.delete_port(network_interface['os_id']) except neutron_exception.PortNotFoundClient: pass return True
def delete_subnet(context, subnet_id): subnet = ec2utils.get_db_item(context, subnet_id) vpc = db_api.get_item_by_id(context, subnet['vpc_id']) network_interfaces = network_interface_api.describe_network_interfaces( context, filter=[{'name': 'subnet-id', 'value': [subnet_id]}])['networkInterfaceSet'] if network_interfaces: msg = _("The subnet '%(subnet_id)s' has dependencies and " "cannot be deleted.") % {'subnet_id': subnet_id} raise exception.DependencyViolation(msg) neutron = clients.neutron(context) with common.OnCrashCleaner() as cleaner: db_api.delete_item(context, subnet['id']) cleaner.addCleanup(db_api.restore_item, context, 'subnet', subnet) vpn_gateway_api._stop_vpn_in_subnet(context, neutron, cleaner, subnet) try: neutron.remove_interface_router(vpc['os_id'], {'subnet_id': subnet['os_id']}) except neutron_exception.NotFound: pass cleaner.addCleanup(neutron.add_interface_router, vpc['os_id'], {'subnet_id': subnet['os_id']}) try: os_subnet = neutron.show_subnet(subnet['os_id'])['subnet'] except neutron_exception.NotFound: pass else: try: neutron.delete_network(os_subnet['network_id']) except neutron_exception.NetworkInUseClient as ex: LOG.warning(_('Failed to delete network %(os_id)s during ' 'deleting Subnet %(id)s. Reason: %(reason)s'), {'id': subnet['id'], 'os_id': os_subnet['network_id'], 'reason': ex.message}) return True
def test_delete_item(self): item = db_api.add_item(self.context, 'fake', {}) db_api.delete_item(self.context, item['id']) item = db_api.get_item_by_id(self.context, item['id']) self.assertIsNone(item) # NOTE(ft): delete not existing item should pass quitely db_api.delete_item(self.context, fakes.random_ec2_id('fake')) item = db_api.add_item(self.context, 'fake', {}) db_api.delete_item(self.other_context, item['id']) item = db_api.get_item_by_id(self.context, item['id']) self.assertIsNotNone(item)
def delete_obsolete_item(self, image): if image['os_id'] in self.local_images_os_ids: db_api.delete_item(self.context, image['id'])
def delete_obsolete_item(self, item): db_api.delete_item(self.context, item['id'])
def delete_obsolete_item(self, item): LOG.info('Deleting obsolete item %(item)s', {'item': str(item)}) db_api.delete_item(self.context, item['id'])
def delete_obsolete_item(self, item): LOG.info(_LI('Deleting obsolete item %(item)s') % {'item': str(item)}) db_api.delete_item(self.context, item['id'])