sigs.k8s.io/cluster-api-provider-azure@v1.17.0/azure/services/tags/client.go (about)

     1  /*
     2  Copyright 2020 The Kubernetes Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package tags
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/resources/armresources"
    23  	"github.com/pkg/errors"
    24  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    25  	"sigs.k8s.io/cluster-api-provider-azure/util/tele"
    26  )
    27  
    28  // client wraps go-sdk.
    29  type client interface {
    30  	GetAtScope(context.Context, string) (armresources.TagsResource, error)
    31  	UpdateAtScope(context.Context, string, armresources.TagsPatchResource) (armresources.TagsResource, error)
    32  }
    33  
    34  // AzureClient contains the Azure go-sdk client.
    35  type AzureClient struct {
    36  	tags *armresources.TagsClient
    37  }
    38  
    39  var _ client = (*AzureClient)(nil)
    40  
    41  // NewClient creates a tags client from an authorizer.
    42  func NewClient(auth azure.Authorizer) (*AzureClient, error) {
    43  	opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "failed to create tags client options")
    46  	}
    47  	factory, err := armresources.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts)
    48  	if err != nil {
    49  		return nil, errors.Wrap(err, "failed to create armresources client factory")
    50  	}
    51  	return &AzureClient{factory.NewTagsClient()}, nil
    52  }
    53  
    54  // GetAtScope sends the get at scope request.
    55  func (ac *AzureClient) GetAtScope(ctx context.Context, scope string) (armresources.TagsResource, error) {
    56  	ctx, _, done := tele.StartSpanWithLogger(ctx, "tags.AzureClient.GetAtScope")
    57  	defer done()
    58  
    59  	resp, err := ac.tags.GetAtScope(ctx, scope, nil)
    60  	if err != nil {
    61  		return armresources.TagsResource{}, err
    62  	}
    63  
    64  	return resp.TagsResource, nil
    65  }
    66  
    67  // UpdateAtScope this operation allows replacing, merging or selectively deleting tags on the specified resource or
    68  // subscription.
    69  func (ac *AzureClient) UpdateAtScope(ctx context.Context, scope string, parameters armresources.TagsPatchResource) (armresources.TagsResource, error) {
    70  	ctx, _, done := tele.StartSpanWithLogger(ctx, "tags.AzureClient.UpdateAtScope")
    71  	defer done()
    72  
    73  	resp, err := ac.tags.UpdateAtScope(ctx, scope, parameters, nil)
    74  	if err != nil {
    75  		return armresources.TagsResource{}, err
    76  	}
    77  
    78  	return resp.TagsResource, nil
    79  }