Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecon committed Aug 21, 2024
1 parent b3f4b42 commit 91fea4e
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 28 deletions.
5 changes: 3 additions & 2 deletions genesyscloud/scripts/data_source_genesyscloud_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ func dataSourceScriptRead(ctx context.Context, d *schema.ResourceData, m interfa
return util.WithRetries(ctx, 15*time.Second, func() *retry.RetryError {
scriptId, retryable, resp, err := scriptsProxy.getScriptIdByName(ctx, name)
if err != nil {
diagErr := util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Failed to get Script %s", err), resp)
if retryable {
return retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Failed to get Script %s", err), resp))
return retry.RetryableError(diagErr)
}
return retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Failed to get Script %s", err), resp))
return retry.NonRetryableError(diagErr)
}
d.SetId(scriptId)
return nil
Expand Down
53 changes: 27 additions & 26 deletions genesyscloud/scripts/resource_genesyscloud_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ func createScript(ctx context.Context, d *schema.ResourceData, meta interface{})
return readScript(ctx, d, meta)
}

// readScript contains all of the logic needed to read a script from Genesys Cloud
func readScript(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
scriptsProxy := getScriptsProxy(sdkConfig)
cc := consistency_checker.NewConsistencyCheck(ctx, d, meta, ResourceScript(), constants.DefaultConsistencyChecks, resourceName)

return util.WithRetriesForRead(ctx, d, func() *retry.RetryError {
script, resp, err := scriptsProxy.getScriptById(ctx, d.Id())
if err != nil && resp.StatusCode == http.StatusNotFound {
diagErr := util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Failed to read flow %s | error: %s", d.Id(), err), resp)
if util.IsStatus404(resp) {
return retry.RetryableError(diagErr)
}
return retry.NonRetryableError(diagErr)
}

if script.Name != nil {
_ = d.Set("script_name", *script.Name)
}

log.Printf("Read script %s %s", d.Id(), *script.Name)
return cc.CheckState(d)
})
}

// updateScript contains all the logic needed to update a script on Genesys Cloud
func updateScript(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
scriptsProxy := getScriptsProxy(sdkConfig)
Expand All @@ -79,32 +105,7 @@ func updateScript(ctx context.Context, d *schema.ResourceData, meta interface{})
return readScript(ctx, d, meta)
}

// readScript contains all of the logic needed to read resource data from Genesys Cloud
func readScript(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
scriptsProxy := getScriptsProxy(sdkConfig)
cc := consistency_checker.NewConsistencyCheck(ctx, d, meta, ResourceScript(), constants.DefaultConsistencyChecks, resourceName)

return util.WithRetriesForRead(ctx, d, func() *retry.RetryError {
script, resp, err := scriptsProxy.getScriptById(ctx, d.Id())
if resp != nil && resp.StatusCode == http.StatusNotFound {
return retry.RetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Failed to read flow %s | error: %s", d.Id(), err), resp))
}

if err != nil {
return retry.NonRetryableError(util.BuildWithRetriesApiDiagnosticError(resourceName, fmt.Sprintf("Failed to read flow %s | error: %s", d.Id(), err), resp))
}

if script.Name != nil {
_ = d.Set("script_name", *script.Name)
}

log.Printf("Read script %s %s", d.Id(), *script.Name)
return cc.CheckState(d)
})
}

// deleteScript contains all the logic needed to delete a resource from Genesys Cloud
// deleteScript contains all the logic needed to delete a script from Genesys Cloud
func deleteScript(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
scriptsProxy := getScriptsProxy(sdkConfig)
Expand Down

0 comments on commit 91fea4e

Please sign in to comment.