sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/roleassignments/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 roleassignments
    18  
    19  import (
    20  	"context"
    21  
    22  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/authorization/armauthorization/v2"
    24  	"github.com/pkg/errors"
    25  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    26  	"sigs.k8s.io/cluster-api-provider-azure/util/tele"
    27  )
    28  
    29  // azureClient contains the Azure go-sdk Client.
    30  type azureClient struct {
    31  	roleassignments armauthorization.RoleAssignmentsClient
    32  }
    33  
    34  // newClient creates a new role assignments client from an authorizer.
    35  func newClient(auth azure.Authorizer) (*azureClient, error) {
    36  	opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
    37  	if err != nil {
    38  		return nil, errors.Wrap(err, "failed to create roleassignments client options")
    39  	}
    40  	factory, err := armauthorization.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts)
    41  	if err != nil {
    42  		return nil, errors.Wrap(err, "failed to create armauthorization client factory")
    43  	}
    44  	return &azureClient{*factory.NewRoleAssignmentsClient()}, nil
    45  }
    46  
    47  // Get gets the specified role assignment.
    48  func (ac *azureClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (interface{}, error) {
    49  	ctx, _, done := tele.StartSpanWithLogger(ctx, "roleassignments.azureClient.Get")
    50  	defer done()
    51  
    52  	resp, err := ac.roleassignments.Get(ctx, spec.ResourceGroupName(), spec.ResourceName(), nil)
    53  	if err != nil {
    54  		return nil, err
    55  	}
    56  	return resp.RoleAssignment, nil
    57  }
    58  
    59  // CreateOrUpdateAsync creates a roleassignment.
    60  // Creating a roleassignment is not a long running operation, so we don't ever return a poller.
    61  func (ac *azureClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string, parameters interface{}) (result interface{}, poller *runtime.Poller[armauthorization.RoleAssignmentsClientCreateResponse], err error) {
    62  	ctx, _, done := tele.StartSpanWithLogger(ctx, "roleassignments.azureClient.CreateOrUpdateAsync")
    63  	defer done()
    64  
    65  	createParams, ok := parameters.(armauthorization.RoleAssignmentCreateParameters)
    66  	if !ok {
    67  		return nil, nil, errors.Errorf("%T is not an armauthorization.RoleAssignmentCreateParameters", parameters)
    68  	}
    69  	resp, err := ac.roleassignments.Create(ctx, spec.OwnerResourceName(), spec.ResourceName(), createParams, nil)
    70  	return resp.RoleAssignment, nil, err
    71  }