Skip to content

Commit 35efd3c

Browse files
BE-468: Terraform support for Scan Budget APIs
1 parent 971d6c2 commit 35efd3c

File tree

3 files changed

+268
-0
lines changed

3 files changed

+268
-0
lines changed

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ func Provider() terraform.ResourceProvider {
121121
"sumologic_rum_source": resourceSumologicRumSource(),
122122
"sumologic_role_v2": resourceSumologicRoleV2(),
123123
"sumologic_azure_event_hub_log_source": resourceSumologicGenericPollingSource(),
124+
"sumologic_scan_budget": resourceSumologicScanBudget(),
124125
},
125126
DataSourcesMap: map[string]*schema.Resource{
126127
"sumologic_cse_log_mapping_vendor_product": dataSourceCSELogMappingVendorAndProduct(),
Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
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+
}

sumologic/sumologic_scan_budget.go

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
package sumologic
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
)
7+
8+
func (s *Client) CreateScanBudget(scanBudget ScanBudget) (string, error) {
9+
urlWithoutParams := "v1/budgets"
10+
11+
data, err := s.Post(urlWithoutParams, scanBudget)
12+
if err != nil {
13+
return "", err
14+
}
15+
16+
var createdScanBudget ScanBudget
17+
18+
err = json.Unmarshal(data, &createdScanBudget)
19+
if err != nil {
20+
return "", err
21+
}
22+
23+
return createdScanBudget.ID, nil
24+
}
25+
26+
func (s *Client) GetScanBudget(id string) (*ScanBudget, error) {
27+
urlWithoutParams := "v1/budgets/%s"
28+
paramString := ""
29+
sprintfArgs := []interface{}{}
30+
sprintfArgs = append(sprintfArgs, id)
31+
32+
urlWithParams := fmt.Sprintf(urlWithoutParams+paramString, sprintfArgs...)
33+
34+
data, _, err := s.Get(urlWithParams)
35+
if err != nil {
36+
return nil, err
37+
}
38+
if data == nil {
39+
return nil, nil
40+
}
41+
42+
var scanBudget ScanBudget
43+
44+
err = json.Unmarshal(data, &scanBudget)
45+
if err != nil {
46+
return nil, err
47+
}
48+
49+
return &scanBudget, nil
50+
51+
}
52+
53+
func (s *Client) DeleteScanBudget(id string) error {
54+
urlWithoutParams := "v1/budgets/%s"
55+
paramString := ""
56+
sprintfArgs := []interface{}{}
57+
sprintfArgs = append(sprintfArgs, id)
58+
59+
urlWithParams := fmt.Sprintf(urlWithoutParams+paramString, sprintfArgs...)
60+
61+
_, err := s.Delete(urlWithParams)
62+
63+
return err
64+
}
65+
66+
func (s *Client) UpdateScanBudget(scanBudget ScanBudget) error {
67+
urlWithoutParams := "v1/budgets/%s"
68+
paramString := ""
69+
sprintfArgs := []interface{}{}
70+
sprintfArgs = append(sprintfArgs, scanBudget.ID)
71+
72+
urlWithParams := fmt.Sprintf(urlWithoutParams+paramString, sprintfArgs...)
73+
74+
scanBudget.ID = ""
75+
76+
_, err := s.Put(urlWithParams, scanBudget)
77+
78+
return err
79+
80+
}
81+
82+
type ScanBudget struct {
83+
ID string `json:"id,omitempty"`
84+
OrgID string `json:"orgId"`
85+
Name string `json:"name"`
86+
Capacity int `json:"capacity"`
87+
Unit string `json:"unit"`
88+
BudgetType string `json:"budgetType,omitempty"`
89+
Window string `json:"window"`
90+
Grouping string `json:"applicableOn"`
91+
GroupingEntity string `json:"groupBy"`
92+
Action string `json:"action"`
93+
Scope map[string]interface{} `json:"scope"`
94+
Status string `json:"status"`
95+
}

0 commit comments

Comments
 (0)