Example #1
0
  def CreateRequests(self, args):
    """Returns a list of requests necessary for adding images."""

    image = self.messages.Image(
        name=args.name,
        description=args.description,
        sourceType=self.messages.Image.SourceTypeValueValuesEnum.RAW)

    # Validate parameters.
    if args.source_disk_zone and not args.source_disk:
      raise exceptions.ToolException(
          'You cannot specify [--source-disk-zone] unless you are specifying '
          '[--source-disk].')

    if args.source_uri:
      source_uri = utils.NormalizeGoogleStorageUri(args.source_uri)
      image.rawDisk = self.messages.Image.RawDiskValue(source=source_uri)
    else:
      source_disk_ref = self.CreateZonalReference(
          args.source_disk,
          args.source_disk_zone,
          flag_names=['--source-disk-zone'],
          resource_type='disks')
      image.sourceDisk = source_disk_ref.SelfLink()

    request = self.messages.ComputeImagesInsertRequest(
        image=image,
        project=self.project)

    return [request]
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        # TODO(b/33690890): remove this check after the deprecation
        if args.bucket is None and not args.no_bucket:
            # The user gave neither flag but one of them is required. Using
            # required=True for the group would be the prefered way to handle this
            # but it would prevent the deprecated case.
            raise BucketRequiredError(
                'one of the arguments --no-bucket --bucket '
                'is required')

        if not args.bucket and args.prefix:
            raise calliope_exceptions.ToolException(
                '[--prefix] cannot be specified when unsetting the usage bucket.'
            )

        bucket_uri = utils.NormalizeGoogleStorageUri(args.bucket or None)

        request = client.messages.ComputeProjectsSetUsageExportBucketRequest(
            project=properties.VALUES.core.project.GetOrFail(),
            usageExportLocation=client.messages.UsageExportLocation(
                bucketName=bucket_uri,
                reportNamePrefix=args.prefix,
            ))

        return client.MakeRequests([(client.apitools_client.projects,
                                     'SetUsageExportBucket', request)])
Example #3
0
    def CreateRequests(self, args):
        # TODO(b/33690890): remove this check after the deprecation
        if args.bucket is None and not args.no_bucket:
            # The user gave neither flag but one of them is required. Using
            # required=True for the group would be the prefered way to handle this
            # but it would prevent the deprecated case.
            raise BucketRequiredError(
                'one of the arguments --no-bucket --bucket '
                'is required')

        if not args.bucket and args.prefix:
            raise calliope_exceptions.ToolException(
                '[--prefix] cannot be specified when unsetting the usage bucket.'
            )

        bucket_uri = utils.NormalizeGoogleStorageUri(args.bucket or None)

        request = self.messages.ComputeProjectsSetUsageExportBucketRequest(
            project=self.project,
            usageExportLocation=self.messages.UsageExportLocation(
                bucketName=bucket_uri,
                reportNamePrefix=args.prefix,
            ))

        return [request]
