Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource: wiz_report_graph_query #184

Merged
merged 33 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 27 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f20d519
early draft
cvirtucio Dec 5, 2023
1566fa0
mvp
cvirtucio Dec 5, 2023
91e8bfe
refactored to type-specific resources
cvirtucio Dec 5, 2023
12c2dd0
moved Read() func to graph_query resource for now; setting query on data
cvirtucio Dec 5, 2023
d07bb27
removed cruft comments
cvirtucio Dec 5, 2023
25783a9
removed more cruft
cvirtucio Dec 5, 2023
21b3041
brought back comments for resource_report.go structs
cvirtucio Dec 5, 2023
34c2e43
added comments to address lint warnings
cvirtucio Dec 5, 2023
cf8896c
attempt to fix codeowners job
cvirtucio Dec 5, 2023
ccf1f3e
force ci
cvirtucio Dec 5, 2023
4b8793a
force ci
cvirtucio Dec 5, 2023
5c1670d
run_interval_hours is nullable, per schema; added configuration for s…
cvirtucio Dec 6, 2023
9bb5fbf
switched to layout used by wiz API
cvirtucio Dec 6, 2023
a8ebe98
minor message improvement
cvirtucio Dec 6, 2023
dacd275
string formatting fixes; added acceptance test
cvirtucio Dec 6, 2023
5927b92
added example and generated docs
cvirtucio Dec 6, 2023
b4ca1e9
Merge branch 'main' into reports
cvirtucio Dec 6, 2023
86897a0
fixed test
cvirtucio Dec 6, 2023
7b052c3
go fmt
cvirtucio Dec 6, 2023
572bc9a
attempt to have the checkout step in the codeowners job checkout usin…
cvirtucio Dec 8, 2023
cf0fc0e
specifying pull_request event HEAD instead
cvirtucio Dec 8, 2023
07449dc
defaulting to star project_id; project_id requires recreating the report
cvirtucio Dec 8, 2023
38f2edc
refactored scheduling code into a func
cvirtucio Dec 8, 2023
bd8e538
updated comment about CreateReportExportDestinationInput
cvirtucio Dec 8, 2023
bc2efb8
regenerated docs
cvirtucio Dec 8, 2023
589940a
lint fix
cvirtucio Dec 8, 2023
5b873a5
fixed docs rendering
cvirtucio Dec 8, 2023
dab2fbd
rounding to the hour in the example
cvirtucio Dec 11, 2023
eccee03
regen docs
cvirtucio Dec 11, 2023
ec26333
description updates
cvirtucio Dec 12, 2023
f3c5f9e
regenerated docs
cvirtucio Dec 12, 2023
85db7b9
Merge branch 'main' into reports
jschoombee Dec 12, 2023
e3c0a90
Delete .terraform-version
jschoombee Dec 12, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .github/workflows/pull.yml
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ jobs:
uses: actions/checkout@v4
with:
jschoombee marked this conversation as resolved.
Show resolved Hide resolved
ref: ${{ github.event.pull_request.head.ref }}
repository: ${{ github.event.pull_request.head.repo.full_name }}
- name: Check for CODEOWNERS file
uses: andstor/file-existence-action@v2
id: check_codeowners_1
Expand Down
78 changes: 78 additions & 0 deletions docs/resources/report_graph_query.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "wiz_report_graph_query Resource - terraform-provider-wiz"
subcategory: ""
description: |-
TBD.
---

# wiz_report_graph_query (Resource)

TBD.

## Example Usage

