예제 #1
0
 def WarnAboutScopeDeprecationsAndMaintenance(self, disk_refs, client):
   # Check if the zone is deprecated or has maintenance coming.
   zone_resource_fetcher = zone_utils.ZoneResourceFetcher(client)
   zone_resource_fetcher.WarnForZonalCreation(
       (ref for ref in disk_refs if ref.Collection() == 'compute.disks'))
   # Check if the region is deprecated or has maintenance coming.
   region_resource_fetcher = region_utils.RegionResourceFetcher(client)
   region_resource_fetcher.WarnForRegionalCreation(
       (ref for ref in disk_refs if ref.Collection() == 'compute.regionDisks'))
예제 #2
0
def WarnAboutScopeDeprecations(ips_refs, client):
    # Check if the zone is deprecated.
    zone_resource_fetcher = zone_utils.ZoneResourceFetcher(client)
    zone_resource_fetcher.WarnForZonalCreation(
        (ref for ref in ips_refs
         if ref.Collection() == 'compute.zoneInPlaceSnapshots'))
    # Check if the region is deprecated.
    region_resource_fetcher = region_utils.RegionResourceFetcher(client)
    region_resource_fetcher.WarnForRegionalCreation(
        (ref for ref in ips_refs
         if ref.Collection() == 'compute.regionInPlaceSnapshots'))
