예제 #1
0
파일: disk_api.py 프로젝트: tshrklr/gce-api
    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))
예제 #2
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
예제 #3
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
예제 #4
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
예제 #5
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
예제 #6
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
예제 #7
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])
예제 #8
0
 def delete_item(self, context, name, scope=None):
     client = clients.cinder(context).volume_snapshots
     snapshots = client.list(search_opts={"display_name": name})
     if not snapshots or len(snapshots) != 1:
         raise exception.NotFound
     operation_util.start_operation(context, self._get_delete_item_progress,
                                    snapshots[0].id)
     client.delete(snapshots[0])
예제 #9
0
 def _get_delete_item_progress(self, context, snapshot_id):
     client = clients.cinder(context)
     try:
         snapshot = client.volume_snapshots.get(snapshot_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_api.gef_final_progress()
     if snapshot.status not in ["deleting", "deleted"]:
         return operation_api.gef_final_progress(True)
예제 #10
0
파일: disk_api.py 프로젝트: tshrklr/gce-api
 def _get_add_item_progress(self, context, volume_id):
     client = clients.cinder(context)
     try:
         volume = client.volumes.get(volume_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_api.gef_final_progress()
     if (volume.status not in ["creating", "downloading"]):
         return operation_api.gef_final_progress(volume.status == "error")
예제 #11
0
파일: disk_api.py 프로젝트: tshrklr/gce-api
 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
예제 #12
0
파일: disk_api.py 프로젝트: tshrklr/gce-api
 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])
예제 #13
0
 def _get_add_item_progress(self, context, snapshot_id):
     client = clients.cinder(context)
     try:
         snapshot = client.volume_snapshots.get(snapshot_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_api.gef_final_progress()
     if (snapshot.status != "creating"):
         return operation_api.gef_final_progress(snapshot.status == "error")
예제 #14
0
 def _get_delete_item_progress(self, context, volume_id):
     client = clients.cinder(context)
     try:
         volume = client.volumes.get(volume_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_util.get_final_progress()
     if volume.status not in ["deleting", "deleted"]:
         return operation_util.get_final_progress(True)
     return None
예제 #15
0
 def _get_add_item_progress(self, context, volume_id):
     client = clients.cinder(context)
     try:
         volume = client.volumes.get(volume_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_util.get_final_progress()
     if (volume.status not in ["creating", "downloading"]):
         return operation_util.get_final_progress(volume.status == "error")
     return None
예제 #16
0
 def _get_delete_item_progress(self, context, snapshot_id):
     client = clients.cinder(context)
     try:
         snapshot = client.volume_snapshots.get(snapshot_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_util.get_final_progress()
     if snapshot.status not in ["deleting", "deleted"]:
         return operation_util.get_final_progress(True)
     return None
예제 #17
0
 def delete_item(self, context, name, scope=None):
     client = clients.cinder(context).volume_snapshots
     snapshots = client.list(search_opts={"display_name": name})
     if not snapshots or len(snapshots) != 1:
         raise exception.NotFound
     operation_util.start_operation(context,
                                    self._get_delete_item_progress,
                                    snapshots[0].id)
     client.delete(snapshots[0])
예제 #18
0
 def _get_add_item_progress(self, context, snapshot_id):
     client = clients.cinder(context)
     try:
         snapshot = client.volume_snapshots.get(snapshot_id)
     except clients.cinderclient.exceptions.NotFound:
         return operation_util.get_final_progress()
     if (snapshot.status != "creating"):
         return operation_util.get_final_progress(
             snapshot.status == "error")
     return None
예제 #19
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))
예제 #20
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))
예제 #21
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
예제 #22
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
예제 #23
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
예제 #24
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))
예제 #25
0
    def _delete_instance(self, context):
        progress = {"progress": 0}
        full_count = context.operation_data.get("count")
        disks = context.operation_data.get("disks")
        instance = context.operation_data.get("instance")
        if instance:
            item_progress = self._get_delete_item_progress(context,
                                                           instance["id"])
            if not operation_util.is_final_progress(item_progress):
                return progress
            context.operation_data.pop("instance")

        progress = {"progress": int(100.0 * (full_count - len(disks))
                                    / full_count)}

        disk = context.operation_data.get("disk")
        if disk:
            volume_id = disk["volume_id"]
            item_progress = disk_api.API()._get_delete_item_progress(context,
                                                                     volume_id)
            if not operation_util.is_final_progress(item_progress):
                return progress
            context.operation_data.pop("disk")
            progress = {"progress": int(100.0 * (full_count - len(disks) + 1)
                                        / full_count)}

        if disks:
            disk = disks.pop()
            try:
                cinder_client = clients.cinder(context)
                volume = cinder_client.volumes.get(disk["volume_id"])
                cinder_client.volumes.delete(volume)
                context.operation_data["disk"] = disk
            except Exception:
                LOG.exception("Failed to remove disk %s of instance" %
                              disk["volume_id"])
            return progress

        return operation_util.get_final_progress()