Skip to content

Commit 62caaf2

Browse files
committed
INFRA-20407: Add cortex_team_role resource
1 parent f7464fc commit 62caaf2

File tree

10 files changed

+684
-7
lines changed

10 files changed

+684
-7
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
resource "cortex_team_role" "engineer" {
2+
tag = "se-1"
3+
name = "Software Engineer 1"
4+
description = "A first-level engineer"
5+
notifcations_enabled = true
6+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-testing v1.5.1
1313
github.com/life4/genesis v1.8.1
1414
github.com/motemen/go-loghttp v0.0.0-20170804080138-974ac5ceac27
15-
github.com/stretchr/testify v1.8.4
15+
github.com/stretchr/testify v1.10.0
1616
gopkg.in/yaml.v3 v3.0.1
1717
)
1818

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,8 @@ github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5
305305
github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
306306
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
307307
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
308-
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
309-
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
308+
github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA=
309+
github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
310310
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
311311
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
312312
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=

internal/cortex/http_client.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ import (
44
"context"
55
"errors"
66
"fmt"
7-
"github.com/dghubble/sling"
8-
"github.com/motemen/go-loghttp"
9-
_ "github.com/motemen/go-loghttp/global" // Just this line!
107
"net/http"
118
"net/url"
129
"os"
10+
11+
"github.com/dghubble/sling"
12+
"github.com/motemen/go-loghttp"
13+
_ "github.com/motemen/go-loghttp/global" // Just this line!
1314
)
1415

