def list_buckets(self, fields_scope=cloud_api.FieldsScope.NO_ACL): """See super class.""" projection = self._get_projection( fields_scope, self.messages.StorageBucketsListRequest) request = self.messages.StorageBucketsListRequest( project=properties.VALUES.core.project.GetOrFail(), projection=projection) global_params = None if fields_scope == cloud_api.FieldsScope.SHORT: global_params = self.messages.StandardQueryParameters() global_params.fields = 'items/name' # TODO(b/160238394) Decrypt metadata fields if necessary. bucket_iter = list_pager.YieldFromList( self.client.buckets, request, batch_size=cloud_api.NUM_ITEMS_PER_LIST_PAGE, global_params=global_params) try: for bucket in bucket_iter: yield gcs_metadata_util.get_bucket_resource_from_metadata( bucket) except apitools_exceptions.HttpError as e: core_exceptions.reraise( cloud_errors.translate_error(e, _ERROR_TRANSLATION))
def get_bucket(self, bucket_name, fields_scope=cloud_api.FieldsScope.NO_ACL): """See super class.""" projection = self._get_projection( fields_scope, self.messages.StorageBucketsGetRequest) request = self.messages.StorageBucketsGetRequest(bucket=bucket_name, projection=projection) metadata = self.client.buckets.Get(request) return gcs_metadata_util.get_bucket_resource_from_metadata(metadata)
def patch_bucket(self, bucket_resource, request_config, fields_scope=cloud_api.FieldsScope.NO_ACL): """See super class.""" projection = self._get_projection( fields_scope, self.messages.StorageBucketsPatchRequest) metadata = self.messages.Bucket( name=bucket_resource.storage_url.bucket_name) gcs_metadata_util.update_bucket_metadata_from_request_config( metadata, request_config) self._handle_append_and_remove_bucket_updates(bucket_resource, request_config, metadata) cleared_fields = gcs_metadata_util.get_cleared_bucket_fields( request_config) if (metadata.defaultObjectAcl and metadata.defaultObjectAcl[0] == gcs_metadata_util.PRIVATE_DEFAULT_OBJECT_ACL): cleared_fields.append('defaultObjectAcl') metadata.defaultObjectAcl = [] # Must null out existing ACLs to apply new ones. if request_config.predefined_acl_string: cleared_fields.append('acl') predefined_acl = getattr( self.messages.StorageBucketsPatchRequest. PredefinedAclValueValuesEnum, request_config.predefined_acl_string) else: predefined_acl = None if request_config.predefined_default_acl_string: cleared_fields.append('defaultObjectAcl') predefined_default_acl = getattr( self.messages.StorageBucketsPatchRequest. PredefinedDefaultObjectAclValueValuesEnum, request_config.predefined_default_acl_string) else: predefined_default_acl = None apitools_request = self.messages.StorageBucketsPatchRequest( bucket=bucket_resource.storage_url.bucket_name, bucketResource=metadata, projection=projection, ifMetagenerationMatch=request_config. precondition_metageneration_match, predefinedAcl=predefined_acl, predefinedDefaultObjectAcl=predefined_default_acl) with self.client.IncludeFields(cleared_fields): return gcs_metadata_util.get_bucket_resource_from_metadata( self.client.buckets.Patch(apitools_request))
def create_bucket(self, bucket_resource, fields_scope=cloud_api.FieldsScope.NO_ACL): """See super class.""" projection = self._get_projection( fields_scope, self.messages.StorageBucketsInsertRequest) if not bucket_resource.metadata: bucket_resource.metadata = gcs_metadata_util.get_metadata_from_bucket_resource( bucket_resource) request = self.messages.StorageBucketsInsertRequest( bucket=bucket_resource.metadata, project=properties.VALUES.core.project.GetOrFail(), projection=projection) created_bucket_metadata = self.client.buckets.Insert(request) return gcs_metadata_util.get_bucket_resource_from_metadata( created_bucket_metadata)
def patch_bucket(self, bucket_resource, request_config=None, fields_scope=cloud_api.FieldsScope.NO_ACL): """See super class.""" validated_request_config = cloud_api.get_provider_request_config( request_config, GcsRequestConfig) projection = self._get_projection( fields_scope, self.messages.StorageBucketsPatchRequest) metadata = (bucket_resource.metadata or gcs_metadata_util.get_metadata_from_bucket_resource( bucket_resource)) # Blank metadata objects need to be explicitly emptied. apitools_include_fields = [] for metadata_field in ( 'billing', 'encryption', 'lifecycle', 'logging', 'retentionPolicy', 'versioning', 'website', ): attr = getattr(metadata, metadata_field, None) if attr and not encoding.MessageToDict(attr): apitools_include_fields.append(metadata_field) setattr(metadata, metadata_field, None) # Handle nulling lists with sentinel values. if metadata.cors and metadata.cors == gcs_metadata_util.REMOVE_CORS_CONFIG: apitools_include_fields.append('cors') metadata.cors = [] if (metadata.defaultObjectAcl and metadata.defaultObjectAcl[0] == gcs_metadata_util.PRIVATE_DEFAULT_OBJECT_ACL): apitools_include_fields.append('defaultObjectAcl') metadata.defaultObjectAcl = [] # Must null out existing ACLs to apply new ones. if validated_request_config.predefined_acl_string: apitools_include_fields.append('acl') predefined_acl = getattr( self.messages.StorageBucketsPatchRequest. PredefinedAclValueValuesEnum, validated_request_config.predefined_acl_string) else: predefined_acl = None if validated_request_config.predefined_default_acl_string: apitools_include_fields.append('defaultObjectAcl') predefined_default_acl = getattr( self.messages.StorageBucketsPatchRequest. PredefinedDefaultObjectAclValueValuesEnum, validated_request_config.predefined_default_acl_string) else: predefined_default_acl = None apitools_request = self.messages.StorageBucketsPatchRequest( bucket=bucket_resource.name, bucketResource=metadata, projection=projection, ifMetagenerationMatch=validated_request_config. precondition_metageneration_match, predefinedAcl=predefined_acl, predefinedDefaultObjectAcl=predefined_default_acl) with self.client.IncludeFields(apitools_include_fields): return gcs_metadata_util.get_bucket_resource_from_metadata( self.client.buckets.Patch(apitools_request))