|
| 1 | +package sumologic |
| 2 | + |
| 3 | +import ( |
| 4 | + "log" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 7 | +) |
| 8 | + |
| 9 | +func resourceSumologicScanBudget() *schema.Resource { |
| 10 | + return &schema.Resource{ |
| 11 | + Create: resourceSumologicScanBudgetCreate, |
| 12 | + Read: resourceSumologicScanBudgetRead, |
| 13 | + Update: resourceSumologicScanBudgetUpdate, |
| 14 | + Delete: resourceSumologicScanBudgetDelete, |
| 15 | + Importer: &schema.ResourceImporter{ |
| 16 | + State: schema.ImportStatePassthrough, |
| 17 | + }, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + |
| 21 | + "name": { |
| 22 | + Type: schema.TypeString, |
| 23 | + Required: true, |
| 24 | + ForceNew: false, |
| 25 | + }, |
| 26 | + |
| 27 | + "orgId": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: false, |
| 31 | + }, |
| 32 | + |
| 33 | + "capacity": { |
| 34 | + Type: schema.TypeInt, |
| 35 | + Required: true, |
| 36 | + ForceNew: false, |
| 37 | + }, |
| 38 | + |
| 39 | + "unit": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Required: true, |
| 42 | + ForceNew: false, |
| 43 | + }, |
| 44 | + |
| 45 | + "budgetType": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ForceNew: false, |
| 49 | + }, |
| 50 | + |
| 51 | + "window": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + ForceNew: false, |
| 55 | + }, |
| 56 | + |
| 57 | + "groupBy": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + ForceNew: false, |
| 61 | + }, |
| 62 | + |
| 63 | + "applicableOn": { |
| 64 | + Type: schema.TypeString, |
| 65 | + Required: true, |
| 66 | + ForceNew: false, |
| 67 | + }, |
| 68 | + |
| 69 | + "scope": { |
| 70 | + Type: schema.TypeMap, |
| 71 | + Required: true, |
| 72 | + ForceNew: false, |
| 73 | + Elem: &schema.Schema{ |
| 74 | + Type: schema.TypeList, |
| 75 | + }, |
| 76 | + }, |
| 77 | + |
| 78 | + "action": { |
| 79 | + Type: schema.TypeString, |
| 80 | + Required: true, |
| 81 | + ForceNew: false, |
| 82 | + }, |
| 83 | + |
| 84 | + "status": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Required: true, |
| 87 | + ForceNew: false, |
| 88 | + }, |
| 89 | + }, |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func resourceSumologicScanBudgetCreate(d *schema.ResourceData, meta interface{}) error { |
| 94 | + c := meta.(*Client) |
| 95 | + |
| 96 | + if d.Id() == "" { |
| 97 | + scanBudget := resourceToScanBudget(d) |
| 98 | + id, err := c.CreateScanBudget(scanBudget) |
| 99 | + if err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + |
| 103 | + d.SetId(id) |
| 104 | + } |
| 105 | + |
| 106 | + return resourceSumologicScanBudgetRead(d, meta) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceSumologicScanBudgetRead(d *schema.ResourceData, meta interface{}) error { |
| 110 | + c := meta.(*Client) |
| 111 | + |
| 112 | + id := d.Id() |
| 113 | + ScanBudget, err := c.GetScanBudget(id) |
| 114 | + if err != nil { |
| 115 | + return err |
| 116 | + } |
| 117 | + |
| 118 | + if ScanBudget == nil { |
| 119 | + log.Printf("[WARN] ScanBudget not found, removing from state: %v - %v", id, err) |
| 120 | + d.SetId("") |
| 121 | + return nil |
| 122 | + } |
| 123 | + |
| 124 | + d.Set("name", ScanBudget.Name) |
| 125 | + d.Set("orgId", ScanBudget.OrgID) |
| 126 | + d.Set("capacity", ScanBudget.Capacity) |
| 127 | + d.Set("unit", ScanBudget.Unit) |
| 128 | + d.Set("budgetType", ScanBudget.BudgetType) |
| 129 | + d.Set("window", ScanBudget.Window) |
| 130 | + d.Set("applicableOn", ScanBudget.Grouping) |
| 131 | + d.Set("groupBy", ScanBudget.GroupingEntity) |
| 132 | + d.Set("action", ScanBudget.Action) |
| 133 | + d.Set("scope", ScanBudget.Scope) |
| 134 | + d.Set("status", ScanBudget.Status) |
| 135 | + |
| 136 | + return nil |
| 137 | +} |
| 138 | + |
| 139 | +func resourceSumologicScanBudgetDelete(d *schema.ResourceData, meta interface{}) error { |
| 140 | + c := meta.(*Client) |
| 141 | + |
| 142 | + return c.DeleteScanBudget(d.Id()) |
| 143 | +} |
| 144 | + |
| 145 | +func resourceSumologicScanBudgetUpdate(d *schema.ResourceData, meta interface{}) error { |
| 146 | + c := meta.(*Client) |
| 147 | + |
| 148 | + ScanBudget := resourceToScanBudget(d) |
| 149 | + err := c.UpdateScanBudget(ScanBudget) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + |
| 154 | + return resourceSumologicScanBudgetRead(d, meta) |
| 155 | +} |
| 156 | + |
| 157 | +func resourceToScanBudget(d *schema.ResourceData) ScanBudget { |
| 158 | + return ScanBudget{ |
| 159 | + ID: d.Id(), |
| 160 | + OrgID: d.Get("orgId").(string), |
| 161 | + Name: d.Get("name").(string), |
| 162 | + Capacity: d.Get("capacity").(int), |
| 163 | + Unit: d.Get("unit").(string), |
| 164 | + BudgetType: d.Get("budgetType").(string), |
| 165 | + Window: d.Get("window").(string), |
| 166 | + Grouping: d.Get("applicableOn").(string), |
| 167 | + GroupingEntity: d.Get("groupBy").(string), |
| 168 | + Action: d.Get("action").(string), |
| 169 | + Scope: d.Get("scope").(map[string]interface{}), |
| 170 | + Status: d.Get("status").(string), |
| 171 | + } |
| 172 | +} |
0 commit comments