Skip to content

Commit

Permalink
comments resolved
Browse files Browse the repository at this point in the history
  • Loading branch information
shrutisuryawanshigenesys committed Jul 26, 2024
1 parent d0e6c75 commit 3a81bb6
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 39 deletions.
2 changes: 1 addition & 1 deletion docs/resources/conversations_messaging_supportedcontent.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ resource "genesyscloud_conversations_messaging_supportedcontent" "supported_cont

### 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))
- `media_types` (Block Set, 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,9 @@ func readSupportedContent(ctx context.Context, d *schema.ResourceData, meta inte
}

resourcedata.SetNillableValue(d, "name", supportedContent.Name)
resourcedata.SetNillableValueWithInterfaceArrayWithFunc(d, "media_types", supportedContent.MediaTypes, flattenMediaTypes)
if supportedContent.MediaTypes != nil {
d.Set("media_types", flattenMediaTypes(supportedContent.MediaTypes))
}

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 @@ -25,9 +25,8 @@ func SetRegistrar(regInstance registrar.Registrar) {
regInstance.RegisterExporter(resourceName, SupportedContentExporter())
}

// ResourceSupportedContent registers the genesyscloud_conversations_messaging_supportedcontent resource with Terraform
func ResourceSupportedContent() *schema.Resource {
mediaTypeResource := &schema.Resource{
var (
mediaTypeResource = &schema.Resource{
Schema: map[string]*schema.Schema{
`type`: {
Description: `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.`,
Expand All @@ -37,7 +36,7 @@ func ResourceSupportedContent() *schema.Resource {
},
}

mediaTypeAccessResource := &schema.Resource{
mediaTypeAccessResource = &schema.Resource{
Schema: map[string]*schema.Schema{
`inbound`: {
Description: `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.`,
Expand All @@ -54,17 +53,21 @@ func ResourceSupportedContent() *schema.Resource {
},
}

mediaTypesResource := &schema.Resource{
mediaTypesResource = &schema.Resource{
Schema: map[string]*schema.Schema{
`allow`: {
Description: `Specify allowed media types for inbound and outbound messages. If this field is empty, all inbound and outbound media will be blocked.`,
Optional: true,
Type: schema.TypeList,
Type: schema.TypeSet,
MaxItems: 1,
Elem: mediaTypeAccessResource,
},
},
}
)

// ResourceSupportedContent registers the genesyscloud_conversations_messaging_supportedcontent resource with Terraform
func ResourceSupportedContent() *schema.Resource {

return &schema.Resource{
Description: `Genesys Cloud supported content`,
Expand All @@ -86,7 +89,7 @@ func ResourceSupportedContent() *schema.Resource {
`media_types`: {
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,
Type: schema.TypeSet,
MaxItems: 1,
Elem: mediaTypesResource,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,6 @@ func TestAccResourceSupportedContent(t *testing.T) {
resource.TestCheckResourceAttr("genesyscloud_conversations_messaging_supportedcontent."+resourceId, "media_types.0.allow.0.outbound.0.type", outboundType),
),
},
{
// Import/Read
ResourceName: "genesyscloud_conversations_messaging_supportedcontent." + resourceId,
ImportState: true,
ImportStateVerify: true,
},
},
CheckDestroy: testVerifySupportedContentDestroyed,
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,58 @@ func getSupportedContentFromResourceData(d *schema.ResourceData) platformclientv
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{}))
supportedContent.MediaTypes = buildMediaTypes(d.Get("media_types").(*schema.Set))
}
return supportedContent
}

// buildMediaTypes maps an []interface{} into a Genesys Cloud *[]platformclientv2.Mediatype
func buildMediaTypes(mediaTypes []interface{}) *platformclientv2.Mediatypes {
func buildMediaTypes(mediaTypes *schema.Set) *platformclientv2.Mediatypes {
var sdkMediaType platformclientv2.Mediatypes
if mediaTypes == nil {
return nil
}

for _, mediaType := range mediaTypes {
mediaTypesMap, ok := mediaType.(map[string]interface{})
if !ok {
continue
}
mediaTypeList := mediaTypes.List()
if len(mediaTypeList) > 0 {
mediaTypesMap := mediaTypeList[0].(map[string]interface{})

resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaType.Allow, mediaTypesMap, "allow", buildAllowedMediaTypeAccess)
if mediaAllow := mediaTypesMap["allow"]; mediaAllow != nil {
sdkMediaType.Allow = buildAllowedMediaTypeAccess(mediaAllow.(*schema.Set))
}
}
return &sdkMediaType
}

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

resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaTypeAccess.Inbound, mediaTypeAccessMap, "inbound", buildInboundOutboundMediaTypes)
resourcedata.BuildSDKInterfaceArrayValueIfNotNil(&sdkMediaTypeAccess.Outbound, mediaTypeAccessMap, "outbound", buildInboundOutboundMediaTypes)
mediaTypeAccessList := mediaTypeAccess.List()

}
if len(mediaTypeAccessList) > 0 {
mediaTypeAccessMap := mediaTypeAccessList[0].(map[string]interface{})

if mediaInbound := mediaTypeAccessMap["inbound"].([]interface{}); len(mediaInbound) > 0 {
sdkMediaTypeAccess.Inbound = buildInboundOutboundMediaTypes(mediaInbound)
}

if mediaOutbound := mediaTypeAccessMap["outbound"].([]interface{}); len(mediaOutbound) > 0 {
sdkMediaTypeAccess.Outbound = buildInboundOutboundMediaTypes(mediaOutbound)
}
}
return &sdkMediaTypeAccess
}

// buildMediaTypess maps an []interface{} into a Genesys Cloud *[]platformclientv2.Mediatypes
func buildInboundOutboundMediaTypes(mediaTypes []interface{}) *[]platformclientv2.Mediatype {
if mediaTypes == nil {
return nil
}

mediaTypesSlice := make([]platformclientv2.Mediatype, 0)
for _, mediaType := range mediaTypes {
var sdkMediaTypes platformclientv2.Mediatype
Expand All @@ -64,7 +77,9 @@ func buildInboundOutboundMediaTypes(mediaTypes []interface{}) *[]platformclientv
continue
}

resourcedata.BuildSDKStringValueIfNotNil(&sdkMediaTypes.VarType, mediaTypesMap, "type")
if fieldType := mediaTypesMap["type"].(string); fieldType != "" {
sdkMediaTypes.VarType = &fieldType
}

mediaTypesSlice = append(mediaTypesSlice, sdkMediaTypes)
}
Expand Down Expand Up @@ -105,13 +120,18 @@ func flattenAllowedMediaTypeAccess(mediaTypeAccess *platformclientv2.Mediatypeac
}

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

resourcedata.SetMapInterfaceArrayWithFuncIfNotNil(mediaTypesMap, "allow", mediaTypes.Allow, flattenAllowedMediaTypeAccess)
mediaAllowedSet := schema.NewSet(schema.HashResource(mediaTypesResource), []interface{}{})
mediaTypesMap := make(map[string]interface{})

mediaTypesList = append(mediaTypesList, mediaTypesMap)
if mediaTypes.Allow != nil {
mediaTypesMap["allow"] = *mediaTypes.Allow
}

return mediaTypesList
mediaAllowedSet.Add(mediaTypesMap)
return mediaAllowedSet
}

0 comments on commit 3a81bb6

Please sign in to comment.