コード例 #1
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 <ResourceGroup> > RemoveTagAsync(string key, 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.RemoveTag");
            scope.Start();

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

                originalTags.Value.Data.Properties.TagsValue.Remove(key);
                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;
            }
        }
コード例 #2
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;
            }
        }
コード例 #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 <GenericResource> > SetTagsAsync(IDictionary <string, string> tags, CancellationToken cancellationToken = default)
        {
            using var scope = Diagnostics.CreateScope("GenericResourceOperations.SetTags");
            scope.Start();
            try
            {
                var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);

                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.GetByIdAsync(Id, apiVersion, cancellationToken).ConfigureAwait(false);

                return(Response.FromValue(new GenericResource(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }
コード例 #4
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 Response <GenericResource> RemoveTag(string key, CancellationToken cancellationToken = default)
 {
     using var scope = Diagnostics.CreateScope("GenericResourceOperations.RemoveTag");
     scope.Start();
     try
     {
         var apiVersion   = GetApiVersion(cancellationToken);
         var originalTags = TagResourceOperations.Get(cancellationToken).Value;
         originalTags.Data.Properties.TagsValue.Remove(key);
         TagContainer.CreateOrUpdate(originalTags.Data, cancellationToken);
         var originalResponse = RestClient.GetById(Id, apiVersion, cancellationToken);
         return(Response.FromValue(new GenericResource(this, originalResponse.Value), originalResponse.GetRawResponse()));
     }
     catch (Exception e)
     {
         scope.Failed(e);
         throw;
     }
 }
コード例 #5
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 = Diagnostics.CreateScope("GenericResourceOperations.RemoveTag");
            scope.Start();
            try
            {
                var apiVersion = await GetApiVersionAsync(cancellationToken).ConfigureAwait(false);

                var originalTags = (await TagResourceOperations.GetAsync(cancellationToken).ConfigureAwait(false)).Value;
                originalTags.Data.Properties.TagsValue.Remove(key);
                await TagContainer.CreateOrUpdateAsync(originalTags.Data, 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>
        /// 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 Response <ResourceGroup> AddTag(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 = TagResourceOperations.Get(cancellationToken).Value;
                originalTags.Data.Properties.TagsValue[key] = value;
                TagContainer.CreateOrUpdate(originalTags.Data, cancellationToken);
                var originalResponse = RestClient.Get(Id.Name, cancellationToken);
                return(Response.FromValue(new ResourceGroup(this, originalResponse.Value), originalResponse.GetRawResponse()));
            }
            catch (Exception e)
            {
                scope.Failed(e);
                throw;
            }
        }