Skip to content
This repository has been archived by the owner on Jan 11, 2023. It is now read-only.

Commit

Permalink
Merge fields of AAD profile during update (#3234)
Browse files Browse the repository at this point in the history
  • Loading branch information
amanohar authored and jackfrancis committed Jun 8, 2018
1 parent 77dafcb commit 004f38c
Show file tree
Hide file tree
Showing 2 changed files with 248 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/api/agentPoolOnlyApi/v20180331/merge.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package v20180331

import (
"fmt"

"github.com/imdario/mergo"
)

// Merge existing ManagedCluster attribute into mc
Expand Down Expand Up @@ -37,9 +39,15 @@ func (mc *ManagedCluster) Merge(emc *ManagedCluster) error {
// For update scenario, the default behavior is to use existing behavior
mc.Properties.NetworkProfile = emc.Properties.NetworkProfile
}
if mc.Properties.AADProfile == nil {
// For update scenario, the default behavior is to use existing behavior
mc.Properties.AADProfile = emc.Properties.AADProfile

if emc.Properties.AADProfile != nil {
if mc.Properties.AADProfile == nil {
mc.Properties.AADProfile = &AADProfile{}
}
if err := mergo.Merge(mc.Properties.AADProfile,
*emc.Properties.AADProfile); err != nil {
return err
}
}
return nil
}
237 changes: 237 additions & 0 deletions pkg/api/agentPoolOnlyApi/v20180331/merge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,240 @@ func TestMerge_EnableRBAC(t *testing.T) {
}

}

func TestMerge_AAD(t *testing.T) {
// Partial AAD profile was passed during update
newMC := &ManagedCluster{
Properties: &Properties{
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
TenantID: "c234-5",
},
},
}

existingMC := &ManagedCluster{
Properties: &Properties{
DNSPrefix: "something",
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
ServerAppSecret: "ba34-5",
TenantID: "c234-5",
},
},
}

e := newMC.Merge(existingMC)
if e != nil {
t.Error("expect error to be nil")
}

if newMC.Properties.AADProfile == nil {
t.Error("AADProfile should not be nil")
}

if newMC.Properties.AADProfile.ServerAppSecret == "" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppID != "1a34-5" {
t.Error("ServerAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppSecret != "ba34-5" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ClientAppID != "1234-5" {
t.Error("ClientAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.TenantID != "c234-5" {
t.Error("TenantID did not have the expected value after merge")
}

// Nil AAD profile was passed during update but DM had AAD Profile
newMC = &ManagedCluster{
Properties: &Properties{
EnableRBAC: helpers.PointerToBool(true),
AADProfile: nil,
},
}

existingMC = &ManagedCluster{
Properties: &Properties{
DNSPrefix: "something",
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
ServerAppSecret: "ba34-5",
TenantID: "c234-5",
},
},
}

e = newMC.Merge(existingMC)
if e != nil {
t.Error("expect error to be nil")
}

if newMC.Properties.AADProfile == nil {
t.Error("AADProfile should not be nil")
}

if newMC.Properties.AADProfile.ServerAppSecret == "" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppID != "1a34-5" {
t.Error("ServerAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppSecret != "ba34-5" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ClientAppID != "1234-5" {
t.Error("ClientAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.TenantID != "c234-5" {
t.Error("TenantID did not have the expected value after merge")
}

// No AAD profile set
newMC = &ManagedCluster{
Properties: &Properties{
EnableRBAC: helpers.PointerToBool(true),
AADProfile: nil,
},
}

existingMC = &ManagedCluster{
Properties: &Properties{
DNSPrefix: "something",
EnableRBAC: helpers.PointerToBool(true),
AADProfile: nil,
},
}

e = newMC.Merge(existingMC)
if e != nil {
t.Error("expect error to be nil")
}

if newMC.Properties.AADProfile != nil {
t.Error("AADProfile should be nil")
}

// Empty field in AAD profile was passed during update but DM had AAD Profile
newMC = &ManagedCluster{
Properties: &Properties{
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
ServerAppSecret: "",
TenantID: "c234-5",
},
},
}

existingMC = &ManagedCluster{
Properties: &Properties{
DNSPrefix: "something",
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
ServerAppSecret: "ba34-5",
TenantID: "c234-5",
},
},
}

e = newMC.Merge(existingMC)
if e != nil {
t.Error("expect error to be nil")
}

if newMC.Properties.AADProfile == nil {
t.Error("AADProfile should not be nil")
}

if newMC.Properties.AADProfile.ServerAppID != "1a34-5" {
t.Error("ServerAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppSecret != "ba34-5" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ClientAppID != "1234-5" {
t.Error("ClientAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.TenantID != "c234-5" {
t.Error("TenantID did not have the expected value after merge")
}

// Full AAD profile was passed during update
newMC = &ManagedCluster{
Properties: &Properties{
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
ServerAppSecret: "ba34-5",
TenantID: "c234-5",
},
},
}

existingMC = &ManagedCluster{
Properties: &Properties{
DNSPrefix: "something",
EnableRBAC: helpers.PointerToBool(true),
AADProfile: &AADProfile{
ClientAppID: "1234-5",
ServerAppID: "1a34-5",
ServerAppSecret: "ba34-5",
TenantID: "c234-5",
},
},
}

e = newMC.Merge(existingMC)
if e != nil {
t.Error("expect error to be nil")
}

if newMC.Properties.AADProfile == nil {
t.Error("AADProfile should not be nil")
}

if newMC.Properties.AADProfile.ServerAppSecret == "" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppID != "1a34-5" {
t.Error("ServerAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ServerAppSecret != "ba34-5" {
t.Error("ServerAppSecret did not have the expected value after merge")
}

if newMC.Properties.AADProfile.ClientAppID != "1234-5" {
t.Error("ClientAppID did not have the expected value after merge")
}

if newMC.Properties.AADProfile.TenantID != "c234-5" {
t.Error("TenantID did not have the expected value after merge")
}
}

0 comments on commit 004f38c

Please sign in to comment.