Example #1
0
        /// <summary>
        /// Removes a label from a bucket, if it previously existed. It is not an error to
        /// attempt to remove a label that doesn't already exist.
        /// </summary>
        /// <remarks>
        /// This method is implemented by creating a single-element dictionary which is passed to
        /// <see cref="ModifyBucketLabels(string, IDictionary{string, string}, ModifyBucketLabelsOptions)"/>.
        /// </remarks>
        /// <param name="bucket">The name of the bucket. Must not be null.</param>
        /// <param name="labelName">The name of the label to remove. Must not be null.</param>
        /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
        /// <returns>The previous value of the label, or null if the label did not previously exist.</returns>
        public virtual string RemoveBucketLabel(string bucket, string labelName, ModifyBucketLabelsOptions options = null)
        {
            StorageClientImpl.ValidateBucketName(bucket);
            GaxPreconditions.CheckNotNull(labelName, nameof(labelName));
            var newLabels = new Dictionary <string, string> {
                [labelName] = null
            };
            var oldLabels = ModifyBucketLabels(bucket, newLabels, options);

            return(oldLabels[labelName]);
        }
        /// <inheritdoc />
        public override IDictionary <string, string> ClearBucketLabels(string bucket, ModifyBucketLabelsOptions options = null)
        {
            ValidateBucketName(bucket);

            int retries    = options?.Retries ?? ModifyBucketLabelsOptions.DefaultRetries;
            var getOptions = new GetBucketOptions {
                IfMetagenerationMatch = options?.IfMetagenerationMatch
            };

            while (true)
            {
                // Outside the catch, as we don't need to retry if *this* fails the precondition.
                // If the bucket originally has the "right" version but then changes before we patch it, we'll end up retrying this
                // part as well, and it will immediately fail.
                var bucketMetadata = GetBucket(bucket, getOptions);
                try
                {
                    var existingLabels = bucketMetadata.Labels;
                    if (existingLabels == null || existingLabels.Count == 0)
                    {
                        return(new Dictionary <string, string>());
                    }
                    var patchOptions = new PatchBucketOptions {
                        IfMetagenerationMatch = bucketMetadata.Metageneration
                    };

                    var labels = existingLabels.Keys.ToDictionary(key => key, key => (string)null);
                    PatchBucket(new Bucket {
                        Name = bucketMetadata.Name, Labels = labels
                    }, patchOptions);

                    return(existingLabels);
                }
                catch (GoogleApiException e) when(retries > 0 && e.HttpStatusCode == HttpStatusCode.PreconditionFailed)
                {
                    retries--;
                    // No delay here: we're only trying to catch the case where another change is made between Get/Patch.
                }
            }
        }
        /// <inheritdoc />
        public override IDictionary <string, string> ModifyBucketLabels(string bucket, IDictionary <string, string> labels, ModifyBucketLabelsOptions options = null)
        {
            ValidateBucketName(bucket);
            GaxPreconditions.CheckNotNull(labels, nameof(labels));

            int retries    = options?.Retries ?? ModifyBucketLabelsOptions.DefaultRetries;
            var getOptions = new GetBucketOptions {
                IfMetagenerationMatch = options?.IfMetagenerationMatch
            };

            while (true)
            {
                // Outside the catch, as we don't need to retry if *this* fails the precondition.
                // If the bucket originally has the "right" version but then changes before we patch it, we'll end up retrying this
                // part as well, and it will immediately fail.
                var bucketMetadata = GetBucket(bucket, getOptions);
                try
                {
                    var existingLabels = bucketMetadata.Labels ?? new Dictionary <string, string>();

                    var differences = GetDifferences(labels, existingLabels);
                    // If everything's already as we want it to be, we don't need to patch.
                    if (differences.Count != 0)
                    {
                        var patchOptions = new PatchBucketOptions {
                            IfMetagenerationMatch = bucketMetadata.Metageneration
                        };
                        PatchBucket(new Bucket {
                            Name = bucketMetadata.Name, Labels = differences
                        }, patchOptions);
                    }

                    var oldValues = new Dictionary <string, string>();
                    foreach (var entry in labels)
                    {
                        existingLabels.TryGetValue(entry.Key, out string existingValue);
                        oldValues[entry.Key] = existingValue; // May be null
                    }
                    return(oldValues);
                }
                catch (GoogleApiException e) when(retries > 0 && e.HttpStatusCode == HttpStatusCode.PreconditionFailed)
                {
                    retries--;
                    // No delay here: we're only trying to catch the case where another change is made between Get/Patch.
                }
            }
        }
Example #4
0
 /// <summary>
 /// Clears all labels on a bucket.
 /// </summary>
 /// <param name="bucket">The name of the bucket. Must not be null.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <returns>A dictionary with the labels on the bucket before they were cleared.</returns>
 public virtual IDictionary <string, string> ClearBucketLabels(string bucket, ModifyBucketLabelsOptions options = null) =>
 throw new NotImplementedException();
Example #5
0
 /// <summary>
 /// Clears all labels on a bucket asynchronously.
 /// </summary>
 /// <param name="bucket">The name of the bucket. Must not be null.</param>
 /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
 /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
 /// <returns>A dictionary with the labels on the bucket before they were cleared.</returns>
 public virtual Task <IDictionary <string, string> > ClearBucketLabelsAsync(string bucket, ModifyBucketLabelsOptions options = null, CancellationToken cancellationToken = default) =>
 throw new NotImplementedException();
Example #6
0
        /// <summary>
        /// Removes a label from a bucket, if it previously existed, asynchronously. It is not an error to
        /// attempt to remove a label that doesn't already exist.
        /// </summary>
        /// <remarks>
        /// This method is implemented by creating a single-element dictionary which is passed to
        /// <see cref="ModifyBucketLabelsAsync(string, IDictionary{string, string}, ModifyBucketLabelsOptions, CancellationToken)"/>.
        /// </remarks>
        /// <param name="bucket">The name of the bucket. Must not be null.</param>
        /// <param name="labelName">The name of the label to remove. Must not be null.</param>
        /// <param name="options">The options for the operation. May be null, in which case defaults will be supplied.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests.</param>
        /// <returns>The previous value of the label, or null if the label did not previously exist.</returns>
        public virtual async Task <string> RemoveBucketLabelAsync(string bucket, string labelName, ModifyBucketLabelsOptions options = null, CancellationToken cancellationToken = default)
        {
            StorageClientImpl.ValidateBucketName(bucket);
            GaxPreconditions.CheckNotNull(labelName, nameof(labelName));
            var newLabels = new Dictionary <string, string> {
                [labelName] = null
            };
            var oldLabels = await ModifyBucketLabelsAsync(bucket, newLabels, options, cancellationToken).ConfigureAwait(false);

            return(oldLabels[labelName]);
        }