Beispiel #1
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))
Beispiel #2
0
 def create_snapshot(self, req, body, scope_id, id):
     body["disk_name"] = id
     scope = self._get_scope(req, scope_id)
     context = self._get_context(req)
     operation_util.init_operation(context, "createSnapshot",
                                   self._type_name, id, scope)
     snapshot = snapshot_api.API().add_item(context, body, scope)
     # TODO(alexey-mr): workaround: have to set item id here
     # snapshot_api.API().add_item set_item_id has no effect because
     # of different type_name disk vs. snapshot
     # but snapshot type_name can't be used in init_operation because
     # targetLink and targetId should point to disk object
     operation_util.set_item_id(context, snapshot['id'], self._type_name)
Beispiel #3
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))
Beispiel #4
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))
Beispiel #5
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))
Beispiel #6
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
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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))
Beispiel #10
0
    def add_item(self, context, name, body, scope=None):
        name = body['name']
        client = clients.nova(context)

        flavor_name = utils._extract_name_from_url(body['machineType'])
        flavor_id = machine_type_api.API().get_item(
            context, flavor_name, scope)["id"]

        try:
            metadatas = body['metadata']['items']
        except KeyError:
            metadatas = []
        instance_metadata = dict([(x['key'], x['value']) for x in metadatas])

        ssh_keys = instance_metadata.pop('sshKeys', None)
        if ssh_keys is not None:
            key_name = ssh_keys.split('\n')[0].split(":")[0]
        else:
            key_name = project_api.API().get_gce_user_keypair_name(context)

        disks = body.get('disks', [])
        disks.sort(None, lambda x: x.get("boot", False), True)
        bdm = dict()
        diskDevice = 0
        for disk in disks:
            device_name = "vd" + string.ascii_lowercase[diskDevice]
            volume_name = utils._extract_name_from_url(disk["source"])
            volume = disk_api.API().get_item(context, volume_name, scope)
            disk["id"] = volume["id"]
            bdm[device_name] = volume["id"]
            diskDevice += 1

        nics = []
        #NOTE(ft) 'default' security group contains output rules
        #but output rules doesn't configurable by GCE API
        #all outgoing traffic permitted
        #so we support this behaviour
        groups_names = set(['default'])
        acs = dict()
        for net_iface in body['networkInterfaces']:
            net_name = utils._extract_name_from_url(net_iface["network"])
            ac = net_iface.get("accessConfigs")
            if ac:
                if len(ac) > 1:
                    msg = _('At most one access config currently supported.')
                    raise exception.InvalidRequest(msg)
                else:
                    acs[net_name] = ac[0]

            network = network_api.API().get_item(context, net_name, None)
            nics.append({"net-id": network["id"]})
            for sg in firewall_api.API().get_network_firewalls(
                    context, net_name):
                groups_names.add(sg["name"])
        groups_names = list(groups_names)

        operation_util.start_operation(context, self._get_add_item_progress)
        instance = client.servers.create(name, None, flavor_id,
            meta=instance_metadata, min_count=1, max_count=1,
            security_groups=groups_names, key_name=key_name,
            availability_zone=scope.get_name(), block_device_mapping=bdm,
            nics=nics)
        if not acs:
            operation_util.set_item_id(context, instance.id)

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

        instance = utils.to_dict(client.servers.get(instance.id))
        instance = self._prepare_instance(client, context, instance)
        if "descripton" in body:
            instance["description"] = body["description"]
        instance = self._add_db_item(context, instance)

        if acs:
            operation_util.continue_operation(
                    context,
                    lambda: self._add_access_config(context, instance,
                                                    scope, acs))

        return instance