Skip to content

Commit aa90768

Browse files
authored
Refactor groupmapping (#301)
* refactor(groupmapping): remove old group mapping client and refactor terraform objects to use v2
1 parent 9845496 commit aa90768

File tree

9 files changed

+151
-313
lines changed

9 files changed

+151
-313
lines changed

sysdig/internal/client/common/client.go

Lines changed: 0 additions & 68 deletions
This file was deleted.

sysdig/internal/client/common/groupmapping.go

Lines changed: 0 additions & 130 deletions
This file was deleted.

sysdig/internal/client/common/helpers.go

Lines changed: 0 additions & 30 deletions
This file was deleted.

sysdig/internal/client/common/models.go

Lines changed: 0 additions & 29 deletions
This file was deleted.
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"errors"
6+
"fmt"
7+
"net/http"
8+
)
9+
10+
var GroupMappingNotFound = errors.New("group mapping not found")
11+
12+
const (
13+
CreateGroupMappingPath = "%s/api/groupmappings"
14+
UpdateGroupMappingPath = "%s/api/groupmappings/%d"
15+
DeleteGroupMappingPath = "%s/api/groupmappings/%d"
16+
GetGroupMappingPath = "%s/api/groupmappings/%d"
17+
)
18+
19+
type GroupMappingInterface interface {
20+
Base
21+
CreateGroupMapping(ctx context.Context, gm *GroupMapping) (*GroupMapping, error)
22+
UpdateGroupMapping(ctx context.Context, gm *GroupMapping, id int) (*GroupMapping, error)
23+
DeleteGroupMapping(ctx context.Context, id int) error
24+
GetGroupMapping(ctx context.Context, id int) (*GroupMapping, error)
25+
}
26+
27+
func (client *Client) CreateGroupMapping(ctx context.Context, gm *GroupMapping) (*GroupMapping, error) {
28+
payload, err := Marshal(gm)
29+
if err != nil {
30+
return nil, err
31+
}
32+
33+
response, err := client.requester.Request(ctx, http.MethodPost, client.CreateGroupMappingURL(), payload)
34+
if err != nil {
35+
return nil, err
36+
}
37+
defer response.Body.Close()
38+
39+
if response.StatusCode != http.StatusOK {
40+
return nil, client.ErrorFromResponse(response)
41+
}
42+
43+
created, err := Unmarshal[GroupMapping](response.Body)
44+
if err != nil {
45+
return nil, err
46+
}
47+
48+
return &created, nil
49+
}
50+
51+
func (client *Client) UpdateGroupMapping(ctx context.Context, gm *GroupMapping, id int) (*GroupMapping, error) {
52+
payload, err := Marshal(gm)
53+
if err != nil {
54+
return nil, err
55+
}
56+
57+
response, err := client.requester.Request(ctx, http.MethodPut, client.UpdateGroupMappingURL(id), payload)
58+
if err != nil {
59+
return nil, err
60+
}
61+
defer response.Body.Close()
62+
63+
if response.StatusCode != http.StatusOK {
64+
return nil, client.ErrorFromResponse(response)
65+
}
66+
67+
updated, err := Unmarshal[GroupMapping](response.Body)
68+
if err != nil {
69+
return nil, err
70+
}
71+
72+
return &updated, nil
73+
}
74+
75+
func (client *Client) DeleteGroupMapping(ctx context.Context, id int) error {
76+
response, err := client.requester.Request(ctx, http.MethodDelete, client.DeleteGroupMappingURL(id), nil)
77+
if err != nil {
78+
return err
79+
}
80+
defer response.Body.Close()
81+
82+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
83+
return client.ErrorFromResponse(response)
84+
}
85+
86+
return nil
87+
}
88+
89+
func (client *Client) GetGroupMapping(ctx context.Context, id int) (*GroupMapping, error) {
90+
response, err := client.requester.Request(ctx, http.MethodGet, client.GetGroupMappingURL(id), nil)
91+
if err != nil {
92+
return nil, err
93+
}
94+
defer response.Body.Close()
95+
96+
if response.StatusCode != http.StatusOK {
97+
if response.StatusCode == http.StatusNotFound {
98+
return nil, GroupMappingNotFound
99+
}
100+
return nil, client.ErrorFromResponse(response)
101+
}
102+
103+
gm, err := Unmarshal[GroupMapping](response.Body)
104+
if err != nil {
105+
return nil, err
106+
}
107+
108+
return &gm, nil
109+
}
110+
111+
func (client *Client) CreateGroupMappingURL() string {
112+
return fmt.Sprintf(CreateGroupMappingPath, client.config.url)
113+
}
114+
115+
func (client *Client) UpdateGroupMappingURL(id int) string {
116+
return fmt.Sprintf(UpdateGroupMappingPath, client.config.url, id)
117+
}
118+
119+
func (client *Client) DeleteGroupMappingURL(id int) string {
120+
return fmt.Sprintf(DeleteGroupMappingPath, client.config.url, id)
121+
}
122+
123+
func (client *Client) GetGroupMappingURL(id int) string {
124+
return fmt.Sprintf(GetGroupMappingPath, client.config.url, id)
125+
}

0 commit comments

Comments
 (0)