Пример #1
0
        public async virtual Task <Response <StorageAccount> > RemoveTagAsync(string key, CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentNullException($"{nameof(key)} provided cannot be null or a whitespace.", nameof(key));
            }

            using var scope = _clientDiagnostics.CreateScope("StorageAccount.RemoveTag");
            scope.Start();
            try
            {
                var originalTags = await TagResource.GetAsync(cancellationToken).ConfigureAwait(false);

                originalTags.Value.Data.Properties.TagsValue.Remove(key);
                await TagContainer.CreateOrUpdateAsync(originalTags.Value.Data, cancellationToken : cancellationToken).ConfigureAwait(false);

                var originalResponse = await _restClient.GetPropertiesAsync(Id.ResourceGroupName, Id.Name, null, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new StorageAccount(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Пример #2
0
        public async virtual Task <Response <StorageAccount> > SetTagsAsync(IDictionary <string, string> tags, CancellationToken cancellationToken = default)
        {
            if (tags == null)
            {
                throw new ArgumentNullException($"{nameof(tags)} provided cannot be null.", nameof(tags));
            }

            using var scope = _clientDiagnostics.CreateScope("StorageAccount.SetTags");
            scope.Start();
            try
            {
                await TagResource.DeleteAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                var originalTags = await TagResource.GetAsync(cancellationToken).ConfigureAwait(false);

                originalTags.Value.Data.Properties.TagsValue.ReplaceWith(tags);
                await TagContainer.CreateOrUpdateAsync(originalTags.Value.Data, cancellationToken : cancellationToken).ConfigureAwait(false);

                var originalResponse = await _restClient.GetPropertiesAsync(Id.ResourceGroupName, Id.Name, null, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new StorageAccount(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Пример #3
0
        /// <summary>
        /// Replace the tags on the resource with the given set.
        /// </summary>
        /// <param name="tags"> The set of tags to use as replacement. </param>
        /// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
        /// <returns> The updated resource with the tag added. </returns>
        public virtual async Task <Response <ResourceGroup> > SetTagsAsync(IDictionary <string, string> tags, CancellationToken cancellationToken = default)
        {
            if (tags == null)
            {
                throw new ArgumentNullException(nameof(tags));
            }

            using var scope = Diagnostics.CreateScope("ResourceGroupOperations.SetTags");
            scope.Start();

            try
            {
                await TagResourceOperations.DeleteAsync(cancellationToken).ConfigureAwait(false);

                var newTags = await TagResourceOperations.GetAsync(cancellationToken).ConfigureAwait(false);

                newTags.Value.Data.Properties.TagsValue.ReplaceWith(tags);
                await TagContainer.CreateOrUpdateAsync(new TagResourceData(newTags.Value.Data.Properties), cancellationToken).ConfigureAwait(false);

                var originalResponse = await RestClient.GetAsync(Id.Name, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new ResourceGroup(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Пример #4
0
        /// <summary>
        /// Add a tag to the current resource.
        /// </summary>
        /// <param name="key"> The key for the tag. </param>
        /// <param name="value"> The value for the tag. </param>
        /// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
        /// <returns> The updated resource with the tag added. </returns>
        public virtual async Task <Response <ResourceGroup> > AddTagAsync(string key, string value, CancellationToken cancellationToken = default)
        {
            if (string.IsNullOrWhiteSpace(key))
            {
                throw new ArgumentException($"{nameof(key)} provided cannot be null or a whitespace.", nameof(key));
            }

            using var scope = Diagnostics.CreateScope("ResourceGroupOperations.AddTag");
            scope.Start();

            try
            {
                var originalTags = await TagResourceOperations.GetAsync(cancellationToken).ConfigureAwait(false);

                originalTags.Value.Data.Properties.TagsValue[key] = value;
                await TagContainer.CreateOrUpdateAsync(originalTags.Value.Data, cancellationToken).ConfigureAwait(false);

                var originalResponse = await RestClient.GetAsync(Id.Name, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new ResourceGroup(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Пример #5
0
        /// <summary>
        /// Replace the tags on the resource with the given set.
        /// </summary>
        /// <param name="tags"> The set of tags to use as replacement. </param>
        /// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
        /// <returns> The updated resource with the tag added. </returns>
        public virtual async Task <Response <GenericResource> > SetTagsAsync(IDictionary <string, string> tags, CancellationToken cancellationToken = default)
        {
            using var scope = _clientDiagnostics.CreateScope("GenericResource.SetTags");
            scope.Start();
            try
            {
                var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);

                await TagResource.DeleteAsync(cancellationToken : cancellationToken).ConfigureAwait(false);

                var newTags = await TagResource.GetAsync(cancellationToken).ConfigureAwait(false);

                newTags.Value.Data.Properties.TagsValue.ReplaceWith(tags);
                await TagContainer.CreateOrUpdateAsync(new TagResourceData(newTags.Value.Data.Properties), cancellationToken : cancellationToken).ConfigureAwait(false);

                var originalResponse = await _restClient.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new GenericResource(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
Пример #6
0
        /// <summary>
        /// Removes a tag by key from the resource.
        /// </summary>
        /// <param name="key"> The key of the tag to remove. </param>
        /// <param name="cancellationToken"> A token to allow the caller to cancel the call to the service. The default value is <see cref="CancellationToken.None" />. </param>
        /// <returns> The updated resource with the tag added. </returns>
        public virtual async Task <Response <GenericResource> > RemoveTagAsync(string key, CancellationToken cancellationToken = default)
        {
            using var scope = _clientDiagnostics.CreateScope("GenericResource.RemoveTag");
            scope.Start();
            try
            {
                var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);

                var originalTags = (await TagResource.GetAsync(cancellationToken).ConfigureAwait(false)).Value;
                originalTags.Data.Properties.TagsValue.Remove(key);
                await TagContainer.CreateOrUpdateAsync(originalTags.Data, cancellationToken : cancellationToken).ConfigureAwait(false);

                var originalResponse = await _restClient.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new GenericResource(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }