Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No jira: Implementing cache for auth divisions data source & removing Deprecation warnings #1274

Merged
merged 3 commits into from
Oct 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/resources/routing_queue.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ resource "genesyscloud_routing_queue" "example_queue" {
- `bullseye_rings` (Block List, Max: 5) The bullseye ring settings for the queue. (see [below for nested schema](#nestedblock--bullseye_rings))
- `calling_party_name` (String) The name to use for caller identification for outbound calls from this queue.
- `calling_party_number` (String) The phone number to use for caller identification for outbound calls from this queue.
- `conditional_group_routing_rules` (Block List, Max: 5, Deprecated) The Conditional Group Routing settings for the queue. (see [below for nested schema](#nestedblock--conditional_group_routing_rules))
- `conditional_group_routing_rules` (Block List, Max: 5) The Conditional Group Routing settings for the queue. **Note**: conditional_group_routing_rules is deprecated in genesyscloud_routing_queue. CGR is now a standalone resource, please set ENABLE_STANDALONE_CGR in your environment variables to enable and use genesyscloud_routing_queue_conditional_group_routing (see [below for nested schema](#nestedblock--conditional_group_routing_rules))
- `default_script_ids` (Map of String) The default script IDs for each communication type. Communication types: (CALL | CALLBACK | CHAT | COBROWSE | EMAIL | MESSAGE | SOCIAL_EXPRESSION | VIDEO | SCREENSHARE)
- `description` (String) Queue description.
- `direct_routing` (Block List, Max: 1) Used by the System to set Direct Routing settings for a system Direct Routing queue. (see [below for nested schema](#nestedblock--direct_routing))
Expand All @@ -108,7 +108,7 @@ resource "genesyscloud_routing_queue" "example_queue" {
- `members` (Set of Object) Users in the queue. If not set, this resource will not manage members. If a user is already assigned to this queue via a group, attempting to assign them using this field will cause an error to be thrown. (see [below for nested schema](#nestedatt--members))
- `message_in_queue_flow_id` (String) The in-queue flow ID to use for message conversations waiting in queue.
- `on_hold_prompt_id` (String) The audio to be played when calls on this queue are on hold. If not configured, the default on-hold music will play.
- `outbound_email_address` (Block List, Max: 1, Deprecated) The outbound email address settings for this queue. (see [below for nested schema](#nestedblock--outbound_email_address))
- `outbound_email_address` (Block List, Max: 1) The outbound email address settings for this queue. **Note**: outbound_email_address is deprecated in genesyscloud_routing_queue. OEA is now a standalone resource, please set ENABLE_STANDALONE_EMAIL_ADDRESS in your environment variables to enable and use genesyscloud_routing_queue_outbound_email_address (see [below for nested schema](#nestedblock--outbound_email_address))
- `outbound_messaging_open_messaging_recipient_id` (String) The unique ID of the outbound messaging open messaging recipient for the queue.
- `outbound_messaging_sms_address_id` (String) The unique ID of the outbound messaging SMS address for the queue.
- `outbound_messaging_whatsapp_recipient_id` (String) The unique ID of the outbound messaging whatsapp recipient for the queue.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ package auth_division
import (
"context"
"fmt"
"log"
"strings"
"terraform-provider-genesyscloud/genesyscloud/provider"
rc "terraform-provider-genesyscloud/genesyscloud/resource_cache"
"terraform-provider-genesyscloud/genesyscloud/util"
"time"

Expand All @@ -13,22 +16,71 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var (
dataSourceAuthDivisionCache *rc.DataSourceCache
)

func dataSourceAuthDivisionRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sdkConfig := m.(*provider.ProviderMeta).ClientConfig
proxy := getAuthDivisionProxy(sdkConfig)
name := d.Get("name").(string)
key := normaliseAuthDivisionName(name)

// Query division by name. Retry in case search has not yet indexed the division.
return util.WithRetries(ctx, 15*time.Second, func() *retry.RetryError {
divisionId, resp, retryable, getErr := proxy.getAuthDivisionIdByName(ctx, name)
if getErr != nil && !retryable {
return retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Error requesting division %s | error: %s", name, getErr), resp))
}
if retryable {
return retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Error requesting division %s | error: %s", name, getErr), resp))
}
if dataSourceAuthDivisionCache == nil {
dataSourceAuthDivisionCache = rc.NewDataSourceCache(sdkConfig, hydrateAuthDivisionCacheFn, getDivisionIdByNameFn)
}

divisionId, err := rc.RetrieveId(dataSourceAuthDivisionCache, resourceName, key, ctx)
if err != nil {
return err
}
d.SetId(divisionId)
return nil
}

func normaliseAuthDivisionName(name string) string {
return strings.ToLower(name)
}

func hydrateAuthDivisionCacheFn(c *rc.DataSourceCache, ctx context.Context) error {
proxy := getAuthDivisionProxy(c.ClientConfig)

log.Printf("hydrating cache for data source %s", resourceName)

d.SetId(divisionId)
allDivisions, resp, err := proxy.getAllAuthDivision(ctx, "")
if err != nil {
return fmt.Errorf("failed to collect all auth divisions. Error: %s | Response: %s", err.Error(), resp.String())
}

if allDivisions == nil || len(*allDivisions) == 0 {
return nil
}

for _, div := range *allDivisions {
c.Cache[normaliseAuthDivisionName(*div.Name)] = *div.Id
}

log.Printf("cache hydration complete for data source %s", resourceName)
return nil
}

func getDivisionIdByNameFn(c *rc.DataSourceCache, name string, ctx context.Context) (string, diag.Diagnostics) {
var (
id string
proxy = getAuthDivisionProxy(c.ClientConfig)
)

diagErr := util.WithRetries(ctx, 15*time.Second, func() *retry.RetryError {
divisionId, resp, retryable, err := proxy.getAuthDivisionIdByName(ctx, name)
if err != nil {
errorDetails := util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf(`Could not find division "%s" | error: %s`, name, err.Error()), resp)
if !retryable {
return retry.NonRetryableError(errorDetails)
}
return retry.RetryableError(errorDetails)
}
id = divisionId
return nil
})

return id, diagErr
}
12 changes: 7 additions & 5 deletions genesyscloud/routing_queue/genesyscloud_routing_queue_proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,13 @@ func getAllRoutingQueueWrapupCodesFn(ctx context.Context, p *RoutingQueueProxy,
return nil, apiResponse, fmt.Errorf("failed to get routing wrapupcode : %v", err)
}

if rc.GetCacheSize(p.wrapupCodeCache) == *wrapupcodes.Total && rc.GetCacheSize(p.wrapupCodeCache) != 0 {
return rc.GetCache(p.wrapupCodeCache), nil, nil
} else if rc.GetCacheSize(p.wrapupCodeCache) != *wrapupcodes.Total && rc.GetCacheSize(p.wrapupCodeCache) != 0 {
// The cache is populated but not with the right data, clear the cache so it can be re populated
p.wrapupCodeCache = rc.NewResourceCache[platformclientv2.Wrapupcode]()
if wrapupcodes.Total != nil {
if rc.GetCacheSize(p.wrapupCodeCache) == *wrapupcodes.Total && rc.GetCacheSize(p.wrapupCodeCache) != 0 {
return rc.GetCache(p.wrapupCodeCache), nil, nil
} else if rc.GetCacheSize(p.wrapupCodeCache) != *wrapupcodes.Total && rc.GetCacheSize(p.wrapupCodeCache) != 0 {
// The cache is populated but not with the right data, clear the cache so it can be re populated
p.wrapupCodeCache = rc.NewResourceCache[platformclientv2.Wrapupcode]()
}
}

if wrapupcodes == nil || wrapupcodes.Entities == nil || len(*wrapupcodes.Entities) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -292,11 +292,10 @@ func ResourceRoutingQueue() *schema.Resource {
},
},
"conditional_group_routing_rules": {
Description: "The Conditional Group Routing settings for the queue.",
Description: "The Conditional Group Routing settings for the queue. **Note**: conditional_group_routing_rules is deprecated in genesyscloud_routing_queue. CGR is now a standalone resource, please set ENABLE_STANDALONE_CGR in your environment variables to enable and use genesyscloud_routing_queue_conditional_group_routing",
Type: schema.TypeList,
Optional: true,
MaxItems: 5,
Deprecated: "conditional_group_routing_rules is deprecated in genesyscloud_routing_queue. CGR is now a standalone resource, please set ENABLE_STANDALONE_CGR in your environment variables to enable and use genesyscloud_routing_queue_conditional_group_routing",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why did you remove the Deprecated field and merge it into the Description?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We noticed it was generating a huge amount of warning messages when AirBnB was running their jobs. Their jobs are unexpectedly crashing so we are trying to see filter the noise (and a potential source of slowness because all of the IO)

Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"queue_id": {
Expand Down Expand Up @@ -464,11 +463,10 @@ func ResourceRoutingQueue() *schema.Resource {
Optional: true,
},
"outbound_email_address": {
Description: "The outbound email address settings for this queue.",
Description: "The outbound email address settings for this queue. **Note**: outbound_email_address is deprecated in genesyscloud_routing_queue. OEA is now a standalone resource, please set ENABLE_STANDALONE_EMAIL_ADDRESS in your environment variables to enable and use genesyscloud_routing_queue_outbound_email_address",
Type: schema.TypeList,
MaxItems: 1,
Optional: true,
Deprecated: "outbound_email_address is deprecated in genesyscloud_routing_queue. OEA is now a standalone resource, please set ENABLE_STANDALONE_EMAIL_ADDRESS in your environment variables to enable and use genesyscloud_routing_queue_outbound_email_address",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"domain_id": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,27 @@ func buildSdkMediaSettings(d *schema.ResourceData) *platformclientv2.Queuemedias
queueMediaSettings := &platformclientv2.Queuemediasettings{}

mediaSettingsCall := d.Get("media_settings_call").([]interface{})
if mediaSettingsCall != nil && len(mediaSettingsCall) > 0 {
if len(mediaSettingsCall) > 0 {
queueMediaSettings.Call = buildSdkMediaSetting(mediaSettingsCall)
}

mediaSettingsCallback := d.Get("media_settings_callback").([]interface{})
if mediaSettingsCallback != nil && len(mediaSettingsCallback) > 0 {
if len(mediaSettingsCallback) > 0 {
queueMediaSettings.Callback = buildSdkMediaSettingCallback(mediaSettingsCallback)
}

mediaSettingsChat := d.Get("media_settings_chat").([]interface{})
if mediaSettingsChat != nil && len(mediaSettingsChat) > 0 {
if len(mediaSettingsChat) > 0 {
queueMediaSettings.Chat = buildSdkMediaSetting(mediaSettingsChat)
}

mediaSettingsEmail := d.Get("media_settings_email").([]interface{})
if mediaSettingsEmail != nil && len(mediaSettingsEmail) > 0 {
if len(mediaSettingsEmail) > 0 {
queueMediaSettings.Email = buildSdkMediaSetting(mediaSettingsEmail)
}

mediaSettingsMessage := d.Get("media_settings_message").([]interface{})
if mediaSettingsMessage != nil && len(mediaSettingsMessage) > 0 {
if len(mediaSettingsMessage) > 0 {
queueMediaSettings.Message = buildSdkMediaSetting(mediaSettingsMessage)
}

Expand Down Expand Up @@ -80,7 +80,7 @@ func buildSdkDefaultScriptsMap(d *schema.ResourceData) *map[string]platformclien

func buildSdkDirectRouting(d *schema.ResourceData) *platformclientv2.Directrouting {
directRouting := d.Get("direct_routing").([]interface{})
if directRouting != nil && len(directRouting) > 0 {
if len(directRouting) > 0 {
settingsMap := directRouting[0].(map[string]interface{})

agentWaitSeconds := settingsMap["agent_wait_seconds"].(int)
Expand Down Expand Up @@ -349,7 +349,7 @@ func buildSdkQueueMessagingAddresses(d *schema.ResourceData) *platformclientv2.Q

func buildSdkQueueEmailAddress(d *schema.ResourceData) *platformclientv2.Queueemailaddress {
outboundEmailAddress := d.Get("outbound_email_address").([]interface{})
if outboundEmailAddress != nil && len(outboundEmailAddress) > 0 {
if len(outboundEmailAddress) > 0 {
settingsMap := outboundEmailAddress[0].(map[string]interface{})

inboundRoute := &platformclientv2.Inboundroute{
Expand Down
Loading