Skip to content

Commit b70e1ac

Browse files
committed
feat: Resource sysdig_user is now using a common client
Signed-off-by: Federico Barcelona <[email protected]>
1 parent 17d7003 commit b70e1ac

File tree

8 files changed

+139
-51
lines changed

8 files changed

+139
-51
lines changed

sysdig/common/client.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package common
2+
3+
import (
4+
"crypto/tls"
5+
"io"
6+
"log"
7+
"net/http"
8+
"net/http/httputil"
9+
)
10+
11+
type SysdigCommonClient interface {
12+
CreateUser(User) (User, error)
13+
GetUserById(int) (User, error)
14+
DeleteUser(int) error
15+
UpdateUser(User) (User, error)
16+
}
17+
18+
func NewSysdigCommonClient(sysdigAPIToken string, url string, insecure bool) SysdigCommonClient {
19+
httpClient := &http.Client{
20+
Transport: &http.Transport{
21+
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
22+
},
23+
}
24+
25+
return &sysdigCommonClient{
26+
SysdigAPIToken: sysdigAPIToken,
27+
URL: url,
28+
httpClient: httpClient,
29+
}
30+
}
31+
32+
type sysdigCommonClient struct {
33+
SysdigAPIToken string
34+
URL string
35+
httpClient *http.Client
36+
}
37+
38+
func (client *sysdigCommonClient) doSysdigCommonRequest(method string, url string, payload io.Reader) (*http.Response, error) {
39+
request, _ := http.NewRequest(method, url, payload)
40+
request.Header.Set("Authorization", "Bearer "+client.SysdigAPIToken)
41+
request.Header.Set("Content-Type", "application/json")
42+
43+
out, _ := httputil.DumpRequestOut(request, true)
44+
log.Printf("[DEBUG] %s", string(out))
45+
response, error := client.httpClient.Do(request)
46+
47+
out, _ = httputil.DumpResponse(response, true)
48+
log.Printf("[DEBUG] %s", string(out))
49+
return response, error
50+
}

sysdig/common/models.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package common
2+
3+
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
7+
)
8+
9+
// -------- User --------
10+
type User struct {
11+
ID int `json:"id,omitempty"`
12+
Version int `json:"version,omitempty"`
13+
SystemRole string `json:"systemRole,omitempty"`
14+
Email string `json:"username"`
15+
FirstName string `json:"firstName,omitempty"`
16+
LastName string `json:"lastName,omitempty"`
17+
}
18+
19+
func (u *User) ToJSON() io.Reader {
20+
payload, _ := json.Marshal(*u)
21+
return bytes.NewBuffer(payload)
22+
}
23+
24+
func UserFromJSON(body []byte) User {
25+
var result userWrapper
26+
json.Unmarshal(body, &result)
27+
28+
return result.User
29+
}
30+
31+
type userWrapper struct {
32+
User User `json:"user"`
33+
}

sysdig/secure/users.go renamed to sysdig/common/users.go

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package secure
1+
package common
22

