示例#1
0
    def delete(self):
        config = flask.current_app.osloconfig
        nova = OpenStackClients(config).nova()
        neutron = OpenStackClients(config).neutron()
        compute_ids = flask.request.args.getlist('computeId')

        ports = neutron.list_ports(device_id=compute_ids)
        for port in ports["ports"]:
            floatingIPs = neutron.list_floatingips(port_id=port['id'])
            for floatingIP in floatingIPs['floatingips']:
                neutron.delete_floatingip(floatingIP['id'])
            neutron.delete_port(port['id'])
        stopped_servers = []
        for compute_id in compute_ids:
            try:
                address = None
                server = nova.servers.get(compute_id)
                for key, value in server.addresses.items():
                    for item in value:
                        if item['OS-EXT-IPS:type'] == 'floating':
                            address = item['addr']
                            break
                server.delete()
                if address != None:
                    floatingip_id = neutron.list_floatingips(
                        floating_ip_address=address)['floatingips'][0]['id']
                    neutron.delete_floatingip(floatingip_id)

                stopped_servers.append(compute_id)
            except Exception:
                pass

        return flask.jsonify(stopped_servers), OK
示例#2
0
    def delete(self):
        ids = flask.request.args.getlist('networkResourceId')
        config = flask.current_app.osloconfig
        neutron = OpenStackClients(config).neutron()

        deleted_ids = []
        for id in ids:

            if neutron.list_networks(id=id)['networks']:
                try:
                    neutron.delete_network(id)
                    deleted_ids.append(id)
                except Exception:
                    pass

            if neutron.list_subnets(id=id)['subnets']:

                # import ipdb; ipdb.set_trace()
                # -- Identifying an admin interface
                # An admin interface is a special interface, when you delete it, you need to delete the vtep vm if there is
                # one and release the floating ip used to access it

                # Check if the subnet has a gateway interface, that means it's an admin subnet
                if subnet_has_gateway_interface(neutron, id):
                    address = None
                    # check if there is a vtep vm to delete
                    nova = OpenStackClients(config).nova()
                    vtep_name = conf.cfg.CONF.vtep.vtep_name
                    server_list = nova.servers.findall()
                    try:
                        for server in server_list:
                            if server.name == vtep_name:
                                for key, value in server.addresses.items():
                                    for item in value:
                                        if item['OS-EXT-IPS:type'] == 'floating':
                                            address = item['addr']
                                            # print ('---> found address: ', address)
                                            break

                                # delete server
                                server = nova.servers.delete(server.id)
                                # just to avoid exception check if we have found a floating address
                                if address != None:
                                    floatingip_id = neutron.list_floatingips(
                                        floating_ip_address=address
                                    )['floatingips'][0]['id']
                                    float_delete = neutron.delete_floatingip(
                                        floatingip_id)
                                    print(float_delete)
                                break
                    except Exception as error_msg:
                        print('-----> exception: ' + str(error_msg))
                        pass
                    # TODO either I look for the router in the list or to improve speed, I put it in conf file
                    # sometime there is more than one router in the tenant so maybe it would be better to have
                    # the router_id in conf

                delete_router_interface_and_router(neutron, id)
                # delete subnet
                neutron.delete_subnet(id)
                deleted_ids.append(id)

            if neutron.list_ports(id=id)['ports']:
                try:
                    neutron.delete_port(id)
                    deleted_ids.append(id)
                except Exception:
                    pass

            if neutron.list_floatingips(id=id)['floatingips']:
                try:
                    neutron.delete_floatingip(id)
                    deleted_ids.append(id)
                except Exception:
                    pass

            # routers = neutron.list_routers(id=id)
            if neutron.list_routers(id=id)['routers']:
                try:
                    neutron.delete_router(id)
                    deleted_ids.append(id)
                except Exception as error_msg:
                    print('-----> exception: ' + str(error_msg))
                    pass

        return flask.jsonify(deleted_ids), OK