Skip to content

Commit

Permalink
Group and User (#201)
Browse files Browse the repository at this point in the history
* patch - group and user
  • Loading branch information
OrNovo authored Feb 13, 2024
1 parent 049befc commit b87506b
Show file tree
Hide file tree
Showing 25 changed files with 1,851 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/acc-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ jobs:
CORALOGIX_ENV: ${{ secrets.CORALOGIX_ENV }}
CORALOGIX_API_KEY: ${{ secrets.CORALOGIX_API_KEY }}
CORALOGIX_ORG_KEY: ${{ secrets.CORALOGIX_ORG_KEY }}

TEST_TEAM_ID: ${{ secrets.TEST_TEAM_ID }}
run: |
make testacc
14 changes: 13 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,4 +454,16 @@ Bug fixing:
## Release 1.11.7
Bug fixing:
#### resource/coralogix_dashboard
* fixing flatten of `json_content` field bug.
* fixing flatten of `json_content` field bug.

## Release 1.11.8
New Features:
#### resource/coralogix_api_key
* Adding `coralogix_api_key` [resource](https://github.com/coralogix/terraform-provider-coralogix/tree/master/docs/resources/api_key.md) and [data-source](https://github.com/coralogix/terraform-provider-coralogix/tree/master/docs/data-sources/api_key.md).

## Release 1.11.9
New Features:
#### resource/coralogix_user
* Adding `coralogix_user` [resource](https://github.com/coralogix/terraform-provider-coralogix/tree/master/docs/resources/user.md) and [data-source](https://github.com/coralogix/terraform-provider-coralogix/tree/master/docs/data-sources/user.md).
#### resource/coralogix_group
* Adding `coralogix_user_group` [resource](https://github.com/coralogix/terraform-provider-coralogix/tree/master/docs/resources/group.md) and [data-source](https://github.com/coralogix/terraform-provider-coralogix/tree/master/docs/data-sources/group.md).
12 changes: 12 additions & 0 deletions coralogix/clientset/clientset.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ type ClientSet struct {
slos *SLOsClient
dahboardsFolders *DashboardsFoldersClient
apiKeys *ApikeysClient
groups *GroupsClient
users *UsersClient
}

func (c *ClientSet) RuleGroups() *RuleGroupsClient {
Expand Down Expand Up @@ -103,6 +105,14 @@ func (c *ClientSet) DashboardsFolders() *DashboardsFoldersClient {
return c.dahboardsFolders
}

func (c *ClientSet) Groups() *GroupsClient {
return c.groups
}

func (c *ClientSet) Users() *UsersClient {
return c.users
}

func NewClientSet(targetUrl, apiKey, orgKey string) *ClientSet {
apikeyCPC := NewCallPropertiesCreator(targetUrl, apiKey)
teamsCPC := NewCallPropertiesCreator(targetUrl, orgKey)
Expand All @@ -128,5 +138,7 @@ func NewClientSet(targetUrl, apiKey, orgKey string) *ClientSet {
slos: NewSLOsClient(apikeyCPC),
dahboardsFolders: NewDashboardsFoldersClient(apikeyCPC),
apiKeys: NewApiKeysClient(teamsCPC),
groups: NewGroupsClient(teamsCPC),
users: NewUsersClient(teamsCPC),
}
}
93 changes: 93 additions & 0 deletions coralogix/clientset/groups-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package clientset

import (
"context"
"encoding/json"
"fmt"
"strings"

"terraform-provider-coralogix/coralogix/clientset/rest"
)

type GroupsClient struct {
client *rest.Client
TargetUrl string
}

type SCIMGroup struct {
ID string `json:"id"`
DisplayName string `json:"displayName"`
Members []SCIMGroupMember `json:"members"`
Role string `json:"role"`
}

type SCIMGroupMember struct {
Value string `json:"value"`
}

func (c GroupsClient) CreateGroup(ctx context.Context, teamID string, groupReq *SCIMGroup) (*SCIMGroup, error) {
body, err := json.Marshal(groupReq)
if err != nil {
return nil, err
}

bodyResp, err := c.client.Post(ctx, "", "application/json", string(body), "cgx-team-id", teamID)
if err != nil {
return nil, err
}

var groupResp SCIMGroup
err = json.Unmarshal([]byte(bodyResp), &groupResp)
if err != nil {
return nil, err
}

return &groupResp, nil
}

func (c GroupsClient) GetGroup(ctx context.Context, teamID, groupID string) (*SCIMGroup, error) {
bodyResp, err := c.client.Get(ctx, fmt.Sprintf("/%s", groupID), "cgx-team-id", teamID)
if err != nil {
return nil, err
}

var groupResp SCIMGroup
err = json.Unmarshal([]byte(bodyResp), &groupResp)
if err != nil {
return nil, err
}

return &groupResp, nil
}

func (c GroupsClient) UpdateGroup(ctx context.Context, teamID, groupID string, groupReq *SCIMGroup) (*SCIMGroup, error) {
body, err := json.Marshal(groupReq)
if err != nil {
return nil, err
}

bodyResp, err := c.client.Put(ctx, fmt.Sprintf("/%s", groupID), "application/json", string(body), "cgx-team-id", teamID)
if err != nil {
return nil, err
}

var groupResp SCIMGroup
err = json.Unmarshal([]byte(bodyResp), &groupResp)
if err != nil {
return nil, err
}

return &groupResp, nil
}

func (c GroupsClient) DeleteGroup(ctx context.Context, teamID, groupID string) error {
_, err := c.client.Delete(ctx, fmt.Sprintf("/%s", groupID), "cgx-team-id", teamID)
return err

}

func NewGroupsClient(c *CallPropertiesCreator) *GroupsClient {
targetUrl := "https://" + strings.Replace(c.targetUrl, "grpc", "http", 1) + "/scim/Groups"
client := rest.NewRestClient(targetUrl, c.apiKey)
return &GroupsClient{client: client, TargetUrl: targetUrl}
}
24 changes: 15 additions & 9 deletions coralogix/clientset/rest/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func NewRestClient(url string, apiKey string) *Client {
}

// Request executes request to Coralogix API
func (c *Client) Request(ctx context.Context, method, path, contentType string, body interface{}) (string, error) {
func (c *Client) Request(ctx context.Context, method, path, contentType string, body interface{}, headers ...string) (string, error) {
var request *http.Request
if body != nil {
bodyReader := bytes.NewBuffer([]byte(body.(string)))
Expand All @@ -42,6 +42,12 @@ func (c *Client) Request(ctx context.Context, method, path, contentType string,
request = request.WithContext(ctx)
request.Header.Set("Cache-Control", "no-cache")
request.Header.Set("Authorization", "Bearer "+c.apiKey)
if len(headers)%2 != 0 {
return "", fmt.Errorf("invalid headers, must be key-value pairs")
}
for i := 0; i < len(headers); i += 2 {
request.Header.Set(headers[i], headers[i+1])
}

response, err := c.client.Do(request)
if err != nil {
Expand Down Expand Up @@ -72,21 +78,21 @@ func (c *Client) Request(ctx context.Context, method, path, contentType string,
}

// Get executes GET request to Coralogix API
func (c *Client) Get(ctx context.Context, path string) (string, error) {
return c.Request(ctx, "GET", path, "", nil)
func (c *Client) Get(ctx context.Context, path string, headers ...string) (string, error) {
return c.Request(ctx, "GET", path, "", nil, headers...)
}

// Post executes POST request to Coralogix API
func (c *Client) Post(ctx context.Context, path, contentType, body string) (string, error) {
return c.Request(ctx, "POST", path, contentType, body)
func (c *Client) Post(ctx context.Context, path, contentType, body string, headers ...string) (string, error) {
return c.Request(ctx, "POST", path, contentType, body, headers...)
}

// Put executes PUT request to Coralogix API
func (c *Client) Put(ctx context.Context, path, contentType, body string) (string, error) {
return c.Request(ctx, "PUT", path, contentType, body)
func (c *Client) Put(ctx context.Context, path, contentType, body string, headers ...string) (string, error) {
return c.Request(ctx, "PUT", path, contentType, body, headers...)
}

// Delete executes DELETE request to Coralogix API
func (c *Client) Delete(ctx context.Context, path string) (string, error) {
return c.Request(ctx, "DELETE", path, "", nil)
func (c *Client) Delete(ctx context.Context, path string, headers ...string) (string, error) {
return c.Request(ctx, "DELETE", path, "", nil, headers...)
}
107 changes: 107 additions & 0 deletions coralogix/clientset/users-client.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
package clientset

import (
"context"
"encoding/json"
"fmt"
"strings"

"terraform-provider-coralogix/coralogix/clientset/rest"
)

type UsersClient struct {
client *rest.Client
TargetUrl string
}

type SCIMUser struct {
Schemas []string `json:"schemas"`
ID *string `json:"id,omitempty"`
UserName string `json:"userName"`
Active bool `json:"active"`
Name *SCIMUserName `json:"name,omitempty"`
Groups []SCIMUserGroup `json:"groups,omitempty"`
Emails []SCIMUserEmail `json:"emails,omitempty"`
}

type SCIMUserName struct {
GivenName string `json:"givenName"`
FamilyName string `json:"familyName"`
}

type SCIMUserEmail struct {
Value string `json:"value"`
Primary bool `json:"primary"`
Type string `json:"type"`
}

type SCIMUserGroup struct {
Value string `json:"value"`
}

func (c UsersClient) CreateUser(ctx context.Context, teamID string, userReq *SCIMUser) (*SCIMUser, error) {
body, err := json.Marshal(userReq)
if err != nil {
return nil, err
}

bodyResp, err := c.client.Post(ctx, "", "application/json", string(body), "cgx-team-id", teamID)
if err != nil {
return nil, err
}

var UserResp SCIMUser
err = json.Unmarshal([]byte(bodyResp), &UserResp)
if err != nil {
return nil, err
}

return &UserResp, nil
}

func (c UsersClient) GetUser(ctx context.Context, teamID, userID string) (*SCIMUser, error) {
bodyResp, err := c.client.Get(ctx, fmt.Sprintf("/%s", userID), "cgx-team-id", teamID)
if err != nil {
return nil, err
}

var UserResp SCIMUser
err = json.Unmarshal([]byte(bodyResp), &UserResp)
if err != nil {
return nil, err
}

return &UserResp, nil
}

func (c UsersClient) UpdateUser(ctx context.Context, teamID, userID string, userReq *SCIMUser) (*SCIMUser, error) {
body, err := json.Marshal(userReq)
if err != nil {
return nil, err
}

bodyResp, err := c.client.Put(ctx, fmt.Sprintf("/%s", userID), "application/json", string(body), "cgx-team-id", teamID)
if err != nil {
return nil, err
}

var UserResp SCIMUser
err = json.Unmarshal([]byte(bodyResp), &UserResp)
if err != nil {
return nil, err
}

return &UserResp, nil
}

func (c UsersClient) DeleteUser(ctx context.Context, teamID, userID string) error {
_, err := c.client.Delete(ctx, fmt.Sprintf("/%s", userID), "cgx-team-id", teamID)
return err

}

func NewUsersClient(c *CallPropertiesCreator) *UsersClient {
targetUrl := "https://" + strings.Replace(c.targetUrl, "grpc", "http", 1) + "/scim/Users"
client := rest.NewRestClient(targetUrl, c.apiKey)
return &UsersClient{client: client, TargetUrl: targetUrl}
}
2 changes: 1 addition & 1 deletion coralogix/data_source_coralogix_api_key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestAccCoralogixDataSourceApiKey(t *testing.T) {
testApiKeyResource_read(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(apiKeyDataSourceName, "name", "Test Key 3"),
resource.TestCheckResourceAttr(apiKeyDataSourceName, "owner.team_id", targetTeam),
resource.TestCheckResourceAttr(apiKeyDataSourceName, "owner.team_id", teamID),
resource.TestCheckResourceAttr(apiKeyDataSourceName, "active", "true"),
resource.TestCheckResourceAttr(apiKeyDataSourceName, "hashed", "false"),
),
Expand Down
Loading

0 comments on commit b87506b

Please sign in to comment.