Exemple #1
0
 def get_item(self, context, name, scope=None):
     client = clients.cinder(context)
     snapshots = client.volume_snapshots.list(
         search_opts={"display_name": name})
     if snapshots and len(snapshots) == 1:
         return self._prepare_item(client, utils.to_dict(snapshots[0]))
     raise exception.NotFound
Exemple #2
0
 def get_items(self, context, scope=None):
     client = clients.cinder(context)
     snapshots = [utils.to_dict(item)
                  for item in client.volume_snapshots.list()]
     for snapshot in snapshots:
         self._prepare_item(client, snapshot)
     return snapshots
Exemple #3
0
 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 _prepare_floating_ip(self,
                             client,
                             context,
                             floating_ip,
                             scope,
                             db_item=None):
        floating_ip = utils.to_dict(floating_ip)
        fixed_ip = floating_ip.get("fixed_ip")
        floating_ip = {
            "fixed_ip_address": fixed_ip if fixed_ip else None,
            "floating_ip_address": floating_ip["ip"],
            "id": floating_ip["id"],
            "port_id": None,
            "tenant_id": context.project_id,
            "scope": scope,
            "status": "IN USE" if fixed_ip else "RESERVED",
        }

        instance_id = floating_ip.get("instance_id")
        if instance_id is not None:
            instance = client.servers.get(instance_id)
            floating_ip["instance_name"] = instance.name
            floating_ip["instance_zone"] = getattr(
                instance, "OS-EXT-AZ:availability_zone")

        return self._prepare_item(floating_ip, db_item)
Exemple #5
0
 def get_item(self, context, name, scope=None):
     client = clients.cinder(context)
     snapshots = client.volume_snapshots.list(
         search_opts={"display_name": name})
     if snapshots and len(snapshots) == 1:
         return self._prepare_item(client, utils.to_dict(snapshots[0]))
     raise exception.NotFound
Exemple #6
0
 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
Exemple #7
0
    def add_item(self, context, name, body, scope=None):
        sizeGb = int(body['sizeGb']) if 'sizeGb' in body else None

        snapshot_uri = body.get("sourceSnapshot")
        image_uri = body.get("sourceImage")
        snapshot_id = None
        image_id = None

        client = clients.cinder(context)
        if snapshot_uri:
            snapshot_name = utils._extract_name_from_url(snapshot_uri)
            snapshots = client.volume_snapshots.list(
                search_opts={"display_name": snapshot_name})
            if not snapshots or len(snapshots) != 1:
                raise exception.NotFound
            snapshot_id = snapshots[0].id
        elif image_uri:
            image_name = utils._extract_name_from_url(image_uri)
            image = image_api.API().get_item(context, image_name, scope)
            image_id = image['id']
            # Cinder API doesn't get size from image, so we do this
            image_size_in_gb = (int(image['size']) + GB - 1) / GB
            if not sizeGb or sizeGb < image_size_in_gb:
                sizeGb = image_size_in_gb

        operation_util.start_operation(context, self._get_add_item_progress)
        volume = client.volumes.create(
            sizeGb, snapshot_id=snapshot_id,
            display_name=body.get('name'),
            display_description=body.get('description'),
            imageRef=image_id,
            availability_zone=scope.get_name())
        operation_util.set_item_id(context, volume.id)

        return self._prepare_item(client, utils.to_dict(volume))
Exemple #8
0
 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
Exemple #9
0
 def get_items(self, context, scope=None):
     client = clients.cinder(context)
     volumes = client.volumes.list()
     volumes = self._filter_volumes_by_zone(volumes, scope)
     volumes = [utils.to_dict(item) for item in volumes]
     for volume in volumes:
         self._prepare_item(client, volume)
     return volumes
Exemple #10
0
 def get_items(self, context, scope=None):
     client = clients.cinder(context)
     volumes = client.volumes.list()
     volumes = self._filter_volumes_by_zone(volumes, scope)
     volumes = [utils.to_dict(item) for item in volumes]
     for volume in volumes:
         self._prepare_item(client, volume)
     return volumes
