This repository has been archived by the owner on Jul 16, 2020. It is now read-only.
/
tenants.go
218 lines (169 loc) · 5.18 KB
/
tenants.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
//
// Copyright (c) 2017 Intel Corporation
//
// 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 client
import (
"bytes"
"encoding/json"
"fmt"
"github.com/ciao-project/ciao/ciao-controller/api"
"github.com/ciao-project/ciao/ciao-controller/types"
jsonpatch "github.com/evanphx/json-patch"
"github.com/pkg/errors"
)
func (client *Client) getCiaoQuotasResource() (string, error) {
return client.getCiaoResource("tenants", api.TenantsV1)
}
// UpdateQuotas updates the quotas for a given tenant
func (client *Client) UpdateQuotas(tenantID string, quotas []types.QuotaDetails) error {
if !client.IsPrivileged() {
return errors.New("This command is only available to admins")
}
url, err := client.getCiaoQuotasResource()
if err != nil {
return errors.Wrap(err, "Error getting quotas resource")
}
url = fmt.Sprintf("%s/%s/quotas", url, tenantID)
req := types.QuotaUpdateRequest{Quotas: quotas}
err = client.putResource(url, api.TenantsV1, &req)
return err
}
// ListQuotas lists the quotas for the specified tenant
func (client *Client) ListQuotas(tenantID string) ([]types.QuotaDetails, error) {
var result types.QuotaListResponse
url, err := client.getCiaoQuotasResource()
if err != nil {
return result.Quotas, errors.Wrap(err, "Error getting quotas resource")
}
if tenantID != "" {
url = fmt.Sprintf("%s/%s/quotas", url, tenantID)
} else {
url = fmt.Sprintf("%s/quotas", url)
}
err = client.getResource(url, api.TenantsV1, nil, &result)
return result.Quotas, err
}
func (client *Client) getCiaoTenantsResource() (string, error) {
url, err := client.getCiaoResource("tenants", api.TenantsV1)
return url, err
}
func (client *Client) getCiaoTenantRef(ID string) (string, error) {
var tenants types.TenantsListResponse
query := queryValue{
name: "id",
value: ID,
}
url, err := client.getCiaoTenantsResource()
if err != nil {
return "", err
}
if !client.IsPrivileged() {
return url, errors.New("This command is only available to admins")
}
err = client.getResource(url, api.TenantsV1, []queryValue{query}, &tenants)
if err != nil {
return "", err
}
if len(tenants.Tenants) != 1 {
return "", errors.New("No tenant by that ID found")
}
links := tenants.Tenants[0].Links
url = client.getRef("self", links)
if url == "" {
return url, errors.New("invalid link returned from controller")
}
return url, nil
}
// GetTenantConfig gets the tenant configuration
func (client *Client) GetTenantConfig(ID string) (types.TenantConfig, error) {
var config types.TenantConfig
url, err := client.getCiaoTenantRef(ID)
if err != nil {
return config, err
}
err = client.getResource(url, api.TenantsV1, nil, &config)
return config, err
}
// UpdateTenantConfig updates the tenant configuration
func (client *Client) UpdateTenantConfig(ID string, config types.TenantConfig) error {
url, err := client.getCiaoTenantRef(ID)
if err != nil {
return err
}
var oldconfig types.TenantConfig
err = client.getResource(url, api.TenantsV1, nil, &oldconfig)
if err != nil {
return err
}
a, err := json.Marshal(oldconfig)
if err != nil {
return err
}
if config.Name == "" {
config.Name = oldconfig.Name
}
if config.SubnetBits == 0 {
config.SubnetBits = oldconfig.SubnetBits
}
b, err := json.Marshal(config)
if err != nil {
return err
}
merge, err := jsonpatch.CreateMergePatch(a, b)
if err != nil {
return err
}
body := bytes.NewReader(merge)
resp, err := client.sendHTTPRequest("PATCH", url, nil, body, "merge-patch+json")
defer resp.Body.Close()
return err
}
// CreateTenantConfig creates a new tenant configuration
func (client *Client) CreateTenantConfig(tenantID string, config types.TenantConfig) (types.TenantSummary, error) {
var req types.TenantRequest
var summary types.TenantSummary
if !client.IsPrivileged() {
return summary, errors.New("This command is only available to admins")
}
url, err := client.getCiaoTenantsResource()
if err != nil {
return summary, err
}
req.ID = tenantID
req.Config = config
err = client.postResource(url, api.TenantsV1, &req, &summary)
return summary, err
}
// DeleteTenant deletes the given tenant
func (client *Client) DeleteTenant(tenantID string) error {
if !client.IsPrivileged() {
return errors.New("This command is only available to admins")
}
url, err := client.getCiaoTenantRef(tenantID)
if err != nil {
return err
}
return client.deleteResource(url, api.TenantsV1)
}
// ListTenants returns a list of the tenants
func (client *Client) ListTenants() (types.TenantsListResponse, error) {
var tenants types.TenantsListResponse
url, err := client.getCiaoTenantsResource()
if err != nil {
return tenants, err
}
err = client.getResource(url, api.TenantsV1, nil, &tenants)
return tenants, err
}