Skip to content

Commit

Permalink
Fix for DEVTOOLING-967 (#1420)
Browse files Browse the repository at this point in the history
* Attempt a fix for DEVTOOLING-967

* Fix null pointer and set default logic

* Fix default Emergency number plan config

* Revert these fixes in favor of fixing in the PR #1432
  • Loading branch information
bbbco authored Dec 23, 2024
1 parent 17a077d commit 4d60f63
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,9 @@ func SiteExporter() *resourceExporter.ResourceExporter {
CustomValidateExports: map[string][]string{
"rrule": {"edge_auto_update_config.rrule"},
},
CustomAttributeResolver: map[string]*resourceExporter.RefAttrCustomResolver{
"number_plans": {ResolverFunc: siteNumberPlansExporterResolver},
},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"log"
"strings"
"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/resource_exporter"
"terraform-provider-genesyscloud/genesyscloud/util"
"time"

Expand Down Expand Up @@ -202,7 +203,7 @@ func updateSiteNumberPlans(ctx context.Context, sp *SiteProxy, d *schema.Resourc
}

updatedNumberPlans := make([]platformclientv2.Numberplan, 0)
namesOfOverridenDefaults := []string{}
namesOfOverriddenDefaults := []string{}

for _, numberPlanFromTf := range numberPlansFromTf {
if plan, ok := nameInPlans(*numberPlanFromTf.Name, *numberPlansFromAPI); ok {
Expand All @@ -214,7 +215,7 @@ func updateSiteNumberPlans(ctx context.Context, sp *SiteProxy, d *schema.Resourc
plan.MatchType = numberPlanFromTf.MatchType
plan.NormalizedFormat = numberPlanFromTf.NormalizedFormat

namesOfOverridenDefaults = append(namesOfOverridenDefaults, *numberPlanFromTf.Name)
namesOfOverriddenDefaults = append(namesOfOverriddenDefaults, *numberPlanFromTf.Name)
updatedNumberPlans = append(updatedNumberPlans, *plan)
} else {
// Add the plan
Expand All @@ -224,7 +225,7 @@ func updateSiteNumberPlans(ctx context.Context, sp *SiteProxy, d *schema.Resourc

for _, numberPlanFromAPI := range *numberPlansFromAPI {
// Keep the default plans which are not overriden.
if isDefaultPlan(*numberPlanFromAPI.Name) && !lists.ItemInSlice(*numberPlanFromAPI.Name, namesOfOverridenDefaults) {
if isDefaultPlan(*numberPlanFromAPI.Name) && !lists.ItemInSlice(*numberPlanFromAPI.Name, namesOfOverriddenDefaults) {
updatedNumberPlans = append(updatedNumberPlans, numberPlanFromAPI)
}
}
Expand Down Expand Up @@ -510,6 +511,82 @@ func buildSdkEdgeAutoUpdateConfig(d *schema.ResourceData) (*platformclientv2.Edg
return nil, nil
}

func siteNumberPlansExporterResolver(configMap map[string]interface{}, exporter map[string]*resource_exporter.ResourceExporter, _ string) error {

if numberPlans, ok := configMap["number_plans"].([]interface{}); ok {

defaultNumberPlans := []*platformclientv2.Numberplan{
{
Name: platformclientv2.String("Suicide Prevention"),
MatchType: platformclientv2.String("regex"),
Match: platformclientv2.String("^988$"),
NormalizedFormat: platformclientv2.String("+18002738255"),
Classification: platformclientv2.String("Suicide Prevention"),
},
{
Name: platformclientv2.String("National"),
MatchType: platformclientv2.String("intraCountryCode"),
Classification: platformclientv2.String("National"),
},
{
Name: platformclientv2.String("Emergency"),
MatchType: platformclientv2.String("numberList"),
Numbers: &[]platformclientv2.Number{
{
Start: platformclientv2.String("112"),
},
},
Classification: platformclientv2.String("Emergency"),
},
{
Name: platformclientv2.String("Network"),
MatchType: platformclientv2.String("regex"),
Match: platformclientv2.String("^([^@\\:]+@)([^@ ]+)?$"),
NormalizedFormat: platformclientv2.String("sip:$1$2"),
Classification: platformclientv2.String("Network"),
},
}

// Check to ensure all of the default classification types are exported
for _, numberPlan := range numberPlans {
numberPlanMap := numberPlan.(map[string]interface{})
classificationType := numberPlanMap["classification"].(string)
for _, defaultNumberPlan := range defaultNumberPlans {
if *defaultNumberPlan.Classification == classificationType {
defaultNumberPlans = lists.Remove(defaultNumberPlans, defaultNumberPlan)
}
}
}

// If not, we need to add them to the list of number plans, otherwise reapplying to a brand new org
// can cause issues due to the Default Outbound Route's ClassificationTypes config that is created
// with every new Site resource.
// This is a hacky way to do this, but it works for now.
if len(defaultNumberPlans) > 0 {
for _, defaultNumberPlan := range defaultNumberPlans {
for _, numberPlan := range numberPlans {
numberPlanMap := numberPlan.(map[string]interface{})
if numberPlanMap["name"] == *defaultNumberPlan.Name {
// Account for an edge case where the number plan has the same name as the
// one of the default number plans, but the classification type is different.
// This is to ensure that the configured number plans are not overwritten, but
// the default number plans are always available, we will rename the default number plan
if numberPlanMap["classification"] != *defaultNumberPlan.Classification {
newDefaultName := fmt.Sprintf("%s (Default)", *defaultNumberPlan.Name)
log.Printf("Renaming default number plan %s to %s", *defaultNumberPlan.Name, newDefaultName)
*defaultNumberPlan.Name = newDefaultName
}
}
}
numberPlans = append(numberPlans, flattenNumberPlan(defaultNumberPlan))
}
}

configMap["number_plans"] = numberPlans
}
return nil
}

func GenerateSiteResourceWithCustomAttrs(
siteResourceLabel,
name,
Expand Down
4 changes: 2 additions & 2 deletions genesyscloud/util/lists/util_lists.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func RemoveStringFromSlice(value string, slice []string) []string {
return s
}

func SubStringInSlice(a string, list []string) bool {
for _, b := range list {
func SubStringInSlice(a string, slice []string) bool {
for _, b := range slice {
if strings.Contains(b, a) {
return true
}
Expand Down

0 comments on commit 4d60f63

Please sign in to comment.