Exemple #11
0
 def get_item(self, context, name, scope=None):
     client = clients.cinder(context)
     volumes = client.volumes.list(search_opts={"display_name": name})
     volumes = self._filter_volumes_by_zone(volumes, scope)
     volumes = [utils.to_dict(item) for item in volumes]
     if not volumes or len(volumes) != 1:
         raise exception.NotFound
     return self._prepare_item(client, volumes[0])
Exemple #12
0
 def get_items(self, context, scope=None):
     client = clients.cinder(context)
     snapshots = [
         utils.to_dict(item) for item in client.volume_snapshots.list()
     ]
     for snapshot in snapshots:
         self._prepare_item(client, snapshot)
     return snapshots
Exemple #13
0
 def get_item(self, context, name, scope=None):
     client = clients.cinder(context)
     volumes = client.volumes.list(search_opts={"display_name": name})
     volumes = self._filter_volumes_by_zone(volumes, scope)
     volumes = [utils.to_dict(item) for item in volumes]
     if not volumes or len(volumes) != 1:
         raise exception.NotFound
     return self._prepare_item(client, volumes[0])
Exemple #14
0
 def _prepare_item(self, client, item):
     item["name"] = item["display_name"]
     try:
         item["disk"] = utils.to_dict(client.volumes.get(item["volume_id"]))
     except Exception:
         pass
     item["status"] = self._status_map.get(item["status"], item["status"])
     return item
Exemple #15
0
 def _prepare_item(self, client, item):
     item["name"] = item["display_name"]
     try:
         item["disk"] = utils.to_dict(client.volumes.get(item["volume_id"]))
     except Exception:
         pass
     item["status"] = self._status_map.get(item["status"], item["status"])
     return item
Exemple #16
0
 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 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)
Exemple #18
0
 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 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))
Exemple #20
0
    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
Exemple #21
0
 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 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
Exemple #23
0
 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
Exemple #24
0
 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
Exemple #25
0
 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
Exemple #26
0
 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
Exemple #27
0
    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
Exemple #28
0
 def _prepare_item(self, client, item):
     snapshot = None
     snapshot_id = item["snapshot_id"]
     if snapshot_id:
         snapshot = utils.to_dict(client.volume_snapshots.get(snapshot_id))
     item["snapshot"] = snapshot
     item["status"] = self._status_map.get(item["status"], item["status"])
     item["name"] = item["display_name"]
     image = item.get("volume_image_metadata")
     if image:
         item["image_name"] = image["image_name"]
     return item
Exemple #29
0
 def _prepare_item(self, client, item):
     snapshot = None
     snapshot_id = item["snapshot_id"]
     if snapshot_id:
         snapshot = utils.to_dict(client.volume_snapshots.get(snapshot_id))
     item["snapshot"] = snapshot
     item["status"] = self._status_map.get(item["status"], item["status"])
     item["name"] = item["display_name"]
     image = item.get("volume_image_metadata")
     if image:
         item["image_name"] = image["image_name"]
     return item
Exemple #30
0
    def add_item(self, context, body, scope=None):
        name = body["name"]
        disk_name = body["disk_name"]
        client = clients.cinder(context)
        volumes = client.volumes.list(search_opts={"display_name": disk_name})
        if not volumes or len(volumes) != 1:
            raise exception.NotFound

        operation_util.start_operation(context, self._get_add_item_progress)
        snapshot = client.volume_snapshots.create(
            volumes[0].id, True, name, body["description"])
        operation_util.set_item_id(context, snapshot.id)

        return self._prepare_item(client, utils.to_dict(snapshot))
Exemple #31
0
    def add_item(self, context, body, scope=None):
        name = body["name"]
        disk_name = body["disk_name"]
        client = clients.cinder(context)
        volumes = client.volumes.list(search_opts={"display_name": disk_name})
        if not volumes or len(volumes) != 1:
            raise exception.NotFound

        operation_util.start_operation(context, self._get_add_item_progress)
        snapshot = client.volume_snapshots.create(volumes[0].id, True, name,
                                                  body.get("description"))
        operation_util.set_item_id(context, snapshot.id, self.KIND)

        return self._prepare_item(client, utils.to_dict(snapshot))