예제 #3
0
    def Run(self, args):
        compute_holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = compute_holder.client

        disk_refs = self.ValidateAndParse(args, compute_holder)

        size_gb = utils.BytesToGb(args.size)

        from_image = args.image or args.image_family
        if not size_gb and not args.source_snapshot and not from_image:
            if args.type and 'pd-ssd' in args.type:
                size_gb = constants.DEFAULT_SSD_DISK_SIZE_GB
            else:
                size_gb = constants.DEFAULT_STANDARD_DISK_SIZE_GB

        utils.WarnIfDiskSizeIsTooSmall(size_gb, args.type)

        requests = []

        # Check if the zone is deprecated or has maintenance coming.
        zone_resource_fetcher = zone_utils.ZoneResourceFetcher(client)
        zone_resource_fetcher.WarnForZonalCreation(
            (ref for ref in disk_refs if ref.Collection() == 'compute.disks'))
        # Check if the region is deprecated or has maintenance coming.
        region_resource_fetcher = region_utils.RegionResourceFetcher(client)
        region_resource_fetcher.WarnForRegionalCreation(
            (ref for ref in disk_refs
             if ref.Collection() == 'compute.regionDisks'))

        project_to_source_image = {}

        image_expander = image_utils.ImageExpander(client,
                                                   compute_holder.resources)

        for disk_ref in disk_refs:
            if from_image:
                if disk_ref.project not in project_to_source_image:
                    source_image_uri, _ = image_expander.ExpandImageFlag(
                        user_project=disk_ref.project,
                        image=args.image,
                        image_family=args.image_family,
                        image_project=args.image_project,
                        return_image_resource=False)
                    project_to_source_image[
                        disk_ref.project] = argparse.Namespace()
                    project_to_source_image[
                        disk_ref.project].uri = source_image_uri
            else:
                project_to_source_image[
                    disk_ref.project] = argparse.Namespace()
                project_to_source_image[disk_ref.project].uri = None

        snapshot_ref = disks_flags.SOURCE_SNAPSHOT_ARG.ResolveAsResource(
            args, compute_holder.resources)
        if snapshot_ref:
            snapshot_uri = snapshot_ref.SelfLink()
        else:
            snapshot_uri = None

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        csek_keys = csek_utils.CsekKeyStore.FromArgs(args, allow_rsa_encrypted)

        for project in project_to_source_image:
            source_image_uri = project_to_source_image[project].uri
            project_to_source_image[project].keys = (
                csek_utils.MaybeLookupKeyMessagesByUri(
                    csek_keys, compute_holder.resources,
                    [source_image_uri, snapshot_uri], client.apitools_client))

        labels = None
        args_labels = getattr(args, 'labels', None)
        if args_labels:
            labels = client.messages.Disk.LabelsValue(additionalProperties=[
                client.messages.Disk.LabelsValue.AdditionalProperty(
                    key=key, value=value)
                for key, value in sorted(args.labels.iteritems())
            ])

        for disk_ref in disk_refs:
            if args.type:
                if disk_ref.Collection() == 'compute.disks':
                    type_ref = compute_holder.resources.Parse(
                        args.type,
                        collection='compute.diskTypes',
                        params={
                            'project': disk_ref.project,
                            'zone': disk_ref.zone
                        })
                elif disk_ref.Collection() == 'compute.regionDisks':
                    type_ref = compute_holder.resources.Parse(
                        args.type,
                        collection='compute.regionDiskTypes',
                        params={
                            'project': disk_ref.project,
                            'region': disk_ref.region
                        })
                type_uri = type_ref.SelfLink()
            else:
                type_uri = None

            if csek_keys:
                disk_key_or_none = csek_keys.LookupKey(
                    disk_ref, args.require_csek_key_create)
                disk_key_message_or_none = csek_utils.MaybeToMessage(
                    disk_key_or_none, client.apitools_client)
                kwargs = {
                    'diskEncryptionKey':
                    disk_key_message_or_none,
                    'sourceImageEncryptionKey':
                    project_to_source_image[disk_ref.project].keys[0],
                    'sourceSnapshotEncryptionKey':
                    project_to_source_image[disk_ref.project].keys[1]
                }
            else:
                kwargs = {}

            if disk_ref.Collection() == 'compute.disks':
                request = client.messages.ComputeDisksInsertRequest(
                    disk=client.messages.Disk(name=disk_ref.Name(),
                                              description=args.description,
                                              sizeGb=size_gb,
                                              sourceSnapshot=snapshot_uri,
                                              type=type_uri,
                                              **kwargs),
                    project=disk_ref.project,
                    sourceImage=project_to_source_image[disk_ref.project].uri,
                    zone=disk_ref.zone)
                if labels:
                    request.disk.labels = labels
                request = (client.apitools_client.disks, 'Insert', request)
            elif disk_ref.Collection() == 'compute.regionDisks':

                def SelfLink(zone, disk_ref):
                    return compute_holder.resources.Parse(
                        zone,
                        collection='compute.zones',
                        params={
                            'project': disk_ref.project
                        }).SelfLink()

                zones = [
                    SelfLink(zone, disk_ref) for zone in args.replica_zones
                ]
                request = client.messages.ComputeRegionDisksInsertRequest(
                    disk=client.messages.Disk(name=disk_ref.Name(),
                                              description=args.description,
                                              sizeGb=size_gb,
                                              sourceSnapshot=snapshot_uri,
                                              type=type_uri,
                                              replicaZones=zones,
                                              **kwargs),
                    project=disk_ref.project,
                    sourceImage=project_to_source_image[disk_ref.project].uri,
                    region=disk_ref.region)
                if labels:
                    request.disk.labels = labels

                request = (client.apitools_client.regionDisks, 'Insert',
                           request)

            requests.append(request)

        return client.MakeRequests(requests)
