Example #1
0
  def test_lifecycle_translation(self):
    """Tests lifecycle translation for various formats."""
    # TODO: Use lifecycle_doc again once Boto is updated to support new fields.
    # json_text = self.lifecycle_doc
    json_text = self.lifecycle_doc_without_storage_class_fields

    entries_list = LifecycleTranslation.JsonLifecycleToMessage(json_text)
    boto_lifecycle = LifecycleTranslation.BotoLifecycleFromMessage(entries_list)
    converted_entries_list = LifecycleTranslation.BotoLifecycleToMessage(
        boto_lifecycle)
    converted_json_text = LifecycleTranslation.JsonLifecycleFromMessage(
        converted_entries_list)
    self.assertEqual(json.loads(json_text), json.loads(converted_json_text))
Example #2
0
    def _SetLifecycleConfig(self):
        """Sets lifecycle configuration for a Google Cloud Storage bucket."""
        lifecycle_arg = self.args[0]
        url_args = self.args[1:]
        # Disallow multi-provider 'lifecycle set' requests.
        if not UrlsAreForSingleProvider(url_args):
            raise CommandException(
                '"%s" command spanning providers not allowed.' %
                self.command_name)

        # Open, read and parse file containing JSON document.
        lifecycle_file = open(lifecycle_arg, 'r')
        lifecycle_txt = lifecycle_file.read()
        lifecycle_file.close()

        # Iterate over URLs, expanding wildcards and setting the lifecycle on each.
        some_matched = False
        for url_str in url_args:
            bucket_iter = self.GetBucketUrlIterFromArg(
                url_str, bucket_fields=['lifecycle'])
            for blr in bucket_iter:
                url = blr.storage_url
                some_matched = True
                self.logger.info('Setting lifecycle configuration on %s...',
                                 blr)
                if url.scheme == 's3':
                    self.gsutil_api.XmlPassThroughSetLifecycle(
                        lifecycle_txt, url, provider=url.scheme)
                else:
                    lifecycle = LifecycleTranslation.JsonLifecycleToMessage(
                        lifecycle_txt)
                    bucket_metadata = apitools_messages.Bucket(
                        lifecycle=lifecycle)
                    self.gsutil_api.PatchBucket(url.bucket_name,
                                                bucket_metadata,
                                                provider=url.scheme,
                                                fields=['id'])
        if not some_matched:
            raise CommandException(NO_URLS_MATCHED_TARGET % list(url_args))
        return 0