|
| 1 | +// ---------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** |
| 4 | +// |
| 5 | +// ---------------------------------------------------------------------------- |
| 6 | +// |
| 7 | +// This file is automatically generated by Sumo Logic and manual |
| 8 | +// changes will be clobbered when the file is regenerated. Do not submit |
| 9 | +// changes to this file. |
| 10 | +// |
| 11 | +// ---------------------------------------------------------------------------- |
| 12 | +package sumologic |
| 13 | + |
| 14 | +import ( |
| 15 | + "log" |
| 16 | + |
| 17 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 18 | +) |
| 19 | + |
| 20 | +func resourceSumologicScanBudget() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Create: resourceSumologicScanBudgetCreate, |
| 23 | + Read: resourceSumologicScanBudgetRead, |
| 24 | + Update: resourceSumologicScanBudgetUpdate, |
| 25 | + Delete: resourceSumologicScanBudgetDelete, |
| 26 | + Importer: &schema.ResourceImporter{ |
| 27 | + State: schema.ImportStatePassthrough, |
| 28 | + }, |
| 29 | + |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + |
| 32 | + "name": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ForceNew: false, |
| 36 | + }, |
| 37 | + |
| 38 | + "capacity": { |
| 39 | + Type: schema.TypeInt, |
| 40 | + Required: true, |
| 41 | + ForceNew: false, |
| 42 | + }, |
| 43 | + |
| 44 | + "unit": { |
| 45 | + Type: schema.TypeString, |
| 46 | + Required: true, |
| 47 | + ForceNew: false, |
| 48 | + }, |
| 49 | + |
| 50 | + "budget_type": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Required: true, |
| 53 | + ForceNew: false, |
| 54 | + }, |
| 55 | + |
| 56 | + "window": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Required: true, |
| 59 | + ForceNew: false, |
| 60 | + }, |
| 61 | + |
| 62 | + "group_by": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Required: true, |
| 65 | + ForceNew: false, |
| 66 | + }, |
| 67 | + |
| 68 | + "applicable_on": { |
| 69 | + Type: schema.TypeString, |
| 70 | + Required: true, |
| 71 | + ForceNew: false, |
| 72 | + }, |
| 73 | + |
| 74 | + "scope": { |
| 75 | + Type: schema.TypeList, |
| 76 | + Required: true, |
| 77 | + ForceNew: false, |
| 78 | + MaxItems: 1, |
| 79 | + Elem: &schema.Resource{ |
| 80 | + Schema: map[string]*schema.Schema{ |
| 81 | + "included_users": { |
| 82 | + Type: schema.TypeList, |
| 83 | + Optional: true, |
| 84 | + Elem: &schema.Schema{ |
| 85 | + Type: schema.TypeString, |
| 86 | + }, |
| 87 | + }, |
| 88 | + "excluded_users": { |
| 89 | + Type: schema.TypeList, |
| 90 | + Optional: true, |
| 91 | + Elem: &schema.Schema{ |
| 92 | + Type: schema.TypeString, |
| 93 | + }, |
| 94 | + }, |
| 95 | + "included_roles": { |
| 96 | + Type: schema.TypeList, |
| 97 | + Optional: true, |
| 98 | + Elem: &schema.Schema{ |
| 99 | + Type: schema.TypeString, |
| 100 | + }, |
| 101 | + }, |
| 102 | + "excluded_roles": { |
| 103 | + Type: schema.TypeList, |
| 104 | + Optional: true, |
| 105 | + Elem: &schema.Schema{ |
| 106 | + Type: schema.TypeString, |
| 107 | + }, |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + |
| 113 | + "action": { |
| 114 | + Type: schema.TypeString, |
| 115 | + Required: true, |
| 116 | + ForceNew: false, |
| 117 | + }, |
| 118 | + |
| 119 | + "status": { |
| 120 | + Type: schema.TypeString, |
| 121 | + Required: true, |
| 122 | + ForceNew: false, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func resourceSumologicScanBudgetCreate(d *schema.ResourceData, meta interface{}) error { |
| 129 | + c := meta.(*Client) |
| 130 | + |
| 131 | + if d.Id() == "" { |
| 132 | + scanBudget := resourceToScanBudget(d) |
| 133 | + id, err := c.CreateScanBudget(scanBudget) |
| 134 | + if err != nil { |
| 135 | + return err |
| 136 | + } |
| 137 | + |
| 138 | + d.SetId(id) |
| 139 | + } |
| 140 | + |
| 141 | + return resourceSumologicScanBudgetRead(d, meta) |
| 142 | +} |
| 143 | + |
| 144 | +func resourceSumologicScanBudgetRead(d *schema.ResourceData, meta interface{}) error { |
| 145 | + c := meta.(*Client) |
| 146 | + |
| 147 | + id := d.Id() |
| 148 | + scanBudget, err := c.GetScanBudget(id) |
| 149 | + if err != nil { |
| 150 | + return err |
| 151 | + } |
| 152 | + |
| 153 | + if scanBudget == nil { |
| 154 | + log.Printf("[WARN] ScanBudget not found, removing from state: %v - %v", id, err) |
| 155 | + d.SetId("") |
| 156 | + return nil |
| 157 | + } |
| 158 | + |
| 159 | + d.Set("name", scanBudget.Name) |
| 160 | + d.Set("capacity", scanBudget.Capacity) |
| 161 | + d.Set("unit", scanBudget.Unit) |
| 162 | + d.Set("budget_type", scanBudget.BudgetType) |
| 163 | + d.Set("window", scanBudget.Window) |
| 164 | + d.Set("applicable_on", scanBudget.ApplicableOn) |
| 165 | + d.Set("group_by", scanBudget.GroupBy) |
| 166 | + d.Set("action", scanBudget.Action) |
| 167 | + d.Set("scope", scanBudgetScopeToResource(scanBudget.Scope)) |
| 168 | + d.Set("status", scanBudget.Status) |
| 169 | + |
| 170 | + return nil |
| 171 | +} |
| 172 | + |
| 173 | +func resourceSumologicScanBudgetDelete(d *schema.ResourceData, meta interface{}) error { |
| 174 | + c := meta.(*Client) |
| 175 | + |
| 176 | + return c.DeleteScanBudget(d.Id()) |
| 177 | +} |
| 178 | + |
| 179 | +func resourceSumologicScanBudgetUpdate(d *schema.ResourceData, meta interface{}) error { |
| 180 | + c := meta.(*Client) |
| 181 | + |
| 182 | + scanBudget := resourceToScanBudget(d) |
| 183 | + err := c.UpdateScanBudget(scanBudget) |
| 184 | + if err != nil { |
| 185 | + return err |
| 186 | + } |
| 187 | + |
| 188 | + return resourceSumologicScanBudgetRead(d, meta) |
| 189 | +} |
| 190 | + |
| 191 | +func resourceToScanBudget(d *schema.ResourceData) ScanBudget { |
| 192 | + return ScanBudget{ |
| 193 | + ID: d.Id(), |
| 194 | + Name: d.Get("name").(string), |
| 195 | + Capacity: d.Get("capacity").(int), |
| 196 | + Unit: d.Get("unit").(string), |
| 197 | + BudgetType: d.Get("budget_type").(string), |
| 198 | + Window: d.Get("window").(string), |
| 199 | + ApplicableOn: d.Get("applicable_on").(string), |
| 200 | + GroupBy: d.Get("group_by").(string), |
| 201 | + Action: d.Get("action").(string), |
| 202 | + Scope: resourceToScanBudgetScope(d.Get("scope")), |
| 203 | + Status: d.Get("status").(string), |
| 204 | + } |
| 205 | +} |
| 206 | + |
| 207 | +func resourceToScanBudgetScope(data interface{}) ScanBudgetScope { |
| 208 | + scopeMap, ok := data.([]interface{})[0].(map[string]interface{}) |
| 209 | + if !ok { |
| 210 | + return ScanBudgetScope{} |
| 211 | + } |
| 212 | + |
| 213 | + scanBudgetScope := ScanBudgetScope{ |
| 214 | + IncludedUsers: convertToStringSlice(scopeMap["included_users"]), |
| 215 | + ExcludedUsers: convertToStringSlice(scopeMap["excluded_users"]), |
| 216 | + IncludedRoles: convertToStringSlice(scopeMap["included_roles"]), |
| 217 | + ExcludedRoles: convertToStringSlice(scopeMap["excluded_roles"]), |
| 218 | + } |
| 219 | + |
| 220 | + return scanBudgetScope |
| 221 | +} |
| 222 | + |
| 223 | +func scanBudgetScopeToResource(scanBudgetScope ScanBudgetScope) []map[string]interface{} { |
| 224 | + return []map[string]interface{}{ |
| 225 | + { |
| 226 | + "included_users": scanBudgetScope.IncludedUsers, |
| 227 | + "excluded_users": scanBudgetScope.ExcludedUsers, |
| 228 | + "included_roles": scanBudgetScope.IncludedRoles, |
| 229 | + "excluded_roles": scanBudgetScope.ExcludedRoles, |
| 230 | + }, |
| 231 | + } |
| 232 | +} |
| 233 | + |
| 234 | +func convertToStringSlice(data interface{}) []string { |
| 235 | + if data == nil { |
| 236 | + return nil |
| 237 | + } |
| 238 | + |
| 239 | + interfaceSlice, ok := data.([]interface{}) |
| 240 | + if !ok { |
| 241 | + return nil |
| 242 | + } |
| 243 | + |
| 244 | + stringSlice := make([]string, len(interfaceSlice)) |
| 245 | + for i, v := range interfaceSlice { |
| 246 | + str, ok := v.(string) |
| 247 | + if ok { |
| 248 | + stringSlice[i] = str |
| 249 | + } |
| 250 | + } |
| 251 | + |
| 252 | + return stringSlice |
| 253 | +} |
0 commit comments