33
import (
44
"errors"
@@ -7,8 +7,8 @@ import (
77
"net/http"
88
)
99

10-
func (client *sysdigSecureClient) GetUserById(id int) (u User, err error) {
11-
response, err := client.doSysdigSecureRequest(http.MethodGet, client.GetUserUrl(id), nil)
10+
func (client *sysdigCommonClient) GetUserById(id int) (u User, err error) {
11+
response, err := client.doSysdigCommonRequest(http.MethodGet, client.GetUserUrl(id), nil)
1212
if err != nil {
1313
return
1414
}
@@ -26,8 +26,8 @@ func (client *sysdigSecureClient) GetUserById(id int) (u User, err error) {
2626
return
2727
}
2828

29-
func (client *sysdigSecureClient) CreateUser(uRequest User) (u User, err error) {
30-
response, err := client.doSysdigSecureRequest(http.MethodPost, client.GetUsersUrl(), uRequest.ToJSON())
29+
func (client *sysdigCommonClient) CreateUser(uRequest User) (u User, err error) {
30+
response, err := client.doSysdigCommonRequest(http.MethodPost, client.GetUsersUrl(), uRequest.ToJSON())
3131

3232
if err != nil {
3333
return
@@ -45,8 +45,8 @@ func (client *sysdigSecureClient) CreateUser(uRequest User) (u User, err error)
4545
return
4646
}
4747

48-
func (client *sysdigSecureClient) UpdateUser(uRequest User) (u User, err error) {
49-
response, err := client.doSysdigSecureRequest(http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
48+
func (client *sysdigCommonClient) UpdateUser(uRequest User) (u User, err error) {
49+
response, err := client.doSysdigCommonRequest(http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
5050
if err != nil {
5151
return
5252
}
@@ -63,8 +63,8 @@ func (client *sysdigSecureClient) UpdateUser(uRequest User) (u User, err error)
6363
return
6464
}
6565

66-
func (client *sysdigSecureClient) DeleteUser(id int) error {
67-
response, err := client.doSysdigSecureRequest(http.MethodDelete, client.GetUserUrl(id), nil)
66+
func (client *sysdigCommonClient) DeleteUser(id int) error {
67+
response, err := client.doSysdigCommonRequest(http.MethodDelete, client.GetUserUrl(id), nil)
6868
if err != nil {
6969
return err
7070
}
@@ -76,10 +76,10 @@ func (client *sysdigSecureClient) DeleteUser(id int) error {
7676
return nil
7777
}
7878

79-
func (client *sysdigSecureClient) GetUsersUrl() string {
79+
func (client *sysdigCommonClient) GetUsersUrl() string {
8080
return fmt.Sprintf("%s/api/users", client.URL)
8181
}
8282

83-
func (client *sysdigSecureClient) GetUserUrl(id int) string {
83+
func (client *sysdigCommonClient) GetUserUrl(id int) string {
8484
return fmt.Sprintf("%s/api/users/%d", client.URL, id)
8585
}

sysdig/resource_sysdig_user.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package sysdig
22

33
import (
4-
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
4+
"github.com/draios/terraform-provider-sysdig/sysdig/common"
55
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
66
"strconv"
77
"time"
@@ -47,7 +47,7 @@ func resourceSysdigUser() *schema.Resource {
4747
}
4848

4949
func resourceSysdigUserCreate(d *schema.ResourceData, meta interface{}) error {
50-
client, err := meta.(SysdigClients).sysdigSecureClient()
50+
client, err := meta.(SysdigClients).sysdigCommonClient()
5151
if err != nil {
5252
return err
5353
}
@@ -67,7 +67,7 @@ func resourceSysdigUserCreate(d *schema.ResourceData, meta interface{}) error {
6767

6868
// Retrieves the information of a resource form the file and loads it in Terraform
6969
func resourceSysdigUserRead(d *schema.ResourceData, meta interface{}) error {
70-
client, err := meta.(SysdigClients).sysdigSecureClient()
70+
client, err := meta.(SysdigClients).sysdigCommonClient()
7171
if err != nil {
7272
return err
7373
}
@@ -90,7 +90,7 @@ func resourceSysdigUserRead(d *schema.ResourceData, meta interface{}) error {
9090
}
9191

9292
func resourceSysdigUserUpdate(d *schema.ResourceData, meta interface{}) error {
93-
client, err := meta.(SysdigClients).sysdigSecureClient()
93+
client, err := meta.(SysdigClients).sysdigCommonClient()
9494
if err != nil {
9595
return err
9696
}
@@ -106,7 +106,7 @@ func resourceSysdigUserUpdate(d *schema.ResourceData, meta interface{}) error {
106106
}
107107

108108
func resourceSysdigUserDelete(d *schema.ResourceData, meta interface{}) error {
109-
client, err := meta.(SysdigClients).sysdigSecureClient()
109+
client, err := meta.(SysdigClients).sysdigCommonClient()
110110
if err != nil {
111111
return err
112112
}
@@ -116,8 +116,8 @@ func resourceSysdigUserDelete(d *schema.ResourceData, meta interface{}) error {
116116
return client.DeleteUser(id)
117117
}
118118

119-
func userFromResourceData(d *schema.ResourceData) (u secure.User) {
120-
u = secure.User{
119+
func userFromResourceData(d *schema.ResourceData) (u common.User) {
120+
u = common.User{
121121
SystemRole: d.Get("system_role").(string),
122122
Email: d.Get("email").(string),
123123
FirstName: d.Get("first_name").(string),

sysdig/resource_sysdig_user_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@ func TestAccUser(t *testing.T) {
1515

1616
resource.Test(t, resource.TestCase{
1717
PreCheck: func() {
18-
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
19-
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
18+
monitor := os.Getenv("SYSDIG_MONITOR_API_TOKEN")
19+
secure := os.Getenv("SYSDIG_SECURE_API_TOKEN")
20+
if monitor == "" && secure == "" {
21+
t.Fatal("either SYSDIG_MONITOR_API_TOKEN or SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
2022
}
2123
},
2224
Providers: map[string]terraform.ResourceProvider{

sysdig/secure/client.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,6 @@ type SysdigSecureClient interface {
2525
DeleteNotificationChannel(int) error
2626
UpdateNotificationChannel(NotificationChannel) (NotificationChannel, error)
2727

28-
CreateUser(User) (User, error)
29-
GetUserById(int) (User, error)
30-
DeleteUser(int) error
31-
UpdateUser(User) (User, error)
32-
3328
CreateTeam(Team) (Team, error)
3429
GetTeamById(int) (Team, error)
3530
DeleteTeam(int) error

sysdig/secure/models.go

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -253,32 +253,6 @@ func MacroFromJSON(body []byte) (macro Macro, err error) {
253253
return
254254
}
255255

256-
// -------- User --------
257-
type User struct {
258-
ID int `json:"id,omitempty"`
259-
Version int `json:"version,omitempty"`
260-
SystemRole string `json:"systemRole,omitempty"`
261-
Email string `json:"username"`
262-
FirstName string `json:"firstName,omitempty"`
263-
LastName string `json:"lastName,omitempty"`
264-
}
265-
266-
func (u *User) ToJSON() io.Reader {
267-
payload, _ := json.Marshal(*u)
268-
return bytes.NewBuffer(payload)
269-
}
270-
271-
func UserFromJSON(body []byte) User {
272-
var result userWrapper
273-
json.Unmarshal(body, &result)
274-
275-
return result.User
276-
}
277-
278-
type userWrapper struct {
279-
User User `json:"user"`
280-
}
281-
282256
// -------- Team --------
283257
type Team struct {
284258
ID int `json:"id,omitempty"`

sysdig/sysdig_clients.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package sysdig
22

33
import (
44
"errors"
5+
"github.com/draios/terraform-provider-sysdig/sysdig/common"
56
"github.com/draios/terraform-provider-sysdig/sysdig/monitor"
67
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
78
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -11,14 +12,17 @@ import (
1112
type SysdigClients interface {
1213
sysdigMonitorClient() (monitor.SysdigMonitorClient, error)
1314
sysdigSecureClient() (secure.SysdigSecureClient, error)
15+
sysdigCommonClient() (common.SysdigCommonClient, error)
1416
}
1517

1618
type sysdigClients struct {
1719
d *schema.ResourceData
1820
onceMonitor sync.Once
1921
onceSecure sync.Once
22+
onceCommon sync.Once
2023
monitorClient monitor.SysdigMonitorClient
2124
secureClient secure.SysdigSecureClient
25+
commonClient common.SysdigCommonClient
2226
}
2327

2428
func (c *sysdigClients) sysdigMonitorClient() (m monitor.SysdigMonitorClient, err error) {
@@ -56,3 +60,33 @@ func (c *sysdigClients) sysdigSecureClient() (s secure.SysdigSecureClient, err e
5660

5761
return c.secureClient, nil
5862
}
63+
64+
func (c *sysdigClients) sysdigCommonClient() (co common.SysdigCommonClient, err error) {
65+
monitorAPIToken := c.d.Get("sysdig_monitor_api_token").(string)
66+
secureAPIToken := c.d.Get("sysdig_secure_api_token").(string)
67+
68+
if monitorAPIToken == "" && secureAPIToken == "" {
69+
err = errors.New("sysdig monitor and sysdig secure tokens not provided")
70+
return
71+
}
72+
73+
commonAPIToken := monitorAPIToken
74+
commonURL := c.d.Get("sysdig_monitor_url").(string)
75+
commonInsecure := c.d.Get("sysdig_monitor_insecure_tls").(bool)
76+
if monitorAPIToken == "" {
77+
commonAPIToken = secureAPIToken
78+
commonURL = c.d.Get("sysdig_secure_url").(string)
79+
commonInsecure = c.d.Get("sysdig_secure_insecure_tls").(bool)
80+
}
81+
82+
c.onceCommon.Do(func() {
83+
c.commonClient = common.NewSysdigCommonClient(
84+
commonAPIToken,
85+
commonURL,
86+
commonInsecure,
87+
)
88+
})
89+
90+
return c.commonClient, nil
91+
92+
}

0 commit comments

Comments
 (0)