Example #4
0
    def CreateRequests(self, args):
        """Returns a list of requests necessary for adding images."""

        image = self.messages.Image(
            name=args.name,
            description=args.description,
            sourceType=self.messages.Image.SourceTypeValueValuesEnum.RAW,
            family=args.family)

        csek_keys = csek_utils.CsekKeyStore.FromArgs(
            args, self._ALLOW_RSA_ENCRYPTED_CSEK_KEYS)
        if csek_keys:
            image_ref = self.resources.Parse(args.name,
                                             collection='compute.images')
            image.imageEncryptionKey = csek_utils.MaybeToMessage(
                csek_keys.LookupKey(
                    image_ref, raise_if_missing=args.require_csek_key_create),
                self.compute_client.apitools_client)

        # Validate parameters.
        if args.source_disk_zone and not args.source_disk:
            raise exceptions.ToolException(
                'You cannot specify [--source-disk-zone] unless you are specifying '
                '[--source-disk].')

        if args.source_disk and args.source_uri:
            raise exceptions.ConflictingArgumentsException(
                '--source-uri', '--source-disk')

        if not (args.source_disk or args.source_uri):
            raise exceptions.MinimumArgumentException([
                '--source-uri', '--source-disk'
            ], 'Please specify either the source disk or the Google Cloud Storage '
                                                      'URI of the disk image.')

        # TODO(user): use resources.REGISTRY.Parse() for GCS URIs (b/30086260).
        if args.source_uri:
            source_uri = utils.NormalizeGoogleStorageUri(args.source_uri)
            image.rawDisk = self.messages.Image.RawDiskValue(source=source_uri)
        else:
            source_disk_ref = flags.SOURCE_DISK_ARG.ResolveAsResource(
                args,
                self.resources,
                scope_lister=compute_flags.GetDefaultScopeLister(
                    self.compute_client, self.project))
            image.sourceDisk = source_disk_ref.SelfLink()
            image.sourceDiskEncryptionKey = csek_utils.MaybeLookupKeyMessage(
                csek_keys, source_disk_ref,
                self.compute_client.apitools_client)

        if args.licenses:
            image.licenses = args.licenses

        request = self.messages.ComputeImagesInsertRequest(
            image=image, project=self.project)

        return [request]
    def CreateRequests(self, args):
        if not args.bucket and args.prefix:
            raise exceptions.ToolException(
                '[--prefix] cannot be specified when unsetting the usage bucket.'
            )

        bucket_uri = utils.NormalizeGoogleStorageUri(args.bucket)

        request = self.messages.ComputeProjectsSetUsageExportBucketRequest(
            project=self.project,
            usageExportLocation=self.messages.UsageExportLocation(
                bucketName=bucket_uri,
                reportNamePrefix=args.prefix,
            ))

        return [request]
Example #6
0
    def Run(self, args):
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client

        if not args.bucket and args.prefix:
            raise calliope_exceptions.ToolException(
                '[--prefix] cannot be specified when unsetting the usage bucket.'
            )

        bucket_uri = utils.NormalizeGoogleStorageUri(args.bucket or None)

        request = client.messages.ComputeProjectsSetUsageExportBucketRequest(
            project=properties.VALUES.core.project.GetOrFail(),
            usageExportLocation=client.messages.UsageExportLocation(
                bucketName=bucket_uri,
                reportNamePrefix=args.prefix,
            ))

        return client.MakeRequests([(client.apitools_client.projects,
                                     'SetUsageExportBucket', request)])
Example #7
0
    def Run(self, args):
        """Returns a list of requests necessary for adding images."""
        holder = base_classes.ComputeApiHolder(self.ReleaseTrack())
        client = holder.client
        messages = client.messages
        resource_parser = holder.resources

        image_ref = Create.DISK_IMAGE_ARG.ResolveAsResource(
            args, holder.resources)
        image = messages.Image(
            name=image_ref.image,
            description=args.description,
            sourceType=messages.Image.SourceTypeValueValuesEnum.RAW,
            family=args.family)

        csek_keys = csek_utils.CsekKeyStore.FromArgs(
            args, self._ALLOW_RSA_ENCRYPTED_CSEK_KEYS)
        if csek_keys:
            image.imageEncryptionKey = csek_utils.MaybeToMessage(
                csek_keys.LookupKey(
                    image_ref, raise_if_missing=args.require_csek_key_create),
                client.apitools_client)

        # Validate parameters.
        if args.source_disk_zone and not args.source_disk:
            raise exceptions.ToolException(
                'You cannot specify [--source-disk-zone] unless you are specifying '
                '[--source-disk].')

        source_image_project = getattr(args, 'source_image_project', None)
        source_image = getattr(args, 'source_image', None)
        source_image_family = getattr(args, 'source_image_family', None)

        if source_image_project and not (source_image or source_image_family):
            raise exceptions.ToolException(
                'You cannot specify [--source-image-project] unless you are '
                'specifying [--source-image] or [--source-image-family].')

        if source_image or source_image_family:
            image_expander = image_utils.ImageExpander(client, resource_parser)
            _, source_image_ref = image_expander.ExpandImageFlag(
                user_project=image_ref.project,
                image=source_image,
                image_family=source_image_family,
                image_project=source_image_project,
                return_image_resource=True)
            image.sourceImage = source_image_ref.selfLink
            image.sourceImageEncryptionKey = csek_utils.MaybeLookupKeyMessage(
                csek_keys, source_image_ref, client.apitools_client)

        # TODO(b/30086260): use resources.REGISTRY.Parse() for GCS URIs.
        if args.source_uri:
            source_uri = utils.NormalizeGoogleStorageUri(args.source_uri)
            image.rawDisk = messages.Image.RawDiskValue(source=source_uri)
        elif args.source_disk:
            source_disk_ref = flags.SOURCE_DISK_ARG.ResolveAsResource(
                args,
                holder.resources,
                scope_lister=compute_flags.GetDefaultScopeLister(client))
            image.sourceDisk = source_disk_ref.SelfLink()
            image.sourceDiskEncryptionKey = csek_utils.MaybeLookupKeyMessage(
                csek_keys, source_disk_ref, client.apitools_client)

        if args.licenses:
            image.licenses = args.licenses

        guest_os_features = getattr(args, 'guest_os_features', [])
        if guest_os_features:
            guest_os_feature_messages = []
            for feature in guest_os_features:
                gf_type = messages.GuestOsFeature.TypeValueValuesEnum(feature)
                guest_os_feature = messages.GuestOsFeature()
                guest_os_feature.type = gf_type
                guest_os_feature_messages.append(guest_os_feature)
            image.guestOsFeatures = guest_os_feature_messages

        request = messages.ComputeImagesInsertRequest(
            image=image, project=image_ref.project)

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

        alpha_or_beta = self.ReleaseTrack() in (base.ReleaseTrack.BETA,
                                                base.ReleaseTrack.ALPHA)
        if alpha_or_beta and args.force_create:
            request.forceCreate = True

        return client.MakeRequests([(client.apitools_client.images, 'Insert',
                                     request)])
