Skip to content

Commit

Permalink
feat/Devtooling 712 resource for supported content (#1159)
Browse files Browse the repository at this point in the history
* resource and test cases added for supported content

* updated resource testcase and added datasource testcase

* updated docs

* added example folder

* added example folder

* updated docs

* updated testcase for datasource

* resolved comments

* updated docs

* changed resource name

* changed resource file names

* changed resource file names

* comments resolved

* updatd docs

* cache added for data source

* resolved merge conflict

* comments resolved

* updated docs

* comments resolved
  • Loading branch information
shrutisuryawanshigenesys authored Jul 29, 2024
1 parent 925ae74 commit c4182e3
Show file tree
Hide file tree
Showing 15 changed files with 1,060 additions and 4 deletions.
30 changes: 30 additions & 0 deletions docs/data-sources/conversations_messaging_supportedcontent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "genesyscloud_conversations_messaging_supportedcontent Data Source - terraform-provider-genesyscloud"
subcategory: ""
description: |-
Genesys Cloud supported content data source. Select an supported content by name
---

# genesyscloud_conversations_messaging_supportedcontent (Data Source)

Genesys Cloud supported content data source. Select an supported content by name

## Example Usage

```terraform
data "genesyscloud_conversations_messaging_supportedcontent" "supported_content" {
name = "Test Supported Content"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) supported content name

### Read-Only

- `id` (String) The ID of this resource.
84 changes: 84 additions & 0 deletions docs/resources/conversations_messaging_supportedcontent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
---
page_title: "genesyscloud_conversations_messaging_supportedcontent Resource - terraform-provider-genesyscloud"
subcategory: ""
description: |-
Genesys Cloud supported content
---
# genesyscloud_conversations_messaging_supportedcontent (Resource)

Genesys Cloud supported content

## API Usage
The following Genesys Cloud APIs are used by this resource. Ensure your OAuth Client has been granted the necessary scopes and permissions to perform these operations:

* [POST /api/v2/conversations/messaging/supportedcontent](https://developer.genesys.cloud/devapps/api-explorer#post-api-v2-conversations-messaging-supportedcontent)
* [GET /api/v2/conversations/messaging/supportedcontent/{supportedContentId}](https://developer.genesys.cloud/devapps/api-explorer#get-api-v2-conversations-messaging-supportedcontent--supportedContentId-)
* [PATCH /api/v2/conversations/messaging/supportedcontent/{supportedContentId}](https://developer.genesys.cloud/devapps/api-explorer#patch-api-v2-conversations-messaging-supportedcontent--supportedContentId-)
* [DELETE /api/v2/conversations/messaging/supportedcontent/{supportedContentId}](https://developer.genesys.cloud/devapps/api-explorer#delete-api-v2-conversations-messaging-supportedcontent--supportedContentId-)
* [GET /api/v2/conversations/messaging/supportedcontent](https://developer.genesys.cloud/devapps/api-explorer#get-api-v2-conversations-messaging-supportedcontent)



## Example Usage

```terraform
resource "genesyscloud_conversations_messaging_supportedcontent" "supported_content" {
name = "test supported_content"
media_types {
allow {
inbound {
type = "image/*"
}
outbound {
type = "video/mpeg"
}
}
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) The name of the supported content profile

### Optional

- `media_types` (Block List, Max: 1) Defines the allowable media that may be accepted for an inbound message or to be sent in an outbound message. (see [below for nested schema](#nestedblock--media_types))

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedblock--media_types"></a>
### Nested Schema for `media_types`

Optional:

- `allow` (Block List, Max: 1) Specify allowed media types for inbound and outbound messages. If this field is empty, all inbound and outbound media will be blocked. (see [below for nested schema](#nestedblock--media_types--allow))

<a id="nestedblock--media_types--allow"></a>
### Nested Schema for `media_types.allow`

Optional:

- `inbound` (Block List) List of media types allowed for inbound messages from customers. If inbound messages from a customer contain media that is not in this list, the media will be dropped from the outbound message. (see [below for nested schema](#nestedblock--media_types--allow--inbound))
- `outbound` (Block List) List of media types allowed for outbound messages to customers. If an outbound message is sent that contains media that is not in this list, the message will not be sent. (see [below for nested schema](#nestedblock--media_types--allow--outbound))

<a id="nestedblock--media_types--allow--inbound"></a>
### Nested Schema for `media_types.allow.inbound`

Optional:

- `type` (String) The media type string as defined by RFC 2046. You can define specific types such as 'image/jpeg', 'video/mpeg', or specify wild cards for a range of types, 'image/*', or all types '*/*'. See https://www.iana.org/assignments/media-types/media-types.xhtml for a list of registered media types.


<a id="nestedblock--media_types--allow--outbound"></a>
### Nested Schema for `media_types.allow.outbound`

Optional:

- `type` (String) The media type string as defined by RFC 2046. You can define specific types such as 'image/jpeg', 'video/mpeg', or specify wild cards for a range of types, 'image/*', or all types '*/*'. See https://www.iana.org/assignments/media-types/media-types.xhtml for a list of registered media types.

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "genesyscloud_conversations_messaging_supportedcontent" "supported_content" {
name = "Test Supported Content"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
* [POST /api/v2/conversations/messaging/supportedcontent](https://developer.genesys.cloud/devapps/api-explorer#post-api-v2-conversations-messaging-supportedcontent)
* [GET /api/v2/conversations/messaging/supportedcontent/{supportedContentId}](https://developer.genesys.cloud/devapps/api-explorer#get-api-v2-conversations-messaging-supportedcontent--supportedContentId-)
* [PATCH /api/v2/conversations/messaging/supportedcontent/{supportedContentId}](https://developer.genesys.cloud/devapps/api-explorer#patch-api-v2-conversations-messaging-supportedcontent--supportedContentId-)
* [DELETE /api/v2/conversations/messaging/supportedcontent/{supportedContentId}](https://developer.genesys.cloud/devapps/api-explorer#delete-api-v2-conversations-messaging-supportedcontent--supportedContentId-)
* [GET /api/v2/conversations/messaging/supportedcontent](https://developer.genesys.cloud/devapps/api-explorer#get-api-v2-conversations-messaging-supportedcontent)

Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "genesyscloud_conversations_messaging_supportedcontent" "supported_content" {
name = "test supported_content"
media_types {
allow {
inbound {
type = "image/*"
}
outbound {
type = "video/mpeg"
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
package conversations_messaging_supportedcontent

import (
"context"
"fmt"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/mypurecloud/platform-client-sdk-go/v133/platformclientv2"

"terraform-provider-genesyscloud/genesyscloud/provider"
rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"
"terraform-provider-genesyscloud/genesyscloud/util"
)

/*
The data_source_genesyscloud_conversations_messaging_supportedcontent.go contains the data source implementation
for the resource.
*/

// dataSourceSupportedContentRead retrieves by name the id in question
func dataSourceSupportedContentRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig

key := ""

key = d.Get("name").(string)

if dataSourceSupportedContentCache == nil {
dataSourceSupportedContentCache = rc.NewDataSourceCache(sdkConfig, hydrateSupportedContentCacheFn, getSupportedContentIdByName)
}

contentId, err := rc.RetrieveId(dataSourceSupportedContentCache, resourceName, key, ctx)
if err != nil {
return err
}

d.SetId(contentId)
return nil
}

var (
dataSourceSupportedContentCache *rc.DataSourceCache
)

func hydrateSupportedContentCacheFn(c *rc.DataSourceCache) error {
log.Printf("hydrating cache for data source " + resourceName)
supportContentApi := platformclientv2.NewConversationsApiWithConfig(c.ClientConfig)

const pageSize = 100

supportedContents, _, err := supportContentApi.GetConversationsMessagingSupportedcontent(pageSize, 1)
if err != nil {
return fmt.Errorf("Failed to get supported content: %v", err)
}
if supportedContents.Entities == nil || len(*supportedContents.Entities) == 0 {
return nil
}

for _, supportedContent := range *supportedContents.Entities {
c.Cache[*supportedContent.Name] = *supportedContent.Id
}

for pageNum := 2; pageNum <= *supportedContents.PageCount; pageNum++ {
supportedContents, _, err := supportContentApi.GetConversationsMessagingSupportedcontent(pageSize, pageNum)

log.Printf("hydrating cache for data source genesyscloud_conversations_messaging_supportedcontent with page number: %v", pageNum)
if err != nil {
return fmt.Errorf("Failed to get supported content: %v", err)
}

if supportedContents.Entities == nil || len(*supportedContents.Entities) == 0 {
break
}

// Add ids to cache
for _, supportedContent := range *supportedContents.Entities {
c.Cache[*supportedContent.Name] = *supportedContent.Id
}
}
log.Printf("cache hydration completed for data source " + resourceName)

return nil
}

func getSupportedContentIdByName(c *rc.DataSourceCache, searchName string, ctx context.Context) (string, diag.Diagnostics) {
proxy := getSupportedContentProxy(c.ClientConfig)
contentId := ""
diag := util.WithRetries(ctx, 15*time.Second, func() *retry.RetryError {
supportedContentId, retryable, resp, err := proxy.getSupportedContentIdByName(ctx, searchName)

if err != nil && !retryable {
retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Error searching supported content %s: %s", searchName, err), resp))
}

if retryable {
retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("No supported content found with name %s", searchName), resp))
}

contentId = supportedContentId
return nil
})

return contentId, diag
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package conversations_messaging_supportedcontent

import (
"fmt"
"testing"

"terraform-provider-genesyscloud/genesyscloud/provider"
"terraform-provider-genesyscloud/genesyscloud/util"

"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

/*
Test Class for the supported content Data Source
*/

func TestAccDataSourceSupportedContent(t *testing.T) {
t.Parallel()
var (
resourceId = "testSupportedContent"
dataSourceId = "testSupportedContent_data"
name = "Terraform Supported Content - " + uuid.NewString()
inboundType = "*/*"
outboundType = "image/*"
)

resource.Test(t, resource.TestCase{
PreCheck: func() { util.TestAccPreCheck(t) },
ProviderFactories: provider.GetProviderFactories(providerResources, providerDataSources),
Steps: []resource.TestStep{
{
Config: GenerateSupportedContentResource(
resourceName,
resourceId,
name,
GenerateInboundTypeBlock(inboundType),
GenerateOutboundTypeBlock(outboundType),
) +
GenerateDataSourceForSupportedContent(
resourceName,
dataSourceId,
name,
"genesyscloud_conversations_messaging_supportedcontent."+resourceId),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair("data.genesyscloud_conversations_messaging_supportedcontent."+dataSourceId, "id", "genesyscloud_conversations_messaging_supportedcontent."+resourceId, "id"),
),
},
},
})
}

func GenerateDataSourceForSupportedContent(
resourceName string,
resourceId string,
name string,
dependsOn string,
) string {
return fmt.Sprintf(`
data "%s" "%s" {
name = "%s"
depends_on = [%s]
}
`, resourceName, resourceId, name, dependsOn)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package conversations_messaging_supportedcontent

import (
"sync"
"testing"

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

/*
The genesyscloud_conversations_messaging_supportedcontent_init_test.go file is used to initialize the data sources and resources
used in testing the supported_content resource.
*/

// 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] = ResourceSupportedContent()
}

// registerTestDataSources registers all data sources used in the tests.
func (r *registerTestInstance) registerTestDataSources() {
r.datasourceMapMutex.Lock()
defer r.datasourceMapMutex.Unlock()

providerDataSources[resourceName] = DataSourceSupportedContent()
}

// initTestResources initializes all test resources and data sources.
func initTestResources() {
providerDataSources = make(map[string]*schema.Resource)
providerResources = make(map[string]*schema.Resource)

regInstance := &registerTestInstance{}

regInstance.registerTestResources()
regInstance.registerTestDataSources()
}

// 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 the supported_content package
initTestResources()

// Run the test suite for the supported_content package
m.Run()
}
Loading

0 comments on commit c4182e3

Please sign in to comment.