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

     1  /*
     2  Copyright 2022 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 privatedns
    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/privatedns/armprivatedns"
    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  // azureZonesClient contains the Azure go-sdk Client for private dns zone.
    32  type azureZonesClient struct {
    33  	privatezones   *armprivatedns.PrivateZonesClient
    34  	apiCallTimeout time.Duration
    35  }
    36  
    37  // newPrivateZonesClient creates a private zones client from an authorizer.
    38  func newPrivateZonesClient(auth azure.Authorizer, apiCallTimeout time.Duration) (*azureZonesClient, error) {
    39  	opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
    40  	if err != nil {
    41  		return nil, errors.Wrap(err, "failed to create privatezones client options")
    42  	}
    43  	factory, err := armprivatedns.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts)
    44  	if err != nil {
    45  		return nil, errors.Wrap(err, "failed to create armprivatedns client factory")
    46  	}
    47  	return &azureZonesClient{factory.NewPrivateZonesClient(), apiCallTimeout}, nil
    48  }
    49  
    50  // Get gets the specified private dns zone.
    51  func (azc *azureZonesClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error) {
    52  	ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.azureZonesClient.Get")
    53  	defer done()
    54  
    55  	resp, err := azc.privatezones.Get(ctx, spec.ResourceGroupName(), spec.ResourceName(), nil)
    56  	if err != nil {
    57  		return nil, err
    58  	}
    59  	return resp.PrivateZone, nil
    60  }
    61  
    62  // CreateOrUpdateAsync creates or updates a private dns zone asynchronously.
    63  // It sends a PUT request to Azure and if accepted without error, the func will return a poller which can be used to track the ongoing
    64  // progress of the operation.
    65  func (azc *azureZonesClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string, parameters interface{}) (result interface{}, poller *runtime.Poller[armprivatedns.PrivateZonesClientCreateOrUpdateResponse], err error) {
    66  	ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.azureZonesClient.CreateOrUpdateAsync")
    67  	defer done()
    68  
    69  	zone, ok := parameters.(armprivatedns.PrivateZone)
    70  	if !ok && parameters != nil {
    71  		return nil, nil, errors.Errorf("%T is not an armprivatedns.PrivateZone", parameters)
    72  	}
    73  
    74  	opts := &armprivatedns.PrivateZonesClientBeginCreateOrUpdateOptions{ResumeToken: resumeToken}
    75  	poller, err = azc.privatezones.BeginCreateOrUpdate(ctx, spec.ResourceGroupName(), spec.ResourceName(), zone, opts)
    76  	if err != nil {
    77  		return nil, nil, err
    78  	}
    79  
    80  	ctx, cancel := context.WithTimeout(ctx, azc.apiCallTimeout)
    81  	defer cancel()
    82  
    83  	pollOpts := &runtime.PollUntilDoneOptions{Frequency: async.DefaultPollerFrequency}
    84  	resp, err := poller.PollUntilDone(ctx, pollOpts)
    85  	if err != nil {
    86  		// if an error occurs, return the poller.
    87  		// this means the long-running operation didn't finish in the specified timeout.
    88  		return nil, poller, err
    89  	}
    90  
    91  	// if the operation completed, return a nil poller
    92  	return resp.PrivateZone, nil, err
    93  }
    94  
    95  // DeleteAsync deletes a private dns zone asynchronously. DeleteAsync sends a DELETE
    96  // request to Azure and if accepted without error, the func will return a poller which can be used to track the ongoing
    97  // progress of the operation.
    98  func (azc *azureZonesClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string) (poller *runtime.Poller[armprivatedns.PrivateZonesClientDeleteResponse], err error) {
    99  	ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.azureZonesClient.DeleteAsync")
   100  	defer done()
   101  
   102  	opts := &armprivatedns.PrivateZonesClientBeginDeleteOptions{ResumeToken: resumeToken}
   103  	poller, err = azc.privatezones.BeginDelete(ctx, spec.ResourceGroupName(), spec.ResourceName(), opts)
   104  	if err != nil {
   105  		return nil, err
   106  	}
   107  
   108  	ctx, cancel := context.WithTimeout(ctx, azc.apiCallTimeout)
   109  	defer cancel()
   110  
   111  	pollOpts := &runtime.PollUntilDoneOptions{Frequency: async.DefaultPollerFrequency}
   112  	_, err = poller.PollUntilDone(ctx, pollOpts)
   113  	if err != nil {
   114  		// if an error occurs, return the Poller.
   115  		// this means the long-running operation didn't finish in the specified timeout.
   116  		return poller, err
   117  	}
   118  
   119  	// if the operation completed, return a nil poller.
   120  	return nil, err
   121  }