Skip to content

Commit 817ab7b

Browse files
authored
Merge pull request #276 from kornypoet/enable-admin-mode
Enable admin mode in provider
2 parents b12f5b0 + 0ea1575 commit 817ab7b

37 files changed

+156
-122
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
## 2.10.0 (September 22, 2021)
44

5+
* Add a provider option `admin_mode`
6+
57
FEATURES:
68

79
* **New Resource:** sumologic_hierarchy (GH-260)

sumologic/data_source_sumologic_role.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ func dataSourceSumologicRoleRead(d *schema.ResourceData, meta interface{}) error
8383
}
8484

8585
func (s *Client) GetRoleName(name string) (*Role, error) {
86-
data, _, err := s.Get(fmt.Sprintf("v1/roles?name=%s", url.QueryEscape(name)), false)
86+
data, _, err := s.Get(fmt.Sprintf("v1/roles?name=%s", url.QueryEscape(name)))
8787
if err != nil {
8888
return nil, err
8989
}

sumologic/provider.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ func Provider() terraform.ResourceProvider {
3636
Optional: true,
3737
Default: os.Getenv("SUMOLOGIC_BASE_URL"),
3838
},
39+
"admin_mode": {
40+
Type: schema.TypeBool,
41+
Optional: true,
42+
Default: false,
43+
},
3944
},
4045
ResourcesMap: map[string]*schema.Resource{
4146
"sumologic_cse_network_block": resourceSumologicCSENetworkBlock(),
@@ -118,6 +123,7 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
118123
accessKey := d.Get("access_key").(string)
119124
environment := d.Get("environment").(string)
120125
baseUrl := d.Get("base_url").(string)
126+
isInAdminMode := d.Get("admin_mode").(bool)
121127

122128
msg := ""
123129
if accessId == "" {
@@ -152,5 +158,6 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) {
152158
accessKey,
153159
environment,
154160
baseUrl,
161+
isInAdminMode,
155162
)
156163
}

sumologic/sumologic_client.go

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,12 @@ type HttpClient interface {
1717
}
1818

1919
type Client struct {
20-
AccessID string
21-
AccessKey string
22-
Environment string
23-
BaseURL *url.URL
24-
httpClient HttpClient
20+
AccessID string
21+
AccessKey string
22+
Environment string
23+
BaseURL *url.URL
24+
IsInAdminMode bool
25+
httpClient HttpClient
2526
}
2627

2728
var ProviderVersion string
@@ -128,7 +129,7 @@ func (s *Client) GetWithCookies(urlPath string, cookies []*http.Cookie) ([]byte,
128129
return d, resp.Header.Get("ETag"), nil
129130
}
130131

