Skip to content

Commit

Permalink
Minor refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecon committed Aug 22, 2024
1 parent c52bd55 commit 5858913
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 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
51 changes: 26 additions & 25 deletions genesyscloud/scripts/resource_genesyscloud_script.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,30 +56,7 @@ func createScript(ctx context.Context, d *schema.ResourceData, meta interface{})
return readScript(ctx, d, meta)
}

func updateScript(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
scriptsProxy := getScriptsProxy(sdkConfig)

filePath := d.Get("filepath").(string)
scriptName := d.Get("script_name").(string)
substitutions := d.Get("substitutions").(map[string]interface{})

log.Printf("Updating script '%s' %s", scriptName, d.Id())

scriptId, err := scriptsProxy.updateScript(ctx, filePath, scriptName, d.Id(), substitutions)
if err != nil {
return diag.FromErr(err)
}
if scriptId != d.Id() {
log.Printf("ID of script '%s' changed from '%s' to '%s' after update.", scriptName, d.Id(), scriptId)
d.SetId(scriptId)
}

log.Printf("Updated script '%s' %s", scriptName, d.Id())
return readScript(ctx, d, meta)
}

// readScript contains all of the logic needed to read resource data from Genesys Cloud
// 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)
Expand All @@ -104,7 +81,31 @@ func readScript(ctx context.Context, d *schema.ResourceData, meta interface{}) d
})
}

// deleteScript contains all the logic needed to delete a resource from Genesys Cloud
// 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)

filePath := d.Get("filepath").(string)
scriptName := d.Get("script_name").(string)
substitutions := d.Get("substitutions").(map[string]interface{})

log.Printf("Updating script '%s' %s", scriptName, d.Id())

scriptId, err := scriptsProxy.updateScript(ctx, filePath, scriptName, d.Id(), substitutions)
if err != nil {
return diag.FromErr(err)
}
if scriptId != d.Id() {
log.Printf("ID of script '%s' changed from '%s' to '%s' after update.", scriptName, d.Id(), scriptId)
d.SetId(scriptId)
}

log.Printf("Updated script '%s' %s", scriptName, d.Id())
return readScript(ctx, d, meta)
}

// 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 5858913

Please sign in to comment.