Exemple #32
0
 def get_items(self, context, scope=None):
     image_service = clients.glance(context).images
     # NOTE(apavlov): Currently glance doesn't report "killed" images in
     # list which causes incompatibility with GCE which reports
     # corresponding "FAILED" images if upload has failed.
     images = image_service.list(filters={"disk_format": "raw"})
     items = list()
     gce_images = self._get_db_items_dict(context)
     for image in images:
         result = self._prepare_image(utils.to_dict(image))
         self._prepare_item(result, gce_images.get(result["id"]))
         items.append(result)
     self._purge_db(context, items, gce_images)
     return items
Exemple #33
0
 def get_items(self, context, scope=None):
     image_service = clients.glance(context).images
     # NOTE(apavlov): Currently glance doesn't report "killed" images in
     # list which causes incompatibility with GCE which reports
     # corresponding "FAILED" images if upload has failed.
     images = image_service.list(filters={"disk_format": "raw"})
     items = list()
     gce_images = self._get_db_items_dict(context)
     for image in images:
         result = self._prepare_image(utils.to_dict(image))
         self._prepare_item(result, gce_images.get(result["id"]))
         items.append(result)
     self._purge_db(context, items, gce_images)
     return items
Exemple #34
0
 def get_item(self, context, name, scope=None):
     image_service = clients.glance(context).images
     images = image_service.list(filters={"name": name, "disk_format": "raw"})
     result = None
     for image in images:
         if image.status in self._deleted_statuses:
             continue
         if result:
             msg = _("Image resource '%s' found more than once") % name
             raise exception.NotFound(msg)
         result = self._prepare_image(utils.to_dict(image))
         db_image = self._get_db_item_by_id(context, result["id"])
         self._prepare_item(result, db_image)
     if not result:
         msg = _("Image resource '%s' could not be found") % name
         raise exception.NotFound(msg)
     return result
Exemple #35
0
 def get_item(self, context, name, scope=None):
     image_service = clients.glance(context).images
     images = image_service.list(
         filters={"name": name, "disk_format": "raw"})
     result = None
     for image in images:
         if image.status in self._deleted_statuses:
             continue
         if result:
             msg = _("Image resource '%s' found more than once") % name
             raise exception.NotFound(msg)
         result = self._prepare_image(utils.to_dict(image))
         db_image = self._get_db_item_by_id(context, result["id"])
         self._prepare_item(result, db_image)
     if not result:
         msg = _("Image resource '%s' could not be found") % name
         raise exception.NotFound(msg)
     return result
Exemple #36
0
    def _prepare_instance(self, client, context, instance):
        instance["statusMessage"] = instance["status"]
        instance["status"] = self._status_map.get(
            instance["status"], "STOPPED")
        instance["flavor"]["name"] = machine_type_api.API().get_item_by_id(
            context, instance["flavor"]["id"])["name"]

        cinder_client = clients.cinder(context)
        volumes = instance["os-extended-volumes:volumes_attached"]
        instance["volumes"] = [utils.to_dict(
            cinder_client.volumes.get(v["id"])) for v in volumes]
        ads = instance_disk_api.API().get_items(context, instance["name"])
        ads = dict((ad["volume_id"], ad) for ad in ads)
        for volume in instance["volumes"]:
            ad = ads.pop(volume["id"], None)
            if not ad:
                name = volume["display_name"]
                ad = instance_disk_api.API().register_item(context,
                    instance["name"], volume["id"], name, False)
            volume["device_name"] = ad["name"]
            volume["auto_delete"] = ad["auto_delete"]
        # NOTE(apavlov): cleanup unused from db for this instance
        for ad in ads:
            ad = instance_disk_api.API().unregister_item(context,
                instance["name"], ads[ad]["name"])

        acs = instance_address_api.API().get_items(context, instance["name"])
        acs = dict((ac["addr"], ac) for ac in acs)
        for network in instance["addresses"]:
            for address in instance["addresses"][network]:
                if address["OS-EXT-IPS:type"] == "floating":
                    ac = acs.pop(address["addr"], None)
                    if not ac:
                        ac = instance_address_api.API().register_item(context,
                            instance["name"], network, address["addr"],
                            None, None)
                    address["name"] = ac["name"]
                    address["type"] = ac["type"]
        # NOTE(apavlov): cleanup unused from db for this instance
        for ac in acs:
            ac = instance_address_api.API().unregister_item(context,
                instance["name"], acs[ac]["name"])

        return instance
