This repository has been archived by the owner on Mar 11, 2021. It is now read-only.
/
team.go
103 lines (82 loc) · 2.94 KB
/
team.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package controller
import (
"strings"
"github.com/fabric8-services/fabric8-auth/app"
"github.com/fabric8-services/fabric8-auth/application"
"github.com/fabric8-services/fabric8-auth/authorization"
"github.com/fabric8-services/fabric8-auth/authorization/token/manager"
"github.com/fabric8-services/fabric8-auth/errors"
"github.com/fabric8-services/fabric8-auth/jsonapi"
"github.com/fabric8-services/fabric8-auth/log"
"github.com/goadesign/goa"
)
// TeamController implements the team resource.
type TeamController struct {
*goa.Controller
app application.Application
}
// NewTeamController creates a team controller.
func NewTeamController(service *goa.Service, app application.Application) *TeamController {
return &TeamController{Controller: service.NewController("TeamController"), app: app}
}
// Create runs the create action.
func (c *TeamController) Create(ctx *app.CreateTeamContext) error {
currentUser, err := manager.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError(err.Error()))
}
if ctx.Payload.Name == nil || len(strings.TrimSpace(*ctx.Payload.Name)) == 0 {
log.Error(ctx, map[string]interface{}{}, "organization name cannot be empty")
return jsonapi.JSONErrorResponse(ctx, goa.ErrBadRequest("organization name cannot be empty"))
}
teamID, err := c.app.TeamService().CreateTeam(ctx, *currentUser, *ctx.Payload.SpaceID, *ctx.Payload.Name)
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
"space_id": *ctx.Payload.SpaceID,
"team_name": *ctx.Payload.Name,
}, "failed to create team")
return jsonapi.JSONErrorResponse(ctx, err)
}
log.Debug(ctx, map[string]interface{}{
"team_id": teamID.String(),
}, "team created")
teamIDStr := teamID.String()
return ctx.Created(&app.CreateTeamResponse{
TeamID: &teamIDStr,
})
}
// List runs the list action.
func (c *TeamController) List(ctx *app.ListTeamContext) error {
currentUser, err := manager.ContextIdentity(ctx)
if err != nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError(err.Error()))
}
if currentUser == nil {
return jsonapi.JSONErrorResponse(ctx, errors.NewUnauthorizedError("error finding the current user"))
}
teams, err := c.app.TeamService().ListTeamsForIdentity(ctx, *currentUser)
if err != nil {
log.Error(ctx, map[string]interface{}{
"err": err,
}, "failed to list teams")
return jsonapi.JSONErrorResponse(ctx, errors.NewInternalError(err))
}
return ctx.OK(&app.IdentityTeamArray{
Data: convertToIdentityTeamData(teams),
})
}
func convertToIdentityTeamData(teams []authorization.IdentityAssociation) []*app.IdentityTeamData {
results := []*app.IdentityTeamData{}
for _, team := range teams {
teamData := &app.IdentityTeamData{
ID: team.IdentityID.String(),
Name: team.ResourceName,
Member: team.Member,
Roles: team.Roles,
SpaceID: *team.ParentResourceID,
}
results = append(results, teamData)
}
return results
}