Skip to content

Commit

Permalink
resolved comments
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutisuryawanshigenesys committed Jul 18, 2024
1 parent d53636c commit ea805b9
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
data "genesyscloud_supported_content" "Test Supported Content" {
name = "Supported_Content_1"
data "genesyscloud_supported_content" "supported_content" {
name = "Test Supported Content"
}
10 changes: 10 additions & 0 deletions examples/resources/genesyscloud_supported_content/resource.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
resource "genesyscloud_supported_content" "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
Expand Up @@ -22,7 +22,7 @@ import (
// 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
proxy := newSupportedContentProxy(sdkConfig)
proxy := getSupportedContentProxy(sdkConfig)

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

Expand All @@ -34,7 +34,7 @@ func dataSourceSupportedContentRead(ctx context.Context, d *schema.ResourceData,
}

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

d.SetId(supportedContentId)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func GenerateDataSourceForSupportedContent(
dependsOn string,
) string {
return fmt.Sprintf(`
data "genesyscloud_supported_content" "%s" {
data "`+resourceName+`" "%s" {
name = "%s"
depends_on = [%s]
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ func (r *registerTestInstance) registerTestResources() {
defer r.resourceMapMutex.Unlock()

providerResources[resourceName] = ResourceSupportedContent()
// TODO: Add references
}

// registerTestDataSources registers all data sources used in the tests.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"log"
rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"

"github.com/mypurecloud/platform-client-sdk-go/v133/platformclientv2"
)
Expand Down Expand Up @@ -35,11 +36,13 @@ type supportedContentProxy struct {
getSupportedContentByIdAttr getSupportedContentByIdFunc
updateSupportedContentAttr updateSupportedContentFunc
deleteSupportedContentAttr deleteSupportedContentFunc
supportedContentCache rc.CacheInterface[platformclientv2.Supportedcontent]
}

// newSupportedContentProxy initializes the supported content proxy with all of the data needed to communicate with Genesys Cloud
func newSupportedContentProxy(clientConfig *platformclientv2.Configuration) *supportedContentProxy {
api := platformclientv2.NewConversationsApiWithConfig(clientConfig)
supportedContentCache := rc.NewResourceCache[platformclientv2.Supportedcontent]()
return &supportedContentProxy{
clientConfig: clientConfig,
conversationsApi: api,
Expand All @@ -49,6 +52,7 @@ func newSupportedContentProxy(clientConfig *platformclientv2.Configuration) *sup
getSupportedContentByIdAttr: getSupportedContentByIdFn,
updateSupportedContentAttr: updateSupportedContentFn,
deleteSupportedContentAttr: deleteSupportedContentFn,
supportedContentCache: supportedContentCache,
}
}

Expand Down Expand Up @@ -109,9 +113,8 @@ func getAllSupportedContentFn(ctx context.Context, p *supportedContentProxy) (*[
if supportedContents.Entities == nil || len(*supportedContents.Entities) == 0 {
return &allSupportedContents, resp, nil
}
for _, supportedContent := range *supportedContents.Entities {
allSupportedContents = append(allSupportedContents, supportedContent)
}

allSupportedContents = append(allSupportedContents, *supportedContents.Entities...)

for pageNum := 2; pageNum <= *supportedContents.PageCount; pageNum++ {
supportedContents, _, err := p.conversationsApi.GetConversationsMessagingSupportedcontent(pageSize, pageNum)
Expand All @@ -123,9 +126,11 @@ func getAllSupportedContentFn(ctx context.Context, p *supportedContentProxy) (*[
break
}

for _, supportedContent := range *supportedContents.Entities {
allSupportedContents = append(allSupportedContents, supportedContent)
}
allSupportedContents = append(allSupportedContents, *supportedContents.Entities...)
}

for _, content := range allSupportedContents {
rc.SetCache(p.supportedContentCache, *content.Id, content)
}

return &allSupportedContents, resp, nil
Expand Down Expand Up @@ -154,6 +159,10 @@ func getSupportedContentIdByNameFn(ctx context.Context, p *supportedContentProxy

// getSupportedContentByIdFn is an implementation of the function to get a Genesys Cloud supported content by Id
func getSupportedContentByIdFn(ctx context.Context, p *supportedContentProxy, id string) (supportedContent *platformclientv2.Supportedcontent, response *platformclientv2.APIResponse, err error) {
content := rc.GetCacheItem(p.supportedContentCache, id)
if content != nil {
return content, nil, nil
}
return p.conversationsApi.GetConversationsMessagingSupportedcontentSupportedContentId(id)
}

Expand All @@ -164,10 +173,5 @@ func updateSupportedContentFn(ctx context.Context, p *supportedContentProxy, id

// deleteSupportedContentFn is an implementation function for deleting a Genesys Cloud supported content
func deleteSupportedContentFn(ctx context.Context, p *supportedContentProxy, id string) (response *platformclientv2.APIResponse, err error) {
resp, err := p.conversationsApi.DeleteConversationsMessagingSupportedcontentSupportedContentId(id)
if err != nil {
return resp, err
}

return resp, nil
return p.conversationsApi.DeleteConversationsMessagingSupportedcontentSupportedContentId(id)
}
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func readSupportedContent(ctx context.Context, d *schema.ResourceData, meta inte
}

resourcedata.SetNillableValue(d, "name", supportedContent.Name)
resourcedata.SetNillableValueWithInterfaceArrayWithFunc(d, "media_types", supportedContent.MediaTypes, flattenMediaTypess)
resourcedata.SetNillableValueWithInterfaceArrayWithFunc(d, "media_types", supportedContent.MediaTypes, flattenMediaTypes)

log.Printf("Read supported content %s %s", d.Id(), *supportedContent.Name)
return cc.CheckState(d)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,18 +84,11 @@ func ResourceSupportedContent() *schema.Resource {
Type: schema.TypeString,
},
`media_types`: {
Description: `Defines the allowable media that may be accepted for an inbound message or to be sent in an outbound message. The following is an example of allowing all inbound media, and for outbound all images and only mpeg video: {
"mediaTypes": {
"allow": {
"inbound": [{"type": "*/*"}],
"outbound": [{"type": "image/*"}, {"type": "video/mpeg"}]
}
}
}`,
Optional: true,
Type: schema.TypeList,
MaxItems: 1,
Elem: mediaTypesResource,
Description: `Defines the allowable media that may be accepted for an inbound message or to be sent in an outbound message.`,
Optional: true,
Type: schema.TypeList,
MaxItems: 1,
Elem: mediaTypesResource,
},
},
}
Expand All @@ -105,9 +98,7 @@ func ResourceSupportedContent() *schema.Resource {
func SupportedContentExporter() *resourceExporter.ResourceExporter {
return &resourceExporter.ResourceExporter{
GetResourcesFunc: provider.GetAllWithPooledClient(getAllAuthSupportedContents),
RefAttrs: map[string]*resourceExporter.RefAttrSettings{
// TODO: Add any reference attributes here
},
RefAttrs: map[string]*resourceExporter.RefAttrSettings{},
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func GenerateSupportedContentResource(
name string,
nestedBlocks ...string,
) string {
return fmt.Sprintf(`resource "genesyscloud_supported_content" "%s" {
return fmt.Sprintf(`resource "`+resourceName+`" "%s" {
name = "%s"
media_types {
allow {
Expand Down Expand Up @@ -113,7 +113,7 @@ func GenerateOutboundTypeBlock(
func testVerifySupportedContentDestroyed(state *terraform.State) error {
supportContentApi := platformclientv2.NewConversationsApi()
for _, rs := range state.RootModule().Resources {
if rs.Type != "genesyscloud_supported_content" {
if rs.Type != resourceName {
continue
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,12 @@ and unmarshal data into formats consumable by Terraform and/or Genesys Cloud.

// getSupportedContentFromResourceData maps data from schema ResourceData object to a platformclientv2.Supportedcontent
func getSupportedContentFromResourceData(d *schema.ResourceData) platformclientv2.Supportedcontent {
return platformclientv2.Supportedcontent{
Name: platformclientv2.String(d.Get("name").(string)),
MediaTypes: buildMediaTypes(d.Get("media_types").([]interface{})),
var supportedContent platformclientv2.Supportedcontent
supportedContent.Name = platformclientv2.String(d.Get("name").(string))
if mediaTypes, ok := d.Get("media_types").([]interface{}); ok && len(mediaTypes) > 0 {
supportedContent.MediaTypes = buildMediaTypes(d.Get("media_types").([]interface{}))
}
return supportedContent
}

// buildMediaTypes maps an []interface{} into a Genesys Cloud *[]platformclientv2.Mediatype
Expand All @@ -30,48 +32,48 @@ func buildMediaTypes(mediaTypes []interface{}) *platformclientv2.Mediatypes {
continue
}

resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaType.Allow, mediaTypesMap, "allow", buildMediaTypeAccesss)
resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaType.Allow, mediaTypesMap, "allow", buildAllowedMediaTypeAccess)
}
return &sdkMediaType
}

// buildMediaTypeAccesss maps an []interface{} into a Genesys Cloud *[]platformclientv2.Mediatypeaccess
func buildMediaTypeAccesss(mediaTypeAccesss []interface{}) *platformclientv2.Mediatypeaccess {
func buildAllowedMediaTypeAccess(mediaTypeAccess []interface{}) *platformclientv2.Mediatypeaccess {
var sdkMediaTypeAccess platformclientv2.Mediatypeaccess
for _, mediaTypeAccess := range mediaTypeAccesss {
mediaTypeAccesssMap, ok := mediaTypeAccess.(map[string]interface{})
for _, mediaType := range mediaTypeAccess {
mediaTypeAccessMap, ok := mediaType.(map[string]interface{})
if !ok {
continue
}

resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaTypeAccess.Inbound, mediaTypeAccesssMap, "inbound", buildMediaTypess)
resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaTypeAccess.Outbound, mediaTypeAccesssMap, "outbound", buildMediaTypess)
resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaTypeAccess.Inbound, mediaTypeAccessMap, "inbound", buildInboundOutboundMediaTypes)
resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaTypeAccess.Outbound, mediaTypeAccessMap, "outbound", buildInboundOutboundMediaTypes)

}

return &sdkMediaTypeAccess
}

// buildMediaTypess maps an []interface{} into a Genesys Cloud *[]platformclientv2.Mediatypes
func buildMediaTypess(mediaTypess []interface{}) *[]platformclientv2.Mediatype {
mediaTypessSlice := make([]platformclientv2.Mediatype, 0)
for _, mediaTypes := range mediaTypess {
func buildInboundOutboundMediaTypes(mediaTypes []interface{}) *[]platformclientv2.Mediatype {
mediaTypesSlice := make([]platformclientv2.Mediatype, 0)
for _, mediaType := range mediaTypes {
var sdkMediaTypes platformclientv2.Mediatype
mediaTypessMap, ok := mediaTypes.(map[string]interface{})
mediaTypesMap, ok := mediaType.(map[string]interface{})
if !ok {
continue
}

resourcedata.BuildSDKStringValueIfNotNil(&sdkMediaTypes.VarType, mediaTypessMap, "type")
resourcedata.BuildSDKStringValueIfNotNil(&sdkMediaTypes.VarType, mediaTypesMap, "type")

mediaTypessSlice = append(mediaTypessSlice, sdkMediaTypes)
mediaTypesSlice = append(mediaTypesSlice, sdkMediaTypes)
}

return &mediaTypessSlice
return &mediaTypesSlice
}

// flattenMediaTypes maps a Genesys Cloud *[]platformclientv2.Mediatype into a []interface{}
func flattenMediaTypes(mediaTypes *[]platformclientv2.Mediatype) []interface{} {
func flattenInboundOutboundMediaTypes(mediaTypes *[]platformclientv2.Mediatype) []interface{} {
if len(*mediaTypes) == 0 {
return nil
}
Expand All @@ -89,25 +91,25 @@ func flattenMediaTypes(mediaTypes *[]platformclientv2.Mediatype) []interface{} {
}

// flattenMediaTypeAccesss maps a Genesys Cloud *[]platformclientv2.Mediatypeaccess into a []interface{}
func flattenMediaTypeAccesss(mediaTypeAccesss *platformclientv2.Mediatypeaccess) []interface{} {
func flattenAllowedMediaTypeAccess(mediaTypeAccess *platformclientv2.Mediatypeaccess) []interface{} {

var mediaTypeAccessList []interface{}
mediaTypeAccessMap := make(map[string]interface{})

resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypeAccessMap, "inbound", mediaTypeAccesss.Inbound, flattenMediaTypes)
resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypeAccessMap, "outbound", mediaTypeAccesss.Outbound, flattenMediaTypes)
resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypeAccessMap, "inbound", mediaTypeAccess.Inbound, flattenInboundOutboundMediaTypes)
resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypeAccessMap, "outbound", mediaTypeAccess.Outbound, flattenInboundOutboundMediaTypes)

mediaTypeAccessList = append(mediaTypeAccessList, mediaTypeAccessMap)

return mediaTypeAccessList
}

// flattenMediaTypess maps a Genesys Cloud *[]platformclientv2.Mediatypes into a []interface{}
func flattenMediaTypess(mediaTypess *platformclientv2.Mediatypes) []interface{} {
func flattenMediaTypes(mediaTypes *platformclientv2.Mediatypes) []interface{} {
var mediaTypesList []interface{}
mediaTypesMap := make(map[string]interface{})

resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypesMap, "allow", mediaTypess.Allow, flattenMediaTypeAccesss)
resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypesMap, "allow", mediaTypes.Allow, flattenAllowedMediaTypeAccess)

mediaTypesList = append(mediaTypesList, mediaTypesMap)

Expand Down

0 comments on commit ea805b9

Please sign in to comment.