def get_gce_user_keypair_name(self, context): client = clients.nova(context) for keypair in client.keypairs.list(): if keypair.name == context.user_name: return keypair.name return None
def add_item(self, context, name, body, scope=None): # expected that either network is provided in parameters or # default network exists (as in Google) network = self._get_network_by_url( context, body.get('network', CONF.default_network_name) ) self._check_rules(body) default_description = _("Firewall rules for network {}") group_description = body.get( "description", default_description.format(network['name']) ) client = clients.nova(context) operation_util.start_operation(context) sg = client.security_groups.create(body['name'], group_description) try: rules = self._convert_to_secgroup_rules(body) for rule in rules: client.security_group_rules.create( sg.id, ip_protocol=rule["protocol"], from_port=rule["from_port"], to_port=rule["to_port"], cidr=rule["cidr"], ) except Exception: client.security_groups.delete(sg) raise new_firewall = utils.to_dict(client.security_groups.get(sg.id)) new_firewall = self._prepare_firewall(new_firewall) new_firewall["network_name"] = network["name"] new_firewall = self._add_db_item(context, new_firewall) self._process_callbacks( context, base_api._callback_reasons.post_add, new_firewall) return new_firewall
def add_item(self, context, name, body, scope=None): # expected that either network is provided in parameters or # default network exists (as in Google) network = self._get_network_by_url( context, body.get('network', CONF.default_network_name)) self._check_rules(body) default_description = _("Firewall rules for network {}") group_description = body.get( "description", default_description.format(network['name'])) client = clients.nova(context) operation_util.start_operation(context) sg = client.security_groups.create(body['name'], group_description) try: rules = self._convert_to_secgroup_rules(body) for rule in rules: client.security_group_rules.create( sg.id, ip_protocol=rule["protocol"], from_port=rule["from_port"], to_port=rule["to_port"], cidr=rule["cidr"], ) except Exception: client.security_groups.delete(sg) raise new_firewall = utils.to_dict(client.security_groups.get(sg.id)) new_firewall = self._prepare_firewall(new_firewall) new_firewall["network_name"] = network["name"] new_firewall = self._add_db_item(context, new_firewall) self._process_callbacks(context, base_api._callback_reasons.post_add, new_firewall) return new_firewall
def get_items(self, context, scope=None): client = clients.nova(context) try: nova_zones = client.availability_zones.list() except clients.novaclient.exceptions.Forbidden as e: try: nova_zones = client.availability_zones.list(detailed=False) except Exception: raise e filtered_zones = list() for zone in nova_zones: if not zone.hosts: filtered_zones.append(zone) continue for host in zone.hosts: if self.COMPUTE_SERVICE in zone.hosts[host]: filtered_zones.append(zone) break zones = list() for zone in filtered_zones: zones.append({ "name": zone.zoneName, "status": "UP" if zone.zoneState["available"] else "DOWN", "hosts": [host for host in zone.hosts] if zone.hosts else list() }) return zones
def _attach_volume(self, context): params = context.operation_data.get("params") if params: scope = context.operation_data["scope"] context.operation_data.pop("params") body = {"sizeGb": params.get("diskSizeGb"), "sourceImage": params["sourceImage"]} volume = disk_api.API().add_item(context, params.get("diskName"), body, scope=scope) context.operation_data["disk"] = volume return None disk = context.operation_data.get("disk") if disk: volume_id = disk["id"] item_progress = disk_api.API()._get_add_item_progress(context, volume_id) if not operation_util.is_final_progress(item_progress): return None context.operation_data.pop("disk") context.operation_data["volume_id"] = volume_id instance_id = context.operation_data["instance_id"] device_name = context.operation_data["device_name"] volume_id = context.operation_data["volume_id"] volumes_client = clients.nova(context).volumes volumes_client.create_server_volume( instance_id, volume_id, "/dev/" + device_name) args = context.operation_data["register_args"] self.register_item(context, args[0], volume_id, args[1], args[2]) return operation_util.get_final_progress()
def add_item(self, context, name, body, scope=None): network = self._get_network_by_url(context, body['network']) self._check_rules(body) group_description = body.get("description", "") client = clients.nova(context) operation_util.start_operation(context) sg = client.security_groups.create(body['name'], group_description) try: rules = self._convert_to_secgroup_rules(body) for rule in rules: client.security_group_rules.create( sg.id, ip_protocol=rule["protocol"], from_port=rule["from_port"], to_port=rule["to_port"], cidr=rule["cidr"], ) except Exception: client.security_groups.delete(sg) raise new_firewall = utils.to_dict(client.security_groups.get(sg.id)) new_firewall = self._prepare_firewall(new_firewall) new_firewall["creationTimestamp"] = 1 new_firewall["network_name"] = network["name"] new_firewall = self._add_db_item(context, new_firewall) self._process_callbacks( context, base_api._callback_reasons.post_add, new_firewall) return new_firewall
def _get_reset_instance_progress(self, context, instance_id): client = clients.nova(context) try: instance = client.servers.get(instance_id) except clients.novaclient.exceptions.NotFound: return operation_api.gef_final_progress() if instance.status != "HARD_REBOOT": return operation_api.gef_final_progress()
def _get_instances_with_network(self, context, network_name, scope): affected_instances = [] client = clients.nova(context) instances = client.servers.list(search_opts=None) for instance in instances: if network_name in instance.networks: affected_instances.append(instance) return affected_instances
def _get_add_item_progress(self, context, instance_id): client = clients.nova(context) try: instance = client.servers.get(instance_id) except clients.novaclient.exceptions.NotFound: return operation_api.gef_final_progress() if instance.status != "BUILD": return operation_api.gef_final_progress(instance.status == "ERROR")
def _get_delete_item_progress(self, context, instance_id): client = clients.nova(context) try: instance = client.servers.get(instance_id) except clients.novaclient.exceptions.NotFound: return operation_api.gef_final_progress() if getattr(instance, "OS-EXT-STS:task_state") != "deleting": return operation_api.gef_final_progress( instance.status != "DELETED")
def get_item(self, context, name, scope=None): client = clients.nova(context) try: network = client.networks.find(label=name) except clients.novaclient.exceptions.NotFound: msg = _("Network resource '%s' could not be found.") % name raise exception.NotFound(msg) gce_network = self._get_db_item_by_id(context, network.id) return self._prepare_network(utils.to_dict(network), gce_network)
def delete_network_firewalls(self, context, network): network_name = network["name"] client = clients.nova(context) for secgroup in self.get_network_firewalls(context, network_name): try: client.security_groups.delete(secgroup["id"]) except Exception: LOG.exception(("Failed to delete security group (%s) while" "delete network (%s))"), secgroup["name"], network_name)
def _get_gce_keypair(self, context): client = clients.nova(context) key_datas = [] for keypair in client.keypairs.list(): key_datas.append(keypair.name + ':' + keypair.public_key) if not key_datas: return None return {'key': 'sshKeys', 'value': "\n".join(key_datas)}
def get_item(self, context, name, scope=None): client = clients.nova(context) try: item = client.flavors.find(name=self._from_gce(name)) except (clients.novaclient.exceptions.NotFound, clients.novaclient.exceptions.NoUniqueMatch): raise exception.NotFound if not item: raise exception.NotFound return self._prepare_item(utils.to_dict(item))
def set_common_instance_metadata(self, context, metadata_list): instance_metadata = dict([(x['key'], x['value']) for x in metadata_list]) operation_util.start_operation(context) ssh_keys = instance_metadata.pop('sshKeys', None) if ssh_keys: nova_client = clients.nova(context) for key_data in ssh_keys.split('\n'): user_name, ssh_key = key_data.split(":") self._update_key(nova_client, user_name, ssh_key)
def set_common_instance_metadata(self, context, metadata_list): instance_metadata = dict( [(x['key'], x['value']) for x in metadata_list]) operation_util.start_operation(context) ssh_keys = instance_metadata.pop('sshKeys', None) if ssh_keys: nova_client = clients.nova(context) for key_data in ssh_keys.split('\n'): user_name, ssh_key = key_data.split(":") self._update_key(nova_client, user_name, ssh_key)
def reset_instance(self, context, scope, name): client = clients.nova(context) instances = client.servers.list(search_opts={"name": name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] operation_util.start_operation(context, self._get_reset_instance_progress, instance.id) instance.reboot("HARD")
def delete_item(self, context, name, scope=None): client = clients.nova(context) instances = client.servers.list(search_opts={"name": name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] operation_util.start_operation( context, base_api.API._get_complex_operation_progress) ads = instance_disk_api.API().get_items(context, instance.name) disks_to_delete = [] for ad in ads: if ad["auto_delete"]: disks_to_delete.append(ad) if not disks_to_delete: operation_util.set_item_id(context, instance.id, self.KIND) client = clients.nova(context) instance.delete() instance = utils.to_dict(instance) instance = self._prepare_instance(client, context, instance) self._delete_db_item(context, instance) ads = instance_disk_api.API().get_items(context, instance["name"]) for ad in ads: ad = instance_disk_api.API().unregister_item(context, instance["name"], ad["name"]) acs = instance_address_api.API().get_items(context, instance["name"]) for ac in acs: ac = instance_address_api.API().unregister_item(context, instance["name"], ac["name"]) if not disks_to_delete: return context.operation_data["scope"] = scope context.operation_data["count"] = 1 + len(disks_to_delete) context.operation_data["instance"] = instance context.operation_data["disks"] = disks_to_delete operation_util.continue_operation( context, lambda: self._delete_instance(context))
def delete_item(self, context, name, scope=None): network = self.get_item(context, name) self._process_callbacks( context, base_api._callback_reasons.check_delete, network) operation_util.start_operation(context) self._delete_db_item(context, network) self._process_callbacks( context, base_api._callback_reasons.pre_delete, network) client = clients.nova(context) client.networks.delete(network["id"])
def add_item(self, context, instance_name, params, source, name, auto_delete, scope): # NOTE(apavlov): name is a 'device_name' here if not name: msg = _("There is no name to assign.") raise exception.InvalidRequest(msg) nova_client = clients.nova(context) instances = nova_client.servers.list( search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] devices = list() volumes_client = nova_client.volumes for server_volume in volumes_client.get_server_volumes(instance.id): devices.append(server_volume.device) device_name = None for letter in string.ascii_lowercase[1:]: device_name = "vd" + letter for device in devices: if device_name in device: break else: break else: raise exception.OverQuota context.operation_data["device_name"] = device_name if source: volume_name = utils._extract_name_from_url(source) if not volume_name: msg = _("There is no volume to assign.") raise exception.NotFound(msg) volume = disk_api.API().get_item(context, volume_name, scope) context.operation_data["volume_id"] = volume["id"] elif params: params.setdefault("diskName", instance_name) context.operation_data["params"] = params context.operation_data["scope"] = scope else: msg = _('Disk config must contain either "source" or ' '"initializeParams".') raise exception.InvalidRequest(msg) context.operation_data["instance_id"] = instance.id context.operation_data["register_args"] = [ instance_name, name, auto_delete ] operation_util.start_operation( context, base_api.API._get_complex_operation_progress) operation_util.continue_operation(context, lambda: self._attach_volume(context), timeout=0)
def delete_item(self, context, name, scope=None): firewall = self.get_item(context, name) operation_util.start_operation(context) self._process_callbacks( context, base_api._callback_reasons.pre_delete, firewall) client = clients.nova(context) try: client.security_groups.delete(firewall["id"]) self._delete_db_item(context, firewall) except clients.novaclient.exceptions.ClientException as ex: raise exception.GceapiException(message=ex.message, code=ex.code)
def get_item(self, context, name, scope=None): client = clients.nova(context) try: firewall = client.security_groups.find(name=name) except (clients.novaclient.exceptions.NotFound, clients.novaclient.exceptions.NoUniqueMatch): raise exception.NotFound() firewall = self._prepare_firewall(utils.to_dict(firewall)) db_firewall = self._get_db_item_by_id(context, firewall["id"]) self._prepare_item(firewall, db_firewall) return firewall
def get_items(self, context, scope=None): client = clients.nova(context) firewalls = client.security_groups.list() items = list() gce_firewalls = self._get_db_items_dict(context) for firewall in firewalls: item = self._prepare_firewall(utils.to_dict(firewall)) self._prepare_item(item, gce_firewalls.get(item["id"])) items.append(item) self._purge_db(context, items, gce_firewalls) return items
def delete_item(self, context, name, scope=None): firewall = self.get_item(context, name) operation_util.start_operation(context) self._process_callbacks(context, base_api._callback_reasons.pre_delete, firewall) client = clients.nova(context) try: client.security_groups.delete(firewall["id"]) self._delete_db_item(context, firewall) except clients.novaclient.exceptions.ClientException as ex: raise exception.GceapiException(message=ex.message, code=ex.code)
def get_items(self, context, scope=None): client = clients.nova(context) networks = client.networks.list() gce_networks = self._get_db_items_dict(context) result_networks = [] for network in networks: result_networks.append( self._prepare_network(utils.to_dict(network), gce_networks.get(network.id))) self._purge_db(context, result_networks, gce_networks) return result_networks
def delete_item(self, context, name, scope=None): network = self.get_item(context, name) self._process_callbacks(context, base_api._callback_reasons.check_delete, network) operation_util.start_operation(context) self._delete_db_item(context, network) self._process_callbacks(context, base_api._callback_reasons.pre_delete, network) client = clients.nova(context) client.networks.delete(network["id"])
def delete_item(self, context, instance_name, name): client = clients.nova(context) instances = client.servers.list(search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] item = self.get_item(context, instance_name, name) floating_ip = item["addr"] operation_util.start_operation(context) instance.remove_floating_ip(floating_ip) self._delete_db_item(context, item)
def add_item(self, context, instance_name, params, source, name, auto_delete, scope): # NOTE(apavlov): name is a 'device_name' here if not name: msg = _("There is no name to assign.") raise exception.InvalidRequest(msg) nova_client = clients.nova(context) instances = nova_client.servers.list( search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] devices = list() volumes_client = nova_client.volumes for server_volume in volumes_client.get_server_volumes(instance.id): devices.append(server_volume.device) device_name = None for letter in string.ascii_lowercase[1:]: device_name = "vd" + letter for device in devices: if device_name in device: break else: break else: raise exception.OverQuota context.operation_data["device_name"] = device_name if source: volume_name = utils._extract_name_from_url(source) if not volume_name: msg = _("There is no volume to assign.") raise exception.NotFound(msg) volume = disk_api.API().get_item(context, volume_name, scope) context.operation_data["volume_id"] = volume["id"] elif params: params.setdefault("diskName", instance_name) context.operation_data["params"] = params context.operation_data["scope"] = scope else: msg = _('Disk config must contain either "source" or ' '"initializeParams".') raise exception.InvalidRequest(msg) context.operation_data["instance_id"] = instance.id context.operation_data["register_args"] = [instance_name, name, auto_delete] operation_util.start_operation( context, base_api.API._get_complex_operation_progress) operation_util.continue_operation( context, lambda: self._attach_volume(context), timeout=0)
def add_item(self, context, name, body, scope=None): client = clients.nova(context) if any(x["name"] == name for x in self._get_floating_ips(client, context, scope)): raise exception.InvalidInput( _("The resource '%s' already exists.") % name) operation_util.start_operation(context) result = client.floating_ips.create() floating_ip = self._prepare_floating_ip(client, context, result, scope) floating_ip["name"] = body["name"] if "description" in body: floating_ip["description"] = body["description"] floating_ip = self._add_db_item(context, floating_ip) return floating_ip
def _add_access_config(self, context, instance, scope, acs): progress = self._get_add_item_progress(context, instance["id"]) if progress is None or not operation_api.is_final_progress(progress): return progress client = clients.nova(context) try: instance = client.servers.get(instance["id"]) except clients.novaclient.exceptions.NotFound: return operation_api.gef_final_progress() for net in acs: ac = acs[net] instance_address_api.API().add_item(context, instance.name, net, ac.get("natIP"), ac.get("type"), ac.get("name")) return operation_api.gef_final_progress()
def add_item(self, context, name, body, scope=None): if any(x["name"] == name for x in self._get_floating_ips(context, scope)): raise exception.InvalidInput( _("The resource '%s' already exists.") % name) public_network_id = network_api.API().get_public_network_id(context) operation_util.start_operation(context) floating_ip = clients.neutron(context).create_floatingip( {"floatingip": {"floating_network_id": public_network_id}}) floating_ip = self._prepare_floating_ip( clients.nova(context), floating_ip["floatingip"], scope) floating_ip["name"] = body["name"] if "description" in body: floating_ip["description"] = body["description"] floating_ip = self._add_db_item(context, floating_ip) return floating_ip
def delete_item(self, context, instance_name, name): item = self.get_item(context, instance_name, name) volume_id = item["volume_id"] nova_client = clients.nova(context) instances = nova_client.servers.list( search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] operation_util.start_operation(context, self._get_delete_item_progress, item["id"]) nova_client.volumes.delete_server_volume(instance.id, volume_id) self._delete_db_item(context, item)
def _get_floating_ips(self, context, scope, name=None): results = clients.neutron(context).list_floatingips( tenant_id=context.project_id)["floatingips"] gce_floating_ips = self._get_db_items_dict(context) nova_client = clients.nova(context) results = [self._prepare_floating_ip(nova_client, x, scope, gce_floating_ips.get(x["id"])) for x in results] unnamed_ips = self._purge_db(context, results, gce_floating_ips) self._add_nonnamed_items(context, unnamed_ips) if name is None: return results for item in results: if item["name"] == name: return [item] raise exception.NotFound
def add_item(self, context, name, body, scope=None): if any(x["name"] == name for x in self._get_floating_ips(context, scope)): raise exception.InvalidInput( _("The resource '%s' already exists.") % name) public_network_id = network_api.API().get_public_network_id(context) operation_util.start_operation(context) floating_ip = clients.neutron(context).create_floatingip( {"floatingip": { "floating_network_id": public_network_id }}) floating_ip = self._prepare_floating_ip(clients.nova(context), floating_ip["floatingip"], scope) floating_ip["name"] = body["name"] if "description" in body: floating_ip["description"] = body["description"] floating_ip = self._add_db_item(context, floating_ip) return floating_ip
def get_item(self, context, name, scope=None): session = clients.admin_session() keystone = clients.keystone(context, session=session) project = keystone.projects.get(context.project_id) result = utils.to_dict(project) result["keypair"] = self._get_gce_keypair(context) project_id = project.id nova = clients.nova(context, session=session) nova_limits = nova.limits.get(tenant_id=project_id) result["nova_limits"] = dict( (l.name, l.value) for l in nova_limits.absolute) cinder_client = clients.cinder(context, session=session) try: result["cinder_quotas"] = utils.to_dict( cinder_client.quotas.get(project_id, usage=True)) except TypeError: # NOTE(apavlov): cinderclient of version 1.0.6 and below # has no usage parameter result["cinder_quotas"] = dict([ ("limit", x) for x in utils.to_dict(cinder_client.quotas.get(project_id)) ]) net_api = CONF.get("network_api") if net_api is None or ("quantum" in net_api or "neutron" in net_api): neutron_client = clients.neutron(context, session=session) result["neutron_quota"] = ( neutron_client.show_quota(project_id)["quota"]) result["neutron_quota"]["network_used"] = len( neutron_client.list_networks(tenant_id=project_id)["networks"]) result["neutron_quota"]["floatingip_used"] = len( neutron_client.list_floatingips( tenant_id=project_id)["floatingips"]) result["neutron_quota"]["security_group_used"] = len( neutron_client.list_security_groups( tenant_id=project_id)["security_groups"]) else: result["neutron_quota"] = {} return result
def _get_floating_ips(self, context, scope, name=None): results = clients.neutron(context).list_floatingips( tenant_id=context.project_id)["floatingips"] gce_floating_ips = self._get_db_items_dict(context) nova_client = clients.nova(context) results = [ self._prepare_floating_ip(nova_client, x, scope, gce_floating_ips.get(x["id"])) for x in results ] unnamed_ips = self._purge_db(context, results, gce_floating_ips) self._add_nonnamed_items(context, unnamed_ips) if name is None: return results for item in results: if item["name"] == name: return [item] raise exception.NotFound
def get_item(self, context, name, scope=None): project_name = context.project_name keystone = clients.keystone(context) project = [t for t in keystone.tenants.list() if t.name == project_name][0] result = utils.to_dict(project) result["keypair"] = self._get_gce_keypair(context) project_id = project.id nova_limits = clients.nova(context).limits.get(tenant_id=project_id) result["nova_limits"] = dict((l.name, l.value) for l in nova_limits.absolute) cinder_client = clients.cinder(context) try: result["cinder_quotas"] = utils.to_dict( cinder_client.quotas.get(project_id, usage=True)) except TypeError: # NOTE(apavlov): cinderclient of version 1.0.6 and below # has no usage parameter result["cinder_quotas"] = dict([("limit", x) for x in utils.to_dict(cinder_client.quotas.get(project_id))]) net_api = CONF.get("network_api") if net_api is None or ("quantum" in net_api or "neutron" in net_api): neutron_client = clients.neutron(context) result["neutron_quota"] = ( neutron_client.show_quota(project_id)["quota"]) result["neutron_quota"]["network_used"] = len(neutron_client .list_networks(tenant_id=project_id)["networks"]) result["neutron_quota"]["floatingip_used"] = len(neutron_client .list_floatingips(tenant_id=project_id)["floatingips"]) result["neutron_quota"]["security_group_used"] = len(neutron_client .list_security_groups(tenant_id=project_id)["security_groups"]) else: result["neutron_quota"] = {} return result
def search_items(self, context, search_opts, scope): client = clients.nova(context) instances = client.servers.list(search_opts=search_opts) filtered_instances = [] for instance in instances: iscope = getattr(instance, "OS-EXT-AZ:availability_zone") if scope is not None and scope.get_name() != iscope: continue instance = utils.to_dict(instance) instance = self._prepare_instance(client, context, instance) db_instance = self._get_db_item_by_id(context, instance["id"]) self._prepare_item(instance, db_instance) filtered_instances.append(instance) if len(filtered_instances) == len(instances) and not search_opts: gce_instances = self._get_db_items_dict(context) self._purge_db(context, filtered_instances, gce_instances) return filtered_instances
def add_item(self, context, name, body, scope=None): ip_range = body.get('IPv4Range', CONF.default_network_ip_range) gateway = body.get('gatewayIPv4') if gateway is None: network_cidr = netaddr.IPNetwork(ip_range) gateway_ip = netaddr.IPAddress(network_cidr.first + 1) gateway = str(gateway_ip) network = None try: network = self.get_item(context, name) except exception.NotFound: pass if network is not None: raise exception.DuplicateVlan kwargs = {'label': name, 'cidr': ip_range, 'gateway': gateway} client = clients.nova(context) operation_util.start_operation(context) network = client.networks.create(**kwargs) network = self._prepare_network(utils.to_dict(network)) if "description" in body: network["description"] = body["description"] return self._add_db_item(context, network)
def _attach_volume(self, context): params = context.operation_data.get("params") if params: scope = context.operation_data["scope"] context.operation_data.pop("params") body = { "sizeGb": params.get("diskSizeGb"), "sourceImage": params["sourceImage"] } volume = disk_api.API().add_item(context, params.get("diskName"), body, scope=scope) context.operation_data["disk"] = volume return None disk = context.operation_data.get("disk") if disk: volume_id = disk["id"] item_progress = disk_api.API()._get_add_item_progress( context, volume_id) if not operation_util.is_final_progress(item_progress): return None context.operation_data.pop("disk") context.operation_data["volume_id"] = volume_id instance_id = context.operation_data["instance_id"] device_name = context.operation_data["device_name"] volume_id = context.operation_data["volume_id"] volumes_client = clients.nova(context).volumes volumes_client.create_server_volume(instance_id, volume_id, "/dev/" + device_name) args = context.operation_data["register_args"] self.register_item(context, args[0], volume_id, args[1], args[2]) return operation_util.get_final_progress()
def get_items(self, context, scope=None): client = clients.nova(context) items = client.flavors.list() return [self._prepare_item(utils.to_dict(item)) for item in items]
def add_item(self, context, instance_name, nic, addr, addr_type, name): if not nic: msg = _("Network interface is invalid or empty") raise exception.InvalidRequest(msg) if addr_type is None: addr_type = self.DEFAULT_ACCESS_CONFIG_TYPE elif addr_type != self.DEFAULT_ACCESS_CONFIG_TYPE: msg = _("Only '%s' type of access config currently supported.")\ % self.DEFAULT_ACCESS_CONFIG_TYPE raise exception.InvalidRequest(msg) client = clients.nova(context) instances = client.servers.list(search_opts={"name": instance_name}) if not instances or len(instances) != 1: raise exception.NotFound instance = instances[0] fixed_ip = None for network in instance.addresses: if nic != network: continue for address in instance.addresses[network]: atype = address["OS-EXT-IPS:type"] if atype == "floating": msg = _('At most one access config currently supported.') raise exception.InvalidRequest(msg) if atype == "fixed": fixed_ip = address["addr"] if not fixed_ip: msg = _('Network interface not found') raise exception.InvalidRequest(msg) floating_ips = client.floating_ips.list() if addr is None: # NOTE(apavlov): try to find unused for floating_ip in floating_ips: if floating_ip.instance_id is None: addr = floating_ip.ip break else: msg = _('There is no unused floating ips.') raise exception.InvalidRequest(msg) else: for floating_ip in floating_ips: if floating_ip.ip != addr: continue if floating_ip.instance_id is None: break msg = _("Floating ip '%s' is already associated") % floating_ip raise exception.InvalidRequest(msg) else: msg = _("There is no such floating ip '%s'.") % addr raise exception.InvalidRequest(msg) operation_util.start_operation(context) instance.add_floating_ip(addr, fixed_ip) return self.register_item(context, instance_name, nic, addr, addr_type, name)
def get_items(self, context, scope=None): client = clients.nova(context) return self._get_floating_ips(client, context, scope)
def get_item_by_id(self, context, machine_type_id): client = clients.nova(context) item = client.flavors.get(machine_type_id) return self._prepare_item(utils.to_dict(item))
def delete_item(self, context, name, scope=None): client = clients.nova(context) floating_ip = self._get_floating_ips(client, context, scope, name)[0] operation_util.start_operation(context) self._delete_db_item(context, floating_ip) client.floating_ips.delete(floating_ip["id"])