|
| 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 | +} |
0 commit comments