From b08671e69086637f244f6541f8cd6391dd34c793 Mon Sep 17 00:00:00 2001 From: Charlie Conneely Date: Mon, 28 Aug 2023 11:52:22 +0100 Subject: [PATCH 1/3] Create 2023 Workshop V1 --- ...ource_genesyscloud_simple_routing_queue.go | 37 ++++++ ..._genesyscloud_simple_routing_queue_test.go | 54 ++++++++ ...syscloud_simple_routing_queue_init_test.go | 64 +++++++++ ...genesyscloud_simple_routing_queue_proxy.go | 124 ++++++++++++++++++ ...ource_genesyscloud_simple_routing_queue.go | 98 ++++++++++++++ ...enesyscloud_simple_routing_queue_schema.go | 60 +++++++++ ..._genesyscloud_simple_routing_queue_test.go | 51 +++++++ main.go | 2 + 8 files changed, 490 insertions(+) create mode 100644 genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go create mode 100644 genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go create mode 100644 genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go create mode 100644 genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go create mode 100644 genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go create mode 100644 genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go create mode 100644 genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go diff --git a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go new file mode 100644 index 000000000..392ffeb40 --- /dev/null +++ b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go @@ -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 + }) +} diff --git a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go new file mode 100644 index 000000000..16b1a131c --- /dev/null +++ b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go @@ -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) +} diff --git a/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go new file mode 100644 index 000000000..71507d6d6 --- /dev/null +++ b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go @@ -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() +} diff --git a/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go new file mode 100644 index 000000000..db2b081aa --- /dev/null +++ b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go @@ -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 +} diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go new file mode 100644 index 000000000..90d1c6f57 --- /dev/null +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go @@ -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())) + }) +} diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go new file mode 100644 index 000000000..075c46f85 --- /dev/null +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go @@ -0,0 +1,60 @@ +package simple_routing_queue + +import ( + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + gcloud "terraform-provider-genesyscloud/genesyscloud" + registrar "terraform-provider-genesyscloud/genesyscloud/resource_register" +) + +const resourceName = "genesyscloud_simple_routing_queue" + +func SetRegistrar(l registrar.Registrar) { + l.RegisterResource(resourceName, ResourceSimpleRoutingQueue()) + l.RegisterDataSource(resourceName, DataSourceSimpleRoutingQueue()) +} + +func ResourceSimpleRoutingQueue() *schema.Resource { + return &schema.Resource{ + Description: "Genesys Cloud Simple Routing Queue", + + CreateContext: gcloud.CreateWithPooledClient(createSimpleRoutingQueue), + ReadContext: gcloud.ReadWithPooledClient(readSimpleRoutingQueue), + UpdateContext: gcloud.UpdateWithPooledClient(updateSimpleRoutingQueue), + DeleteContext: gcloud.DeleteWithPooledClient(deleteSimpleRoutingQueue), + Importer: &schema.ResourceImporter{ + StateContext: schema.ImportStatePassthroughContext, + }, + SchemaVersion: 1, + Schema: map[string]*schema.Schema{ + "name": { + Description: "The name for our routing queue.", + Type: schema.TypeString, + Required: true, + }, + "calling_party_name": { + Description: "The name to use for caller identification for outbound calls from this queue.\n", + Type: schema.TypeString, + Optional: true, + }, + "enable_transcription": { + Description: "Indicates whether voice transcription is enabled for this queue.", + Type: schema.TypeBool, + Optional: true, + }, + }, + } +} + +func DataSourceSimpleRoutingQueue() *schema.Resource { + return &schema.Resource{ + Description: "Data source for Genesys Cloud Simple Routing Queues.", + ReadContext: gcloud.ReadWithPooledClient(dataSourceSimpleRoutingQueueRead), + Schema: map[string]*schema.Schema{ + "name": { + Description: "The queue name.", + Type: schema.TypeString, + Required: true, + }, + }, + } +} diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go new file mode 100644 index 000000000..9fac9554e --- /dev/null +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go @@ -0,0 +1,51 @@ +package simple_routing_queue + +import ( + "fmt" + "github.com/google/uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "strconv" + gcloud "terraform-provider-genesyscloud/genesyscloud" + "testing" +) + +func TestAccResourceSimpleRoutingQueue(t *testing.T) { + var ( + resourceId = "queue" + name = "Create 2023 Queue " + uuid.NewString() + callingPartyName = "Example Inc." + enableTranscription = "true" + + fullResourcePath = resourceName + "." + resourceId + ) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { gcloud.TestAccPreCheck(t) }, + ProviderFactories: gcloud.GetProviderFactories(providerResources, providerDataSources), + Steps: []resource.TestStep{ + { + Config: generateSimpleRoutingQueueResource( + resourceId, + name, + strconv.Quote(callingPartyName), + enableTranscription, + ), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr(fullResourcePath, "name", name), + resource.TestCheckResourceAttr(fullResourcePath, "calling_party_name", callingPartyName), + resource.TestCheckResourceAttr(fullResourcePath, "enable_transcription", "true"), + ), + }, + }, + }) +} + +func generateSimpleRoutingQueueResource(resourceId, name, callingPartyName, enableTranscription string) string { + return fmt.Sprintf(` +resource "genesyscloud_simple_routing_queue" "%s" { + name = "%s" + calling_party_name = %s + enable_transcription = %s +} +`, resourceId, name, callingPartyName, enableTranscription) +} diff --git a/main.go b/main.go index 9402dbdb1..690c7ea27 100644 --- a/main.go +++ b/main.go @@ -14,6 +14,7 @@ import ( resourceExporter "terraform-provider-genesyscloud/genesyscloud/resource_exporter" registrar "terraform-provider-genesyscloud/genesyscloud/resource_register" "terraform-provider-genesyscloud/genesyscloud/scripts" + simpleRoutingQueue "terraform-provider-genesyscloud/genesyscloud/simple_routing_queue" tfexp "terraform-provider-genesyscloud/genesyscloud/tfexporter" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" @@ -86,6 +87,7 @@ func registerResources() { obRuleset.SetRegistrar(reg_instance) scripts.SetRegistrar(reg_instance) externalContacts.SetRegistrar(reg_instance) + simpleRoutingQueue.SetRegistrar(reg_instance) resourceExporter.SetRegisterExporter(resourceExporters) // setting resources for Use cases like TF export where provider is used in resource classes. From 435da22684b1fabbb5dfcd587d800149dc92a1c8 Mon Sep 17 00:00:00 2001 From: Charlie Conneely Date: Mon, 28 Aug 2023 12:09:12 +0100 Subject: [PATCH 2/3] Create 2023 Workshop v2 --- ...ource_genesyscloud_simple_routing_queue.go | 30 ++++-- ..._genesyscloud_simple_routing_queue_test.go | 4 +- ...syscloud_simple_routing_queue_init_test.go | 2 - ...genesyscloud_simple_routing_queue_proxy.go | 20 ++-- ...ource_genesyscloud_simple_routing_queue.go | 97 ++++++++++++++++--- ...enesyscloud_simple_routing_queue_schema.go | 21 +++- ..._genesyscloud_simple_routing_queue_test.go | 6 +- main.go | 28 +++--- 8 files changed, 152 insertions(+), 56 deletions(-) diff --git a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go index 392ffeb40..94c28b335 100644 --- a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go +++ b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go @@ -2,9 +2,11 @@ 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" "time" ) @@ -16,22 +18,32 @@ import ( // 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 + // TODO 1: . Get an instance of our proxy + sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig + proxy := getSimpleRoutingQueueProxy(sdkConfig) - // Grab our queue name from the schema.ResourceData object + // TODO 2: Grab our queue name from the schema.ResourceData object + name := d.Get("name").(string) + log.Printf("Finding queue with name '%s'", name) return gcloud.WithRetries(ctx, 15*time.Second, func() *resource.RetryError { - - // Call to the proxy function getRoutingQueueIdByName(context.Context, string) + // TODO 3: Call to the proxy function getRoutingQueueIdByName(context.Context, string) // This function returns values in the following order: queueId (string), retryable (bool), err (error) + queueId, retryable, err := proxy.getRoutingQueueIdByName(ctx, name) - // If the error is not nil, and retryable equals false, return a resource.NonRetryableError - // letting the user know that an error occurred + // TODO 4: If the error is not nil, and retryable equals false, return a resource.NonRetryableError + // to let the user know that an error occurred + if err != nil && !retryable { + return resource.NonRetryableError(fmt.Errorf("error finding queue '%s': %v", name, err)) + } - // If retryable equals true, return a resource.RetryableError and let them know the queue could not be found - // with that name + // TODO 5: If retryable equals true, return a resource.RetryableError and let them know the queue could not be found with that name + if retryable { + return resource.RetryableError(fmt.Errorf("no queue found with name '%s'", name)) + } - // If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil + // TODO 6: If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil + d.SetId(queueId) return nil }) } diff --git a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go index 16b1a131c..c65c33b7d 100644 --- a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go +++ b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue_test.go @@ -20,14 +20,14 @@ func TestAccDataSourceSimpleRoutingQueue(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { gcloud.TestAccPreCheck(t) }, - ProviderFactories: gcloud.GetProviderFactories(nil, nil), + ProviderFactories: gcloud.GetProviderFactories(providerResources, providerDataSources), Steps: []resource.TestStep{ { Config: generateSimpleRoutingQueueResource( resourceId, simpleQueueName, "null", - "null", + "false", ) + generateSimpleRoutingQueueDataSource( dataSourceId, simpleQueueName, diff --git a/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go index 71507d6d6..c432dc0f7 100644 --- a/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go +++ b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_init_test.go @@ -31,7 +31,6 @@ func (r *registerTestInstance) registerTestResources() { defer r.resourceMapMutex.Unlock() providerResources[resourceName] = ResourceSimpleRoutingQueue() - } // registerTestDataSources registers all data sources used in the tests. @@ -40,7 +39,6 @@ func (r *registerTestInstance) registerTestDataSources() { defer r.datasourceMapMutex.Unlock() providerDataSources[resourceName] = DataSourceSimpleRoutingQueue() - } // initTestResources initializes all test resources and data sources. diff --git a/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go index db2b081aa..0d0c44ba0 100644 --- a/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go +++ b/genesyscloud/simple_routing_queue/genesyscloud_simple_routing_queue_proxy.go @@ -7,9 +7,9 @@ import ( ) 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 getRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.Queue, int, 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 deleteRoutingQueueFunc func(context.Context, *simpleRoutingQueueProxy, string) (*platformclientv2.APIResponse, error) type getRoutingQueueIdByNameFunc func(context.Context, *simpleRoutingQueueProxy, string) (id string, retryable bool, err error) var internalProxy *simpleRoutingQueueProxy @@ -51,7 +51,7 @@ func (p *simpleRoutingQueueProxy) createRoutingQueue(ctx context.Context, queue } // getRoutingQueue retrieves a Genesys Cloud Routing Queue by ID -func (p *simpleRoutingQueueProxy) getRoutingQueue(ctx context.Context, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { +func (p *simpleRoutingQueueProxy) getRoutingQueue(ctx context.Context, id string) (*platformclientv2.Queue, int, error) { return p.getRoutingQueueAttr(ctx, p, id) } @@ -66,8 +66,8 @@ func (p *simpleRoutingQueueProxy) updateRoutingQueue(ctx context.Context, id str } // 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) +func (p *simpleRoutingQueueProxy) deleteRoutingQueue(ctx context.Context, id string) (*platformclientv2.APIResponse, error) { + return p.deleteRoutingQueueAttr(ctx, p, id) } // createRoutingQueueFn is an implementation function for creating a Genesys Cloud Routing Queue @@ -79,12 +79,12 @@ func createRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, q return sdkQueue, response, err } -func getRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.Queue, *platformclientv2.APIResponse, error) { +func getRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.Queue, int, 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 nil, response.StatusCode, fmt.Errorf("failed to get routing queue by id '%s': %v", id, err) } - return queue, response, err + return queue, 0, err } func getRoutingQueueIdByNameFn(ctx context.Context, proxy *simpleRoutingQueueProxy, name string) (string, bool, error) { @@ -115,8 +115,8 @@ func updateRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, i 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) +func deleteRoutingQueueFn(ctx context.Context, proxy *simpleRoutingQueueProxy, id string) (*platformclientv2.APIResponse, error) { + response, err := proxy.routingApi.DeleteRoutingQueue(id, true) if err != nil { return nil, fmt.Errorf("failed to delete queue '%s': %v", id, err) } diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go index 90d1c6f57..c5c1c3657 100644 --- a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go @@ -6,9 +6,11 @@ import ( "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" + "github.com/mypurecloud/platform-client-sdk-go/v105/platformclientv2" "log" gcloud "terraform-provider-genesyscloud/genesyscloud" "terraform-provider-genesyscloud/genesyscloud/consistency_checker" + "terraform-provider-genesyscloud/genesyscloud/util/resourcedata" "time" ) @@ -32,30 +34,66 @@ utils file in the package. This will keep the code manageable and easy to work // 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) + // TODO 1: Get an instance of the proxy (example can be found in the delete method below) + sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig + proxy := getSimpleRoutingQueueProxy(sdkConfig) + + // TODO 2: Create variables for each field in our schema.ResourceData object + name := d.Get("name").(string) + callingPartyName := d.Get("calling_party_name").(string) + enableTranscription := d.Get("enable_transcription").(bool) + + log.Printf("Creating simple queue %s", name) - // Create a queue struct using the Genesys Cloud platform go sdk + // TODO 3: Create a queue struct using the Genesys Cloud platform go sdk + queueCreate := &platformclientv2.Createqueuerequest{ + Name: &name, + CallingPartyName: &callingPartyName, + EnableTranscription: &enableTranscription, + } - // Call the proxy function to create our queue + // TODO 4: Call the proxy function to create our queue. The proxy function we want to use here is createRoutingQueue(ctx context.Context, queue *platformclientv2.Createqueuerequest) + // Note: We won't need the response object returned. Also, don't forget about error handling! + queueResp, _, err := proxy.createRoutingQueue(ctx, queueCreate) + if err != nil { + return diag.Errorf("failed to create queue %s: %v", name, err) + } + + // TODO 5: Set ID in the schema.ResourceData object + d.SetId(*queueResp.Id) 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 + // TODO 1: Get an instance of the proxy + sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig + proxy := getSimpleRoutingQueueProxy(sdkConfig) + log.Printf("Reading simple queue %s", d.Id()) 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 + /* + TODO 2: Call the proxy function getRoutingQueue(ctx context.Context, id string) to find our queue, passing in the ID from the resource data object + The returned value are: Queue, Status Code (int), error + If the error is not nil, we should pass the status code to the function gcloud.IsStatus404ByInt(int) + If the status code is 404, return a resource.RetryableError. Otherwise, it should be a NonRetryableError + */ + currentQueue, respCode, err := proxy.getRoutingQueue(ctx, d.Id()) + if err != nil { + if gcloud.IsStatus404ByInt(respCode) { + return resource.RetryableError(fmt.Errorf("failed to read queue %s: %v", d.Id(), err)) + } + return resource.NonRetryableError(fmt.Errorf("failed to read queue %s: %v", d.Id(), err)) + } // 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 + // TODO 3: Set our values in the schema resource data, based on the values in the Queue object returned from the API + _ = d.Set("name", *currentQueue.Name) + resourcedata.SetNillableValue(d, "calling_party_name", currentQueue.CallingPartyName) + resourcedata.SetNillableValue(d, "enable_transcription", currentQueue.EnableTranscription) return cc.CheckState() }) @@ -63,31 +101,58 @@ func readSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta in // 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 + // TODO 1: Get an instance of the proxy + sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig + proxy := getSimpleRoutingQueueProxy(sdkConfig) - // Create a queue struct using the Genesys Cloud platform go sdk + log.Printf("Updating simple queue %s", d.Id()) - // Call the proxy function to update our queue, passing in the struct and the queue ID + // TODO 2: Create variables for each field in our schema.ResourceData object + name := d.Get("name").(string) + callingPartyName := d.Get("calling_party_name").(string) + enableTranscription := d.Get("enable_transcription").(bool) + + // TODO 3: Create a queue struct using the Genesys Cloud platform go sdk + queueUpdate := &platformclientv2.Queuerequest{ + Name: &name, + CallingPartyName: &callingPartyName, + EnableTranscription: &enableTranscription, + } + + // TODO 4: Call the proxy function updateRoutingQueue(context.Context, id string, *platformclientv2.Queuerequest) to update our queue + _, _, err := proxy.updateRoutingQueue(ctx, d.Id(), queueUpdate) + if err != nil { + return diag.Errorf("failed to update queue %s: %v", name, err) + } 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 + // TODO 1: Get an instance of the proxy (done) sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig proxy := getSimpleRoutingQueueProxy(sdkConfig) - /* Call the delete queue proxy function, passing in our queue ID from the schema.ResourceData object */ + log.Printf("Deleting simple queue %s", d.Id()) + + /* + TODO 2: Call the proxy function deleteRoutingQueue(ctx context.Context, id string) + Again, we won't be needing the returned response object + */ + if _, err := proxy.deleteRoutingQueue(ctx, d.Id()); err != nil { + return diag.Errorf("failed to delete queue %s: %v", d.Id(), err) + } // Check that queue has been deleted by trying to get it from the API + time.Sleep(5 * time.Second) 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) { + if gcloud.IsStatus404ByInt(respCode) { // Success: Routing Queue deleted log.Printf("Deleted routing queue %s", d.Id()) return nil diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go index 075c46f85..32c9f30cc 100644 --- a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go @@ -9,23 +9,35 @@ import ( const resourceName = "genesyscloud_simple_routing_queue" func SetRegistrar(l registrar.Registrar) { - l.RegisterResource(resourceName, ResourceSimpleRoutingQueue()) l.RegisterDataSource(resourceName, DataSourceSimpleRoutingQueue()) + l.RegisterResource(resourceName, ResourceSimpleRoutingQueue()) } func ResourceSimpleRoutingQueue() *schema.Resource { return &schema.Resource{ Description: "Genesys Cloud Simple Routing Queue", + // TODO: Specify our our functions that we defined in resource_genesyscloud_simple_routing_queue.go for performing CRUD operations. + // For example: + // ReadContext: gcloud.ReadWithPooledClient(readSimpleRoutingQueue) CreateContext: gcloud.CreateWithPooledClient(createSimpleRoutingQueue), ReadContext: gcloud.ReadWithPooledClient(readSimpleRoutingQueue), UpdateContext: gcloud.UpdateWithPooledClient(updateSimpleRoutingQueue), DeleteContext: gcloud.DeleteWithPooledClient(deleteSimpleRoutingQueue), + Importer: &schema.ResourceImporter{ StateContext: schema.ImportStatePassthroughContext, }, SchemaVersion: 1, Schema: map[string]*schema.Schema{ + /* + TODO: Define the following three fields: + 1. "name" | type: string | required | description: "The name of our routing queue." + 2. "calling_party_name" | type: string | optional | description: "The name to use for caller identification for outbound calls from this queue." + 3. "enable_transcription" | type: boolean | optional | description: "Indicates whether voice transcription is enabled for this queue." + Note: The field "enable_transcription" is also Computed. This lets the provider know that the API will compute and return a value, should + the user not specify one in the resource config. + */ "name": { Description: "The name for our routing queue.", Type: schema.TypeString, @@ -40,6 +52,7 @@ func ResourceSimpleRoutingQueue() *schema.Resource { Description: "Indicates whether voice transcription is enabled for this queue.", Type: schema.TypeBool, Optional: true, + Computed: true, }, }, } @@ -48,8 +61,14 @@ func ResourceSimpleRoutingQueue() *schema.Resource { func DataSourceSimpleRoutingQueue() *schema.Resource { return &schema.Resource{ Description: "Data source for Genesys Cloud Simple Routing Queues.", + // TODO: As above, specify the function dataSourceSimpleRoutingQueueRead as the ReadContext of this Resource object ReadContext: gcloud.ReadWithPooledClient(dataSourceSimpleRoutingQueueRead), + Schema: map[string]*schema.Schema{ + /* + TODO: Define the only field in our data source: + "name" | type: string | required | description: "The name of our routing queue." + */ "name": { Description: "The queue name.", Type: schema.TypeString, diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go index 9fac9554e..85e1d6177 100644 --- a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_test.go @@ -33,7 +33,7 @@ func TestAccResourceSimpleRoutingQueue(t *testing.T) { Check: resource.ComposeTestCheckFunc( resource.TestCheckResourceAttr(fullResourcePath, "name", name), resource.TestCheckResourceAttr(fullResourcePath, "calling_party_name", callingPartyName), - resource.TestCheckResourceAttr(fullResourcePath, "enable_transcription", "true"), + resource.TestCheckResourceAttr(fullResourcePath, "enable_transcription", enableTranscription), ), }, }, @@ -42,10 +42,10 @@ func TestAccResourceSimpleRoutingQueue(t *testing.T) { func generateSimpleRoutingQueueResource(resourceId, name, callingPartyName, enableTranscription string) string { return fmt.Sprintf(` -resource "genesyscloud_simple_routing_queue" "%s" { +resource "%s" "%s" { name = "%s" calling_party_name = %s enable_transcription = %s } -`, resourceId, name, callingPartyName, enableTranscription) +`, resourceName, resourceId, name, callingPartyName, enableTranscription) } diff --git a/main.go b/main.go index 690c7ea27..42d4a7c48 100644 --- a/main.go +++ b/main.go @@ -76,23 +76,25 @@ type RegisterInstance struct { func registerResources() { - reg_instance := &RegisterInstance{} - - pat.SetRegistrar(reg_instance) - - ob.SetRegistrar(reg_instance) - gcloud.SetRegistrar(reg_instance) - obAttemptLimit.SetRegistrar(reg_instance) - obContactList.SetRegistrar(reg_instance) - obRuleset.SetRegistrar(reg_instance) - scripts.SetRegistrar(reg_instance) - externalContacts.SetRegistrar(reg_instance) - simpleRoutingQueue.SetRegistrar(reg_instance) + regInstance := &RegisterInstance{} + + pat.SetRegistrar(regInstance) + + ob.SetRegistrar(regInstance) + gcloud.SetRegistrar(regInstance) + obAttemptLimit.SetRegistrar(regInstance) + obContactList.SetRegistrar(regInstance) + obRuleset.SetRegistrar(regInstance) + scripts.SetRegistrar(regInstance) + externalContacts.SetRegistrar(regInstance) resourceExporter.SetRegisterExporter(resourceExporters) + /* TODO: Import the simple_routing_queue package and call it's SetRegistrar function, as above. */ + simpleRoutingQueue.SetRegistrar(regInstance) + // setting resources for Use cases like TF export where provider is used in resource classes. //tfexp.GetRegistrarresources() - tfexp.SetRegistrar(reg_instance) + tfexp.SetRegistrar(regInstance) registrar.SetResources(providerResources, providerDataSources) } From 400f2c8a86d6bf23a51b65d6a9eb73655fadde73 Mon Sep 17 00:00:00 2001 From: Charlie Conneely Date: Wed, 20 Sep 2023 15:52:35 -0500 Subject: [PATCH 3/3] Changing comments so they're unique and searchable in the project --- ...ource_genesyscloud_simple_routing_queue.go | 12 ++++---- ...ource_genesyscloud_simple_routing_queue.go | 28 +++++++++---------- ...enesyscloud_simple_routing_queue_schema.go | 8 +++--- main.go | 2 +- 4 files changed, 25 insertions(+), 25 deletions(-) diff --git a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go index 94c28b335..7f60395f7 100644 --- a/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go +++ b/genesyscloud/simple_routing_queue/data_source_genesyscloud_simple_routing_queue.go @@ -18,31 +18,31 @@ import ( // dataSourceSimpleRoutingQueueRead retrieves by search term the id in question func dataSourceSimpleRoutingQueueRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { - // TODO 1: . Get an instance of our proxy + // CREATE-TODO 1: . Get an instance of our proxy sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig proxy := getSimpleRoutingQueueProxy(sdkConfig) - // TODO 2: Grab our queue name from the schema.ResourceData object + // CREATE-TODO 2: Grab our queue name from the schema.ResourceData object name := d.Get("name").(string) log.Printf("Finding queue with name '%s'", name) return gcloud.WithRetries(ctx, 15*time.Second, func() *resource.RetryError { - // TODO 3: Call to the proxy function getRoutingQueueIdByName(context.Context, string) + // CREATE-TODO 3: Call to the proxy function getRoutingQueueIdByName(context.Context, string) // This function returns values in the following order: queueId (string), retryable (bool), err (error) queueId, retryable, err := proxy.getRoutingQueueIdByName(ctx, name) - // TODO 4: If the error is not nil, and retryable equals false, return a resource.NonRetryableError + // CREATE-TODO 4: If the error is not nil, and retryable equals false, return a resource.NonRetryableError // to let the user know that an error occurred if err != nil && !retryable { return resource.NonRetryableError(fmt.Errorf("error finding queue '%s': %v", name, err)) } - // TODO 5: If retryable equals true, return a resource.RetryableError and let them know the queue could not be found with that name + // CREATE-TODO 5: If retryable equals true, return a resource.RetryableError and let them know the queue could not be found with that name if retryable { return resource.RetryableError(fmt.Errorf("no queue found with name '%s'", name)) } - // TODO 6: If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil + // CREATE-TODO 6: If we made it this far, we can set the queue ID in the schema.ResourceData object, and return nil d.SetId(queueId) return nil }) diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go index c5c1c3657..d6cc6d34b 100644 --- a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue.go @@ -34,32 +34,32 @@ utils file in the package. This will keep the code manageable and easy to work // 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 { - // TODO 1: Get an instance of the proxy (example can be found in the delete method below) + // CREATE-TODO 1: Get an instance of the proxy (example can be found in the delete method below) sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig proxy := getSimpleRoutingQueueProxy(sdkConfig) - // TODO 2: Create variables for each field in our schema.ResourceData object + // CREATE-TODO 2: Create variables for each field in our schema.ResourceData object name := d.Get("name").(string) callingPartyName := d.Get("calling_party_name").(string) enableTranscription := d.Get("enable_transcription").(bool) log.Printf("Creating simple queue %s", name) - // TODO 3: Create a queue struct using the Genesys Cloud platform go sdk + // CREATE-TODO 3: Create a queue struct using the Genesys Cloud platform go sdk queueCreate := &platformclientv2.Createqueuerequest{ Name: &name, CallingPartyName: &callingPartyName, EnableTranscription: &enableTranscription, } - // TODO 4: Call the proxy function to create our queue. The proxy function we want to use here is createRoutingQueue(ctx context.Context, queue *platformclientv2.Createqueuerequest) + // CREATE-TODO 4: Call the proxy function to create our queue. The proxy function we want to use here is createRoutingQueue(ctx context.Context, queue *platformclientv2.Createqueuerequest) // Note: We won't need the response object returned. Also, don't forget about error handling! queueResp, _, err := proxy.createRoutingQueue(ctx, queueCreate) if err != nil { return diag.Errorf("failed to create queue %s: %v", name, err) } - // TODO 5: Set ID in the schema.ResourceData object + // CREATE-TODO 5: Set ID in the schema.ResourceData object d.SetId(*queueResp.Id) return readSimpleRoutingQueue(ctx, d, meta) @@ -67,14 +67,14 @@ func createSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, 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 { - // TODO 1: Get an instance of the proxy + // CREATE-TODO 1: Get an instance of the proxy sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig proxy := getSimpleRoutingQueueProxy(sdkConfig) log.Printf("Reading simple queue %s", d.Id()) return gcloud.WithRetriesForRead(ctx, d, func() *resource.RetryError { /* - TODO 2: Call the proxy function getRoutingQueue(ctx context.Context, id string) to find our queue, passing in the ID from the resource data object + CREATE-TODO 2: Call the proxy function getRoutingQueue(ctx context.Context, id string) to find our queue, passing in the ID from the resource data object The returned value are: Queue, Status Code (int), error If the error is not nil, we should pass the status code to the function gcloud.IsStatus404ByInt(int) If the status code is 404, return a resource.RetryableError. Otherwise, it should be a NonRetryableError @@ -90,7 +90,7 @@ func readSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta in // Define consistency checker cc := consistency_checker.NewConsistencyCheck(ctx, d, meta, ResourceSimpleRoutingQueue()) - // TODO 3: Set our values in the schema resource data, based on the values in the Queue object returned from the API + // CREATE-TODO 3: Set our values in the schema resource data, based on the values in the Queue object returned from the API _ = d.Set("name", *currentQueue.Name) resourcedata.SetNillableValue(d, "calling_party_name", currentQueue.CallingPartyName) resourcedata.SetNillableValue(d, "enable_transcription", currentQueue.EnableTranscription) @@ -101,25 +101,25 @@ func readSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, meta in // 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 { - // TODO 1: Get an instance of the proxy + // CREATE-TODO 1: Get an instance of the proxy sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig proxy := getSimpleRoutingQueueProxy(sdkConfig) log.Printf("Updating simple queue %s", d.Id()) - // TODO 2: Create variables for each field in our schema.ResourceData object + // CREATE-TODO 2: Create variables for each field in our schema.ResourceData object name := d.Get("name").(string) callingPartyName := d.Get("calling_party_name").(string) enableTranscription := d.Get("enable_transcription").(bool) - // TODO 3: Create a queue struct using the Genesys Cloud platform go sdk + // CREATE-TODO 3: Create a queue struct using the Genesys Cloud platform go sdk queueUpdate := &platformclientv2.Queuerequest{ Name: &name, CallingPartyName: &callingPartyName, EnableTranscription: &enableTranscription, } - // TODO 4: Call the proxy function updateRoutingQueue(context.Context, id string, *platformclientv2.Queuerequest) to update our queue + // CREATE-TODO 4: Call the proxy function updateRoutingQueue(context.Context, id string, *platformclientv2.Queuerequest) to update our queue _, _, err := proxy.updateRoutingQueue(ctx, d.Id(), queueUpdate) if err != nil { return diag.Errorf("failed to update queue %s: %v", name, err) @@ -130,14 +130,14 @@ func updateSimpleRoutingQueue(ctx context.Context, d *schema.ResourceData, 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 { - // TODO 1: Get an instance of the proxy (done) + // CREATE-TODO 1: Get an instance of the proxy (done) sdkConfig := meta.(*gcloud.ProviderMeta).ClientConfig proxy := getSimpleRoutingQueueProxy(sdkConfig) log.Printf("Deleting simple queue %s", d.Id()) /* - TODO 2: Call the proxy function deleteRoutingQueue(ctx context.Context, id string) + CREATE-TODO 2: Call the proxy function deleteRoutingQueue(ctx context.Context, id string) Again, we won't be needing the returned response object */ if _, err := proxy.deleteRoutingQueue(ctx, d.Id()); err != nil { diff --git a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go index 32c9f30cc..31f5b894a 100644 --- a/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go +++ b/genesyscloud/simple_routing_queue/resource_genesyscloud_simple_routing_queue_schema.go @@ -17,7 +17,7 @@ func ResourceSimpleRoutingQueue() *schema.Resource { return &schema.Resource{ Description: "Genesys Cloud Simple Routing Queue", - // TODO: Specify our our functions that we defined in resource_genesyscloud_simple_routing_queue.go for performing CRUD operations. + // CREATE-TODO: Specify our our functions that we defined in resource_genesyscloud_simple_routing_queue.go for performing CRUD operations. // For example: // ReadContext: gcloud.ReadWithPooledClient(readSimpleRoutingQueue) CreateContext: gcloud.CreateWithPooledClient(createSimpleRoutingQueue), @@ -31,7 +31,7 @@ func ResourceSimpleRoutingQueue() *schema.Resource { SchemaVersion: 1, Schema: map[string]*schema.Schema{ /* - TODO: Define the following three fields: + CREATE-TODO: Define the following three fields: 1. "name" | type: string | required | description: "The name of our routing queue." 2. "calling_party_name" | type: string | optional | description: "The name to use for caller identification for outbound calls from this queue." 3. "enable_transcription" | type: boolean | optional | description: "Indicates whether voice transcription is enabled for this queue." @@ -61,12 +61,12 @@ func ResourceSimpleRoutingQueue() *schema.Resource { func DataSourceSimpleRoutingQueue() *schema.Resource { return &schema.Resource{ Description: "Data source for Genesys Cloud Simple Routing Queues.", - // TODO: As above, specify the function dataSourceSimpleRoutingQueueRead as the ReadContext of this Resource object + // CREATE-TODO: As above, specify the function dataSourceSimpleRoutingQueueRead as the ReadContext of this Resource object ReadContext: gcloud.ReadWithPooledClient(dataSourceSimpleRoutingQueueRead), Schema: map[string]*schema.Schema{ /* - TODO: Define the only field in our data source: + CREATE-TODO: Define the only field in our data source: "name" | type: string | required | description: "The name of our routing queue." */ "name": { diff --git a/main.go b/main.go index 42d4a7c48..153506955 100644 --- a/main.go +++ b/main.go @@ -89,7 +89,7 @@ func registerResources() { externalContacts.SetRegistrar(regInstance) resourceExporter.SetRegisterExporter(resourceExporters) - /* TODO: Import the simple_routing_queue package and call it's SetRegistrar function, as above. */ + /* CREATE-TODO: Import the simple_routing_queue package and call it's SetRegistrar function, as above. */ simpleRoutingQueue.SetRegistrar(regInstance) // setting resources for Use cases like TF export where provider is used in resource classes.