Skip to content

Commit

Permalink
feat: Resource sysdig_user is now using a common client
Browse files Browse the repository at this point in the history
Signed-off-by: Federico Barcelona <[email protected]>
  • Loading branch information
tembleking committed May 12, 2020
1 parent 17d7003 commit b70e1ac
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 51 deletions.
50 changes: 50 additions & 0 deletions sysdig/common/client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package common

import (
"crypto/tls"
"io"
"log"
"net/http"
"net/http/httputil"
)

type SysdigCommonClient interface {
CreateUser(User) (User, error)
GetUserById(int) (User, error)
DeleteUser(int) error
UpdateUser(User) (User, error)
}

func NewSysdigCommonClient(sysdigAPIToken string, url string, insecure bool) SysdigCommonClient {
httpClient := &http.Client{
Transport: &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: insecure},
},
}

return &sysdigCommonClient{
SysdigAPIToken: sysdigAPIToken,
URL: url,
httpClient: httpClient,
}
}

type sysdigCommonClient struct {
SysdigAPIToken string
URL string
httpClient *http.Client
}

func (client *sysdigCommonClient) doSysdigCommonRequest(method string, url string, payload io.Reader) (*http.Response, error) {
request, _ := http.NewRequest(method, url, payload)
request.Header.Set("Authorization", "Bearer "+client.SysdigAPIToken)
request.Header.Set("Content-Type", "application/json")

out, _ := httputil.DumpRequestOut(request, true)
log.Printf("[DEBUG] %s", string(out))
response, error := client.httpClient.Do(request)

out, _ = httputil.DumpResponse(response, true)
log.Printf("[DEBUG] %s", string(out))
return response, error
}
33 changes: 33 additions & 0 deletions sysdig/common/models.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package common

import (
"bytes"
"encoding/json"
"io"
)

