Skip to content

Commit

Permalink
Add showTrend to Question
Browse files Browse the repository at this point in the history
Expose the showTrend field to allow terraform users to read and
update its value. Add tests for reading it (no update needed to
cassettes, as they already contained it) and for validating it as
a proper boolean.
  • Loading branch information
MartinodF committed Aug 2, 2023
1 parent 43c2e74 commit 65cc612
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/resources/question.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ resource "jupiterone_question" "unencrypted_critical_data_stores" {
- `compliance` (Block List) (see [below for nested schema](#nestedblock--compliance))
- `polling_interval` (String) Frequency of automated question evaluation. Defaults to ONE_DAY.
- `query` (Block List) (see [below for nested schema](#nestedblock--query))
- `show_trend` (Boolean) Whether to enable trend data collection. Defaults to false.
- `tags` (List of String)

### Read-Only
Expand Down
3 changes: 3 additions & 0 deletions jupiterone/cassettes/TestQuestion_Config_Errors.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
version: 2
interactions: []
1 change: 1 addition & 0 deletions jupiterone/internal/client/question.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ query GetQuestionById($id: ID!) {
id
title
description
showTrend
pollingInterval
# @genqlient(typename: QuestionQuery)
queries {
Expand Down
11 changes: 11 additions & 0 deletions jupiterone/resource_question.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type QuestionModel struct {
Id types.String `json:"id,omitempty" tfsdk:"id"`
Title types.String `json:"title,omitempty" tfsdk:"title"`
Description types.String `json:"description,omitempty" tfsdk:"description"`
ShowTrend types.Bool `json:"show_trend,omitempty" tfsdk:"show_trend"`
PollingInterval types.String `json:"polling_interval,omitempty" tfsdk:"polling_interval"`
Tags []string `json:"tags,omitempty" tfsdk:"tags"`
Query []*QuestionQueryModel `json:"query,omitempty" tfsdk:"query"`
Expand Down Expand Up @@ -97,6 +98,14 @@ func (*QuestionResource) Schema(ctx context.Context, req resource.SchemaRequest,
"description": schema.StringAttribute{
Required: true,
},
"show_trend": schema.BoolAttribute{
Description: "Whether to enable daily trend data collection. Defaults to false.",
Optional: true,
Computed: true,
PlanModifiers: []planmodifier.Bool{
BoolDefaultValuePlanModifier(false),
},
},
"polling_interval": schema.StringAttribute{
Description: "Frequency of automated question evaluation. Defaults to ONE_DAY.",
Computed: true,
Expand Down Expand Up @@ -328,6 +337,7 @@ func (qm *QuestionModel) BuildQuestion() client.QuestionUpdate {
Title: qm.Title.ValueString(),
Description: qm.Description.ValueString(),
Tags: qm.Tags,
ShowTrend: qm.ShowTrend.ValueBool(),
PollingInterval: client.SchedulerPollingInterval(qm.PollingInterval.ValueString()),
}

Expand Down Expand Up @@ -359,6 +369,7 @@ func (qm *QuestionModel) BuildCreateQuestionInput() client.CreateQuestionInput {
Title: qm.Title.ValueString(),
Description: qm.Description.ValueString(),
PollingInterval: client.SchedulerPollingInterval(qm.PollingInterval.ValueString()),
ShowTrend: qm.ShowTrend.ValueBool(),
Tags: qm.Tags,
}

Expand Down
42 changes: 42 additions & 0 deletions jupiterone/resource_question_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@ package jupiterone
import (
"context"
"fmt"
"regexp"
"strings"
"testing"
"time"

"github.com/Khan/genqlient/graphql"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
"github.com/jupiterone/terraform-provider-jupiterone/jupiterone/internal/client"
Expand All @@ -33,6 +35,7 @@ func TestQuestion_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "title", questionTitle),
resource.TestCheckResourceAttr(resourceName, "description", "Test"),
resource.TestCheckResourceAttr(resourceName, "show_trend", "false"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "tf_acc:1"),
resource.TestCheckResourceAttr(resourceName, "query.#", "1"),
Expand All @@ -48,6 +51,7 @@ func TestQuestion_Basic(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "title", questionTitle),
resource.TestCheckResourceAttr(resourceName, "description", "Test"),
resource.TestCheckResourceAttr(resourceName, "show_trend", "false"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "tf_acc:2"),
resource.TestCheckResourceAttr(resourceName, "query.#", "1"),
Expand Down Expand Up @@ -84,6 +88,7 @@ func TestQuestion_BasicImport(t *testing.T) {
resource.TestCheckResourceAttrSet(resourceName, "id"),
resource.TestCheckResourceAttr(resourceName, "title", questionTitle),
resource.TestCheckResourceAttr(resourceName, "description", "Test"),
resource.TestCheckResourceAttr(resourceName, "show_trend", "false"),
resource.TestCheckResourceAttr(resourceName, "tags.#", "1"),
resource.TestCheckResourceAttr(resourceName, "tags.0", "tf_acc:1"),
resource.TestCheckResourceAttr(resourceName, "query.#", "1"),
Expand All @@ -96,6 +101,25 @@ func TestQuestion_BasicImport(t *testing.T) {
})
}

func TestQuestion_Config_Errors(t *testing.T) {
ctx := context.TODO()

recordingClient, _, cleanup := setupTestClients(ctx, t)
defer cleanup(t)

questionTitle := acctest.RandomWithPrefix("tf-provider-test-question")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories(recordingClient),
Steps: []resource.TestStep{
{
Config: testQuestionBasicConfigWithShowTrend(questionTitle, "INVALID_SHOW_TREND"),
ExpectError: regexp.MustCompile(`Inappropriate value for attribute "show_trend"`),
},
},
})
}

// createTestQuestion directly calls the client to create a question directly
// for import or other tests. Because the id must be returned, this must
// called with the recorder client.
Expand Down Expand Up @@ -199,6 +223,24 @@ func questionDestroyHelper(ctx context.Context, s *terraform.State, qlient graph
return nil
}

func testQuestionBasicConfigWithShowTrend(rName string, showTrend string) string {
return fmt.Sprintf(`
provider "jupiterone" {}
resource "jupiterone_question" "test" {
title = %q
description = "Test"
show_trend = %q
query {
name = "query0"
query = "Find DataStore with classification=('critical' or 'sensitive' or 'confidential' or 'restricted') and encrypted!=true"
version = "v1"
}
}
`, rName, showTrend)
}

func testQuestionBasicConfigWithTags(rName string, tag string) string {
return fmt.Sprintf(`
provider "jupiterone" {}
Expand Down

0 comments on commit 65cc612

Please sign in to comment.