예제 #4
0
    def CreateRequests(self, args):
        """Returns a list of requests necessary for adding disks."""

        self.Validate(args)

        size_gb = utils.BytesToGb(args.size)

        from_image = args.image or args.image_family
        if not size_gb and not args.source_snapshot and not from_image:
            if args.type and 'pd-ssd' in args.type:
                size_gb = constants.DEFAULT_SSD_DISK_SIZE_GB
            else:
                size_gb = constants.DEFAULT_STANDARD_DISK_SIZE_GB

        utils.WarnIfDiskSizeIsTooSmall(size_gb, args.type)

        requests = []
        disk_refs = Create.disks_arg.ResolveAsResource(
            args,
            self.resources,
            scope_lister=flags.GetDefaultScopeLister(self.compute_client,
                                                     self.project))

        # Check if the zone is deprecated or has maintenance coming.
        self.WarnForZonalCreation(
            (ref for ref in disk_refs if ref.Collection() == 'compute.disks'))
        # Check if the region is deprecated or has maintenance coming.
        region_resource_fetcher = region_utils.RegionResourceFetcher(
            self.compute_client)
        region_resource_fetcher.WarnForRegionalCreation(
            (ref for ref in disk_refs
             if ref.Collection() == 'compute.regionDisks'))

        if from_image:
            source_image_uri, _ = self.ExpandImageFlag(
                user_project=self.project,
                image=args.image,
                image_family=args.image_family,
                image_project=args.image_project,
                return_image_resource=False)
        else:
            source_image_uri = None

        snapshot_ref = disks_flags.SOURCE_SNAPSHOT_ARG.ResolveAsResource(
            args, self.resources)
        if snapshot_ref:
            snapshot_uri = snapshot_ref.SelfLink()
        else:
            snapshot_uri = None

        # This feature is only exposed in alpha/beta
        allow_rsa_encrypted = self.ReleaseTrack() in [
            base.ReleaseTrack.ALPHA, base.ReleaseTrack.BETA
        ]
        csek_keys = csek_utils.CsekKeyStore.FromArgs(args, allow_rsa_encrypted)

        image_key_message_or_none, snapshot_key_message_or_none = (
            csek_utils.MaybeLookupKeyMessagesByUri(
                csek_keys, self.resources, [source_image_uri, snapshot_uri],
                self.compute))

        for disk_ref in disk_refs:
            if args.type:
                if disk_ref.Collection() == 'compute.disks':
                    type_ref = self.resources.Parse(
                        args.type,
                        collection='compute.diskTypes',
                        params={'zone': disk_ref.zone})
                elif disk_ref.Collection() == 'compute.regionDisks':
                    type_ref = self.resources.Parse(
                        args.type,
                        collection='compute.regionDiskTypes',
                        params={'region': disk_ref.region})
                type_uri = type_ref.SelfLink()
            else:
                type_uri = None

            if csek_keys:
                disk_key_or_none = csek_keys.LookupKey(
                    disk_ref, args.require_csek_key_create)
                disk_key_message_or_none = csek_utils.MaybeToMessage(
                    disk_key_or_none, self.compute)
                kwargs = {
                    'diskEncryptionKey': disk_key_message_or_none,
                    'sourceImageEncryptionKey': image_key_message_or_none,
                    'sourceSnapshotEncryptionKey': snapshot_key_message_or_none
                }
            else:
                kwargs = {}

            if disk_ref.Collection() == 'compute.disks':
                request = self.messages.ComputeDisksInsertRequest(
                    disk=self.messages.Disk(name=disk_ref.Name(),
                                            description=args.description,
                                            sizeGb=size_gb,
                                            sourceSnapshot=snapshot_uri,
                                            type=type_uri,
                                            **kwargs),
                    project=disk_ref.project,
                    sourceImage=source_image_uri,
                    zone=disk_ref.zone)
            elif disk_ref.Collection() == 'compute.regionDisks':

                def SelfLink(zone, disk_ref):
                    return self.resources.Parse(zone,
                                                collection='compute.zones',
                                                params={
                                                    'project': disk_ref.project
                                                }).SelfLink()

                zones = [
                    SelfLink(zone, disk_ref) for zone in args.replica_zones
                ]
                request = self.messages.ComputeRegionDisksInsertRequest(
                    disk=self.messages.Disk(name=disk_ref.Name(),
                                            description=args.description,
                                            sizeGb=size_gb,
                                            sourceSnapshot=snapshot_uri,
                                            type=type_uri,
                                            replicaZones=zones,
                                            **kwargs),
                    project=disk_ref.project,
                    sourceImage=source_image_uri,
                    region=disk_ref.region)
                request = (self.compute.regionDisks, self.method, request)
            requests.append(request)

        return requests