forked from MyPureCloud/terraform-provider-genesyscloud
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1c8015a
commit b08671e
Showing
8 changed files
with
490 additions
and
0 deletions.
There are no files selected for viewing
37 changes: 37 additions & 0 deletions
37
genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package simple_routing_queue | ||
|
||
import ( | ||
"context" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
gcloud "terraform-provider-genesyscloud/genesyscloud" | ||
"time" | ||
) | ||
|
||
/* | ||
The data_source_genesyscloud_simple_routing_queue.go contains the data source implementation | ||
for the resource. | ||
*/ | ||
|
||
// dataSourceSimpleRoutingQueueRead retrieves by search term the id in question | ||
func dataSourceSimpleRoutingQueueRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// Get an instance of our proxy | ||
|
||
// Grab our queue name from the schema.ResourceData object | ||
|
||
return gcloud.WithRetries(ctx, 15*time.Second, func() *resource.RetryError { | ||
|
||
// Call to the proxy function getRoutingQueueIdByName(context.Context, string) | ||
// This function returns values in the following order: queueId (string), retryable (bool), err (error) | ||
|
||
// If the error is not nil, and retryable equals false, return a resource.NonRetryableError | ||
// letting the user know that an error occurred | ||
|
||
// If retryable equals true, return a resource.RetryableError and let them know the queue could not be found | ||
// with that name | ||
|
||
// If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil | ||
return nil | ||
}) | ||
} |
54 changes: 54 additions & 0 deletions
54
genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package simple_routing_queue | ||
|
||
import ( | ||
"fmt" | ||
"github.com/google/uuid" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
gcloud "terraform-provider-genesyscloud/genesyscloud" | ||
"testing" | ||
) | ||
|
||
func TestAccDataSourceSimpleRoutingQueue(t *testing.T) { | ||
var ( | ||
resourceId = "queue_resource" | ||
dataSourceId = "queue_data" | ||
simpleQueueName = "Create2023 queue " + uuid.NewString() | ||
|
||
fullPathToResource = resourceName + "." + resourceId | ||
fullPathToDataSource = "data." + resourceName + "." + dataSourceId | ||
) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { gcloud.TestAccPreCheck(t) }, | ||
ProviderFactories: gcloud.GetProviderFactories(nil, nil), | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: generateSimpleRoutingQueueResource( | ||
resourceId, | ||
simpleQueueName, | ||
"null", | ||
"null", | ||
) + generateSimpleRoutingQueueDataSource( | ||
dataSourceId, | ||
simpleQueueName, | ||
fullPathToResource, | ||
), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair( | ||
fullPathToDataSource, "id", | ||
fullPathToResource, "id", | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func generateSimpleRoutingQueueDataSource(dataSourceId, queueName, dependsOn string) string { | ||
return fmt.Sprintf(` | ||
data "genesyscloud_simple_routing_queue" "%s" { | ||
name = "%s" | ||
depends_on = [%s] | ||
} | ||
`, dataSourceId, queueName, dependsOn) | ||
} |
64 changes: 64 additions & 0 deletions
64
genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package simple_routing_queue | ||
|
||
import ( | ||
"sync" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
/* | ||
The genesyscloud_simple_routing_queue_init_test.go file is used to initialize the data sources and resources | ||
used in testing the simple_routing_queues resource. | ||
Please make sure you register ALL resources and data sources your test cases will use. | ||
*/ | ||
|
||
// providerDataSources holds a map of all registered datasources | ||
var providerDataSources map[string]*schema.Resource | ||
|
||
// providerResources holds a map of all registered resources | ||
var providerResources map[string]*schema.Resource | ||
|
||
type registerTestInstance struct { | ||
resourceMapMutex sync.RWMutex | ||
datasourceMapMutex sync.RWMutex | ||
} | ||
|
||
// registerTestResources registers all resources used in the tests | ||
func (r *registerTestInstance) registerTestResources() { | ||
r.resourceMapMutex.Lock() | ||
defer r.resourceMapMutex.Unlock() | ||
|
||
providerResources[resourceName] = ResourceSimpleRoutingQueue() | ||
|
||
} | ||
|
||
// registerTestDataSources registers all data sources used in the tests. | ||
func (r *registerTestInstance) registerTestDataSources() { | ||
r.datasourceMapMutex.Lock() | ||
defer r.datasourceMapMutex.Unlock() | ||
|
||
providerDataSources[resourceName] = DataSourceSimpleRoutingQueue() | ||
|
||
} | ||
|
||
// initTestResources initializes all test resources and data sources. | ||
func initTestResources() { | ||
providerDataSources = make(map[string]*schema.Resource) | ||
providerResources = make(map[string]*schema.Resource) | ||
|
||
regInstance := ®isterTestInstance{} | ||
|
||
regInstance.registerTestDataSources() | ||
regInstance.registerTestResources() | ||
} | ||
|
||
// TestMain is a "setup" function called by the testing framework when run the test | ||
func TestMain(m *testing.M) { | ||
// Run setup function before starting the test suite for simple_routing_queue package | ||
initTestResources() | ||
|
||
// Run the test suite for the simple_routing_queue package | ||
m.Run() | ||
} |
124 changes: 124 additions & 0 deletions
124
genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
package simple_routing_queue | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/mypurecloud/platform-client-sdk-go/v105/platformclientv2" | ||
) | ||
|
||
type createRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) | ||
type getRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) | ||
type updateRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string, *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) | ||
type deleteRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string, bool) (*platformclientv2.APIResponse, error) | ||
type getRoutingQueueIdByNameFunc func(context.Context, *simpleRoutingQueueProxy, string) (id string, retryable bool, err error) | ||
|
||
var internalProxy *simpleRoutingQueueProxy | ||
|
||
type simpleRoutingQueueProxy struct { | ||
routingApi *platformclientv2.RoutingApi | ||
createRoutingQueueAttr createRoutingQueueFunc | ||
getRoutingQueueAttr getRoutingQueueFunc | ||
getRoutingQueueIdByNameAttr getRoutingQueueIdByNameFunc | ||
updateRoutingQueueAttr updateRoutingQueueFunc | ||
deleteRoutingQueueAttr deleteRoutingQueueFunc | ||
} | ||
|
||
// newSimpleRoutingQueueProxy initializes the simple routing queue proxy with all the data needed to communicate with Genesys Cloud | ||
func newSimpleRoutingQueueProxy(clientConfig *platformclientv2.Configuration) *simpleRoutingQueueProxy { | ||
api := platformclientv2.NewRoutingApiWithConfig(clientConfig) | ||
return &simpleRoutingQueueProxy{ | ||
routingApi: api, | ||
createRoutingQueueAttr: createRoutingQueueFn, | ||
getRoutingQueueAttr: getRoutingQueueFn, | ||
getRoutingQueueIdByNameAttr: getRoutingQueueIdByNameFn, | ||
updateRoutingQueueAttr: updateRoutingQueueFn, | ||
deleteRoutingQueueAttr: deleteRoutingQueueFn, | ||
} | ||
} | ||
|
||
// getSimpleRoutingQueueProxy 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 getSimpleRoutingQueueProxy(clientConfig *platformclientv2.Configuration) *simpleRoutingQueueProxy { | ||
if internalProxy == nil { | ||
internalProxy = newSimpleRoutingQueueProxy(clientConfig) | ||
} | ||
return internalProxy | ||
} | ||
|
||
// createRoutingQueue creates a Genesys Cloud Routing Queue | ||
func (p *simpleRoutingQueueProxy) createRoutingQueue(ctx context.Context, queue *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { | ||
return p.createRoutingQueueAttr(ctx, p, queue) | ||
} | ||
|
||
// getRoutingQueue retrieves a Genesys Cloud Routing Queue by ID | ||
func (p *simpleRoutingQueueProxy) getRoutingQueue(ctx context.Context, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { | ||
return p.getRoutingQueueAttr(ctx, p, id) | ||
} | ||
|
||
// getRoutingQueueIdByName retrieves a Genesys Cloud Routing Queue ID by its name | ||
func (p *simpleRoutingQueueProxy) getRoutingQueueIdByName(ctx context.Context, name string) (string, bool, error) { | ||
return p.getRoutingQueueIdByNameAttr(ctx, p, name) | ||
} | ||
|
||
// updateRoutingQueue updates a Genesys Cloud Routing Queue | ||
func (p *simpleRoutingQueueProxy) updateRoutingQueue(ctx context.Context, id string, queue *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { | ||
return p.updateRoutingQueueAttr(ctx, p, id, queue) | ||
} | ||
|
||
// deleteRoutingQueue deletes a Genesys Cloud Routing Queue | ||
func (p *simpleRoutingQueueProxy) deleteRoutingQueue(ctx context.Context, id string, forceDelete bool) (*platformclientv2.APIResponse, error) { | ||
return p.deleteRoutingQueueAttr(ctx, p, id, forceDelete) | ||
} | ||
|
||
// createRoutingQueueFn is an implementation function for creating a Genesys Cloud Routing Queue | ||
func createRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, queue *platformclientv2.Createqueuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { | ||
sdkQueue, response, err := proxy.routingApi.PostRoutingQueues(*queue) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to create queue: %v", err) | ||
} | ||
return sdkQueue, response, err | ||
} | ||
|
||
func getRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { | ||
queue, response, err := proxy.routingApi.GetRoutingQueue(id) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to get routing queue by id '%s': %v", id, err) | ||
} | ||
return queue, response, err | ||
} | ||
|
||
func getRoutingQueueIdByNameFn(ctx context.Context, proxy *simpleRoutingQueueProxy, name string) (string, bool, error) { | ||
for pageNum := 1; ; pageNum++ { | ||
const pageSize = 100 | ||
queues, _, getErr := proxy.routingApi.GetRoutingQueues(pageNum, pageSize, name, "", nil, nil, nil, false) | ||
if getErr != nil { | ||
return "", false, getErr | ||
} | ||
|
||
if queues.Entities == nil || len(*queues.Entities) == 0 { | ||
return "", true, fmt.Errorf("no routing queues found with name %s", name) | ||
} | ||
|
||
for _, queue := range *queues.Entities { | ||
if queue.Name != nil && *queue.Name == name { | ||
return *queue.Id, false, nil | ||
} | ||
} | ||
} | ||
} | ||
|
||
func updateRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string, body *platformclientv2.Queuerequest) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { | ||
queue, response, err := proxy.routingApi.PutRoutingQueue(id, *body) | ||
if err != nil { | ||
return nil, nil, fmt.Errorf("failed to update queue %s: %v", id, err) | ||
} | ||
return queue, response, err | ||
} | ||
|
||
func deleteRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string, forceDelete bool) (*platformclientv2.APIResponse, error) { | ||
response, err := proxy.routingApi.DeleteRoutingQueue(id, forceDelete) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to delete queue '%s': %v", id, err) | ||
} | ||
return response, err | ||
} |
98 changes: 98 additions & 0 deletions
98
genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
package simple_routing_queue | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"log" | ||
gcloud "terraform-provider-genesyscloud/genesyscloud" | ||
"terraform-provider-genesyscloud/genesyscloud/consistency_checker" | ||
"time" | ||
) | ||
|
||
/* | ||
The resource_genesyscloud_simple_routing_queue.go contains all of the methods that perform the core logic for a resource. | ||
In general a resource should have a approximately 5 methods in it: | ||
1. A create.... function that the resource will use to create a Genesys Cloud object (e.g. genesyscloud_simple_routing_queue) | ||
2. A read.... function that looks up a single resource. | ||
3. An update... function that updates a single resource. | ||
4. A delete.... function that deletes a single resource. | ||
Two things to note: | ||
1. All code in these methods should be focused on getting data in and out of Terraform. All code that is used for interacting | ||
with a Genesys API should be encapsulated into a proxy class contained within the package. | ||
2. In general, to keep this file somewhat manageable, if you find yourself with a number of helper functions move them to a | ||
utils file in the package. This will keep the code manageable and easy to work through. | ||
*/ | ||
|
||
// createSimpleRoutingQueue is used by the genesyscloud_simple_routing_queue resource to create a simple queue in Genesys cloud. | ||
func createSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// Get an instance of the proxy (example can be found in the delete method below) | ||
|
||
// Create a queue struct using the Genesys Cloud platform go sdk | ||
|
||
// Call the proxy function to create our queue | ||
|
||
return readSimpleRoutingQueue(ctx, d, meta) | ||
} | ||
|
||
// readSimpleRoutingQueue is used by the genesyscloud_simple_routing_queue resource to read a simple queue from Genesys cloud. | ||
func readSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// Get an instance of the proxy | ||
|
||
return gcloud.WithRetriesForRead(ctx, d, func() *resource.RetryError { | ||
// Call the read queue function to find our queue, passing in the ID from the resource data ( d.Id() ) | ||
// The returned value are: Queue, APIResponse, error | ||
// If the error is not nil, we should pass the response to the function gcloud.IsStatus404(response) | ||
// If the status is 404, return a resource.RetryableError. Otherwise, it should be a NonRetryableError | ||
|
||
// Define consistency checker | ||
cc := consistency_checker.NewConsistencyCheck(ctx, d, meta, ResourceSimpleRoutingQueue()) | ||
|
||
// Set our values in the schema resource data, based on the values | ||
// in the Queue object returned from the API | ||
|
||
return cc.CheckState() | ||
}) | ||
} | ||
|
||
// updateSimpleRoutingQueue is used by the genesyscloud_simple_routing_queue resource to update a simple queue in Genesys cloud. | ||
func updateSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// Get an instance of the proxy | ||
|
||
// Create a queue struct using the Genesys Cloud platform go sdk | ||
|
||
// Call the proxy function to update our queue, passing in the struct and the queue ID | ||
|
||
return readSimpleRoutingQueue(ctx, d, meta) | ||
} | ||
|
||
// deleteSimpleRoutingQueue is used by the genesyscloud_simple_routing_queue resource to delete a simple queue from Genesys cloud. | ||
func deleteSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
// Get an instance of the proxy | ||
sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig | ||
proxy := getSimpleRoutingQueueProxy(sdkConfig) | ||
|
||
/* Call the delete queue proxy function, passing in our queue ID from the schema.ResourceData object */ | ||
|
||
// Check that queue has been deleted by trying to get it from the API | ||
return gcloud.WithRetries(ctx, 30*time.Second, func() *resource.RetryError { | ||
_, respCode, err := proxy.getRoutingQueue(ctx, d.Id()) | ||
|
||
if err == nil { | ||
return resource.NonRetryableError(fmt.Errorf("error deleting routing queue %s: %s", d.Id(), err)) | ||
} | ||
if gcloud.IsStatus404(respCode) { | ||
// Success: Routing Queue deleted | ||
log.Printf("Deleted routing queue %s", d.Id()) | ||
return nil | ||
} | ||
|
||
return resource.RetryableError(fmt.Errorf("routing queue %s still exists", d.Id())) | ||
}) | ||
} |
Oops, something went wrong.