Skip to content

Commit

Permalink
Made ID fields read-only by removing Optional attr
Browse files Browse the repository at this point in the history
  • Loading branch information
charliecon committed Jul 18, 2024
1 parent 309125a commit 6802a2a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 85 deletions.
14 changes: 10 additions & 4 deletions docs/resources/quality_forms_evaluation.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,11 +121,14 @@ 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. 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))

Read-Only:

- `id` (String) ID of the question group.

<a id="nestedblock--question_groups--questions"></a>
### Nested Schema for `question_groups.questions`

Expand All @@ -138,12 +141,15 @@ 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. 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`.
- `visibility_condition` (Block List, Max: 1) Defines conditions where question would be visible (see [below for nested schema](#nestedblock--question_groups--questions--visibility_condition))

Read-Only:

- `id` (String) ID of the question.

<a id="nestedblock--question_groups--questions--answer_options"></a>
### Nested Schema for `question_groups.questions.answer_options`

Expand All @@ -152,9 +158,9 @@ Required:
- `text` (String)
- `value` (Number)

Optional:
Read-Only:

- `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.
- `id` (String) The ID for the answer option.


<a id="nestedblock--question_groups--questions--visibility_condition"></a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import (
)

type EvaluationFormQuestionGroupStruct struct {
Id string
Name string
DefaultAnswersToHighest bool
DefaultAnswersToNA bool
Expand All @@ -33,7 +32,6 @@ type EvaluationFormStruct struct {
}

type EvaluationFormQuestionStruct struct {
Id string
Text string
HelpText string
NaEnabled bool
Expand All @@ -45,7 +43,6 @@ type EvaluationFormQuestionStruct struct {
}

type AnswerOptionStruct struct {
Id string
Text string
Value int
AssistanceConditions []AssistanceConditionStruct
Expand Down
85 changes: 16 additions & 69 deletions genesyscloud/resource_genesyscloud_quality_forms_evaluation.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,12 @@ import (
"github.com/mypurecloud/platform-client-sdk-go/v133/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: fmt.Sprintf("ID of the question group. %s", readOnlyWarningMessage),
Description: "ID of the question group.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Expand Down Expand Up @@ -89,9 +86,8 @@ var (
evaluationFormQuestion = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Description: fmt.Sprintf("ID of the question. %s", readOnlyWarningMessage),
Description: "ID of the question.",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"text": {
Expand Down Expand Up @@ -166,8 +162,7 @@ var (
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Description: fmt.Sprintf("The ID for the answer option. %s", readOnlyWarningMessage),
Optional: true,
Description: "The ID for the answer option.",
Computed: true,
},
"text": {
Expand Down Expand Up @@ -251,18 +246,13 @@ func createEvaluationForm(ctx context.Context, d *schema.ResourceData, meta inte
name := d.Get("name").(string)
published := d.Get("published").(bool)

questionGroups, qgErr := buildSdkQuestionGroups(d)
if qgErr != nil {
return qgErr
}

sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
qualityAPI := platformclientv2.NewQualityApiWithConfig(sdkConfig)

log.Printf("Creating Evaluation Form %s", name)
form, resp, err := qualityAPI.PostQualityFormsEvaluations(platformclientv2.Evaluationform{
Name: &name,
QuestionGroups: questionGroups,
QuestionGroups: buildSdkQuestionGroups(d),
})
if err != nil {
return util.BuildAPIDiagnosticError("genesyscloud_quality_forms_evaluation", fmt.Sprintf("Failed to create evaluation form %s error: %s", name, err), resp)
Expand Down Expand Up @@ -340,11 +330,6 @@ func updateEvaluationForm(ctx context.Context, d *schema.ResourceData, meta inte
name := d.Get("name").(string)
published := d.Get("published").(bool)

questionGroups, qgErr := buildSdkQuestionGroups(d)
if qgErr != nil {
return qgErr
}

sdkConfig := meta.(*provider.ProviderMeta).ClientConfig
qualityAPI := platformclientv2.NewQualityApiWithConfig(sdkConfig)

Expand All @@ -359,7 +344,7 @@ func updateEvaluationForm(ctx context.Context, d *schema.ResourceData, meta inte
log.Printf("Updating Evaluation Form %s", name)
form, resp, err := qualityAPI.PutQualityFormsEvaluation(*unpublishedForm.Id, platformclientv2.Evaluationform{
Name: &name,
QuestionGroups: questionGroups,
QuestionGroups: buildSdkQuestionGroups(d),
})
if err != nil {
return util.BuildAPIDiagnosticError("genesyscloud_quality_forms_evaluation", fmt.Sprintf("Failed to update evaluation form %s error: %s", name, err), resp)
Expand Down Expand Up @@ -418,18 +403,14 @@ func deleteEvaluationForm(ctx context.Context, d *schema.ResourceData, meta inte
})
}

func buildSdkQuestionGroups(d *schema.ResourceData) (*[]platformclientv2.Evaluationquestiongroup, diag.Diagnostics) {
func buildSdkQuestionGroups(d *schema.ResourceData) *[]platformclientv2.Evaluationquestiongroup {
questionGroupType := "questionGroup"

var evalQuestionGroups []platformclientv2.Evaluationquestiongroup
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 @@ -446,35 +427,26 @@ func buildSdkQuestionGroups(d *schema.ResourceData) (*[]platformclientv2.Evaluat
NaEnabled: &naEnabled,
Weight: &weight,
ManualWeight: &manualWeight,
Questions: buildSdkQuestions(questions),
}

sdkQuestions, diagErr := buildSdkQuestions(questions)
if diagErr != nil {
return nil, diagErr
}
sdkquestionGroup.Questions = sdkQuestions

visibilityCondition := questionGroupsMap["visibility_condition"].([]interface{})
sdkquestionGroup.VisibilityCondition = buildSdkVisibilityCondition(visibilityCondition)

evalQuestionGroups = append(evalQuestionGroups, sdkquestionGroup)
}
}

return &evalQuestionGroups, nil
return &evalQuestionGroups
}

func buildSdkQuestions(questions []interface{}) (*[]platformclientv2.Evaluationquestion, diag.Diagnostics) {
func buildSdkQuestions(questions []interface{}) *[]platformclientv2.Evaluationquestion {
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 @@ -491,32 +463,23 @@ func buildSdkQuestions(questions []interface{}) (*[]platformclientv2.Evaluationq
CommentsRequired: &commentsRequired,
IsKill: &isKill,
IsCritical: &isCritical,
AnswerOptions: buildSdkAnswerOptions(answerQuestions),
}

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, nil
return &sdkQuestions
}

func buildSdkAnswerOptions(answerOptions []interface{}) (*[]platformclientv2.Answeroption, diag.Diagnostics) {
func buildSdkAnswerOptions(answerOptions []interface{}) *[]platformclientv2.Answeroption {
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 @@ -528,7 +491,7 @@ func buildSdkAnswerOptions(answerOptions []interface{}) (*[]platformclientv2.Ans
sdkAnswerOptions = append(sdkAnswerOptions, sdkAnswerOption)
}

return &sdkAnswerOptions, nil
return &sdkAnswerOptions
}

func buildSdkVisibilityCondition(visibilityCondition []interface{}) *platformclientv2.Visibilitycondition {
Expand Down Expand Up @@ -694,13 +657,8 @@ func GenerateEvaluationFormQuestionGroups(questionGroups *[]EvaluationFormQuesti
questionGroupsString := ""

for _, questionGroup := range *questionGroups {
idValue := util.NullValue
if questionGroup.Id != "" {
idValue = strconv.Quote(questionGroup.Id)
}
questionGroupString := fmt.Sprintf(`
question_groups {
id = %s
name = "%s"
default_answers_to_highest = %v
default_answers_to_na = %v
Expand All @@ -710,7 +668,7 @@ func GenerateEvaluationFormQuestionGroups(questionGroups *[]EvaluationFormQuesti
%s
%s
}
`, idValue, questionGroup.Name,
`, questionGroup.Name,
questionGroup.DefaultAnswersToHighest,
questionGroup.DefaultAnswersToNA,
questionGroup.NaEnabled,
Expand All @@ -734,13 +692,8 @@ func GenerateEvaluationFormQuestions(questions *[]EvaluationFormQuestionStruct)
questionsString := ""

for _, question := range *questions {
idValue := util.NullValue
if question.Id != "" {
idValue = strconv.Quote(question.Id)
}
questionString := fmt.Sprintf(`
questions {
id = %s
text = "%s"
help_text = "%s"
na_enabled = %v
Expand All @@ -750,7 +703,7 @@ func GenerateEvaluationFormQuestions(questions *[]EvaluationFormQuestionStruct)
%s
%s
}
`, idValue, question.Text,
`, question.Text,
question.HelpText,
question.NaEnabled,
question.CommentsRequired,
Expand All @@ -774,18 +727,12 @@ func GenerateFormAnswerOptions(answerOptions *[]AnswerOptionStruct) string {
answerOptionsString := ""

for _, answerOption := range *answerOptions {
idValue := util.NullValue
if answerOption.Id != "" {
idValue = strconv.Quote(answerOption.Id)
}
answerOptionString := fmt.Sprintf(`
answer_options {
id = %s
text = "%s"
value = %v
}
`, idValue,
answerOption.Text,
`, answerOption.Text,
answerOption.Value,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ import (

func TestAccResourceEvaluationFormBasic(t *testing.T) {
formResource1 := "test-evaluation-form-1"
questionGroupId := uuid.NewString()
questionId := uuid.NewString()
answer1Id := uuid.NewString()
answer1Text := "Yes"
answer1Value := 1

Expand All @@ -26,16 +23,13 @@ func TestAccResourceEvaluationFormBasic(t *testing.T) {
Name: "terraform-form-evaluations-" + uuid.NewString(),
QuestionGroups: []EvaluationFormQuestionGroupStruct{
{
Id: questionGroupId,
Name: "Test Question Group 1",
Weight: 1,
Questions: []EvaluationFormQuestionStruct{
{
Id: questionId,
Text: "Did the agent perform the opening spiel?",
AnswerOptions: []AnswerOptionStruct{
{
Id: answer1Id,
Text: answer1Text,
Value: answer1Value,
},
Expand Down Expand Up @@ -104,9 +98,7 @@ func TestAccResourceEvaluationFormBasic(t *testing.T) {
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "name", evaluationForm1.Name),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "published", util.FalseValue),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "question_groups.0.name", evaluationForm1.QuestionGroups[0].Name),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "question_groups.0.id", evaluationForm1.QuestionGroups[0].Id),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "question_groups.#", fmt.Sprint(len(evaluationForm1.QuestionGroups))),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "question_groups.0.questions.0.id", evaluationForm1.QuestionGroups[0].Questions[0].Id),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "question_groups.0.questions.0.text", evaluationForm1.QuestionGroups[0].Questions[0].Text),
resource.TestCheckResourceAttr("genesyscloud_quality_forms_evaluation."+formResource1, "question_groups.0.questions.0.answer_options.#", fmt.Sprint(len(evaluationForm1.QuestionGroups[0].Questions[0].AnswerOptions))),
),
Expand Down
2 changes: 1 addition & 1 deletion genesyscloud/resource_genesyscloud_quality_forms_survey.go
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,7 @@ 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)
sdkAnswerOptions := buildSdkAnswerOptions(answerQuestions)

sdkQuestion := platformclientv2.Surveyquestion{
Text: &text,
Expand Down

0 comments on commit 6802a2a

Please sign in to comment.