Skip to content

Commit

Permalink
Trunk Package Refactor. (#775)
Browse files Browse the repository at this point in the history
* moved contents to a package

* saving create

* final changes

* added reference in main.go

* fixed comments

* resolved cyclic

* updated with singleton ref
  • Loading branch information
kavinbalagen authored Jan 3, 2024
1 parent 2aeea09 commit c6d7902
Show file tree
Hide file tree
Showing 12 changed files with 312 additions and 116 deletions.
4 changes: 0 additions & 4 deletions genesyscloud/telephony/genesyscloud_telephony_init.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ import (

func SetRegistrar(l registrar.Registrar) {
l.RegisterDataSource("genesyscloud_telephony_providers_edges_trunkbasesettings", DataSourceTrunkBaseSettings())
l.RegisterDataSource("genesyscloud_telephony_providers_edges_trunk", DataSourceTrunk())
l.RegisterResource("genesyscloud_telephony_providers_edges_trunkbasesettings", ResourceTrunkBaseSettings())
l.RegisterResource("genesyscloud_telephony_providers_edges_trunk", ResourceTrunk())

l.RegisterExporter("genesyscloud_telephony_providers_edges_trunkbasesettings", TrunkBaseSettingsExporter())
l.RegisterExporter("genesyscloud_telephony_providers_edges_trunk", TrunkExporter())

}
11 changes: 3 additions & 8 deletions genesyscloud/telephony/genesyscloud_telephony_init_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
gcloud "terraform-provider-genesyscloud/genesyscloud"

edgeSite "terraform-provider-genesyscloud/genesyscloud/telephony_providers_edges_site"
edgeTrunk "terraform-provider-genesyscloud/genesyscloud/telephony_providers_edges_trunk"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand All @@ -24,11 +25,9 @@ func (r *registerTestInstance) registerTestResources() {
defer r.resourceMapMutex.Unlock()

providerResources["genesyscloud_telephony_providers_edges_trunkbasesettings"] = ResourceTrunkBaseSettings()
providerResources["genesyscloud_telephony_providers_edges_trunk"] = ResourceTrunk()

providerResources["genesyscloud_telephony_providers_edges_trunk"] = edgeTrunk.ResourceTrunk()
// external package dependencies for outbound
providerResources["genesyscloud_telephony_providers_edges_site"] = edgeSite.ResourceSite()

providerResources["genesyscloud_location"] = gcloud.ResourceLocation()

}
Expand All @@ -37,9 +36,8 @@ func (r *registerTestInstance) registerTestDataSources() {

r.datasourceMapMutex.Lock()
defer r.datasourceMapMutex.Unlock()

providerDataSources["genesyscloud_telephony_providers_edges_trunkbasesettings"] = DataSourceTrunkBaseSettings()
providerDataSources["genesyscloud_telephony_providers_edges_trunk"] = DataSourceTrunk()
providerDataSources["genesyscloud_telephony_providers_edges_trunk"] = edgeTrunk.DataSourceTrunk()
// external package dependencies for outbound
providerDataSources["genesyscloud_telephony_providers_edges_site"] = edgeSite.DataSourceSite()

Expand All @@ -48,17 +46,14 @@ func (r *registerTestInstance) registerTestDataSources() {
func initTestResources() {
providerDataSources = make(map[string]*schema.Resource)
providerResources = make(map[string]*schema.Resource)

regInstance := &registerTestInstance{}

regInstance.registerTestDataSources()
regInstance.registerTestResources()
}

func TestMain(m *testing.M) {
// Run setup function before starting the test suite for Outbound Package
initTestResources()

// Run the test suite for outbound
m.Run()
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package telephony
package telephony_providers_edges_trunk

import (
"context"
Expand All @@ -13,20 +13,6 @@ import (
"github.com/mypurecloud/platform-client-sdk-go/v116/platformclientv2"
)

func DataSourceTrunk() *schema.Resource {
return &schema.Resource{
Description: "Data source for Genesys Cloud Trunk. Select a trunk by name",
ReadContext: gcloud.ReadWithPooledClient(dataSourceTrunkRead),
Schema: map[string]*schema.Schema{
"name": {
Description: "Trunk name.",
Type: schema.TypeString,
Required: true,
},
},
}
}

func dataSourceTrunkRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sdkConfig := m.(*gcloud.ProviderMeta).ClientConfig
edgesAPI := platformclientv2.NewTelephonyProvidersEdgeApiWithConfig(sdkConfig)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package telephony
package telephony_providers_edges_trunk

import (
"fmt"
gcloud "terraform-provider-genesyscloud/genesyscloud"
telephony "terraform-provider-genesyscloud/genesyscloud/telephony"
edgeGroup "terraform-provider-genesyscloud/genesyscloud/telephony_providers_edges_edge_group"
"testing"

Expand All @@ -27,15 +27,15 @@ func TestAccDataSourceTrunk(t *testing.T) {
trunkData = "trunkData"
)

phoneTrunkBaseSettings := GenerateTrunkBaseSettingsResourceWithCustomAttrs(
phoneTrunkBaseSettings := telephony.GenerateTrunkBaseSettingsResourceWithCustomAttrs(
phoneTrunkBaseSettingsRes,
"phone trunk base settings "+uuid.NewString(),
"",
"phone_connections_webrtc.json",
"PHONE",
false)

trunkBaseSettingsConfig := GenerateTrunkBaseSettingsResourceWithCustomAttrs(
trunkBaseSettingsConfig := telephony.GenerateTrunkBaseSettingsResourceWithCustomAttrs(
trunkBaseSettingsRes,
"test trunk base settings "+uuid.NewString(),
"test description 1",
Expand Down Expand Up @@ -72,16 +72,3 @@ func TestAccDataSourceTrunk(t *testing.T) {
},
})
}

func generateTrunkDataSource(
resourceID string,
name string,
// Must explicitly use depends_on in terraform v0.13 when a data source references a resource
// Fixed in v0.14 https://github.com/hashicorp/terraform/pull/26284
dependsOnResource string) string {
return fmt.Sprintf(`data "genesyscloud_telephony_providers_edges_trunk" "%s" {
name = %s
depends_on=[%s]
}
`, resourceID, name, dependsOnResource)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package telephony_providers_edges_trunk

import (
"sync"
gcloud "terraform-provider-genesyscloud/genesyscloud"

telephony "terraform-provider-genesyscloud/genesyscloud/telephony"
edgeSite "terraform-provider-genesyscloud/genesyscloud/telephony_providers_edges_site"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var providerDataSources map[string]*schema.Resource
var providerResources map[string]*schema.Resource

type registerTestInstance struct {
resourceMapMutex sync.RWMutex
datasourceMapMutex sync.RWMutex
}

func (r *registerTestInstance) registerTestResources() {

r.resourceMapMutex.Lock()
defer r.resourceMapMutex.Unlock()

providerResources["genesyscloud_telephony_providers_edges_trunkbasesettings"] = telephony.ResourceTrunkBaseSettings()
providerResources["genesyscloud_telephony_providers_edges_trunk"] = ResourceTrunk()

// external package dependencies
providerResources["genesyscloud_telephony_providers_edges_site"] = edgeSite.ResourceSite()

providerResources["genesyscloud_location"] = gcloud.ResourceLocation()

}

func (r *registerTestInstance) registerTestDataSources() {

r.datasourceMapMutex.Lock()
defer r.datasourceMapMutex.Unlock()

providerDataSources["genesyscloud_telephony_providers_edges_trunkbasesettings"] = telephony.DataSourceTrunkBaseSettings()
providerDataSources["genesyscloud_telephony_providers_edges_trunk"] = DataSourceTrunk()
// external package dependencies
providerDataSources["genesyscloud_telephony_providers_edges_site"] = edgeSite.DataSourceSite()

}

func initTestResources() {
providerDataSources = make(map[string]*schema.Resource)
providerResources = make(map[string]*schema.Resource)

regInstance := &registerTestInstance{}

regInstance.registerTestDataSources()
regInstance.registerTestResources()
}

func TestMain(m *testing.M) {
// Run setup function before starting the test suite
initTestResources()

// Run the test suite
m.Run()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package telephony_providers_edges_trunk

import (
"context"

"github.com/mypurecloud/platform-client-sdk-go/v116/platformclientv2"
)

//generate a proxy for telephony_providers_edges_trunk

// internalProxy holds a proxy instance that can be used throughout the package
var internalProxy *trunkProxy

// Type definitions for each func on our proxy so we can easily mock them out later

type getTrunkByIdFunc func(ctx context.Context, p *trunkProxy, id string) (*platformclientv2.Trunk, *platformclientv2.APIResponse, error)
type getAllTrunksFunc func(ctx context.Context, p *trunkProxy, pageNum int, pageSize int) (*platformclientv2.Trunkentitylisting, *platformclientv2.APIResponse, error)
type getTrunkBaseSettingsFunc func(ctx context.Context, p *trunkProxy, trunkBaseSettingsId string) (*platformclientv2.Trunkbase, *platformclientv2.APIResponse, error)
type getEdgeFunc func(ctx context.Context, p *trunkProxy, edgeId string) (*platformclientv2.Edge, *platformclientv2.APIResponse, error)
type putEdgeFunc func(ctx context.Context, p *trunkProxy, edgeId string, edge platformclientv2.Edge) (*platformclientv2.Edge, *platformclientv2.APIResponse, error)
type getEdgeGroupFunc func(ctx context.Context, p *trunkProxy, edgeGroupId string) (*platformclientv2.Edgegroup, *platformclientv2.APIResponse, error)
type putEdgeGroupFunc func(ctx context.Context, p *trunkProxy, edgeGroupId string, edgeGroup platformclientv2.Edgegroup) (*platformclientv2.Edgegroup, *platformclientv2.APIResponse, error)

// Proxy contains all of the methods that call genesys cloud APIs.
type trunkProxy struct {
clientConfig *platformclientv2.Configuration
edgesApi *platformclientv2.TelephonyProvidersEdgeApi

getTrunkByIdAttr getTrunkByIdFunc
getAllTrunksAttr getAllTrunksFunc
getTrunkBaseSettingsAttr getTrunkBaseSettingsFunc
getEdgeAttr getEdgeFunc
putEdgeAttr putEdgeFunc
getEdgeGroupAttr getEdgeGroupFunc
putEdgeGroupAttr putEdgeGroupFunc
}

// initializes the proxy with all of the data needed to communicate with Genesys Cloud
func newTrunkProxy(clientConfig *platformclientv2.Configuration) *trunkProxy {
edgesApi := platformclientv2.NewTelephonyProvidersEdgeApiWithConfig(clientConfig)
return &trunkProxy{
clientConfig: clientConfig,
edgesApi: edgesApi,
getTrunkByIdAttr: getTrunkByIdFn,
getAllTrunksAttr: getAllTrunksFn,
getEdgeAttr: getEdgeFn,
putEdgeAttr: putEdgeFn,
getEdgeGroupAttr: getEdgeGroupFn,
putEdgeGroupAttr: putEdgeGroupFn,
getTrunkBaseSettingsAttr: getTrunkBaseSettingsFn,
}
}

// getTeamProxy acts as a singleton to for the internalProxy. It also ensures
// that we can still proxy our tests by directly setting internalProxy package variable
func getTrunkProxy(clientConfig *platformclientv2.Configuration) *trunkProxy {
if internalProxy == nil {
internalProxy = newTrunkProxy(clientConfig)
}

return internalProxy
}

func (p *trunkProxy) getEdge(ctx context.Context, edgeId string) (*platformclientv2.Edge, *platformclientv2.APIResponse, error) {
return p.getEdgeAttr(ctx, p, edgeId)
}

func (p *trunkProxy) putEdge(ctx context.Context, edgeId string, edge platformclientv2.Edge) (*platformclientv2.Edge, *platformclientv2.APIResponse, error) {
return p.putEdgeAttr(ctx, p, edgeId, edge)
}

func (p *trunkProxy) getEdgeGroup(ctx context.Context, edgeGroupId string) (*platformclientv2.Edgegroup, *platformclientv2.APIResponse, error) {
return p.getEdgeGroupAttr(ctx, p, edgeGroupId)
}

func (p *trunkProxy) putEdgeGroup(ctx context.Context, edgeGroupId string, edgeGroup platformclientv2.Edgegroup) (*platformclientv2.Edgegroup, *platformclientv2.APIResponse, error) {
return p.putEdgeGroupAttr(ctx, p, edgeGroupId, edgeGroup)
}

func (p *trunkProxy) getTrunkBaseSettings(ctx context.Context, trunkBaseSettingsId string) (*platformclientv2.Trunkbase, *platformclientv2.APIResponse, error) {
return p.getTrunkBaseSettingsAttr(ctx, p, trunkBaseSettingsId)
}

func (p *trunkProxy) getTrunkById(ctx context.Context, id string) (*platformclientv2.Trunk, *platformclientv2.APIResponse, error) {
return p.getTrunkByIdAttr(ctx, p, id)
}

func (p *trunkProxy) getAllTrunks(ctx context.Context, pageNum int, pageSize int) (*platformclientv2.Trunkentitylisting, *platformclientv2.APIResponse, error) {
return p.getAllTrunksAttr(ctx, p, pageNum, pageSize)
}

func getEdgeFn(ctx context.Context, p *trunkProxy, edgeId string) (*platformclientv2.Edge, *platformclientv2.APIResponse, error) {
return p.edgesApi.GetTelephonyProvidersEdge(edgeId, nil)
}

func putEdgeFn(ctx context.Context, p *trunkProxy, edgeId string, edge platformclientv2.Edge) (*platformclientv2.Edge, *platformclientv2.APIResponse, error) {
return p.edgesApi.PutTelephonyProvidersEdge(edgeId, edge)
}

func getEdgeGroupFn(ctx context.Context, p *trunkProxy, edgeGroupId string) (*platformclientv2.Edgegroup, *platformclientv2.APIResponse, error) {
return p.edgesApi.GetTelephonyProvidersEdgesEdgegroup(edgeGroupId, nil)
}

func putEdgeGroupFn(ctx context.Context, p *trunkProxy, edgeGroupId string, edgeGroup platformclientv2.Edgegroup) (*platformclientv2.Edgegroup, *platformclientv2.APIResponse, error) {
return p.edgesApi.PutTelephonyProvidersEdgesEdgegroup(edgeGroupId, edgeGroup)
}

func getTrunkBaseSettingsFn(ctx context.Context, p *trunkProxy, trunkBaseSettingsId string) (*platformclientv2.Trunkbase, *platformclientv2.APIResponse, error) {
return p.edgesApi.GetTelephonyProvidersEdgesTrunkbasesetting(trunkBaseSettingsId, true)
}

func getTrunkByIdFn(ctx context.Context, p *trunkProxy, trunkBaseSettingsId string) (*platformclientv2.Trunk, *platformclientv2.APIResponse, error) {
return p.edgesApi.GetTelephonyProvidersEdgesTrunk(trunkBaseSettingsId)
}

func getAllTrunksFn(ctx context.Context, p *trunkProxy, pageNum int, pageSize int) (*platformclientv2.Trunkentitylisting, *platformclientv2.APIResponse, error) {

return p.edgesApi.GetTelephonyProvidersEdgesTrunks(pageNum, pageSize, "", "", "", "", "")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package telephony_providers_edges_trunk

import (
gcloud "terraform-provider-genesyscloud/genesyscloud"
registrar "terraform-provider-genesyscloud/genesyscloud/resource_register"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

const resourceName = "genesyscloud_telephony_providers_edges_did"

func SetRegistrar(l registrar.Registrar) {
l.RegisterDataSource("genesyscloud_telephony_providers_edges_trunk", DataSourceTrunk())
l.RegisterResource("genesyscloud_telephony_providers_edges_trunk", ResourceTrunk())
l.RegisterExporter("genesyscloud_telephony_providers_edges_trunk", TrunkExporter())
}

func DataSourceTrunk() *schema.Resource {
return &schema.Resource{
Description: "Data source for Genesys Cloud Trunk. Select a trunk by name",
ReadContext: gcloud.ReadWithPooledClient(dataSourceTrunkRead),
Schema: map[string]*schema.Schema{
"name": {
Description: "Trunk name.",
Type: schema.TypeString,
Required: true,
},
},
}
}

func ResourceTrunk() *schema.Resource {
return &schema.Resource{
Description: "Genesys Cloud Trunk. Created by assigning a trunk base settings to an edge or edge group",

CreateContext: gcloud.CreateWithPooledClient(createTrunk),
ReadContext: gcloud.ReadWithPooledClient(readTrunk),
UpdateContext: gcloud.UpdateWithPooledClient(updateTrunk),
DeleteContext: gcloud.DeleteWithPooledClient(deleteTrunk),
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"trunk_base_settings_id": {
Description: "The trunk base settings reference",
Type: schema.TypeString,
Optional: true,
},
"edge_group_id": {
Description: "The edge group associated with this trunk. Either this or \"edge_id\" must be set",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"edge_id": {
Description: "The edge associated with this trunk. Either this or \"edge_group_id\" must be set",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Description: "The name of the trunk. This property is read only and populated with the auto generated name.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
Loading

0 comments on commit c6d7902

Please sign in to comment.