-
Notifications
You must be signed in to change notification settings - Fork 25
/
client.go
177 lines (159 loc) · 4.96 KB
/
client.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2021 Upbound Inc
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package organizations
import (
"context"
"errors"
"fmt"
"net/http"
"path"
"strconv"
"github.com/upbound/up-sdk-go"
)
const (
basePath = "v1/organizations"
errOrgNotFound = "could not find an organization with given name"
)
// Client is an organizations client.
type Client struct {
*up.Config
}
// NewClient builds an organizations client from the passed config.
func NewClient(cfg *up.Config) *Client {
return &Client{
cfg,
}
}
// Create an organization on Upbound.
func (c *Client) Create(ctx context.Context, params *OrganizationCreateParameters) error {
req, err := c.Client.NewRequest(ctx, http.MethodPost, basePath, "", params)
if err != nil {
return err
}
return c.Client.Do(req, nil)
}
// Get an organization on Upbound.
func (c *Client) Get(ctx context.Context, id uint) (*Organization, error) {
req, err := c.Client.NewRequest(ctx, http.MethodGet, basePath, strconv.FormatUint(uint64(id), 10), nil)
if err != nil {
return nil, err
}
org := &Organization{}
err = c.Client.Do(req, &org)
if err != nil {
return nil, err
}
return org, nil
}
// GetOrgID returns the ID of an org given its name.
// There is a requirement that orgs are uniquely named, so we know
// there is at most one org with the given name.
func (c *Client) GetOrgID(ctx context.Context, name string) (uint, error) {
orgs, err := c.List(ctx)
if err != nil {
return 0, err
}
for _, o := range orgs {
if o.Name == name {
return o.ID, nil
}
}
return 0, errors.New(errOrgNotFound)
}
// List all organizations for the authenticated user on Upbound.
func (c *Client) List(ctx context.Context) ([]Organization, error) {
req, err := c.Client.NewRequest(ctx, http.MethodGet, basePath, "", nil)
if err != nil {
return nil, err
}
orgs := []Organization{}
err = c.Client.Do(req, &orgs)
if err != nil {
return nil, err
}
return orgs, nil
}
// ListRobots list all robots the user can access in the organization on
// Upbound.
// TODO(hasheddan): move this to robots client when API is updated.
func (c *Client) ListRobots(ctx context.Context, id uint) ([]Robot, error) {
req, err := c.Client.NewRequest(ctx, http.MethodGet, basePath, path.Join(strconv.FormatUint(uint64(id), 10), "robots"), nil)
if err != nil {
return nil, err
}
rs := []Robot{}
err = c.Client.Do(req, &rs)
if err != nil {
return nil, err
}
return rs, nil
}
// Delete an organization on Upbound.
func (c *Client) Delete(ctx context.Context, id uint) error {
req, err := c.Client.NewRequest(ctx, http.MethodDelete, basePath, strconv.FormatUint(uint64(id), 10), nil)
if err != nil {
return err
}
return c.Client.Do(req, nil)
}
// ListInvites list all invites for the organization on Upbound.
func (c *Client) ListInvites(ctx context.Context, orgID uint) ([]Invite, error) {
req, err := c.Client.NewRequest(ctx, http.MethodGet, basePath, fmt.Sprintf("%d/invites", orgID), nil)
if err != nil {
return nil, err
}
invites := []Invite{}
err = c.Client.Do(req, &invites)
if err != nil {
return nil, err
}
return invites, nil
}
// DeleteInvite deletes an invite for the organization on Upbound.
func (c *Client) DeleteInvite(ctx context.Context, orgID uint, inviteID uint) error {
req, err := c.Client.NewRequest(ctx, http.MethodDelete, basePath, fmt.Sprintf("%d/invites/%d", orgID, inviteID), nil)
if err != nil {
return err
}
return c.Client.Do(req, nil)
}
// CreateInvite creates an invite for the organization on Upbound.
func (c *Client) CreateInvite(ctx context.Context, orgID uint, params *OrganizationInviteCreateParameters) error {
req, err := c.Client.NewRequest(ctx, http.MethodPost, basePath, fmt.Sprintf("%d/invites", orgID), params)
if err != nil {
return err
}
return c.Client.Do(req, nil)
}
// ListMembers list all members for the organization on Upbound.
func (c *Client) ListMembers(ctx context.Context, orgID uint) ([]Member, error) {
req, err := c.Client.NewRequest(ctx, http.MethodGet, basePath, fmt.Sprintf("%d/members", orgID), nil)
if err != nil {
return nil, err
}
members := []Member{}
err = c.Client.Do(req, &members)
if err != nil {
return nil, err
}
return members, nil
}
// RemoveMember removes a member for the organization on Upbound.
func (c *Client) RemoveMember(ctx context.Context, orgID uint, userID uint) error {
req, err := c.Client.NewRequest(ctx, http.MethodDelete, basePath, fmt.Sprintf("%d/members/%d", orgID, userID), nil)
if err != nil {
return err
}
return c.Client.Do(req, nil)
}