Skip to content

Commit

Permalink
wip: add tests and implementation for Parameters method and add some …
Browse files Browse the repository at this point in the history
…TODOs

Signed-off-by: Karuppiah Natarajan <[email protected]>
  • Loading branch information
karuppiah7890 committed Oct 2, 2021
1 parent b5f986b commit 676769b
Show file tree
Hide file tree
Showing 2 changed files with 89 additions and 22 deletions.
72 changes: 60 additions & 12 deletions azure/services/publicips/publicip_spec_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,25 @@ import (
func TestParameters(t *testing.T) {
testCases := []struct {
name string
existingPublicIPAddress *network.PublicIPAddress
existingPublicIPAddress interface{}
publicIPSpec PublicIPSpec
expectedPublicIPAddress network.PublicIPAddress
}{
{
name: "ipv4 public ip address with dns",
name: "public ipv4 address with dns",
existingPublicIPAddress: nil,
publicIPSpec: PublicIPSpec{
Name: "my-publicip",
DNSName: "fakedns.mydomain.io",
},
expectedPublicIPAddress: network.PublicIPAddress{
Name: to.StringPtr("my-publicip"),
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard},
Location: to.StringPtr("testlocation"),
Tags: map[string]*string{
"Name": to.StringPtr("my-publicip"),
"sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": to.StringPtr("owned"),
},
Name: to.StringPtr("my-publicip"),
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard},
// Location: to.StringPtr("testlocation"),
// Tags: map[string]*string{
// "Name": to.StringPtr("my-publicip"),
// "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": to.StringPtr("owned"),
// },
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
PublicIPAddressVersion: network.IPVersionIPv4,
PublicIPAllocationMethod: network.IPAllocationMethodStatic,
Expand All @@ -56,17 +56,65 @@ func TestParameters(t *testing.T) {
},
},
},
{
name: "public ipv4 address without dns",
existingPublicIPAddress: nil,
publicIPSpec: PublicIPSpec{
Name: "my-publicip-2",
},
expectedPublicIPAddress: network.PublicIPAddress{
Name: to.StringPtr("my-publicip-2"),
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard},
// Location: to.StringPtr("testlocation"),
// Tags: map[string]*string{
// "Name": to.StringPtr("my-publicip"),
// "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": to.StringPtr("owned"),
// },
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
PublicIPAddressVersion: network.IPVersionIPv4,
PublicIPAllocationMethod: network.IPAllocationMethodStatic,
},
},
},
{
name: "public ipv6 address with dns",
existingPublicIPAddress: nil,
publicIPSpec: PublicIPSpec{
Name: "my-publicip-ipv6",
IsIPv6: true,
DNSName: "fakename.mydomain.io",
},
expectedPublicIPAddress: network.PublicIPAddress{
Name: to.StringPtr("my-publicip-ipv6"),
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard},
// Location: to.StringPtr("testlocation"),
// Tags: map[string]*string{
// "Name": to.StringPtr("my-publicip"),
// "sigs.k8s.io_cluster-api-provider-azure_cluster_my-cluster": to.StringPtr("owned"),
// },
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
PublicIPAddressVersion: network.IPVersionIPv6,
PublicIPAllocationMethod: network.IPAllocationMethodStatic,
DNSSettings: &network.PublicIPAddressDNSSettings{
DomainNameLabel: to.StringPtr("fakename"),
Fqdn: to.StringPtr("fakename.mydomain.io"),
},
},
},
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
t.Parallel()

g := NewWithT(t)

publicIPAddress, err := testCase.publicIPSpec.Parameters(testCase.existingPublicIPAddress)
publicIPAddressI, err := testCase.publicIPSpec.Parameters(testCase.existingPublicIPAddress)

g.Expect(err).To(BeNil())

publicIPAddress, ok := publicIPAddressI.(network.PublicIPAddress)

g.Expect(ok).To(BeTrue())
g.Expect(publicIPAddress).To(Equal(testCase.expectedPublicIPAddress))
})
}
Expand Down
39 changes: 29 additions & 10 deletions azure/services/publicips/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,12 @@ limitations under the License.
package publicips

import (
"strings"

"github.com/Azure/azure-sdk-for-go/services/network/mgmt/2021-02-01/network"
// "sigs.k8s.io/cluster-api-provider-azure/azure/converters"
"github.com/Azure/go-autorest/autorest/to"
// infrav1 "sigs.k8s.io/cluster-api-provider-azure/api/v1alpha4"
// "sigs.k8s.io/cluster-api-provider-azure/azure/converters"
)

// PublicIPSpec defines the specification for a Public IP.
Expand Down Expand Up @@ -49,24 +52,40 @@ func (s PublicIPSpec) ResourceGroupName() string {
func (s PublicIPSpec) Parameters(existing interface{}) (interface{}, error) {
if existing != nil {
// public IP already exists
// TODO: handle update later
// TODO(karuppiah7890): handle update later
return nil, nil
}

addressVersion := network.IPVersionIPv4
if s.IsIPv6 {
addressVersion = network.IPVersionIPv6
}

// only set DNS properties if there is a DNS name specified
var dnsSettings *network.PublicIPAddressDNSSettings
if s.DNSName != "" {
dnsSettings = &network.PublicIPAddressDNSSettings{
DomainNameLabel: to.StringPtr(strings.Split(s.DNSName, ".")[0]),
Fqdn: to.StringPtr(s.DNSName),
}
}

return network.PublicIPAddress{
// TODO(karuppiah7890): Add Tags with Cluster Name and other input
// Tags: converters.TagsToMap(infrav1.Build(infrav1.BuildParams{
// ClusterName: s.Scope.ClusterName(),
// Lifecycle: infrav1.ResourceLifecycleOwned,
// Name: to.StringPtr(ip.Name),
// Additional: s.Scope.AdditionalTags(),
// ClusterName: s.Scope.ClusterName(),
// Lifecycle: infrav1.ResourceLifecycleOwned,
// Name: to.StringPtr(s.Name),
// Additional: s.Scope.AdditionalTags(),
// })),
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard},
// Name: to.StringPtr(ip.Name),
Sku: &network.PublicIPAddressSku{Name: network.PublicIPAddressSkuNameStandard},
Name: to.StringPtr(s.Name),
// TODO(karuppiah7890): Add Location
// Location: to.StringPtr(s.Scope.Location()),
PublicIPAddressPropertiesFormat: &network.PublicIPAddressPropertiesFormat{
// PublicIPAddressVersion: addressVersion,
PublicIPAddressVersion: addressVersion,
PublicIPAllocationMethod: network.IPAllocationMethodStatic,
// DNSSettings: dnsSettings,
DNSSettings: dnsSettings,
},
}, nil
}

0 comments on commit 676769b

Please sign in to comment.