sigs.k8s.io/cluster-api-provider-azure@v1.14.3/azure/services/privatedns/record_spec.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/resourcemanager/privatedns/armprivatedns"
    23  	"github.com/pkg/errors"
    24  	"k8s.io/utils/ptr"
    25  	infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1beta1"
    26  	"sigs.k8s.io/cluster-api-provider-azure/azure/converters"
    27  )
    28  
    29  // RecordSpec defines the specification for a record set.
    30  type RecordSpec struct {
    31  	Record        infrav1.AddressRecord
    32  	ZoneName      string
    33  	ResourceGroup string
    34  }
    35  
    36  // ResourceName returns the name of a record set.
    37  func (s RecordSpec) ResourceName() string {
    38  	return s.Record.Hostname
    39  }
    40  
    41  // OwnerResourceName returns the zone name of a record set.
    42  func (s RecordSpec) OwnerResourceName() string {
    43  	return s.ZoneName
    44  }
    45  
    46  // ResourceGroupName returns the name of the resource group of a record set.
    47  func (s RecordSpec) ResourceGroupName() string {
    48  	return s.ResourceGroup
    49  }
    50  
    51  // Parameters returns the parameters for a record set.
    52  func (s RecordSpec) Parameters(ctx context.Context, existing interface{}) (params interface{}, err error) {
    53  	if existing != nil {
    54  		if _, ok := existing.(armprivatedns.RecordSet); !ok {
    55  			return nil, errors.Errorf("%T is not an armprivatedns.RecordSet", existing)
    56  		}
    57  	}
    58  	set := armprivatedns.RecordSet{
    59  		Properties: &armprivatedns.RecordSetProperties{
    60  			TTL: ptr.To[int64](300),
    61  		},
    62  	}
    63  	recordType := converters.GetRecordType(s.Record.IP)
    64  	switch recordType {
    65  	case armprivatedns.RecordTypeA:
    66  		set.Properties.ARecords = []*armprivatedns.ARecord{{
    67  			IPv4Address: &s.Record.IP,
    68  		}}
    69  	case armprivatedns.RecordTypeAAAA:
    70  		set.Properties.AaaaRecords = []*armprivatedns.AaaaRecord{{
    71  			IPv6Address: &s.Record.IP,
    72  		}}
    73  	default:
    74  		return nil, errors.Errorf("unknown record type %s", recordType)
    75  	}
    76  
    77  	return set, nil
    78  }