Skip to content

Commit e05ef8d

Browse files
feat: add more functions for team management (#3)
* Allow team creation Signed-off-by: Brian Wilkinson <[email protected]> * List permissions Signed-off-by: Brian Wilkinson <[email protected]> * Add permissions to client Signed-off-by: Brian Wilkinson <[email protected]> * Teams use Permissions type Signed-off-by: Brian Wilkinson <[email protected]> * Not possible to create team with perms, try adding after Signed-off-by: Brian Wilkinson <[email protected]> * Incorrect type returned Signed-off-by: Brian Wilkinson <[email protected]> * Team permission has a type Signed-off-by: Brian Wilkinson <[email protected]> * Team permission has a type Signed-off-by: Brian Wilkinson <[email protected]> * Need to delete team with obj not uuid Signed-off-by: Brian Wilkinson <[email protected]> * Path incorrectly specified Signed-off-by: Brian Wilkinson <[email protected]> * Revert module name for PR Signed-off-by: Brian Wilkinson <[email protected]> --------- Signed-off-by: Brian Wilkinson <[email protected]>
1 parent 3bbbd01 commit e05ef8d

File tree

3 files changed

+70
-5
lines changed

3 files changed

+70
-5
lines changed

client.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type Client struct {
3535
Finding FindingService
3636
License LicenseService
3737
Metrics MetricsService
38+
Permission PermissionService
3839
Policy PolicyService
3940
PolicyViolation PolicyViolationService
4041
Project ProjectService
@@ -79,6 +80,7 @@ func NewClient(baseURL string, options ...ClientOption) (*Client, error) {
7980
client.Finding = FindingService{client: &client}
8081
client.License = LicenseService{client: &client}
8182
client.Metrics = MetricsService{client: &client}
83+
client.Permission = PermissionService{client: &client}
8284
client.Policy = PolicyService{client: &client}
8385
client.PolicyViolation = PolicyViolationService{client: &client}
8486
client.Project = ProjectService{client: &client}

permission.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package dtrack
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/google/uuid"
7+
"net/http"
8+
)
9+
10+
type PermissionService struct {
11+
client *Client
12+
}
13+
14+
type Permission struct {
15+
Name string `json:"name"`
16+
Description string `json:"description"`
17+
}
18+
19+
func (ps PermissionService) GetAll(ctx context.Context, po PageOptions) (p Page[Permission], err error) {
20+
req, err := ps.client.newRequest(ctx, http.MethodGet, "/api/v1/permission", withPageOptions(po))
21+
if err != nil {
22+
return
23+
}
24+
25+
res, err := ps.client.doRequest(req, &p.Items)
26+
if err != nil {
27+
return
28+
}
29+
30+
p.TotalCount = res.TotalCount
31+
return
32+
}
33+
34+
func (ps PermissionService) AddPermissionToTeam(ctx context.Context, permission Permission, team uuid.UUID) (t Team, err error) {
35+
req, err := ps.client.newRequest(ctx, http.MethodPost, fmt.Sprintf("/api/v1/permission/%s/team/%s", permission.Name, team.String()))
36+
if err != nil {
37+
return
38+
}
39+
40+
_, err = ps.client.doRequest(req, &t)
41+
return
42+
}

team.go

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

1111
type Team struct {
12-
UUID uuid.UUID `json:"uuid,omitempty"`
13-
Name string `json:"name,omitempty"`
14-
APIKeys []APIKey `json:"apiKeys,omitempty"`
12+
UUID uuid.UUID `json:"uuid,omitempty"`
13+
Name string `json:"name,omitempty"`
14+
APIKeys []APIKey `json:"apiKeys,omitempty"`
15+
Permissions []Permission `json:"permissions"`
1516
}
1617

1718
type APIKey struct {
@@ -22,13 +23,13 @@ type TeamService struct {
2223
client *Client
2324
}
2425

25-
func (ts TeamService) Get(ctx context.Context, teamUUID uuid.UUID) (p Project, err error) {
26+
func (ts TeamService) Get(ctx context.Context, teamUUID uuid.UUID) (t Team, err error) {
2627
req, err := ts.client.newRequest(ctx, http.MethodGet, fmt.Sprintf("/api/v1/team/%s", teamUUID))
2728
if err != nil {
2829
return
2930
}
3031

31-
_, err = ts.client.doRequest(req, &p)
32+
_, err = ts.client.doRequest(req, &t)
3233
return
3334
}
3435

@@ -58,3 +59,23 @@ func (ts TeamService) GenerateAPIKey(ctx context.Context, teamUUID uuid.UUID) (k
5859
key = apiKey.Key
5960
return
6061
}
62+
63+
func (ts TeamService) Create(ctx context.Context, team Team) (t Team, err error) {
64+
req, err := ts.client.newRequest(ctx, http.MethodPut, "/api/v1/team", withBody(team))
65+
if err != nil {
66+
return
67+
}
68+
69+
_, err = ts.client.doRequest(req, &t)
70+
return
71+
}
72+
73+
func (ts TeamService) Delete(ctx context.Context, team Team) (err error) {
74+
req, err := ts.client.newRequest(ctx, http.MethodDelete, "/api/v1/team", withBody(team))
75+
if err != nil {
76+
return
77+
}
78+
79+
_, err = ts.client.doRequest(req, nil)
80+
return
81+
}

0 commit comments

Comments
 (0)