Skip to content

Commit

Permalink
Tidy
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianMoyles committed Jun 28, 2024
1 parent e0c8c34 commit 49f6174
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,34 +68,33 @@ func createSiteOutboundRoutes(ctx context.Context, d *schema.ResourceData, meta
proxy := getSiteOutboundRouteProxy(sdkConfig)
siteId := d.Get("site_id").(string)
outboundRoutes := buildOutboundRoutes(d.Get("outbound_routes").(*schema.Set))

log.Printf("creating outbound routes for site %s", siteId)
var newRoutes []platformclientv2.Outboundroutebase

// When creating outbound routes, routes may already exist in the site. This can lead to error `Outbound Route Already Exists`
// To prevent this, existing routes for the site are obtained and compared with the routes to be created
// ONLY non-existing routes are created for the site

// Get the current outbound routes
log.Printf("Retrieving existing outbound routes for side %s before creation", siteId)

outboundRoutesAPI, resp, err := proxy.getSiteOutboundRoutes(ctx, siteId)
if err != nil {
return util.BuildAPIDiagnosticError(resourceName, fmt.Sprintf("Failed to get outbound routes for site %s error: %s", d.Id(), err), resp)
}

// If the site already has routes, filter and create routes that don't exist
// Otherwise, create every route
if outboundRoutesAPI != nil && len(*outboundRoutesAPI) > 0 {
// check which routes need to be created and which routes already exist
createRoutes, existingRoutes := checkExistingRoutes(outboundRoutes, outboundRoutesAPI)
if existingRoutes != nil && len(existingRoutes) > 0 {
log.Printf("Some outbound routes already exist for site %s. Creating non-existing routes", siteId)
}
newRoutes = checkExistingRoutes(outboundRoutes, outboundRoutesAPI, siteId)
} else {
newRoutes = append(newRoutes, *outboundRoutes...)
}

// create only the non-existing routes
if createRoutes != nil && len(createRoutes) > 0 {
for _, outboundRoute := range createRoutes {
_, resp, err := proxy.createSiteOutboundRoute(ctx, siteId, &outboundRoute)
if err != nil {
return util.BuildAPIDiagnosticError(resourceName, fmt.Sprintf("failed to create outbound route %s for site %s: %s", *outboundRoute.Name, siteId, err), resp)
}
}
log.Printf("creating outbound routes for site %s", siteId)

for _, outboundRoute := range newRoutes {
_, resp, err := proxy.createSiteOutboundRoute(ctx, siteId, &outboundRoute)
if err != nil {
return util.BuildAPIDiagnosticError(resourceName, fmt.Sprintf("failed to create outbound route %s for site %s: %s", *outboundRoute.Name, siteId, err), resp)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,183 +208,6 @@ func TestAccResourceSiteoutboundRoutes(t *testing.T) {
})
}

func TestAccResourceSiteOutboundRoutesExistingRoutes(t *testing.T) {
defer func() {
err := os.Unsetenv(featureToggles.OutboundRoutesToggleName())
if err != nil {
log.Printf("%s", err)
}
}()

err := os.Setenv(featureToggles.OutboundRoutesToggleName(), "enabled")
if err != nil {
t.Errorf("%s is not set", featureToggles.OutboundRoutesToggleName())
}

// Add route to site
// Try add route with same name to site
// try add same route and new route to site

var (
outboundRouteResource = "outbound_route"

// site
siteRes = "site"
name = "site " + uuid.NewString()
description = "terraform description 1"
mediaModel = "Cloud"

// location
locationRes = "test-location1"
)

emergencyNumber := "+13173124741"
if err := telephonyProvidersEdgesSite.DeleteLocationWithNumber(emergencyNumber, sdkConfig); err != nil {
t.Skipf("failed to delete location with number %s, %v", emergencyNumber, err)
}

location := gcloud.GenerateLocationResource(
locationRes,
"Terraform location"+uuid.NewString(),
"HQ1",
[]string{},
gcloud.GenerateLocationEmergencyNum(
emergencyNumber,
util.NullValue, // Default number type
), gcloud.GenerateLocationAddress(
"7601 Interactive Way",
"Indianapolis",
"IN",
"US",
"46278",
))

trunkBaseSettings1 := telephony.GenerateTrunkBaseSettingsResourceWithCustomAttrs(
"trunkBaseSettings1",
"test trunk base settings "+uuid.NewString(),
"test description",
"external_sip.json",
"EXTERNAL",
false)

trunkBaseSettings2 := telephony.GenerateTrunkBaseSettingsResourceWithCustomAttrs(
"trunkBaseSettings2",
"test trunk base settings "+uuid.NewString(),
"test description",
"external_sip.json",
"EXTERNAL",
false)

site := telephonyProvidersEdgesSite.GenerateSiteResourceWithCustomAttrs(
siteRes,
name,
description,
"genesyscloud_location."+locationRes+".id",
mediaModel,
false,
util.AssignRegion(),
strconv.Quote("+19205551212"),
strconv.Quote("Wilco plumbing"),
"set_as_default_site = false")

resource.Test(t, resource.TestCase{
PreCheck: func() { util.TestAccPreCheck(t) },
ProviderFactories: provider.GetProviderFactories(providerResources, nil),
Steps: []resource.TestStep{
{
Config: trunkBaseSettings1 + trunkBaseSettings2 + location + site + generateOutboundRoutesResource(
outboundRouteResource,
"genesyscloud_telephony_providers_edges_site."+siteRes+".id",
generateSiteOutboundRoutes(
"outboundRoute name 1",
"outboundRoute description",
"\"International\"",
"genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings1.id",
"RANDOM",
util.FalseValue),
generateSiteOutboundRoutes(
"outboundRoute name 2",
"outboundRoute description",
"\"National\"",
"genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings2.id",
"SEQUENTIAL",
util.FalseValue),
),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.name", "outboundRoute name 1"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.description", "outboundRoute description"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.classification_types.0", "International"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.distribution", "RANDOM"),
resource.TestCheckResourceAttrPair("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.external_trunk_base_ids.0", "genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings1", "id"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.enabled", util.FalseValue),

resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.name", "outboundRoute name 2"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.description", "outboundRoute description"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.classification_types.0", "National"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.distribution", "SEQUENTIAL"),
resource.TestCheckResourceAttrPair("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.external_trunk_base_ids.0", "genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings2", "id"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.enabled", util.FalseValue),
),
},
// Add a new route and existing route to ensure creation only adds non-existent route
{
Config: trunkBaseSettings1 + trunkBaseSettings2 + location + site + generateOutboundRoutesResource(
outboundRouteResource,
"genesyscloud_telephony_providers_edges_site."+siteRes+".id",
generateSiteOutboundRoutes(
"outboundRoute name 1",
"outboundRoute description",
"\"International\"",
"genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings1.id",
"RANDOM",
util.FalseValue),
generateSiteOutboundRoutes(
"outboundRoute name 2",
"outboundRoute description",
"\"National\"",
"genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings2.id",
"SEQUENTIAL",
util.FalseValue),
generateSiteOutboundRoutes(
"outboundRoute name 3",
"outboundRoute description",
"\"International\"",
"genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings1.id",
"RANDOM",
util.FalseValue)),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.name", "outboundRoute name 1"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.description", "outboundRoute description"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.classification_types.0", "International"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.distribution", "RANDOM"),
resource.TestCheckResourceAttrPair("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.external_trunk_base_ids.0", "genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings1", "id"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.0.enabled", util.FalseValue),

resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.name", "outboundRoute name 2"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.description", "outboundRoute description"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.classification_types.0", "National"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.distribution", "SEQUENTIAL"),
resource.TestCheckResourceAttrPair("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.external_trunk_base_ids.0", "genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings2", "id"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.1.enabled", util.FalseValue),

resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.3.name", "outboundRoute name 3"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.3.description", "outboundRoute description"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.3.classification_types.0", "International"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.3.distribution", "RANDOM"),
resource.TestCheckResourceAttrPair("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.3.external_trunk_base_ids.0", "genesyscloud_telephony_providers_edges_trunkbasesettings.trunkBaseSettings1", "id"),
resource.TestCheckResourceAttr("genesyscloud_telephony_providers_edges_site_outbound_route."+outboundRouteResource, "outbound_routes.3.enabled", util.FalseValue),
),
},
{
// Import/Read
ResourceName: "genesyscloud_telephony_providers_edges_site_outbound_route." + outboundRouteResource,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func generateOutboundRoutesResource(routesResource, siteId string, nestedBlocks ...string) string {
return fmt.Sprintf(`resource "genesyscloud_telephony_providers_edges_site_outbound_route" "%s" {
site_id = %s
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package telephony_providers_edges_site_outbound_route

import (
"log"
"terraform-provider-genesyscloud/genesyscloud/util/resourcedata"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -38,15 +39,15 @@ func buildOutboundRoutes(outboundRoutes *schema.Set) *[]platformclientv2.Outboun
return &outboundRoutesSdk
}

func checkExistingRoutes(definedRoutes, apiRoutes *[]platformclientv2.Outboundroutebase) (createRoutes, existingRoutes []platformclientv2.Outboundroutebase) {
func checkExistingRoutes(definedRoutes, apiRoutes *[]platformclientv2.Outboundroutebase, siteId string) (newRoutes []platformclientv2.Outboundroutebase) {
for _, definedRoute := range *definedRoutes {
if _, present := nameInOutboundRoutes(*definedRoute.Name, *apiRoutes); present {
existingRoutes = append(existingRoutes, definedRoute)
log.Printf("Route %s associated with site %s already exists. Creating only non-existing routes", *definedRoute.Name, siteId)
} else {
createRoutes = append(createRoutes, definedRoute)
newRoutes = append(newRoutes, definedRoute)
}
}
return createRoutes, existingRoutes
return newRoutes
}

func nameInOutboundRoutes(name string, outboundRoutes []platformclientv2.Outboundroutebase) (*platformclientv2.Outboundroutebase, bool) {
Expand Down

0 comments on commit 49f6174

Please sign in to comment.