Exemple #37
0
    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))
Exemple #38
0
    def add_item(self, context, name, body, scope=None):
        name = body["name"]
        image_ref = body["rawDisk"]["source"]
        meta = {
            "name": name,
            "disk_format": "raw",
            "container_format": "bare",
            "min_disk": 0,
            "min_ram": 0,
            "copy_from": image_ref,
        }
        image_service = clients.glance(context).images
        operation_util.start_operation(context, self._get_add_item_progress)
        image = image_service.create(**meta)
        operation_util.set_item_id(context, image.id, self.KIND)

        new_image = self._prepare_image(utils.to_dict(image))
        new_image["description"] = body.get("description", "")
        new_image["image_ref"] = image_ref
        new_image = self._add_db_item(context, new_image)
        return new_image
Exemple #39
0
    def add_item(self, context, name, body, scope=None):
        name = body['name']
        image_ref = body['rawDisk']['source']
        meta = {
            'name': name,
            'disk_format': 'raw',
            'container_format': 'bare',
            'min_disk': 0,
            'min_ram': 0,
            'copy_from': image_ref,
        }
        image_service = clients.glance(context).images
        operation_util.start_operation(context, self._get_add_item_progress)
        image = image_service.create(**meta)
        operation_util.set_item_id(context, image.id, self.KIND)

        new_image = self._prepare_image(utils.to_dict(image))
        new_image["description"] = body.get("description", "")
        new_image["image_ref"] = image_ref
        new_image = self._add_db_item(context, new_image)
        return new_image
Exemple #40
0
    def add_item(self, context, name, body, scope=None):
        name = body['name']
        image_ref = body['rawDisk']['source']
        meta = {
            'name': name,
            'disk_format': 'raw',
            'container_format': 'bare',
            'min_disk': 0,
            'min_ram': 0,
            'copy_from': image_ref,
        }
        image_service = clients.glance(context).images
        operation_util.start_operation(context, self._get_add_item_progress)
        image = image_service.create(**meta)
        operation_util.set_item_id(context, image.id)

        new_image = self._prepare_image(utils.to_dict(image))
        new_image["description"] = body.get("description", "")
        new_image["image_ref"] = image_ref
        new_image = self._add_db_item(context, new_image)
        return new_image
Exemple #41
0
    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)
Exemple #43
0
    def _prepare_floating_ip(self, client, context, floating_ip, scope,
                             db_item=None):
        floating_ip = utils.to_dict(floating_ip)
        fixed_ip = floating_ip.get("fixed_ip")
        floating_ip = {
            "fixed_ip_address": fixed_ip if fixed_ip else None,
            "floating_ip_address": floating_ip["ip"],
            "id": floating_ip["id"],
            "port_id": None,
            "tenant_id": context.project_id,
            "scope": scope,
            "status": "IN USE" if fixed_ip else "RESERVED",
        }

        instance_id = floating_ip.get("instance_id")
        if instance_id is not None:
            instance = client.servers.get(instance_id)
            floating_ip["instance_name"] = instance.name
            floating_ip["instance_zone"] = getattr(
                instance, "OS-EXT-AZ:availability_zone")

        return self._prepare_item(floating_ip, db_item)
Exemple #44
0
 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)
