From 4d60f63b33b64b3a8cde44c44c17d600134b1935 Mon Sep 17 00:00:00 2001 From: Brian Goad Date: Mon, 23 Dec 2024 07:39:14 -0500 Subject: [PATCH] Fix for DEVTOOLING-967 (#1420) * 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 --- ...d_telephony_providers_edges_site_schema.go | 3 + ...ud_telephony_providers_edges_site_utils.go | 83 ++++++++++++++++++- genesyscloud/util/lists/util_lists.go | 4 +- 3 files changed, 85 insertions(+), 5 deletions(-) diff --git a/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_schema.go b/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_schema.go index 32a18c226..de3927faa 100644 --- a/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_schema.go +++ b/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_schema.go @@ -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}, + }, } } diff --git a/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_utils.go b/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_utils.go index ee953a9b9..4faac4f97 100644 --- a/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_utils.go +++ b/genesyscloud/telephony_providers_edges_site/resource_genesyscloud_telephony_providers_edges_site_utils.go @@ -6,6 +6,7 @@ import ( "log" "strings" "terraform-provider-genesyscloud/genesyscloud/provider" + "terraform-provider-genesyscloud/genesyscloud/resource_exporter" "terraform-provider-genesyscloud/genesyscloud/util" "time" @@ -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 { @@ -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 @@ -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) } } @@ -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, diff --git a/genesyscloud/util/lists/util_lists.go b/genesyscloud/util/lists/util_lists.go index 21b2462fe..c5c912973 100644 --- a/genesyscloud/util/lists/util_lists.go +++ b/genesyscloud/util/lists/util_lists.go @@ -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 }