Skip to content
This repository was archived by the owner on Jun 4, 2024. It is now read-only.

Commit 3699982

Browse files
tags and justifcations
1 parent 048e97a commit 3699982

14 files changed

+635
-2
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
package bridgecrew
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"log"
7+
"reflect"
8+
"strconv"
9+
"time"
10+
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
)
14+
15+
func dataSourceJustifications() *schema.Resource {
16+
return &schema.Resource{
17+
ReadContext: dataSourceJustificationsRead,
18+
Schema: map[string]*schema.Schema{
19+
"policyid": {
20+
Type: schema.TypeString,
21+
Required: true,
22+
},
23+
"accounts": {
24+
Type: schema.TypeList,
25+
Required: true,
26+
Elem: &schema.Schema{
27+
Type: schema.TypeString,
28+
},
29+
},
30+
"justifications": {
31+
Type: schema.TypeList,
32+
Computed: true,
33+
Elem: &schema.Resource{
34+
Schema: map[string]*schema.Schema{
35+
"customer": {
36+
Type: schema.TypeString,
37+
Required: true,
38+
},
39+
"id": {
40+
Type: schema.TypeString,
41+
Computed: true,
42+
},
43+
"date": {
44+
Type: schema.TypeInt,
45+
Computed: true,
46+
},
47+
"owner": {
48+
Type: schema.TypeString,
49+
Computed: true,
50+
},
51+
"comment": {
52+
Type: schema.TypeString,
53+
Computed: true,
54+
},
55+
"suppression_type": {
56+
Type: schema.TypeString,
57+
Computed: true,
58+
},
59+
"violation_id": {
60+
Type: schema.TypeString,
61+
Computed: true,
62+
},
63+
"origin": {
64+
Type: schema.TypeString,
65+
Computed: true,
66+
},
67+
"active": {
68+
Type: schema.TypeBool,
69+
Computed: true,
70+
},
71+
"type": {
72+
Type: schema.TypeString,
73+
Computed: true,
74+
},
75+
},
76+
},
77+
},
78+
},
79+
}
80+
}
81+
82+
func dataSourceJustificationsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
83+
84+
policyid := d.Get("policyid").(string)
85+
accounts := d.Get("accounts").([]interface{})
86+
87+
var query string
88+
for i, account := range accounts {
89+
query = query + "accounts=" + account.(string)
90+
if i < (len(accounts) - 1) {
91+
query = query + "&"
92+
}
93+
}
94+
95+
url := "%s/suppressions/" + policyid + "/justifications?" + query
96+
97+
params := RequestParams{url, "v1", "GET"}
98+
99+
configure := m.(ProviderConfig)
100+
client, req, diagnostics, done := authClient(params, configure, nil)
101+
102+
if done {
103+
return diagnostics
104+
}
105+
106+
r, err := client.Do(req)
107+
108+
if err != nil {
109+
log.Fatal("Failed at client.Do")
110+
}
111+
112+
//goland:noinspection GoUnhandledErrorResult
113+
defer r.Body.Close()
114+
115+
Justifications := make([]map[string]interface{}, 0)
116+
err = json.NewDecoder(r.Body).Decode(&Justifications)
117+
118+
if err != nil {
119+
log.Fatal("Failed to parse data")
120+
}
121+
122+
flatJustice := flattenJustificationsData(&Justifications)
123+
124+
if err := d.Set("justifications", flatJustice); err != nil {
125+
log.Fatal(reflect.TypeOf(Justifications))
126+
}
127+
128+
// always run
129+
d.SetId(strconv.FormatInt(time.Now().Unix(), 10))
130+
131+
return diagnostics
132+
}
133+
134+
func flattenJustificationsData(Justifications *[]map[string]interface{}) []interface{} {
135+
if Justifications != nil {
136+
ois := make([]interface{}, len(*Justifications))
137+
138+
for i, Justify := range *Justifications {
139+
oi := make(map[string]interface{})
140+
141+
oi["customer"] = Justify["customer"]
142+
oi["id"] = Justify["id"]
143+
oi["date"] = Justify["date"].(float64)
144+
oi["owner"] = Justify["owner"]
145+
oi["comment"] = Justify["comment"]
146+
oi["suppression_type"] = Justify["suppressionType"]
147+
oi["violation_id"] = Justify["violationId"]
148+
oi["origin"] = Justify["origin"]
149+
oi["active"] = Justify["active"].(bool)
150+
oi["type"] = Justify["type"]
151+
152+
ois[i] = oi
153+
}
154+
155+
return ois
156+
}
157+
158+
return make([]interface{}, 0)
159+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package bridgecrew
2+
3+
import (
4+
"testing"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
)
8+
9+
func TestAccDataSourceJustifications(t *testing.T) {
10+
resource.ParallelTest(t, resource.TestCase{
11+
PreCheck: func() { testAccPreCheck(t) },
12+
ProviderFactories: testAccProviders,
13+
Steps: []resource.TestStep{
14+
{
15+
Config: testAccDataSourceJustifications(),
16+
Check: resource.ComposeAggregateTestCheckFunc(
17+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "accounts.0"),
18+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "id"),
19+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "policyid"),
20+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.active"),
21+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.comment"),
22+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.customer"),
23+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.date"),
24+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.id"),
25+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.origin"),
26+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.owner"),
27+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.suppression_type"),
28+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.type"),
29+
resource.TestCheckResourceAttrSet("data.bridgecrew_justifications.test", "justifications.0.violation_id"),
30+
),
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccDataSourceJustifications() string {
37+
return `
38+
data "bridgecrew_justifications" "test" {
39+
policyid="james_aws_1643121179054"
40+
accounts=["JamesWoolfenden/full-fast-fail", "JamesWoolfenden/terraform-aws-s3"]
41+
}`
42+
}

0 commit comments

Comments
 (0)