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

     1  /*
     2  Copyright 2019 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 disks
    18  
    19  import (
    20  	"context"
    21  	"time"
    22  
    23  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
    24  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/compute/armcompute/v5"
    25  	"github.com/pkg/errors"
    26  	"sigs.k8s.io/cluster-api-provider-azure/azure"
    27  	"sigs.k8s.io/cluster-api-provider-azure/azure/services/async"
    28  	"sigs.k8s.io/cluster-api-provider-azure/util/tele"
    29  )
    30  
    31  // azureClient contains the Azure go-sdk Client.
    32  type azureClient struct {
    33  	disks          *armcompute.DisksClient
    34  	apiCallTimeout time.Duration
    35  }
    36  
    37  // newClient creates a new disks client from an authorizer.
    38  func newClient(auth azure.Authorizer, apiCallTimeout time.Duration) (*azureClient, error) {
    39  	opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
    40  	if err != nil {
    41  		return nil, errors.Wrap(err, "failed to create disks client options")
    42  	}
    43  	factory, err := armcompute.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts)
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "failed to create armcompute client factory")
    46  	}
    47  	return &azureClient{factory.NewDisksClient(), apiCallTimeout}, nil
    48  }
    49  
    50  // DeleteAsync deletes a disk asynchronously. DeleteAsync sends a DELETE
    51  // request to Azure and if accepted without error, the func will return a Poller which can be used to track the ongoing
    52  // progress of the operation.
    53  func (ac *azureClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string) (poller *runtime.Poller[armcompute.DisksClientDeleteResponse], err error) {
    54  	ctx, _, done := tele.StartSpanWithLogger(ctx, "disks.azureClient.DeleteAsync")
    55  	defer done()
    56  
    57  	opts := &armcompute.DisksClientBeginDeleteOptions{ResumeToken: resumeToken}
    58  	poller, err = ac.disks.BeginDelete(ctx, spec.ResourceGroupName(), spec.ResourceName(), opts)
    59  	if err != nil {
    60  		return nil, err
    61  	}
    62  
    63  	ctx, cancel := context.WithTimeout(ctx, ac.apiCallTimeout)
    64  	defer cancel()
    65  
    66  	pollOpts := &runtime.PollUntilDoneOptions{Frequency: async.DefaultPollerFrequency}
    67  	_, err = poller.PollUntilDone(ctx, pollOpts)
    68  	if err != nil {
    69  		// if an error occurs, return the Poller.
    70  		// this means the long-running operation didn't finish in the specified timeout.
    71  		return poller, err
    72  	}
    73  
    74  	// if the operation completed, return a nil poller.
    75  	return nil, err
    76  }