-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Resource sysdig_user is now using a common client
Signed-off-by: Federico Barcelona <[email protected]>
- Loading branch information
1 parent
17d7003
commit b70e1ac
Showing
8 changed files
with
139 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"` | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters