Skip to content

Commit 648b868

Browse files
filiptubicFilip Tubic
andauthored
feat(group mapping) Add group mapping resource which can be used for both monitor and secure (#254)
* feat(groupmapping): add group mapping resource * feat(groupmapping): read resource after create update * feat(groupmapping): enable import for group mapping resource * feat(groupmapping): add acceptance tests for group mapping * feat(groupmapping): update tests * feat(groupmapping): add group mapping docs * feat(groupmapping): add filiptubic as group mapping code owner * feat(groupmapping): fix lint errors * feat(groupmapping): update docs * feat(groupmapping): add system_role to group mapping --------- Co-authored-by: Filip Tubic <[email protected]>
1 parent f7e091b commit 648b868

File tree

8 files changed

+585
-1
lines changed

8 files changed

+585
-1
lines changed

CODEOWNERS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
*monitor*alert* @arturodilecce @dbonf
99
*monitor*dashboard* @brokenpip3
1010
*monitor*providers* @jwsarna1
11+
*monitor*groupmapping* @filiptubic
1112

1213
# policies/rules
1314
*secure*policy* @jacklongsd @kmvachhani @ben-m-lucas

sysdig/internal/client/common/client.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ type SysdigCommonClient interface {
1818
DeleteUser(context.Context, int) error
1919
UpdateUser(context.Context, *User) (*User, error)
2020
GetCurrentUser(context.Context) (*User, error)
21+
22+
CreateGroupMapping(ctx context.Context, request *GroupMapping) (*GroupMapping, error)
23+
UpdateGroupMapping(ctx context.Context, request *GroupMapping, id int) (*GroupMapping, error)
24+
DeleteGroupMapping(ctx context.Context, id int) error
25+
GetGroupMapping(ctx context.Context, id int) (*GroupMapping, error)
2126
}
2227

2328
func WithExtraHeaders(client SysdigCommonClient, extraHeaders map[string]string) SysdigCommonClient {
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"io"
9+
"net/http"
10+
)
11+
12+
var GroupMappingNotFound = errors.New("group mapping not found")
13+
14+
func (client *sysdigCommonClient) CreateGroupMapping(ctx context.Context, request *GroupMapping) (*GroupMapping, error) {
15+
payload, err := request.ToJSON()
16+
if err != nil {
17+
return nil, err
18+
}
19+
20+
response, err := client.doSysdigCommonRequest(ctx, http.MethodPost, client.CreateGroupMappingUrl(), payload)
21+
if err != nil {
22+
return nil, err
23+
}
24+
defer response.Body.Close()
25+
26+
if response.StatusCode != http.StatusOK {
27+
return nil, errorFromResponse(response)
28+
}
29+
30+
body, err := io.ReadAll(response.Body)
31+
if err != nil {
32+
return nil, err
33+
}
34+
35+
var groupMapping GroupMapping
36+
err = json.Unmarshal(body, &groupMapping)
37+
if err != nil {
38+
return nil, err
39+
}
40+
41+
return &groupMapping, nil
42+
}
43+
44+
func (client *sysdigCommonClient) UpdateGroupMapping(ctx context.Context, request *GroupMapping, id int) (*GroupMapping, error) {
45+
payload, err := request.ToJSON()
46+
if err != nil {
47+
return nil, err
48+
}
49+
50+
response, err := client.doSysdigCommonRequest(ctx, http.MethodPut, client.UpdateGroupMappingUrl(id), payload)
51+
if err != nil {
52+
return nil, err
53+
}
54+
defer response.Body.Close()
55+
56+
if response.StatusCode != http.StatusOK {
57+
return nil, errorFromResponse(response)
58+
}
59+
60+
body, err := io.ReadAll(response.Body)
61+
if err != nil {
62+
return nil, err
63+
}
64+
65+
var groupMapping GroupMapping
66+
err = json.Unmarshal(body, &groupMapping)
67+
if err != nil {
68+
return nil, err
69+
}
70+
71+
return &groupMapping, nil
72+
}
73+
74+
func (client *sysdigCommonClient) DeleteGroupMapping(ctx context.Context, id int) error {
75+
response, err := client.doSysdigCommonRequest(ctx, http.MethodDelete, client.DeleteGroupMappingUrl(id), nil)
76+
if err != nil {
77+
return err
78+
}
79+
defer response.Body.Close()
80+
81+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
82+
return errorFromResponse(response)
83+
}
84+
85+
return nil
86+
}
87+
88+
func (client *sysdigCommonClient) GetGroupMapping(ctx context.Context, id int) (*GroupMapping, error) {
89+
response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetGroupMappingUrl(id), nil)
90+
if err != nil {
91+
return nil, err
92+
}
93+
defer response.Body.Close()
94+
95+
if response.StatusCode != http.StatusOK {
96+
if response.StatusCode == http.StatusNotFound {
97+
return nil, GroupMappingNotFound
98+
}
99+
return nil, errorFromResponse(response)
100+
}
101+
102+
body, err := io.ReadAll(response.Body)
103+
if err != nil {
104+
return nil, err
105+
}
106+
107+
var groupMapping GroupMapping
108+
err = json.Unmarshal(body, &groupMapping)
109+
if err != nil {
110+
return nil, err
111+
}
112+
113+
return &groupMapping, nil
114+
}
115+
116+
func (client *sysdigCommonClient) GetGroupMappingUrl(id int) string {
117+
return fmt.Sprintf("%s/api/groupmappings/%d", client.URL, id)
118+
}
119+
120+
func (client *sysdigCommonClient) CreateGroupMappingUrl() string {
121+
return fmt.Sprintf("%s/api/groupmappings", client.URL)
122+
}
123+
124+
func (client *sysdigCommonClient) UpdateGroupMappingUrl(id int) string {
125+
return fmt.Sprintf("%s/api/groupmappings/%d", client.URL, id)
126+
}
127+
128+
func (client *sysdigCommonClient) DeleteGroupMappingUrl(id int) string {
129+
return fmt.Sprintf("%s/api/groupmappings/%d", client.URL, id)
130+
}

sysdig/internal/client/common/models.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,28 @@ import (
66
"io"
77
)
88

9+
// -------- Group mapping --------
10+
type TeamMap struct {
11+
AllTeams bool `json:"allTeams"`
12+
TeamIDs []int `json:"teamIds"`
13+
}
14+
15+
type GroupMapping struct {
16+
ID int `json:"id,omitempty"`
17+
GroupName string `json:"groupName,omitempty"`
18+
Role string `json:"role,omitempty"`
19+
SystemRole string `json:"systemRole,omitempty"`
20+
TeamMap *TeamMap `json:"teamMap,omitempty"`
21+
}
22+
23+
func (gm *GroupMapping) ToJSON() (io.Reader, error) {
24+
payload, err := json.Marshal(*gm)
25+
if err != nil {
26+
return nil, err
27+
}
28+
return bytes.NewBuffer(payload), nil
29+
}
30+
931
// -------- User --------
1032
type User struct {
1133
ID int `json:"id,omitempty"`

sysdig/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ func Provider() *schema.Provider {
4949
},
5050
},
5151
ResourcesMap: map[string]*schema.Resource{
52-
"sysdig_user": resourceSysdigUser(),
52+
"sysdig_user": resourceSysdigUser(),
53+
"sysdig_group_mapping": resourceSysdigGroupMapping(),
5354

5455
"sysdig_secure_policy": resourceSysdigSecurePolicy(),
5556
"sysdig_secure_notification_channel_email": resourceSysdigSecureNotificationChannelEmail(),

0 commit comments

Comments
 (0)