131-
func (s *Client) Post(urlPath string, payload interface{}, isAdminMode bool) ([]byte, error) {
132+
func (s *Client) Post(urlPath string, payload interface{}) ([]byte, error) {
132133
relativeURL, _ := url.Parse(urlPath)
133134
sumoURL := s.BaseURL.ResolveReference(relativeURL)
134135

@@ -138,7 +139,7 @@ func (s *Client) Post(urlPath string, payload interface{}, isAdminMode bool) ([]
138139
return nil, err
139140
}
140141

141-
if isAdminMode {
142+
if s.IsInAdminMode {
142143
req.Header.Add("isAdminMode", "true")
143144
}
144145

@@ -185,11 +186,11 @@ func (s *Client) PostRawPayload(urlPath string, payload string) ([]byte, error)
185186
return d, nil
186187
}
187188

188-
func (s *Client) Put(urlPath string, payload interface{}, isAdminMode bool) ([]byte, error) {
189+
func (s *Client) Put(urlPath string, payload interface{}) ([]byte, error) {
189190
relativeURL, _ := url.Parse(urlPath)
190191
sumoURL := s.BaseURL.ResolveReference(relativeURL)
191192

192-
_, etag, _ := s.Get(sumoURL.String(), false)
193+
_, etag, _ := s.Get(sumoURL.String())
193194

194195
body, _ := json.Marshal(payload)
195196
req, err := createNewRequest(http.MethodPut, sumoURL.String(), bytes.NewBuffer(body), s.AccessID, s.AccessKey)
@@ -198,7 +199,7 @@ func (s *Client) Put(urlPath string, payload interface{}, isAdminMode bool) ([]b
198199
}
199200
req.Header.Add("If-Match", etag)
200201

201-
if isAdminMode {
202+
if s.IsInAdminMode {
202203
req.Header.Add("isAdminMode", "true")
203204
}
204205

@@ -221,7 +222,7 @@ func (s *Client) Put(urlPath string, payload interface{}, isAdminMode bool) ([]b
221222
return d, nil
222223
}
223224

224-
func (s *Client) Get(urlPath string, isAdminMode bool) ([]byte, string, error) {
225+
func (s *Client) Get(urlPath string) ([]byte, string, error) {
225226
relativeURL, _ := url.Parse(urlPath)
226227
sumoURL := s.BaseURL.ResolveReference(relativeURL)
227228

@@ -230,7 +231,7 @@ func (s *Client) Get(urlPath string, isAdminMode bool) ([]byte, string, error) {
230231
return nil, "", err
231232
}
232233

233-
if isAdminMode {
234+
if s.IsInAdminMode {
234235
req.Header.Add("isAdminMode", "true")
235236
}
236237

@@ -264,6 +265,10 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
264265
return nil, err
265266
}
266267

268+
if s.IsInAdminMode {
269+
req.Header.Add("isAdminMode", "true")
270+
}
271+
267272
<-rateLimiter.C
268273
resp, err := s.httpClient.Do(req)
269274
if err != nil {
@@ -283,12 +288,13 @@ func (s *Client) Delete(urlPath string) ([]byte, error) {
283288
return d, nil
284289
}
285290

286-
func NewClient(accessID, accessKey, environment, base_url string) (*Client, error) {
291+
func NewClient(accessID, accessKey, environment, base_url string, admin bool) (*Client, error) {
287292
client := Client{
288-
AccessID: accessID,
289-
AccessKey: accessKey,
290-
httpClient: http.DefaultClient,
291-
Environment: environment,
293+
AccessID: accessID,
294+
AccessKey: accessKey,
295+
httpClient: http.DefaultClient,
296+
Environment: environment,
297+
IsInAdminMode: admin,
292298
}
293299

294300
if base_url == "" {

sumologic/sumologic_cloud_to_cloud_source.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (s *Client) CreateCloudToCloudSource(source CloudToCloudSource, collectorID
2828

2929
urlPath := fmt.Sprintf("v1/collectors/%d/sources", collectorID)
3030

31-
body, err := s.Post(urlPath, request, false)
31+
body, err := s.Post(urlPath, request)
3232

3333
if err != nil {
3434
return -1, err
@@ -46,7 +46,7 @@ func (s *Client) CreateCloudToCloudSource(source CloudToCloudSource, collectorID
4646

4747
func (s *Client) GetCloudToCloudSource(collectorID, sourceID int) (*CloudToCloudSource, error) {
4848
urlPath := fmt.Sprintf("v1/collectors/%d/sources/%d", collectorID, sourceID)
49-
body, _, err := s.Get(urlPath, false)
49+
body, _, err := s.Get(urlPath)
5050

5151
if err != nil {
5252
return nil, err
@@ -81,7 +81,7 @@ func (s *Client) UpdateCloudToCloudSource(source CloudToCloudSource, collectorID
8181
Source: source,
8282
}
8383

84-
_, err := s.Put(url, request, false)
84+
_, err := s.Put(url, request)
8585

8686
return err
8787
}

sumologic/sumologic_cloudsyslog_source.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ func (s *Client) CreateCloudsyslogSource(cloudSyslogSource CloudSyslogSource, co
2121
}
2222

2323
urlPath := fmt.Sprintf("v1/collectors/%d/sources", collectorID)
24-
body, err := s.Post(urlPath, request, false)
24+
body, err := s.Post(urlPath, request)
2525

2626
if err != nil {
2727
return -1, err
@@ -39,7 +39,7 @@ func (s *Client) CreateCloudsyslogSource(cloudSyslogSource CloudSyslogSource, co
3939

4040
func (s *Client) GetCloudSyslogSource(collectorID, sourceID int) (*CloudSyslogSource, error) {
4141

42-
body, _, err := s.Get(fmt.Sprintf("v1/collectors/%d/sources/%d", collectorID, sourceID), false)
42+
body, _, err := s.Get(fmt.Sprintf("v1/collectors/%d/sources/%d", collectorID, sourceID))
4343
if err != nil {
4444
return nil, err
4545
}
@@ -73,7 +73,7 @@ func (s *Client) UpdateCloudSyslogSource(source CloudSyslogSource, collectorID i
7373
}
7474

7575
urlPath := fmt.Sprintf("v1/collectors/%d/sources/%d", collectorID, source.ID)
76-
_, err := s.Put(urlPath, request, false)
76+
_, err := s.Put(urlPath, request)
7777

7878
return err
7979
}

sumologic/sumologic_collectors.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func (s *Client) GetCollector(id int) (*Collector, error) {
9-
data, _, err := s.Get(fmt.Sprintf("v1/collectors/%d", id), false)
9+
data, _, err := s.Get(fmt.Sprintf("v1/collectors/%d", id))
1010
if err != nil {
1111
return nil, err
1212
}
@@ -25,7 +25,7 @@ func (s *Client) GetCollector(id int) (*Collector, error) {
2525
}
2626

2727
func (s *Client) GetCollectorName(name string) (*Collector, error) {
28-
data, _, err := s.Get(fmt.Sprintf("v1/collectors/name/%s", name), false)
28+
data, _, err := s.Get(fmt.Sprintf("v1/collectors/name/%s", name))
2929
if err != nil {
3030
return nil, err
3131
}
@@ -57,7 +57,7 @@ func (s *Client) CreateCollector(collector Collector) (int64, error) {
5757

5858
var response CollectorResponse
5959

60-
responseBody, err := s.Post("v1/collectors", request, false)
60+
responseBody, err := s.Post("v1/collectors", request)
6161
if err != nil {
6262
return -1, err
6363
}
@@ -78,7 +78,7 @@ func (s *Client) UpdateCollector(collector Collector) error {
7878
Collector: collector,
7979
}
8080

81-
_, err := s.Put(url, request, false)
81+
_, err := s.Put(url, request)
8282

8383
return err
8484
}

sumologic/sumologic_connection.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ func (s *Client) GetConnection(id string) (*Connection, error) {
1414
url := fmt.Sprintf("v1/connections/%s", id)
1515
log.Printf("connection read url: %s", url)
1616

17-
rawConnection, _, err := s.Get(url, false)
17+
rawConnection, _, err := s.Get(url)
1818
if err != nil {
1919
log.Printf("SSAIN: The err: %s", err.Error())
2020
if strings.Contains(err.Error(), "Connection with given ID does not exist.") {
@@ -60,7 +60,7 @@ func (s *Client) CreateConnection(connection Connection) (string, error) {
6060
log.Printf("Create connection url: %s", url)
6161

6262
connection.Type = convertConToDef(connection.Type)
63-
responseData, err := s.Post(url, connection, false)
63+
responseData, err := s.Post(url, connection)
6464
if err != nil {
6565
return "", err
6666
}
@@ -83,7 +83,7 @@ func (s *Client) UpdateConnection(connection Connection) error {
8383
log.Printf("Update connection job status url: %s", url)
8484

8585
connection.Type = convertConToDef(connection.Type)
86-
_, err := s.Put(url, connection, false)
86+
_, err := s.Put(url, connection)
8787

8888
log.Println("#### End connection update ####")
8989
return err

sumologic/sumologic_content.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func (s *Client) GetContent(id string, timeout time.Duration) (*Content, error)
1616
log.Printf("[DEBUG] Exporting content with id: %s", id)
1717

1818
// Begin the content export job
19-
rawJID, err := s.Post(url, nil, false)
19+
rawJID, err := s.Post(url, nil)
2020
if err != nil {
2121
if strings.Contains(err.Error(), "Content with the given ID does not exist.") {
2222
return nil, nil
@@ -41,7 +41,7 @@ func (s *Client) GetContent(id string, timeout time.Duration) (*Content, error)
4141
// Request the results of the job
4242
var content Content
4343
url = fmt.Sprintf("v2/content/%s/export/%s/result", id, jid.ID)
44-
rawContent, _, err := s.Get(url, false)
44+
rawContent, _, err := s.Get(url)
4545
if err != nil {
4646
return nil, err
4747
}
@@ -115,7 +115,7 @@ func waitForJob(url string, timeout time.Duration, s *Client) (*Status, error) {
115115
},
116116
Refresh: func() (interface{}, string, error) {
117117
var status Status
118-
b, _, err := s.Get(url, false)
118+
b, _, err := s.Get(url)
119119
if err != nil {
120120
return nil, "", err
121121
}

sumologic/sumologic_cse_network_block.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
)
77

88
func (s *Client) GetCSENetworkBlock(id string) (*CSENetworkBlock, error) {
9-
data, _, err := s.Get(fmt.Sprintf("sec/v1/network-blocks/%s", id), false)
9+
data, _, err := s.Get(fmt.Sprintf("sec/v1/network-blocks/%s", id))
1010
if err != nil {
1111
return nil, err
1212
}
@@ -38,7 +38,7 @@ func (s *Client) CreateCSENetworkBlock(cseNetworkBlock CSENetworkBlock) (string,
3838

3939
var response CSENetworkBlockResponse
4040

41-
responseBody, err := s.Post("sec/v1/network-blocks", request, false)
41+
responseBody, err := s.Post("sec/v1/network-blocks", request)
4242
if err != nil {
4343
return "", err
4444
}
@@ -59,7 +59,7 @@ func (s *Client) UpdateCSENetworkBlock(cseNetworkBlock CSENetworkBlock) error {
5959
CSENetworkBlock: cseNetworkBlock,
6060
}
6161

62-
_, err := s.Put(url, request, false)
62+
_, err := s.Put(url, request)
6363

6464
return err
6565
}

0 commit comments

Comments
 (0)