Exemple #45
0
    def add_item(self, context, name, body, scope=None):
        sizeGb = body.get("sizeGb")
        sizeGb = int(sizeGb) if sizeGb else None

        snapshot_uri = body.get("sourceSnapshot")
        image_uri = body.get("sourceImage")
        snapshot_id = None
        image_id = None

        client = clients.cinder(context)
        if snapshot_uri:
            snapshot_name = utils._extract_name_from_url(snapshot_uri)
            snapshots = client.volume_snapshots.list(
                search_opts={"display_name": snapshot_name})
            if not snapshots or len(snapshots) != 1:
                raise exception.NotFound
            snapshot_id = snapshots[0].id
        elif image_uri:
            image_name = utils._extract_name_from_url(image_uri)
            image = image_api.API().get_item(context, image_name, scope)
            image_id = image['id']
            # Cinder API doesn't get size from image, so we do this
            image_size_in_gb = (int(image['size']) + GB - 1) / GB
            if not sizeGb or sizeGb < image_size_in_gb:
                sizeGb = image_size_in_gb
        else:
            if not sizeGb:
                sizeGb = CONF.default_volume_size_gb

        operation_util.start_operation(context, self._get_add_item_progress)
        volume = client.volumes.create(
            sizeGb, snapshot_id=snapshot_id,
            display_name=body.get('name', name),
            display_description=body.get('description'),
            imageRef=image_id,
            availability_zone=scope.get_name())
        operation_util.set_item_id(context, volume.id, self.KIND)

        return self._prepare_item(client, utils.to_dict(volume))
Exemple #46
0
    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,
                                       self._get_delete_item_progress,
                                       instance.id)
        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"])
Exemple #47
0
 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]
Exemple #48
0
    def _create_instance(self, context):
        disks = context.operation_data["disks"]
        acs = context.operation_data["acs"]
        full_count = 1 + len(disks) + (1 if acs else 0)
        disk_device = context.operation_data["disk_device"]
        instance = context.operation_data.get("instance")
        progress = {"progress": int(100.0 * disk_device / full_count)}

        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 progress
            context.operation_data.pop("disk")
            disk_device += 1
            context.operation_data["disk_device"] = disk_device
            progress["progress"] = int(100.0 * disk_device / full_count)

        scope = context.operation_data["scope"]
        args = context.operation_data["args"]

        bdm = context.operation_data["bdm"]
        while disk_device < len(disks):
            disk = disks[disk_device]
            if "initializeParams" in disk:
                da = disk_api.API()
                params = disk["initializeParams"]
                body = {"sizeGb": params.get("diskSizeGb"),
                        "sourceImage": params["sourceImage"]}
                volume = da.add_item(context, params.get("diskName", args[0]),
                                     body, scope=scope)
                disk["id"] = volume["id"]
                context.operation_data["disk"] = disk
            # deviceName is optional parameter
            # use passed value if given, othewise generate new dev name
            device_name = disk.get("deviceName")
            if device_name is None:
                device_name = "vd" + string.ascii_lowercase[disk_device]
                disk["deviceName"] = device_name
            bdm[device_name] = disk["id"]

            if "initializeParams" in disk:
                return progress
            disk_device += 1
            context.operation_data["disk_device"] = disk_device

        if not instance:
            kwargs = context.operation_data["kwargs"]
            kwargs["block_device_mapping"] = bdm
            kwargs["availability_zone"] = scope.get_name()
            client = clients.nova(context)
            try:
                instance = client.servers.create(*args, **kwargs)

                for disk in disks:
                    instance_disk_api.API().register_item(context, args[0],
                        disk["id"], disk["deviceName"], disk["autoDelete"])

                instance = utils.to_dict(client.servers.get(instance.id))
                instance = self._prepare_instance(client, context, instance)
                instance["description"] = context.operation_data["description"]
                instance = self._add_db_item(context, instance)
            finally:
                try:
                    ssh_keys = context.operation_data["ssh_keys"]
                    if ssh_keys is not None:
                        client.keypairs.delete(kwargs["key_name"])
                except Exception:
                    pass
            context.operation_data["instance"] = instance
            return progress

        progress = self._get_add_item_progress(context, instance["id"])
        if not operation_util.is_final_progress(progress):
            return progress

        client = clients.nova(context)
        try:
            instance = client.servers.get(instance["id"])
        except clients.novaclient.exceptions.NotFound:
            return operation_util.get_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_util.get_final_progress()
Exemple #49
0
 def get_item(self, context, name, scope=None):
     client = clients.nova(context)
     network = client.networks.find(label=name)
     gce_network = self._get_db_item_by_id(context, network.id)
     return self._prepare_network(utils.to_dict(network), gce_network)
 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))
Exemple #51
0
 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 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]