sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/privatedns/record_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  
    22  	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
    23  	"github.com/Azure/azure-sdk-for-go/sdk/resourcemanager/privatedns/armprivatedns"
    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  // azureRecordsClient contains the Azure go-sdk Client for record sets.
    30  type azureRecordsClient struct {
    31  	recordsets *armprivatedns.RecordSetsClient
    32  }
    33  
    34  // newRecordSetsClient creates a record sets client from an authorizer.
    35  func newRecordSetsClient(auth azure.Authorizer) (*azureRecordsClient, error) {
    36  	opts, err := azure.ARMClientOptions(auth.CloudEnvironment())
    37  	if err != nil {
    38  		return nil, errors.Wrap(err, "failed to create recordsets client options")
    39  	}
    40  	factory, err := armprivatedns.NewClientFactory(auth.SubscriptionID(), auth.Token(), opts)
    41  	if err != nil {
    42  		return nil, errors.Wrap(err, "failed to create armprivatedns client factory")
    43  	}
    44  	return &azureRecordsClient{factory.NewRecordSetsClient()}, nil
    45  }
    46  
    47  // Get gets the specified record set. Noop for records.
    48  func (arc *azureRecordsClient) Get(ctx context.Context, spec azure.ResourceSpecGetter) (result interface{}, err error) {
    49  	return nil, nil
    50  }
    51  
    52  // CreateOrUpdateAsync creates or updates a record asynchronously.
    53  // Creating a record set is not a long-running operation, so we don't ever return a future.
    54  func (arc *azureRecordsClient) CreateOrUpdateAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string, parameters interface{}) (result interface{}, poller *runtime.Poller[armprivatedns.RecordSetsClientCreateOrUpdateResponse], err error) {
    55  	ctx, _, done := tele.StartSpanWithLogger(ctx, "privatedns.azureRecordsClient.CreateOrUpdateAsync")
    56  	defer done()
    57  
    58  	set, ok := parameters.(armprivatedns.RecordSet)
    59  	if !ok && parameters != nil {
    60  		return nil, nil, errors.Errorf("%T is not an armprivatedns.RecordSet", parameters)
    61  	}
    62  
    63  	// Determine record type.
    64  	var (
    65  		recordType armprivatedns.RecordType
    66  		aRecords   = set.Properties.ARecords
    67  		aaaRecords = set.Properties.AaaaRecords
    68  	)
    69  	if len(aRecords) > 0 && (aRecords)[0].IPv4Address != nil {
    70  		recordType = armprivatedns.RecordTypeA
    71  	} else if len(aaaRecords) > 0 && (aaaRecords)[0].IPv6Address != nil {
    72  		recordType = armprivatedns.RecordTypeAAAA
    73  	}
    74  
    75  	recordSet, err := arc.recordsets.CreateOrUpdate(ctx, spec.ResourceGroupName(), spec.OwnerResourceName(), recordType, spec.ResourceName(), set, nil)
    76  	if err != nil {
    77  		return nil, nil, err
    78  	}
    79  	return recordSet, nil, err
    80  }
    81  
    82  // DeleteAsync deletes a record asynchronously. Noop for records.
    83  func (arc *azureRecordsClient) DeleteAsync(ctx context.Context, spec azure.ResourceSpecGetter, resumeToken string) (poller *runtime.Poller[armprivatedns.RecordSetsClientDeleteResponse], err error) {
    84  	return nil, nil
    85  }