Skip to content

Commit

Permalink
feat: Invitations API
Browse files Browse the repository at this point in the history
  • Loading branch information
efirs committed May 8, 2023
1 parent 03b0761 commit 0bb4e04
Show file tree
Hide file tree
Showing 7 changed files with 411 additions and 4 deletions.
70 changes: 70 additions & 0 deletions driver/grpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -731,6 +731,76 @@ func (c *grpcDriver) GetAccessToken(ctx context.Context, clientID string, client
return (*TokenResponse)(r), nil
}

func (c *grpcDriver) CreateInvitations(ctx context.Context, invitations []*InvitationInfo) error {
invs := make([]*api.InvitationInfo, 0, len(invitations))
for _, a := range invitations {
invs = append(invs, (*api.InvitationInfo)(a))
}

_, err := c.auth.CreateInvitations(ctx, &api.CreateInvitationsRequest{
Invitations: invs,
})
if err != nil {
return GRPCError(err)
}

return nil
}

func (c *grpcDriver) DeleteInvitations(ctx context.Context, email string, status string) error {
_, err := c.auth.DeleteInvitations(ctx, &api.DeleteInvitationsRequest{
Email: email,
Status: &status,
})
if err != nil {
return GRPCError(err)
}

return nil
}

func (c *grpcDriver) ListInvitations(ctx context.Context, status string) ([]*Invitation, error) {
resp, err := c.auth.ListInvitations(ctx, &api.ListInvitationsRequest{
Status: &status,
})
if err != nil {
return nil, GRPCError(err)
}

invs := make([]*Invitation, 0, len(resp.Invitations))
for _, a := range resp.Invitations {
invs = append(invs, (*Invitation)(a))
}

return invs, nil
}

func (c *grpcDriver) VerifyInvitation(ctx context.Context, email string, code string) error {
_, err := c.auth.VerifyInvitation(ctx, &api.VerifyInvitationRequest{
Email: email,
Code: code,
})
if err != nil {
return GRPCError(err)
}

return nil
}

func (c *grpcDriver) ListUsers(ctx context.Context) ([]*User, error) {
resp, err := c.auth.ListUsers(ctx, &api.ListUsersRequest{})
if err != nil {
return nil, GRPCError(err)
}

users := make([]*User, 0, len(resp.Users))
for _, a := range resp.Users {
users = append(users, (*User)(a))
}

return users, nil
}

func (c *grpcDriver) CreateNamespace(ctx context.Context, name string) error {
_, err := c.mgmt.CreateNamespace(ctx, &api.CreateNamespaceRequest{Name: name})
if err != nil {
Expand Down
79 changes: 79 additions & 0 deletions driver/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,85 @@ func getAccessToken(ctx context.Context, tokenURL string, cfg *config.Driver, cl
return &tr, nil
}

func (c *httpDriver) CreateInvitations(ctx context.Context, invitations []*InvitationInfo) error {
invs := make([]apiHTTP.InvitationInfo, 0, len(invitations))
for _, a := range invitations {
invs = append(invs, apiHTTP.InvitationInfo{
Email: &a.Email,
InvitationSentByName: &a.InvitationSentByName,
Role: &a.Role,
})
}

resp, err := c.api.AuthCreateInvitations(ctx, apiHTTP.AuthCreateInvitationsJSONRequestBody{
Invitations: &invs,
})
if err = HTTPError(err, resp); err != nil {
return err
}

return nil
}

func (c *httpDriver) DeleteInvitations(ctx context.Context, email string, status string) error {
resp, err := c.api.AuthDeleteInvitations(ctx, apiHTTP.AuthDeleteInvitationsJSONRequestBody{
Email: &email,
Status: &status,
})

return HTTPError(err, resp)
}

func (c *httpDriver) ListInvitations(ctx context.Context, status string) ([]*Invitation, error) {
resp, err := c.api.AuthListInvitations(ctx, &apiHTTP.AuthListInvitationsParams{
Status: &status,
})
if err = HTTPError(err, resp); err != nil {
return nil, err
}

var r api.ListInvitationsResponse
if err := respDecode(resp.Body, &r); err != nil {
return nil, err
}

invs := make([]*Invitation, 0, len(r.Invitations))
for _, a := range r.Invitations {
invs = append(invs, (*Invitation)(a))
}

return invs, nil
}

func (c *httpDriver) VerifyInvitation(ctx context.Context, email string, code string) error {
resp, err := c.api.AuthVerifyInvitation(ctx, apiHTTP.AuthVerifyInvitationJSONRequestBody{
Email: &email,
Code: &code,
})

return HTTPError(err, resp)
}

func (c *httpDriver) ListUsers(ctx context.Context) ([]*User, error) {
resp, err := c.api.AuthListUsers(ctx)

if err = HTTPError(err, resp); err != nil {
return nil, err
}

var r api.ListUsersResponse
if err := respDecode(resp.Body, &r); err != nil {
return nil, err
}

users := make([]*User, 0, len(r.Users))
for _, a := range r.Users {
users = append(users, (*User)(a))
}

return users, nil
}

func (c *httpDriver) CreateNamespace(ctx context.Context, name string) error {
resp, err := c.api.ManagementCreateNamespace(ctx, apiHTTP.ManagementCreateNamespaceJSONRequestBody{Name: &name})
if err := HTTPError(err, resp); err != nil {
Expand Down
6 changes: 6 additions & 0 deletions driver/management.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,12 @@ type Management interface {
CreateNamespace(ctx context.Context, name string) error
ListNamespaces(ctx context.Context) ([]*Namespace, error)

CreateInvitations(ctx context.Context, invitations []*InvitationInfo) error
DeleteInvitations(ctx context.Context, email string, status string) error
ListInvitations(ctx context.Context, status string) ([]*Invitation, error)
VerifyInvitation(ctx context.Context, email string, code string) error
ListUsers(ctx context.Context) ([]*User, error)

Close() error
}

Expand Down
2 changes: 2 additions & 0 deletions driver/management_grpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,8 @@ func TestGRPCAuthDriver(t *testing.T) {
testGlobalAppKeys(t, drv, mockServers.API)
testDriverAuthNegative(t, drv, managementClient, mockServers.API, mockServers.Mgmt)
testGlobalGlobalAppKeysNegative(t, drv, mockServers.API)
testInvitations(t, managementClient, mockServers.Auth)
testInvitationsNegative(t, managementClient, mockServers.Auth)
}

func TestGRPCDriverCredentials(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions driver/management_http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ func TestHTTPAuthDriver(t *testing.T) {
testGlobalAppKeys(t, drv, mockServers.API)
testDriverAuthNegative(t, drv, managementClient, mockServers.API, mockServers.Mgmt)
testGlobalGlobalAppKeysNegative(t, drv, mockServers.API)
testInvitations(t, managementClient, mockServers.Auth)
testInvitationsNegative(t, managementClient, mockServers.Auth)
}

func TestHTTPGetInfo(t *testing.T) {
Expand Down
Loading

0 comments on commit 0bb4e04

Please sign in to comment.