Skip to content

Commit

Permalink
Rename check* functions as is*UpToDate
Browse files Browse the repository at this point in the history
Signed-off-by: Alper Rifat Ulucinar <[email protected]>
  • Loading branch information
ulucinar committed Dec 2, 2021
1 parent 4515f75 commit 3ff1b85
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 26 deletions.
38 changes: 20 additions & 18 deletions pkg/clients/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,28 +173,30 @@ func UpdateSubnetStatusFromAzure(v *v1alpha3.Subnet, az networkmgmt.Subnet) {
v.Status.Purpose = azure.ToString(az.Purpose)
}

// UpdatePublicIPAddressStatusFromAzure updates the status related to the external
// Azure public ip address in the PublicIPAddressStatus
func UpdatePublicIPAddressStatusFromAzure(v *v1alpha3.PublicIPAddress, az networkmgmt.PublicIPAddress) {
v.Status.AtProvider.State = azure.ToString(az.ProvisioningState)
v.Status.AtProvider.Etag = azure.ToString(az.Etag)
v.Status.AtProvider.ID = azure.ToString(az.ID)
v.Status.AtProvider.Address = azure.ToString(az.IPAddress)
v.Status.AtProvider.Version = string(az.PublicIPAddressVersion)
// GeneratePublicIPAddressObservation returns the observation object related to the external
// Azure public IP address in the PublicIPAddressStatus
func GeneratePublicIPAddressObservation(az networkmgmt.PublicIPAddress) *v1alpha3.PublicIPAddressObservation {
v := &v1alpha3.PublicIPAddressObservation{}
v.State = azure.ToString(az.ProvisioningState)
v.Etag = azure.ToString(az.Etag)
v.ID = azure.ToString(az.ID)
v.Address = azure.ToString(az.IPAddress)
v.Version = string(az.PublicIPAddressVersion)
if az.IPConfiguration != nil {
v.Status.AtProvider.IPConfiguration = &v1alpha3.IPConfiguration{
v.IPConfiguration = &v1alpha3.IPConfiguration{
PrivateIPAllocationMethod: string(az.IPConfiguration.PrivateIPAllocationMethod),
PrivateIPAddress: az.IPConfiguration.PrivateIPAddress,
ProvisioningState: azure.ToString(az.IPConfiguration.ProvisioningState),
}
}
if az.DNSSettings != nil {
v.Status.AtProvider.DNSSettings = &v1alpha3.PublicIPAddressDNSSettingsObservation{
v.DNSSettings = &v1alpha3.PublicIPAddressDNSSettingsObservation{
DomainNameLabel: az.DNSSettings.DomainNameLabel,
FQDN: az.DNSSettings.Fqdn,
ReverseFQDN: az.DNSSettings.ReverseFqdn,
}
}
return v
}

// LateInitializePublicIPAddress late-initilizes a PublicIPAddress resource
Expand Down Expand Up @@ -249,26 +251,26 @@ func IsPublicIPAddressUpToDate(p v1alpha3.PublicIPAddressProperties, in networkm
return false
}

if !checkIPPrefixID(p.PublicIPPrefixID, in.PublicIPPrefix) {
if !isIPPrefixIDUpToDate(p.PublicIPPrefixID, in.PublicIPPrefix) {
return false
}

if azure.ToInt(p.TCPIdleTimeoutInMinutes) != azure.ToInt(in.IdleTimeoutInMinutes) {
return false
}

if !checkIPTags(p.IPTags, in.IPTags) {
if !isIPTagsUpToDate(p.IPTags, in.IPTags) {
return false
}

if !checkSKU(p.SKU, in.Sku) {
if !isSKUUpToDate(p.SKU, in.Sku) {
return false
}

return checkDNSSettings(p.PublicIPAddressDNSSettings, in.PublicIPAddressPropertiesFormat.DNSSettings)
return isDNSSettingsUpToDate(p.PublicIPAddressDNSSettings, in.PublicIPAddressPropertiesFormat.DNSSettings)
}

func checkDNSSettings(d *v1alpha3.PublicIPAddressDNSSettings, in *networkmgmt.PublicIPAddressDNSSettings) bool { // nolint:gocyclo
func isDNSSettingsUpToDate(d *v1alpha3.PublicIPAddressDNSSettings, in *networkmgmt.PublicIPAddressDNSSettings) bool {
if d == nil {
d = &v1alpha3.PublicIPAddressDNSSettings{}
}
Expand All @@ -279,14 +281,14 @@ func checkDNSSettings(d *v1alpha3.PublicIPAddressDNSSettings, in *networkmgmt.Pu
azure.ToString(d.ReverseFQDN) == azure.ToString(in.ReverseFqdn)
}

func checkIPPrefixID(p *string, in *networkmgmt.SubResource) bool {
func isIPPrefixIDUpToDate(p *string, in *networkmgmt.SubResource) bool {
if in == nil {
in = &networkmgmt.SubResource{}
}
return azure.ToString(p) == azure.ToString(in.ID)
}

func checkIPTags(t []v1alpha3.IPTag, in *[]networkmgmt.IPTag) bool {
func isIPTagsUpToDate(t []v1alpha3.IPTag, in *[]networkmgmt.IPTag) bool {
if in == nil {
in = &[]networkmgmt.IPTag{}
}
Expand Down Expand Up @@ -319,7 +321,7 @@ func checkIPTags(t []v1alpha3.IPTag, in *[]networkmgmt.IPTag) bool {
return true
}

func checkSKU(s *v1alpha3.SKU, in *networkmgmt.PublicIPAddressSku) bool {
func isSKUUpToDate(s *v1alpha3.SKU, in *networkmgmt.PublicIPAddressSku) bool {
if in == nil {
in = &networkmgmt.PublicIPAddressSku{}
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/clients/network/network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -693,7 +693,7 @@ func TestUpdatePublicIPAddressStatusFromAzure(t *testing.T) {
},
}

UpdatePublicIPAddressStatusFromAzure(v, tc.r)
v.Status.AtProvider = *GeneratePublicIPAddressObservation(tc.r)

// make sure that internal resource status hasn't changed
if diff := cmp.Diff(mockCondition, v.Status.ResourceStatus.Conditions[0]); diff != "" {
Expand Down
12 changes: 5 additions & 7 deletions pkg/controller/network/publicipaddress/managed.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ func (e *external) Observe(ctx context.Context, mg resource.Managed) (managed.Ex
return managed.ExternalObservation{}, errors.Wrap(err, errUpdateCR)
}

network.UpdatePublicIPAddressStatusFromAzure(s, az)
s.Status.AtProvider = *network.GeneratePublicIPAddressObservation(az)
s.SetConditions(xpv1.Available())

return managed.ExternalObservation{
Expand All @@ -130,16 +130,14 @@ func (e *external) Create(ctx context.Context, mg resource.Managed) (managed.Ext
}

func (e *external) Update(ctx context.Context, mg resource.Managed) (managed.ExternalUpdate, error) {
s, ok := mg.(*v1alpha3.PublicIPAddress)
cr, ok := mg.(*v1alpha3.PublicIPAddress)
if !ok {
return managed.ExternalUpdate{}, errors.New(errNotPublicIPAddress)
}

snet := network.NewPublicIPAddressParameters(s)
if _, err := e.client.CreateOrUpdate(ctx, s.Spec.ForProvider.ResourceGroupName, meta.GetExternalName(s), snet); err != nil {
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdatePublicIPAddress)
}
return managed.ExternalUpdate{}, nil
snet := network.NewPublicIPAddressParameters(cr)
_, err := e.client.CreateOrUpdate(ctx, cr.Spec.ForProvider.ResourceGroupName, meta.GetExternalName(cr), snet)
return managed.ExternalUpdate{}, errors.Wrap(err, errUpdatePublicIPAddress)
}

func (e *external) Delete(ctx context.Context, mg resource.Managed) error {
Expand Down
47 changes: 47 additions & 0 deletions pkg/controller/network/publicipaddress/managed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,3 +258,50 @@ func TestDelete(t *testing.T) {
})
}
}

func TestUpdate(t *testing.T) {
cases := []testCase{
{
name: "NotPublicIPAddress",
e: &external{client: &fake.MockPublicIPAddressClient{}},
r: &v1alpha3.Subnet{},
want: &v1alpha3.Subnet{},
wantErr: errors.New(errNotPublicIPAddress),
},
{
name: "SuccessfulUpdate",
e: &external{client: &fake.MockPublicIPAddressClient{
MockCreateOrUpdate: func(ctx context.Context, resourceGroupName string, publicIPAddressName string, parameters network.PublicIPAddress) (result network.PublicIPAddressesCreateOrUpdateFuture, err error) {
return network.PublicIPAddressesCreateOrUpdateFuture{}, nil
},
}},
r: publicIPAddress(),
want: publicIPAddress(),
},
{
name: "FailedUpdate",
e: &external{client: &fake.MockPublicIPAddressClient{
MockCreateOrUpdate: func(ctx context.Context, resourceGroupName string, publicIPAddressName string, parameters network.PublicIPAddress) (result network.PublicIPAddressesCreateOrUpdateFuture, err error) {
return network.PublicIPAddressesCreateOrUpdateFuture{}, errorBoom
},
}},
r: publicIPAddress(),
want: publicIPAddress(),
wantErr: errors.Wrap(errorBoom, errUpdatePublicIPAddress),
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
_, err := tc.e.Update(ctx, tc.r)

if diff := cmp.Diff(tc.wantErr, err, test.EquateErrors()); diff != "" {
t.Errorf("tc.e.Create(...): want error != got error:\n%s", diff)
}

if diff := cmp.Diff(tc.want, tc.r, test.EquateConditions()); diff != "" {
t.Errorf("r: -want, +got:\n%s", diff)
}
})
}
}

0 comments on commit 3ff1b85

Please sign in to comment.