// -------- User --------
type User struct {
ID int `json:"id,omitempty"`
Version int `json:"version,omitempty"`
SystemRole string `json:"systemRole,omitempty"`
Email string `json:"username"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
}

func (u *User) ToJSON() io.Reader {
payload, _ := json.Marshal(*u)
return bytes.NewBuffer(payload)
}

func UserFromJSON(body []byte) User {
var result userWrapper
json.Unmarshal(body, &result)

return result.User
}

type userWrapper struct {
User User `json:"user"`
}
22 changes: 11 additions & 11 deletions sysdig/secure/users.go → sysdig/common/users.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package secure
package common

import (
"errors"
Expand All @@ -7,8 +7,8 @@ import (
"net/http"
)

func (client *sysdigSecureClient) GetUserById(id int) (u User, err error) {
response, err := client.doSysdigSecureRequest(http.MethodGet, client.GetUserUrl(id), nil)
func (client *sysdigCommonClient) GetUserById(id int) (u User, err error) {
response, err := client.doSysdigCommonRequest(http.MethodGet, client.GetUserUrl(id), nil)
if err != nil {
return
}
Expand All @@ -26,8 +26,8 @@ func (client *sysdigSecureClient) GetUserById(id int) (u User, err error) {
return
}

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

if err != nil {
return
Expand All @@ -45,8 +45,8 @@ func (client *sysdigSecureClient) CreateUser(uRequest User) (u User, err error)
return
}

func (client *sysdigSecureClient) UpdateUser(uRequest User) (u User, err error) {
response, err := client.doSysdigSecureRequest(http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
func (client *sysdigCommonClient) UpdateUser(uRequest User) (u User, err error) {
response, err := client.doSysdigCommonRequest(http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
if err != nil {
return
}
Expand All @@ -63,8 +63,8 @@ func (client *sysdigSecureClient) UpdateUser(uRequest User) (u User, err error)
return
}

func (client *sysdigSecureClient) DeleteUser(id int) error {
response, err := client.doSysdigSecureRequest(http.MethodDelete, client.GetUserUrl(id), nil)
func (client *sysdigCommonClient) DeleteUser(id int) error {
response, err := client.doSysdigCommonRequest(http.MethodDelete, client.GetUserUrl(id), nil)
if err != nil {
return err
}
Expand All @@ -76,10 +76,10 @@ func (client *sysdigSecureClient) DeleteUser(id int) error {
return nil
}

func (client *sysdigSecureClient) GetUsersUrl() string {
func (client *sysdigCommonClient) GetUsersUrl() string {
return fmt.Sprintf("%s/api/users", client.URL)
}

func (client *sysdigSecureClient) GetUserUrl(id int) string {
func (client *sysdigCommonClient) GetUserUrl(id int) string {
return fmt.Sprintf("%s/api/users/%d", client.URL, id)
}
14 changes: 7 additions & 7 deletions sysdig/resource_sysdig_user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package sysdig

import (
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
"github.com/draios/terraform-provider-sysdig/sysdig/common"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"strconv"
"time"
Expand Down Expand Up @@ -47,7 +47,7 @@ func resourceSysdigUser() *schema.Resource {
}

func resourceSysdigUserCreate(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(SysdigClients).sysdigSecureClient()
client, err := meta.(SysdigClients).sysdigCommonClient()
if err != nil {
return err
}
Expand All @@ -67,7 +67,7 @@ func resourceSysdigUserCreate(d *schema.ResourceData, meta interface{}) error {

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

func resourceSysdigUserUpdate(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(SysdigClients).sysdigSecureClient()
client, err := meta.(SysdigClients).sysdigCommonClient()
if err != nil {
return err
}
Expand All @@ -106,7 +106,7 @@ func resourceSysdigUserUpdate(d *schema.ResourceData, meta interface{}) error {
}

func resourceSysdigUserDelete(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(SysdigClients).sysdigSecureClient()
client, err := meta.(SysdigClients).sysdigCommonClient()
if err != nil {
return err
}
Expand All @@ -116,8 +116,8 @@ func resourceSysdigUserDelete(d *schema.ResourceData, meta interface{}) error {
return client.DeleteUser(id)
}

func userFromResourceData(d *schema.ResourceData) (u secure.User) {
u = secure.User{
func userFromResourceData(d *schema.ResourceData) (u common.User) {
u = common.User{
SystemRole: d.Get("system_role").(string),
Email: d.Get("email").(string),
FirstName: d.Get("first_name").(string),
Expand Down
6 changes: 4 additions & 2 deletions sysdig/resource_sysdig_user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@ func TestAccUser(t *testing.T) {

resource.Test(t, resource.TestCase{
PreCheck: func() {
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
monitor := os.Getenv("SYSDIG_MONITOR_API_TOKEN")
secure := os.Getenv("SYSDIG_SECURE_API_TOKEN")
if monitor == "" && secure == "" {
t.Fatal("either SYSDIG_MONITOR_API_TOKEN or SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
}
},
Providers: map[string]terraform.ResourceProvider{
Expand Down
5 changes: 0 additions & 5 deletions sysdig/secure/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,6 @@ type SysdigSecureClient interface {
DeleteNotificationChannel(int) error
UpdateNotificationChannel(NotificationChannel) (NotificationChannel, error)

CreateUser(User) (User, error)
GetUserById(int) (User, error)
DeleteUser(int) error
UpdateUser(User) (User, error)

CreateTeam(Team) (Team, error)
GetTeamById(int) (Team, error)
DeleteTeam(int) error
Expand Down
26 changes: 0 additions & 26 deletions sysdig/secure/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -253,32 +253,6 @@ func MacroFromJSON(body []byte) (macro Macro, err error) {
return
}

// -------- User --------
type User struct {
ID int `json:"id,omitempty"`
Version int `json:"version,omitempty"`
SystemRole string `json:"systemRole,omitempty"`
Email string `json:"username"`
FirstName string `json:"firstName,omitempty"`
LastName string `json:"lastName,omitempty"`
}

func (u *User) ToJSON() io.Reader {
payload, _ := json.Marshal(*u)
return bytes.NewBuffer(payload)
}

func UserFromJSON(body []byte) User {
var result userWrapper
json.Unmarshal(body, &result)

return result.User
}

type userWrapper struct {
User User `json:"user"`
}

// -------- Team --------
type Team struct {
ID int `json:"id,omitempty"`
Expand Down
34 changes: 34 additions & 0 deletions sysdig/sysdig_clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package sysdig

import (
"errors"
"github.com/draios/terraform-provider-sysdig/sysdig/common"
"github.com/draios/terraform-provider-sysdig/sysdig/monitor"
"github.com/draios/terraform-provider-sysdig/sysdig/secure"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
Expand All @@ -11,14 +12,17 @@ import (
type SysdigClients interface {
sysdigMonitorClient() (monitor.SysdigMonitorClient, error)
sysdigSecureClient() (secure.SysdigSecureClient, error)
sysdigCommonClient() (common.SysdigCommonClient, error)
}

type sysdigClients struct {
d *schema.ResourceData
onceMonitor sync.Once
onceSecure sync.Once
onceCommon sync.Once
monitorClient monitor.SysdigMonitorClient
secureClient secure.SysdigSecureClient
commonClient common.SysdigCommonClient
}

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

return c.secureClient, nil
}

func (c *sysdigClients) sysdigCommonClient() (co common.SysdigCommonClient, err error) {
monitorAPIToken := c.d.Get("sysdig_monitor_api_token").(string)
secureAPIToken := c.d.Get("sysdig_secure_api_token").(string)

if monitorAPIToken == "" && secureAPIToken == "" {
err = errors.New("sysdig monitor and sysdig secure tokens not provided")
return
}

commonAPIToken := monitorAPIToken
commonURL := c.d.Get("sysdig_monitor_url").(string)
commonInsecure := c.d.Get("sysdig_monitor_insecure_tls").(bool)
if monitorAPIToken == "" {
commonAPIToken = secureAPIToken
commonURL = c.d.Get("sysdig_secure_url").(string)
commonInsecure = c.d.Get("sysdig_secure_insecure_tls").(bool)
}

c.onceCommon.Do(func() {
c.commonClient = common.NewSysdigCommonClient(
commonAPIToken,
commonURL,
commonInsecure,
)
})

return c.commonClient, nil

}

0 comments on commit b70e1ac

Please sign in to comment.