Example #8
0
  def CreateRequests(self, args):
    """Returns a list of requests necessary for adding images."""

    image_ref = flags.DISK_IMAGE_ARG.ResolveAsResource(args, self.resources)
    image = self.messages.Image(
        name=image_ref.image,
        description=args.description,
        sourceType=self.messages.Image.SourceTypeValueValuesEnum.RAW,
        family=args.family)

    csek_keys = csek_utils.CsekKeyStore.FromArgs(
        args, self._ALLOW_RSA_ENCRYPTED_CSEK_KEYS)
    if csek_keys:
      image.imageEncryptionKey = csek_utils.MaybeToMessage(
          csek_keys.LookupKey(image_ref,
                              raise_if_missing=args.require_csek_key_create),
          self.compute_client.apitools_client)

    # Validate parameters.
    if args.source_disk_zone and not args.source_disk:
      raise exceptions.ToolException(
          'You cannot specify [--source-disk-zone] unless you are specifying '
          '[--source-disk].')

    if args.source_disk and args.source_uri:
      raise exceptions.ConflictingArgumentsException(
          '--source-uri', '--source-disk')

    if not (args.source_disk or args.source_uri):
      raise exceptions.MinimumArgumentException(
          ['--source-uri', '--source-disk'],
          'Please specify either the source disk or the Google Cloud Storage '
          'URI of the disk image.'
      )

    # TODO(b/30086260): use resources.REGISTRY.Parse() for GCS URIs.
    if args.source_uri:
      source_uri = utils.NormalizeGoogleStorageUri(args.source_uri)
      image.rawDisk = self.messages.Image.RawDiskValue(source=source_uri)
    else:
      source_disk_ref = flags.SOURCE_DISK_ARG.ResolveAsResource(
          args, self.resources,
          scope_lister=compute_flags.GetDefaultScopeLister(self.compute_client))
      image.sourceDisk = source_disk_ref.SelfLink()
      image.sourceDiskEncryptionKey = csek_utils.MaybeLookupKeyMessage(
          csek_keys, source_disk_ref, self.compute_client.apitools_client)

    if args.licenses:
      image.licenses = args.licenses

    guest_os_features = getattr(args, 'guest_os_features', [])
    if guest_os_features:
      guest_os_feature_messages = []
      for feature in guest_os_features:
        gf_type = self.messages.GuestOsFeature.TypeValueValuesEnum(feature)
        guest_os_feature = self.messages.GuestOsFeature()
        guest_os_feature.type = gf_type
        guest_os_feature_messages.append(guest_os_feature)
      image.guestOsFeatures = guest_os_feature_messages

    request = self.messages.ComputeImagesInsertRequest(
        image=image,
        project=image_ref.project)

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

    return [request]