/
organizations.go
163 lines (143 loc) · 5.28 KB
/
organizations.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package model
import (
"strings"
"github.com/go-openapi/strfmt"
"github.com/thoas/go-funk"
"github.com/ActiveState/cli/internal/errs"
"github.com/ActiveState/cli/internal/locale"
"github.com/ActiveState/cli/internal/logging"
"github.com/ActiveState/cli/pkg/platform/api"
"github.com/ActiveState/cli/pkg/platform/api/graphql"
"github.com/ActiveState/cli/pkg/platform/api/graphql/model"
"github.com/ActiveState/cli/pkg/platform/api/graphql/request"
clientOrgs "github.com/ActiveState/cli/pkg/platform/api/mono/mono_client/organizations"
"github.com/ActiveState/cli/pkg/platform/api/mono/mono_models"
"github.com/ActiveState/cli/pkg/platform/authentication"
)
var ErrMemberNotFound = errs.New("member not found")
// FetchOrganizations fetches all organizations for the current user.
func FetchOrganizations(auth *authentication.Auth) ([]*mono_models.Organization, error) {
authClient, err := auth.Client()
if err != nil {
return nil, errs.Wrap(err, "Could not get auth client")
}
params := clientOrgs.NewListOrganizationsParams()
memberOnly := true
params.SetMemberOnly(&memberOnly)
res, err := authClient.Organizations.ListOrganizations(params, auth.ClientAuth())
if err != nil {
return nil, processOrgErrorResponse(err)
}
return res.Payload, nil
}
// FetchOrgByURLName fetches an organization accessible to the current user by it's URL Name.
func FetchOrgByURLName(urlName string, auth *authentication.Auth) (*mono_models.Organization, error) {
authClient, err := auth.Client()
if err != nil {
return nil, errs.Wrap(err, "Could not get auth client")
}
params := clientOrgs.NewGetOrganizationParams()
params.OrganizationIdentifier = urlName
resOk, err := authClient.Organizations.GetOrganization(params, auth.ClientAuth())
if err != nil {
return nil, processOrgErrorResponse(err)
}
return resOk.Payload, nil
}
// FetchOrgMembers fetches the members of an organization accessible to the current user by it's URL Name.
func FetchOrgMembers(urlName string, auth *authentication.Auth) ([]*mono_models.Member, error) {
authClient, err := auth.Client()
if err != nil {
return nil, errs.Wrap(err, "Could not get auth client")
}
params := clientOrgs.NewGetOrganizationMembersParams()
params.OrganizationName = urlName
resOk, err := authClient.Organizations.GetOrganizationMembers(params, auth.ClientAuth())
if err != nil {
return nil, processOrgErrorResponse(err)
}
return resOk.Payload, nil
}
// FetchOrgMember fetches the member of an organization accessible to the current user by it's URL Name.
func FetchOrgMember(orgName, name string, auth *authentication.Auth) (*mono_models.Member, error) {
members, err := FetchOrgMembers(orgName, auth)
if err != nil {
return nil, err
}
for _, member := range members {
if strings.EqualFold(name, member.User.Username) {
return member, nil
}
}
return nil, locale.WrapError(ErrMemberNotFound, "err_api_member_not_found")
}
// InviteUserToOrg invites a single user (via email address) to a given
// organization.
//
// # The invited user can be added as an owner or a member
//
// Note: This method only returns the invitation for the new user, not existing
// users.
func InviteUserToOrg(orgName string, asOwner bool, email string, auth *authentication.Auth) (*mono_models.Invitation, error) {
authClient, err := auth.Client()
if err != nil {
return nil, errs.Wrap(err, "Could not get auth client")
}
params := clientOrgs.NewInviteOrganizationParams()
role := mono_models.RoleReader
if asOwner {
role = mono_models.RoleAdmin
}
body := clientOrgs.InviteOrganizationBody{
AddedOnly: true,
Role: &role,
}
params.SetOrganizationName(orgName)
params.SetAttributes(body)
params.SetEmail(email)
resOk, err := authClient.Organizations.InviteOrganization(params, auth.ClientAuth())
if err != nil {
return nil, processInviteErrorResponse(err)
}
if len(resOk.Payload) != 1 {
return nil, locale.NewError("err_api_org_invite_expected_one_invite")
}
return resOk.Payload[0], nil
}
// FetchOrganizationsByIDs fetches organizations by their IDs
func FetchOrganizationsByIDs(ids []strfmt.UUID, auth *authentication.Auth) ([]model.Organization, error) {
ids = funk.Uniq(ids).([]strfmt.UUID)
request := request.OrganizationsByIDs(ids)
gql := graphql.New(auth)
response := model.Organizations{}
err := gql.Run(request, &response)
if err != nil {
return nil, errs.Wrap(err, "gql.Run failed")
}
if len(response.Organizations) != len(ids) {
logging.Debug("Organization membership mismatch: %d members returned for %d members requested. Caller must account for this.", len(response.Organizations), len(ids))
}
return response.Organizations, nil
}
func processOrgErrorResponse(err error) error {
switch statusCode := api.ErrorCode(err); statusCode {
case 401:
return locale.NewInputError("err_api_not_authenticated")
case 404:
return locale.NewInputError("err_api_org_not_found")
default:
return err
}
}
func processInviteErrorResponse(err error) error {
switch statusCode := api.ErrorCode(err); statusCode {
case 400:
return locale.WrapInputError(err, "err_api_invite_400", "Invalid request, did you enter a valid email address?")
case 401:
return locale.NewInputError("err_api_not_authenticated")
case 404:
return locale.NewInputError("err_api_org_not_found")
default:
return locale.WrapError(err, api.ErrorMessageFromPayload(err))
}
}