1516
const (
@@ -19,6 +20,7 @@ const (
1920

2021
var BaseUris = map[string]string{
2122
"teams": "/api/v1/teams/",
23+
"teams_roles": "/api/v1/teams/roles/",
2224
"departments": "/api/v1/teams/departments/",
2325
"scorecards": "/api/v1/scorecards/",
2426
"catalog_entities": "/api/v1/catalog/",
@@ -157,6 +159,8 @@ func (c *HttpClient) Teams() TeamsClientInterface {
157159
return &TeamsClient{client: c}
158160
}
159161

162+
func (c *HttpClient) TeamRoles() TeamRolesClientInterface { return &TeamRolesClient{client: c} }
163+
160164
func (c *HttpClient) Departments() DepartmentsClientInterface {
161165
return &DepartmentsClient{client: c}
162166
}

internal/cortex/team_roles.go

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,204 @@
1+
package cortex
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"errors"
7+
"fmt"
8+
"log"
9+
10+
"github.com/dghubble/sling"
11+
)
12+
13+
type TeamRolesClientInterface interface {
14+
Get(ctx context.Context, id int64) (*TeamRole, error)
15+
List(ctx context.Context, params *TeamRolesListParams) (*TeamRolesListResponse, error)
16+
Create(ctx context.Context, req CreateTeamRoleRequest) (*TeamRole, error)
17+
Update(ctx context.Context, req UpdateTeamRoleRequest) (*TeamRole, error)
18+
Delete(ctx context.Context, id int64) error
19+
}
20+
21+
type TeamRolesClient struct {
22+
client *HttpClient
23+
}
24+
25+
var _ TeamRolesClientInterface = &TeamRolesClient{}
26+
27+
func (c *TeamRolesClient) Client() *sling.Sling {
28+
return c.client.Client()
29+
}
30+
31+
/***********************************************************************************************************************
32+
* Types
33+
**********************************************************************************************************************/
34+
35+
type TeamRole struct {
36+
ID int64 `json:"id"`
37+
Name string `json:"name"`
38+
Tag string `json:"tag"`
39+
Description string `json:"description,omitempty"`
40+
NotificationsEnabled bool `json:"notificationsEnabled,omitempty"`
41+
}
42+
43+
/***********************************************************************************************************************
44+
* GET /api/v1/teams/roles/:id
45+
**********************************************************************************************************************/
46+
47+
func (c *TeamRolesClient) Get(_ context.Context, id int64) (*TeamRole, error) {
48+
apiResponse := &TeamRole{}
49+
apiError := &ApiError{}
50+
response, err := c.Client().Get(Route("teams_roles", fmt.Sprintf("%d", id))).Receive(apiResponse, apiError)
51+
if err != nil {
52+
return apiResponse, errors.New("could not get team role: " + err.Error())
53+
}
54+
55+
err = c.client.handleResponseStatus(response, apiError)
56+
if err != nil {
57+
return apiResponse, errors.Join(errors.New("Failed getting team role: "), err)
58+
}
59+
60+
return apiResponse, nil
61+
}
62+
63+
/***********************************************************************************************************************
64+
* GET /api/v1/teams/roles
65+
**********************************************************************************************************************/
66+
67+
// TeamRolesListParams are the query parameters for the GET /v1/teams/roles endpoint.
68+
type TeamRolesListParams struct {
69+
Page int `url:"page,omitempty"`
70+
PerPage int `url:"size,omitempty"`
71+
Query string `url:"query,omitempty"`
72+
}
73+
74+
// TeamRolesListResponse is the response from the GET /v1/teams/roles endpoint.
75+
type TeamRolesListResponse struct {
76+
Roles []TeamRole `json:"items"`
77+
}
78+
79+
func (c *TeamRolesClient) List(_ context.Context, params *TeamRolesListParams) (*TeamRolesListResponse, error) {
80+
apiResponse := &TeamRolesListResponse{}
81+
apiError := &ApiError{}
82+
83+
response, err := c.Client().Get(Route("teams_roles", "")).QueryStruct(&params).Receive(apiResponse, apiError)
84+
if err != nil {
85+
return nil, errors.New("could not get team roles: " + err.Error())
86+
}
87+
88+
err = c.client.handleResponseStatus(response, apiError)
89+
if err != nil {
90+
return nil, err
91+
}
92+
93+
return apiResponse, nil
94+
}
95+
96+
/***********************************************************************************************************************
97+
* POST /api/v1/teams/roles
98+
**********************************************************************************************************************/
99+
100+
type CreateTeamRoleRequest struct {
101+
Tag string `json:"tag"`
102+
Name string `json:"name"`
103+
Description string `json:"description,omitempty"`
104+
NotificationsEnabled bool `json:"notificationsEnabled,omitempty"`
105+
}
106+
107+
// ToCreateRequest converts a TeamRole to a CreateTeamRoleRequest.
108+
func (r *TeamRole) ToCreateRequest() CreateTeamRoleRequest {
109+
return CreateTeamRoleRequest{
110+
Tag: r.Tag,
111+
Name: r.Name,
112+
Description: r.Description,
113+
NotificationsEnabled: r.NotificationsEnabled,
114+
}
115+
}
116+
117+
func (c *TeamRolesClient) Create(_ context.Context, req CreateTeamRoleRequest) (*TeamRole, error) {
118+
apiResponse := &TeamRole{}
119+
apiError := &ApiError{}
120+
121+
response, err := c.Client().Post(Route("teams_roles", "")).BodyJSON(&req).Receive(apiResponse, apiError)
122+
if err != nil {
123+
return apiResponse, errors.New("could not create team role: " + err.Error())
124+
}
125+
126+
err = c.client.handleResponseStatus(response, apiError)
127+
if err != nil {
128+
reqJson, _ := json.Marshal(req)
129+
log.Printf("Failed creating team role: %+v\n\nRequest:\n%+v", err, string(reqJson))
130+
return apiResponse, err
131+
}
132+
133+
return apiResponse, nil
134+
}
135+
136+
/***********************************************************************************************************************
137+
* PUT /api/v1/teams/roles/:id
138+
**********************************************************************************************************************/
139+
140+
type UpdateTeamRoleRequest struct {
141+
ID int64 `json:"id"`
142+
Tag string `json:"tag"`
143+
Name string `json:"name"`
144+
Description string `json:"description,omitempty"`
145+
NotificationsEnabled bool `json:"notificationsEnabled,omitempty"`
146+
}
147+
148+
// ToUpdateRequest converts a TeamRole to an UpdateTeamRoleRequest.
149+
func (r *TeamRole) ToUpdateRequest() UpdateTeamRoleRequest {
150+
return UpdateTeamRoleRequest{
151+
ID: r.ID,
152+
Tag: r.Tag,
153+
Name: r.Name,
154+
Description: r.Description,
155+
NotificationsEnabled: r.NotificationsEnabled,
156+
}
157+
}
158+
159+
func (c *TeamRolesClient) Update(_ context.Context, req UpdateTeamRoleRequest) (*TeamRole, error) {
160+
apiResponse := &TeamRole{}
161+
apiError := &ApiError{}
162+
163+
response, err := c.Client().Put(Route("teams_roles", fmt.Sprintf("%d", req.ID))).BodyJSON(&req).Receive(apiResponse, apiError)
164+
if err != nil {
165+
return apiResponse, errors.New("could not update team role: " + err.Error())
166+
}
167+
168+
err = c.client.handleResponseStatus(response, apiError)
169+
if err != nil {
170+
reqJson, _ := json.Marshal(req)
171+
log.Printf("Failed updating team role: %+v\n\nRequest:\n%+v\n%+v", err, string(reqJson), apiError.String())
172+
return apiResponse, err
173+
}
174+
175+
return apiResponse, nil
176+
}
177+
178+
/***********************************************************************************************************************
179+
* DELETE /api/v1/teams/roles/:id
180+
**********************************************************************************************************************/
181+
182+
type DeleteTeamRoleResponse struct{}
183+
type DeleteTeamRoleRequest struct {
184+
ID int64 `json:"id"`
185+
}
186+
187+
func (c *TeamRolesClient) Delete(_ context.Context, id int64) error {
188+
apiError := &ApiError{}
189+
apiResponse := &DeleteTeamRoleResponse{}
190+
191+
req := DeleteTeamRoleRequest{ID: id}
192+
resp, err := c.Client().Delete(Route("teams_roles", fmt.Sprintf("%d", id))).QueryStruct(req).Receive(apiResponse, apiError)
193+
if err != nil {
194+
return fmt.Errorf("could not delete team role %d:\n\n%+v", id, err.Error())
195+
}
196+
197+
err = c.client.handleResponseStatus(resp, apiError)
198+
if err != nil {
199+
log.Printf("Could not delete team role %d:\n\n%+v", id, err.Error())
200+
return err
201+
}
202+
203+
return nil
204+
}

internal/cortex/team_roles_test.go

Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
package cortex_test
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/cortexapps/terraform-provider-cortex/internal/cortex"
9+
"github.com/stretchr/testify/suite"
10+
)
11+
12+
/** Suite *************************************************************************************************************/
13+
14+
type teamRolesSuite struct {
15+
suite.Suite
16+
}
17+
18+
func TestCortexTeamRoles(t *testing.T) {
19+
suite.Run(t, new(teamRolesSuite))
20+
}
21+
22+
func (s *teamRolesSuite) newTeamRole() *cortex.TeamRole {
23+
return &cortex.TeamRole{
24+
ID: 1,
25+
Tag: "engineer",
26+
Name: "Engineer",
27+
Description: "A team role",
28+
NotificationsEnabled: true,
29+
}
30+
}
31+
32+
func (s *teamRolesSuite) TestGet() {
33+
id := int64(1)
34+
expectedResp := s.newTeamRole()
35+
ctx := context.Background()
36+
c, teardown, err := setupClient(cortex.Route("teams_roles", fmt.Sprintf("%d", id)), expectedResp, AssertRequestMethod(s.T(), "GET"))
37+
defer teardown()
38+
s.NoError(err)
39+
40+
actualResp, err := c.TeamRoles().Get(ctx, id)
41+
s.NoError(err)
42+
s.Equal(expectedResp, actualResp)
43+
}
44+
45+
func (s *teamRolesSuite) TestList() {
46+
expectedResp := &cortex.TeamRolesListResponse{
47+
Roles: []cortex.TeamRole{
48+
*s.newTeamRole(),
49+
},
50+
}
51+
ctx := context.Background()
52+
c, teardown, err := setupClient(cortex.Route("teams_roles", ""), expectedResp, AssertRequestMethod(s.T(), "GET"))
53+
defer teardown()
54+
s.NoError(err)
55+
56+
actualResp, err := c.TeamRoles().List(ctx, &cortex.TeamRolesListParams{})
57+
s.NoError(err)
58+
s.Equal(expectedResp, actualResp)
59+
}
60+
61+
func (s *teamRolesSuite) TestCreate() {
62+
req := cortex.CreateTeamRoleRequest{
63+
Name: "Engineer",
64+
Tag: "engineer",
65+
Description: "A team role",
66+
NotificationsEnabled: true,
67+
}
68+
expectedResp := s.newTeamRole()
69+
ctx := context.Background()
70+
c, teardown, err := setupClient(
71+
cortex.Route("teams_roles", ""),
72+
expectedResp,
73+
AssertRequestMethod(s.T(), "POST"),
74+
AssertRequestBody(s.T(), req),
75+
)
76+
defer teardown()
77+
s.NoError(err)
78+
79+
actualResp, err := c.TeamRoles().Create(ctx, req)
80+
s.NoError(err)
81+
s.Equal(expectedResp, actualResp)
82+
}
83+
84+
func (s *teamRolesSuite) TestUpdate() {
85+
id := int64(1)
86+
req := cortex.UpdateTeamRoleRequest{
87+
ID: id,
88+
Name: "Engineer",
89+
Description: "A team role",
90+
NotificationsEnabled: true,
91+
}
92+
expectedResp := s.newTeamRole()
93+
ctx := context.Background()
94+
c, teardown, err := setupClient(
95+
cortex.Route("teams_roles", fmt.Sprintf("%d", id)),
96+
expectedResp,
97+
AssertRequestMethod(s.T(), "PUT"),
98+
AssertRequestBody(s.T(), req),
99+
)
100+
defer teardown()
101+
s.NoError(err)
102+
103+
actualResp, err := c.TeamRoles().Update(ctx, req)
104+
s.NoError(err)
105+
s.Equal(expectedResp, actualResp)
106+
}
107+
108+
func (s *teamRolesSuite) TestDelete() {
109+
id := int64(2)
110+
ctx := context.Background()
111+
c, teardown, err := setupClient(cortex.Route("teams_roles", fmt.Sprintf("%d", id)), nil, AssertRequestMethod(s.T(), "DELETE"))
112+
defer teardown()
113+
s.NoError(err)
114+
115+
err = c.TeamRoles().Delete(ctx, id)
116+
s.NoError(err)
117+
}

0 commit comments

Comments
 (0)