Skip to content

Commit

Permalink
Making the ID fields nested within forms_eval resource read only
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecon committed Jul 18, 2024
1 parent bbf91b5 commit 0c2a1d8
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 26 deletions.
6 changes: 3 additions & 3 deletions docs/resources/quality_forms_evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ Optional:

- `default_answers_to_highest` (Boolean) Specifies whether to default answers to highest score. Defaults to `false`.
- `default_answers_to_na` (Boolean) Specifies whether to default answers to not applicable. Defaults to `false`.
- `id` (String) ID of the question group.
- `id` (String) ID of the question group. READ-ONLY. No value should be provided for this field as one will be generated by the API.
- `manual_weight` (Boolean) Specifies whether a manual weight is set. Defaults to `true`.
- `na_enabled` (Boolean) Specifies whether a not applicable answer is enabled. Defaults to `false`.
- `visibility_condition` (Block List, Max: 1) Defines conditions where question would be visible (see [below for nested schema](#nestedblock--question_groups--visibility_condition))
Expand All @@ -138,7 +138,7 @@ Optional:

- `comments_required` (Boolean) Specifies whether comments are required. Defaults to `false`.
- `help_text` (String) Help text for the question.
- `id` (String) ID of the question.
- `id` (String) ID of the question. READ-ONLY. No value should be provided for this field as one will be generated by the API.
- `is_critical` (Boolean) True if the question is a critical question Defaults to `false`.
- `is_kill` (Boolean) True if the question is a fatal question Defaults to `false`.
- `na_enabled` (Boolean) Specifies whether a not applicable answer is enabled. Defaults to `false`.
Expand All @@ -154,7 +154,7 @@ Required:

Optional:

- `id` (String)
- `id` (String) The ID for the answer option. READ-ONLY. No value should be provided for this field as one will be generated by the API.


<a id="nestedblock--question_groups--questions--visibility_condition"></a>
Expand Down
60 changes: 38 additions & 22 deletions genesyscloud/resource_genesyscloud_quality_forms_evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import (
"github.com/mypurecloud/platform-client-sdk-go/v130/platformclientv2"
)

const readOnlyWarningMessage = "READ-ONLY. No value should be provided for this field as one will be generated by the API."

var (
evaluationFormQuestionGroup = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "ID of the question group.",
Description: fmt.Sprintf("ID of the question group. %s", readOnlyWarningMessage),
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand Down Expand Up @@ -87,7 +89,7 @@ var (
evaluationFormQuestion = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: "ID of the question.",
Description: fmt.Sprintf("ID of the question. %s", readOnlyWarningMessage),
Type: schema.TypeString,
Optional: true,
Computed: true,
Expand Down Expand Up @@ -163,9 +165,10 @@ var (
evaluationFormAnswerOptionsResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Type: schema.TypeString,
Description: fmt.Sprintf("The ID for the answer option. %s", readOnlyWarningMessage),
Optional: true,
Computed: true,
},
"text": {
Type: schema.TypeString,
Expand Down Expand Up @@ -419,11 +422,14 @@ func buildSdkQuestionGroups(d *schema.ResourceData) (*[]platformclientv2.Evaluat
questionGroupType := "questionGroup"

var evalQuestionGroups []platformclientv2.Evaluationquestiongroup
if questionGroups, ok := d.GetOk("question_groups"); ok {
questionGroupList := questionGroups.([]interface{})
if questionGroupList, ok := d.Get("question_groups").([]interface{}); ok {
for _, questionGroup := range questionGroupList {
questionGroupsMap := questionGroup.(map[string]interface{})

if id, _ := questionGroupsMap["id"].(string); id != "" {
return nil, diag.Errorf("The id field for question_groups is intended to be read-only. Got: '%s'", id)
}

questionGroupName := questionGroupsMap["name"].(string)
defaultAnswersToHighest := questionGroupsMap["default_answers_to_highest"].(bool)
defaultAnswersToNA := questionGroupsMap["default_answers_to_na"].(bool)
Expand All @@ -440,12 +446,13 @@ func buildSdkQuestionGroups(d *schema.ResourceData) (*[]platformclientv2.Evaluat
NaEnabled: &naEnabled,
Weight: &weight,
ManualWeight: &manualWeight,
Questions: buildSdkQuestions(questions),
}

if id, _ := questionGroupsMap["id"].(string); id != "" {
sdkquestionGroup.Id = &id
sdkQuestions, diagErr := buildSdkQuestions(questions)
if diagErr != nil {
return nil, diagErr
}
sdkquestionGroup.Questions = sdkQuestions

visibilityCondition := questionGroupsMap["visibility_condition"].([]interface{})
sdkquestionGroup.VisibilityCondition = buildSdkVisibilityCondition(visibilityCondition)
Expand All @@ -457,12 +464,17 @@ func buildSdkQuestionGroups(d *schema.ResourceData) (*[]platformclientv2.Evaluat
return &evalQuestionGroups, nil
}

func buildSdkQuestions(questions []interface{}) *[]platformclientv2.Evaluationquestion {
func buildSdkQuestions(questions []interface{}) (*[]platformclientv2.Evaluationquestion, diag.Diagnostics) {
questionType := "multipleChoiceQuestion"

sdkQuestions := make([]platformclientv2.Evaluationquestion, 0)
for _, question := range questions {
questionsMap := question.(map[string]interface{})

if id, _ := questionsMap["id"].(string); id != "" {
return nil, diag.Errorf("The id field for questions is intended to be read-only. Got: '%s'", id)
}

text := questionsMap["text"].(string)
helpText := questionsMap["help_text"].(string)
naEnabled := questionsMap["na_enabled"].(bool)
Expand All @@ -477,29 +489,34 @@ func buildSdkQuestions(questions []interface{}) *[]platformclientv2.Evaluationqu
VarType: &questionType,
NaEnabled: &naEnabled,
CommentsRequired: &commentsRequired,
AnswerOptions: buildSdkAnswerOptions(answerQuestions),
IsKill: &isKill,
IsCritical: &isCritical,
}

if id, _ := questionsMap["id"].(string); id != "" {
sdkQuestion.Id = &id
sdkAnswerOptions, err := buildSdkAnswerOptions(answerQuestions)
if err != nil {
return nil, err
}
sdkQuestion.AnswerOptions = sdkAnswerOptions

visibilityCondition := questionsMap["visibility_condition"].([]interface{})
sdkQuestion.VisibilityCondition = buildSdkVisibilityCondition(visibilityCondition)

sdkQuestions = append(sdkQuestions, sdkQuestion)
}

return &sdkQuestions
return &sdkQuestions, nil
}

func buildSdkAnswerOptions(answerOptions []interface{}) *[]platformclientv2.Answeroption {
func buildSdkAnswerOptions(answerOptions []interface{}) (*[]platformclientv2.Answeroption, diag.Diagnostics) {
sdkAnswerOptions := make([]platformclientv2.Answeroption, 0)
for _, answerOptionsList := range answerOptions {
answerOptionsMap := answerOptionsList.(map[string]interface{})

if id, _ := answerOptionsMap["id"].(string); id != "" {
return nil, diag.Errorf("The id field for answer_options is intended to be read-only. Got: '%s'", id)
}

answerText := answerOptionsMap["text"].(string)
answerValue := answerOptionsMap["value"].(int)

Expand All @@ -508,22 +525,21 @@ func buildSdkAnswerOptions(answerOptions []interface{}) *[]platformclientv2.Answ
Value: &answerValue,
}

if id, _ := answerOptionsMap["id"].(string); id != "" {
sdkAnswerOption.Id = &id
}

sdkAnswerOptions = append(sdkAnswerOptions, sdkAnswerOption)
}

return &sdkAnswerOptions
return &sdkAnswerOptions, nil
}

func buildSdkVisibilityCondition(visibilityCondition []interface{}) *platformclientv2.Visibilitycondition {
if visibilityCondition == nil || len(visibilityCondition) <= 0 {
return nil
}

visibilityConditionMap := visibilityCondition[0].(map[string]interface{})
visibilityConditionMap, ok := visibilityCondition[0].(map[string]interface{})
if !ok {
return nil
}

combiningOperation := visibilityConditionMap["combining_operation"].(string)
predicates := visibilityConditionMap["predicates"].([]interface{})
Expand Down
3 changes: 2 additions & 1 deletion genesyscloud/resource_genesyscloud_quality_forms_survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,13 +512,14 @@ func buildSurveyQuestions(questions []interface{}) *[]platformclientv2.Surveyque
naEnabled := questionsMap["na_enabled"].(bool)
answerQuestions := questionsMap["answer_options"].([]interface{})
maxResponseCharacters := questionsMap["max_response_characters"].(int)
sdkAnswerOptions, _ := buildSdkAnswerOptions(answerQuestions)

sdkQuestion := platformclientv2.Surveyquestion{
Text: &text,
HelpText: &helpText,
VarType: &questionType,
NaEnabled: &naEnabled,
AnswerOptions: buildSdkAnswerOptions(answerQuestions),
AnswerOptions: sdkAnswerOptions,
MaxResponseCharacters: &maxResponseCharacters,
}

Expand Down

0 comments on commit 0c2a1d8

Please sign in to comment.