Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DXE-2430 Keep as much as possible traffic targets and liveness tests declarati… #404

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ require (
github.com/spf13/cast v1.3.1
github.com/stretchr/testify v1.7.2
github.com/tj/assert v0.0.3
golang.org/x/exp v0.0.0-20230307190834-24139beb5833
google.golang.org/grpc v1.50.1
)

Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,8 @@ golang.org/x/crypto v0.0.0-20210616213533-5ff15b29337e/go.mod h1:GvvjBRRGRdwPK5y
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 h1:O8uGbHCqlTp2P6QJSLmCojM4mN6UemYv8K+dCnmHmu0=
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833 h1:SChBja7BCQewoTAU7IgvucQKMIXrEpFxNMs0spT3/5s=
golang.org/x/exp v0.0.0-20230307190834-24139beb5833/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down Expand Up @@ -318,6 +320,8 @@ golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBc
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.5.0 h1:MUK/U/4lj1t1oPg0HfuXDN/Z1wv31ZJ/YcPiGccS4DU=
golang.org/x/sys v0.5.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f h1:v4INt8xihDGvnrfjMDVXGxw9wrfxYyCjk0KbXjhR55s=
golang.org/x/sys v0.0.0-20220722155257-8c9f86f7a55f/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
Expand Down
91 changes: 58 additions & 33 deletions pkg/providers/gtm/resource_akamai_gtm_geomap.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import (
"github.com/akamai/terraform-provider-akamai/v3/pkg/tools"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"golang.org/x/exp/slices"
)

func resourceGTMv1Geomap() *schema.Resource {
Expand Down Expand Up @@ -456,47 +458,70 @@ func populateGeoAssignmentsObject(d *schema.ResourceData, geo *gtm.GeoMap, m int
}
}

// create and populate Terraform geoMap assignments schema
func populateTerraformGeoAssignmentsState(d *schema.ResourceData, geo *gtm.GeoMap, m interface{}) {
func setGeoAssignmentAtIndex(asArr *[]interface{}, as *gtm.GeoAssignment, index int, doReplace bool, m interface{}) {
meta := akamai.Meta(m)
logger := meta.Log("Akamai GTM", "populateTerraformGeoAssignmentsState")
logger := meta.Log("Akamai GTM", "setGeoAssignmentAtIndex")

objectInventory := make(map[int]*gtm.GeoAssignment, len(geo.Assignments))
if len(geo.Assignments) > 0 {
for _, aObj := range geo.Assignments {
objectInventory[aObj.DatacenterId] = aObj
if !doReplace {
newInt := make([]interface{}, 1)
newInt[0] = make(map[string]interface{})
if index == 0 {
*asArr = append(newInt, *asArr...)
} else {
*asArr = append((*asArr)[:index], append(newInt, (*asArr)[index:]...)...)
}
}
aStateList, _ := tools.GetInterfaceArrayValue("assignment", d)
for _, aMap := range aStateList {
a := aMap.(map[string]interface{})
objIndex := a["datacenter_id"].(int)
aObject := objectInventory[objIndex]
if aObject == nil {
logger.Warnf("Geo Assignment %d NOT FOUND in returned GTM Object", a["datacenter_id"])
continue
}
a["datacenter_id"] = aObject.DatacenterId
a["nickname"] = aObject.Nickname
countries, ok := a["countries"].(*schema.Set)
if !ok {

targetAs := (*asArr)[index].(map[string]interface{})
targetAs["datacenter_id"] = as.DatacenterId
targetAs["nickname"] = as.Nickname

cts, ok := targetAs["countries"]
if ok {
countries, cOk := cts.(*schema.Set)
if !cOk {
logger.Warnf("wrong type conversion: expected *schema.Set, got %T", countries)
}
a["countries"] = reconcileTerraformLists(countries.List(), convertStringToInterfaceList(aObject.Countries, m), m)
// remove object
delete(objectInventory, objIndex)
}
if len(objectInventory) > 0 {
logger.Debugf("Geo Assignment objects left...")
// Objects not in the state yet. Add. Unfortunately, they not align with instance indices in the config
for _, maObj := range objectInventory {
aNew := map[string]interface{}{
"datacenter_id": maObj.DatacenterId,
"nickname": maObj.Nickname,
"countries": maObj.Countries,
targetAs["countries"] = reconcileTerraformLists(countries.List(), convertStringToInterfaceList(as.Countries, m), m)
} else {
targetAs["countries"] = as.Countries
}
}

// create and populate Terraform geoMap assignments schema
func populateTerraformGeoAssignmentsState(d *schema.ResourceData, geo *gtm.GeoMap, m interface{}) {
meta := akamai.Meta(m)
logger := meta.Log("Akamai GTM", "populateTerraformGeoAssignmentsState")

aStateList, _ := tools.GetInterfaceArrayValue("assignment", d)
stateIdx := 0

for _, aObj := range geo.Assignments {
idx := slices.IndexFunc(aStateList, func(as interface{}) bool {
return as.(map[string]interface{})["datacenter_id"].(int) == aObj.DatacenterId
})

if idx == -1 {
if stateIdx == 0 {
logger.Debugf("assignment for dc %s not found into the state, prepending it", aObj.DatacenterId)
setGeoAssignmentAtIndex(&aStateList, aObj, 0, false, m)
} else {
logger.Debugf("assignment for dc %s not found into the state, inserting it at position %s into the state", aObj.DatacenterId, stateIdx)
setGeoAssignmentAtIndex(&aStateList, aObj, stateIdx, false, m)
}
} else {
if idx < stateIdx {
stateIdx--
logger.Debugf("assignment for dc %s will now be placed after the position it was in the past (moving it from %s to %s)", aObj.DatacenterId, idx, stateIdx)
aStateList = append(aStateList[:idx], aStateList[idx+1:]...)
setGeoAssignmentAtIndex(&aStateList, aObj, stateIdx, false, m)
} else {
logger.Debugf("assignment for dc %s will be updated in-place into the state (index %s)", aObj.DatacenterId, idx)
setGeoAssignmentAtIndex(&aStateList, aObj, idx, true, m)
stateIdx = idx
}
aStateList = append(aStateList, aNew)
}
stateIdx++
}
if err := d.Set("assignment", aStateList); err != nil {
logger.Errorf("populateTerraformGeoAssignmentsState failed: %s", err.Error())
Expand Down
Loading