```terraform
# A simple example
resource "wiz_report_graph_query" "foo" {
name = "foo"
project_id = "2c38b8fa-c315-57ea-9de4-e3a19592d796"
query = <<EOF
{
"select": true,
"type": [
"CONTAINER_IMAGE"
],
"where": {
"name": {
"CONTAINS": [
"foo"
]
}
}
}
EOF
}

# Scheduling enabled (both run_interval_hours and run_starts_at must be set)
resource "wiz_report_graph_query" "foo" {
name = "foo"
project_id = "2c38b8fa-c315-57ea-9de4-e3a19592d796"
run_interval_hours = 24
run_starts_at = "2023-12-06 16:30:00 +0000 UTC"
query = <<EOF
{
"select": true,
"type": [
"CONTAINER_IMAGE"
],
"where": {
"name": {
"CONTAINS": [
"foo"
]
}
}
}
EOF
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `name` (String) Name of the Report.
- `query` (String) The query that the report will run. Required by the GRAPH_QUERY report type.

### Optional

- `project_id` (String) The ID of the project that this report belongs to (changing this requires re-creatting the report).
- Defaults to `*`.
- `run_interval_hours` (Number) Run interval for scheduled reports (in hours).
- `run_starts_at` (String) String representing the time and date when the scheduling should start (required when run_interval_hours is set). Must be in the following format: 2006-01-02 15:04:05 +0000 UTC. Also, Wiz will always round this down by the hour.

### Read-Only

- `id` (String) The ID of this resource.
43 changes: 43 additions & 0 deletions examples/resources/wiz_report_graph_query/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# A simple example
resource "wiz_report_graph_query" "foo" {
name = "foo"
project_id = "2c38b8fa-c315-57ea-9de4-e3a19592d796"
query = <<EOF
{
"select": true,
"type": [
"CONTAINER_IMAGE"
],
"where": {
"name": {
"CONTAINS": [
"foo"
]
}
}
}
EOF
}

# Scheduling enabled (both run_interval_hours and run_starts_at must be set)
resource "wiz_report_graph_query" "foo" {
name = "foo"
project_id = "2c38b8fa-c315-57ea-9de4-e3a19592d796"
run_interval_hours = 24
run_starts_at = "2023-12-06 16:30:00 +0000 UTC"
jschoombee marked this conversation as resolved.
Show resolved Hide resolved
query = <<EOF
{
"select": true,
"type": [
"CONTAINER_IMAGE"
],
"where": {
"name": {
"CONTAINS": [
"foo"
]
}
}
}
EOF
}
2 changes: 2 additions & 0 deletions internal/acceptance/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ const (
TcSubscriptionResourceGroups TestCase = "SUBSCRIPTION_RESOURCE_GROUPS"
// TcProject test case
TcProject TestCase = "PROJECT"
// TcReportGraphQuery test case
TcReportGraphQuery TestCase = "REPORT_GRAPH_QUERY"
// TcCloudConfigRule test case
TcCloudConfigRule TestCase = "CLOUD_CONFIG_RULE"
)
2 changes: 2 additions & 0 deletions internal/acceptance/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ func testAccPreCheck(t *testing.T, tc TestCase) {
envVars = append(commonEnvVars, "WIZ_SUBSCRIPTION_ID")
case TcCloudConfigRule:
envVars = append(commonEnvVars, "WIZ_SUBSCRIPTION_ID")
case TcReportGraphQuery:
envVars = append(commonEnvVars, "WIZ_PROJECT_ID")
default:
t.Fatalf("unknown testCase: %s", tc)
}
Expand Down
64 changes: 64 additions & 0 deletions internal/acceptance/resource_report_graph_query_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
package acceptance

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-testing/helper/acctest"
"github.com/hashicorp/terraform-plugin-testing/helper/resource"
)

func TestAccResourceWizReportGraphQuery_basic(t *testing.T) {
rName := acctest.RandomWithPrefix(ResourcePrefix)
projectID := os.Getenv("WIZ_PROJECT_ID")

resource.UnitTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t, TestCase(TcReportGraphQuery)) },
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testResourceWizReportGraphQueryBasic(rName, projectID),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(
"wiz_report_graph_query.foo",
"name",
rName,
),
resource.TestCheckResourceAttr(
"wiz_report_graph_query.foo",
"project_id",
projectID,
),
resource.TestCheckResourceAttr(
"wiz_report_graph_query.foo",
"query",
"{\"select\": true, \"type\": [\"CONTAINER_IMAGE\"], \"where\": {\"name\": {\"CONTAINS\": [\"foo\"]}}}",
),
resource.TestCheckResourceAttr(
"wiz_report_graph_query.foo",
"run_interval_hours",
"48",
),
resource.TestCheckResourceAttr(
"wiz_report_graph_query.foo",
"run_starts_at",
"2023-12-06 16:00:00 +0000 UTC",
),
),
},
},
})
}

func testResourceWizReportGraphQueryBasic(rName, projectID string) string {
return fmt.Sprintf(`
resource "wiz_report_graph_query" "foo" {
name = "%s"
project_id = "%s"
run_interval_hours = 48
run_starts_at = "2023-12-06 16:00:00 +0000 UTC"
query = "{\"select\": true, \"type\": [\"CONTAINER_IMAGE\"], \"where\": {\"name\": {\"CONTAINS\": [\"foo\"]}}}"
}
`, rName, projectID)
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ yLyKQXhw2W2Xs0qLeC1etA+jTGDK4UfLeC0SF7FSi8o5LL21L8IzApar2pR/
"wiz_integration_aws_sns": resourceWizIntegrationAwsSNS(),
"wiz_integration_servicenow": resourceWizIntegrationServiceNow(),
"wiz_integration_jira": resourceWizIntegrationJira(),
"wiz_report_graph_query": resourceWizReportGraphQuery(),
"wiz_project": resourceWizProject(),
"wiz_saml_idp": resourceWizSAMLIdP(),
"wiz_security_framework": resourceWizSecurityFramework(),
Expand Down
66 changes: 66 additions & 0 deletions internal/provider/resource_report.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package provider

import (
"context"

"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"wiz.io/hashicorp/terraform-provider-wiz/internal/client"
"wiz.io/hashicorp/terraform-provider-wiz/internal/wiz"
)

// CreateReport struct
type CreateReport struct {
CreateReport wiz.CreateReportPayload `json:"createReport"`
}

// UpdateReport struct
type UpdateReport struct {
UpdateReport wiz.Report `json:"updateReport"`
}

// DeleteReport struct
type DeleteReport struct {
DeleteReport wiz.DeleteReportPayload `json:"deleteReport"`
}

// ReadReportPayload struct -- updates
type ReadReportPayload struct {
Report wiz.Report `json:"report"`
}

func resourceWizReportDelete(ctx context.Context, d *schema.ResourceData, m interface{}) (diags diag.Diagnostics) {
tflog.Info(ctx, "resourceWizReportDelete called...")

// check the id
if d.Id() == "" {
return nil
}

// define the graphql query
query := `mutation DeleteReport (
$input: DeleteReportInput!
) {
deleteReport(
input: $input
) {
_stub
}
}`

// populate the graphql variables
vars := &wiz.DeleteReportInput{}
vars.ID = d.Id()

// process the request
data := &UpdateReport{}
requestDiags := client.ProcessRequest(ctx, m, vars, data, query, "report", "delete")
diags = append(diags, requestDiags...)
if len(diags) > 0 {
return diags
}

return diags
}
Loading