Skip to content

Commit 7efdb65

Browse files
committed
chore: create org specific integration test
1 parent 0844bca commit 7efdb65

File tree

5 files changed

+350
-323
lines changed

5 files changed

+350
-323
lines changed

examples/resource_lacework_integration_gcp_org_agentless_scanning/main.tf

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@ terraform {
77
}
88

99
provider "lacework" {
10-
organization = true
10+
organization = true
1111
}
1212

13-
variable "name" {
13+
variable "integration_name" {
1414
type = string
15-
default = "GCP Agentless Scanning org_example"
15+
default = "GCP Agentless Scanning Example"
1616
}
1717

1818
variable "client_id" {
@@ -47,15 +47,15 @@ variable "integration_type" {
4747

4848
variable "project_id" {
4949
type = string
50-
default = "org-example-project-id"
50+
default = "example-project-id"
5151
}
5252

5353
variable "bucket_name" {
5454
type = string
5555
default = "storage bucket id"
5656
}
5757

58-
variable "scanning_project_id" {
58+
variable "scanning-project-id" {
5959
type = string
6060
default = "scanning-project-id"
6161
}
@@ -70,11 +70,6 @@ variable "filter_list" {
7070
default = ["proj1", "proj2"]
7171
}
7272

73-
variable "scan_frequency" {
74-
type = number
75-
default = 24
76-
}
77-
7873
variable "org_account_mappings" {
7974
type = list(object({
8075
default_lacework_account = string
@@ -88,7 +83,7 @@ variable "org_account_mappings" {
8883
}
8984

9085
resource "lacework_integration_gcp_agentless_scanning" "org_example" {
91-
name = var.name
86+
name = var.integration_name
9287
credentials {
9388
client_id = var.client_id
9489
client_email = var.client_email
@@ -99,12 +94,6 @@ resource "lacework_integration_gcp_agentless_scanning" "org_example" {
9994
resource_level = "ORGANIZATION"
10095
resource_id = "techally-test"
10196
bucket_name = var.bucket_name
102-
scanning_project_id = "gcp-lw-scanner"
103-
scan_frequency = var.scan_frequency
104-
scan_containers = true
105-
scan_host_vulnerabilities = true
106-
scan_multi_volume = false
107-
scan_stopped_instances = true
10897
query_text = var.query_text
10998
filter_list = var.filter_list
11099

@@ -125,33 +114,29 @@ resource "lacework_integration_gcp_agentless_scanning" "org_example" {
125114
}
126115

127116
output "name" {
128-
value = lacework_integration_gcp_agentless_scanning.org_example.name
117+
value = lacework_integration_gcp_agentless_scanning.example.name
129118
}
130119

131120
output "client_id" {
132-
value = lacework_integration_gcp_agentless_scanning.org_example.credentials[0].client_id
121+
value = lacework_integration_gcp_agentless_scanning.example.credentials[0].client_id
133122
}
134123

135124
output "client_email" {
136-
value = lacework_integration_gcp_agentless_scanning.org_example.credentials[0].client_email
125+
value = lacework_integration_gcp_agentless_scanning.example.credentials[0].client_email
137126
}
138127

139128
output "bucket_name" {
140-
value = lacework_integration_gcp_agentless_scanning.org_example.bucket_name
129+
value = lacework_integration_gcp_agentless_scanning.example.bucket_name
141130
}
142131

143132
output "scanning_project_id" {
144-
value = lacework_integration_gcp_agentless_scanning.org_example.scanning_project_id
133+
value = lacework_integration_gcp_agentless_scanning.example.scanning_project_id
145134
}
146135

147136
output "scan_frequency" {
148-
value = lacework_integration_gcp_agentless_scanning.org_example.scan_frequency
137+
value = lacework_integration_gcp_agentless_scanning.example.scan_frequency
149138
}
150139

151140
output "server_token" {
152-
value = lacework_integration_gcp_agentless_scanning.org_example.server_token
141+
value = lacework_integration_gcp_agentless_scanning.example.server_token
153142
}
154-
155-
output "org_account_mappings" {
156-
value = lacework_integration_gcp_agentless_scanning.org_example.org_account_mappings
157-
}

integration/resource_lacework_integration_gcp_agentless_scanning_test.go

Lines changed: 1 addition & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -20,58 +20,12 @@ func TestIntegrationGcpAgentlessScanningCreate(t *testing.T) {
2020
if assert.Nil(t, err, "this test requires you to set GOOGLE_CREDENTIALS environment variable") {
2121
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
2222
TerraformDir: "../examples/resource_lacework_integration_gcp_agentless_scanning",
23-
Vars: map[string]interface{}{
24-
"integration_name": integration_name,
25-
"client_id": gcreds.ClientID,
26-
"client_email": gcreds.ClientEmail,
27-
"private_key_id": gcreds.PrivateKeyID,
28-
"bucket_name": "storage bucket id",
29-
},
30-
EnvVars: map[string]string{
31-
"TF_VAR_private_key": gcreds.PrivateKey,
32-
"LW_API_TOKEN": LwApiToken,
33-
},
34-
})
35-
defer terraform.Destroy(t, terraformOptions)
36-
37-
// Create new Google Agentless Scanning integration
38-
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
39-
createData := GetGcpAgentlessScanningResponse(create)
40-
assert.Equal(t, integration_name, createData.Data.Name)
41-
42-
// Update Gcp integration
43-
terraformOptions.Vars["integration_name"] = update_integration_name
44-
45-
update := terraform.ApplyAndIdempotent(t, terraformOptions)
46-
updateData := GetGcpAgentlessScanningResponse(update)
47-
assert.Equal(t, update_integration_name, updateData.Data.Name)
48-
}
49-
}
50-
51-
func TestIntegrationGcpAgentlessOrgScanningCreate(t *testing.T) {
52-
gcreds, err := googleLoadDefaultCredentials()
53-
integration_name := "GCP Agentless Scanning Example Integration Test"
54-
update_integration_name := fmt.Sprintf("%s Updated", integration_name)
55-
if assert.Nil(t, err, "this test requires you to set GOOGLE_CREDENTIALS environment variable") {
56-
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
57-
TerraformDir: "../examples/resource_lacework_integration_gcp_org_agentless_scanning",
5823
Vars: map[string]interface{}{
5924
"name": integration_name,
6025
"client_id": gcreds.ClientID,
6126
"client_email": gcreds.ClientEmail,
6227
"private_key_id": gcreds.PrivateKeyID,
6328
"bucket_name": "storage bucket id",
64-
"org_account_mappings": []map[string]interface{}{
65-
{
66-
"default_lacework_account": "customerdemo",
67-
"mapping": []map[string]interface{}{
68-
{
69-
"lacework_account": "abc",
70-
"gcp_projects": []string{"lw-scanner-5"},
71-
},
72-
},
73-
},
74-
},
7529
},
7630
EnvVars: map[string]string{
7731
"TF_VAR_private_key": gcreds.PrivateKey,
@@ -86,24 +40,7 @@ func TestIntegrationGcpAgentlessOrgScanningCreate(t *testing.T) {
8640
assert.Equal(t, integration_name, createData.Data.Name)
8741

8842
// Update Gcp integration
89-
terraformOptions.Vars = map[string]interface{}{
90-
"name": update_integration_name,
91-
"client_id": gcreds.ClientID,
92-
"client_email": gcreds.ClientEmail,
93-
"private_key_id": gcreds.PrivateKeyID,
94-
"bucket_name": "storage bucket id",
95-
"org_account_mappings": []map[string]interface{}{
96-
{
97-
"default_lacework_account": "customerdemo",
98-
"mapping": []map[string]interface{}{
99-
{
100-
"lacework_account": "abc",
101-
"gcp_projects": []string{"lw-scanner-5"},
102-
},
103-
},
104-
},
105-
},
106-
}
43+
terraformOptions.Vars["integration_name"] = update_integration_name
10744

10845
update := terraform.ApplyAndIdempotent(t, terraformOptions)
10946
updateData := GetGcpAgentlessScanningResponse(update)
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package integration
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/gruntwork-io/terratest/modules/terraform"
8+
"github.com/stretchr/testify/assert"
9+
)
10+
11+
func TestIntegrationGcpAgentlessOrgScanningCreateAndUpdate(t *testing.T) {
12+
gcreds, err := googleLoadDefaultCredentials()
13+
integration_name := "GCP Org Agentless Scanning Example Integration Test"
14+
update_integration_name := fmt.Sprintf("%s Updated", integration_name)
15+
if assert.Nil(t, err, "this test requires you to set GOOGLE_CREDENTIALS environment variable") {
16+
terraformOptions := terraform.WithDefaultRetryableErrors(t, &terraform.Options{
17+
TerraformDir: "../examples/resource_lacework_integration_gcp_org_agentless_scanning",
18+
Vars: map[string]interface{}{
19+
"name": integration_name,
20+
"client_id": gcreds.ClientID,
21+
"client_email": gcreds.ClientEmail,
22+
"private_key_id": gcreds.PrivateKeyID,
23+
"bucket_name": "storage bucket id",
24+
"org_account_mappings": []map[string]interface{}{
25+
{
26+
"default_lacework_account": "customerdemo",
27+
"mapping": []map[string]interface{}{
28+
{
29+
"lacework_account": "abc",
30+
"gcp_projects": []string{"techally-test"},
31+
},
32+
},
33+
},
34+
},
35+
},
36+
EnvVars: map[string]string{
37+
"TF_VAR_private_key": gcreds.PrivateKey,
38+
"LW_API_TOKEN": LwApiToken,
39+
},
40+
})
41+
defer terraform.Destroy(t, terraformOptions)
42+
43+
// Create new Google Agentless Scanning integration
44+
create := terraform.InitAndApplyAndIdempotent(t, terraformOptions)
45+
createData := GetGcpAgentlessScanningResponse(create)
46+
assert.Equal(t, integration_name, createData.Data.Name)
47+
48+
// Update Gcp integration
49+
terraformOptions.Vars = map[string]interface{}{
50+
"name": update_integration_name,
51+
"client_id": gcreds.ClientID,
52+
"client_email": gcreds.ClientEmail,
53+
"private_key_id": gcreds.PrivateKeyID,
54+
"bucket_name": "storage bucket id",
55+
"org_account_mappings": []map[string]interface{}{
56+
{
57+
"default_lacework_account": "customerdemo",
58+
"mapping": []map[string]interface{}{
59+
{
60+
"lacework_account": "abc",
61+
"gcp_projects": []string{"techally-test"},
62+
},
63+
},
64+
},
65+
},
66+
}
67+
68+
update := terraform.ApplyAndIdempotent(t, terraformOptions)
69+
updateData := GetGcpAgentlessScanningResponse(update)
70+
assert.Equal(t, update_integration_name, updateData.Data.Name)
71+
}
72+
}

lacework/account_mapping_helper.go

Lines changed: 44 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import (
55
)
66

77
type accountMappingsFile struct {
8-
DefaultLaceworkAccount string `json:"defaultLaceworkAccountAws"`
8+
DefaultLaceworkAccount string `json:"defaultLaceworkAccount"`
99
Mappings map[string]interface{} `json:"integration_mappings"`
1010
}
1111

@@ -58,21 +58,57 @@ func flattenOrgAccountMappings(mappingFile *accountMappingsFile, mappingsType st
5858

5959
func flattenMappings(mappings map[string]interface{}, mappingsType string) *schema.Set {
6060
var (
61-
orgAccountMappingsSchema = awsCloudTrailIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
62-
mappingSchema = orgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
63-
accountsSchema = mappingSchema.Schema[mappingsType].Elem.(*schema.Schema)
64-
res = schema.NewSet(schema.HashResource(mappingSchema), []interface{}{})
61+
awsOrgAccountMappingsSchema = awsCloudTrailIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
62+
awsMappingSchema = awsOrgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
63+
awsAccountsSchema = awsMappingSchema.Schema[mappingsType].Elem.(*schema.Schema)
64+
awsRes = schema.NewSet(schema.HashResource(awsMappingSchema), []interface{}{})
6565
)
6666

6767
for laceworkAccount, m := range mappings {
6868
mappingValue := m.(map[string]interface{})
69-
res.Add(map[string]interface{}{
69+
awsRes.Add(map[string]interface{}{
7070
"lacework_account": laceworkAccount,
71-
mappingsType: schema.NewSet(schema.HashSchema(accountsSchema),
71+
mappingsType: schema.NewSet(schema.HashSchema(awsAccountsSchema),
7272
mappingValue[mappingsType].([]interface{}),
7373
),
7474
})
7575
}
7676

77-
return res
77+
return awsRes
78+
}
79+
80+
func flattenOrgGcpAccountMappings(mappingFile *accountMappingsFile, mappingsType string) []map[string]interface{} {
81+
orgAccMappings := make([]map[string]interface{}, 0, 1)
82+
83+
if mappingFile.Empty() {
84+
return orgAccMappings
85+
}
86+
87+
mappings := map[string]interface{}{
88+
"default_lacework_account": mappingFile.DefaultLaceworkAccount,
89+
"mapping": flattenGcpMappings(mappingFile.Mappings, mappingsType),
90+
}
91+
92+
orgAccMappings = append(orgAccMappings, mappings)
93+
return orgAccMappings
94+
}
95+
96+
func flattenGcpMappings(mappings map[string]interface{}, mappingsType string) *schema.Set {
97+
var (
98+
gcpOrgAccountMappingsSchema = gcpAgentlessScanningIntegrationSchema["org_account_mappings"].Elem.(*schema.Resource)
99+
gcpMappingSchema = gcpOrgAccountMappingsSchema.Schema["mapping"].Elem.(*schema.Resource)
100+
gcpAccountsSchema = gcpMappingSchema.Schema[mappingsType].Elem.(*schema.Schema)
101+
gcpRes = schema.NewSet(schema.HashResource(gcpMappingSchema), []interface{}{})
102+
)
103+
104+
for laceworkAccount, m := range mappings {
105+
mappingValue := m.(map[string]interface{})
106+
gcpRes.Add(map[string]interface{}{
107+
"lacework_account": laceworkAccount,
108+
mappingsType: schema.NewSet(schema.HashSchema(gcpAccountsSchema),
109+
mappingValue[mappingsType].([]interface{}),
110+
),
111+
})
112+
}
113+
return gcpRes
78114
}

0 commit comments

Comments
 (0)