-
Notifications
You must be signed in to change notification settings - Fork 13
/
Quota.go
201 lines (159 loc) · 4.21 KB
/
Quota.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
/*
* This file is automatically generated
*/
package catalog
import (
"fmt"
v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/clients/api/v1"
apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
m "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/catalog/v1alpha1"
)
type QuotaMergeFunc func(*m.Quota, *m.Quota) (*m.Quota, error)
// QuotaMerge builds a merge option for an update operation
func QuotaMerge(f QuotaMergeFunc) v1.UpdateOption {
return v1.Merge(func(prev, new apiv1.Interface) (apiv1.Interface, error) {
p, n := &m.Quota{}, &m.Quota{}
switch t := prev.(type) {
case *m.Quota:
p = t
case *apiv1.ResourceInstance:
err := p.FromInstance(t)
if err != nil {
return nil, fmt.Errorf("merge: failed to unserialise prev resource: %w", err)
}
default:
return nil, fmt.Errorf("merge: failed to unserialise prev resource, unxexpected resource type: %T", t)
}
switch t := new.(type) {
case *m.Quota:
n = t
case *apiv1.ResourceInstance:
err := n.FromInstance(t)
if err != nil {
return nil, fmt.Errorf("merge: failed to unserialize new resource: %w", err)
}
default:
return nil, fmt.Errorf("merge: failed to unserialise new resource, unxexpected resource type: %T", t)
}
return f(p, n)
})
}
// QuotaClient - rest client for Quota resources that have a defined resource scope
type QuotaClient struct {
client v1.Scoped
}
// UnscopedQuotaClient - rest client for Quota resources that do not have a defined scope
type UnscopedQuotaClient struct {
client v1.Unscoped
}
// NewQuotaClient - creates a client that is not scoped to any resource
func NewQuotaClient(c v1.Base) (*UnscopedQuotaClient, error) {
client, err := c.ForKind(m.QuotaGVK())
if err != nil {
return nil, err
}
return &UnscopedQuotaClient{client}, nil
}
// WithScope - sets the resource scope for the client
func (c *UnscopedQuotaClient) WithScope(scope string) *QuotaClient {
return &QuotaClient{
c.client.WithScope(scope),
}
}
// Get - gets a resource by name
func (c *UnscopedQuotaClient) Get(name string) (*m.Quota, error) {
ri, err := c.client.Get(name)
if err != nil {
return nil, err
}
service := &m.Quota{}
service.FromInstance(ri)
return service, nil
}
// Update - updates a resource
func (c *UnscopedQuotaClient) Update(res *m.Quota, opts ...v1.UpdateOption) (*m.Quota, error) {
ri, err := res.AsInstance()
if err != nil {
return nil, err
}
resource, err := c.client.Update(ri, opts...)
if err != nil {
return nil, err
}
updated := &m.Quota{}
// Updates the resource in place
err = updated.FromInstance(resource)
if err != nil {
return nil, err
}
return updated, nil
}
// List - gets a list of resources
func (c *QuotaClient) List(options ...v1.ListOptions) ([]*m.Quota, error) {
riList, err := c.client.List(options...)
if err != nil {
return nil, err
}
result := make([]*m.Quota, len(riList))
for i := range riList {
result[i] = &m.Quota{}
err := result[i].FromInstance(riList[i])
if err != nil {
return nil, err
}
}
return result, nil
}
// Get - gets a resource by name
func (c *QuotaClient) Get(name string) (*m.Quota, error) {
ri, err := c.client.Get(name)
if err != nil {
return nil, err
}
service := &m.Quota{}
service.FromInstance(ri)
return service, nil
}
// Delete - deletes a resource
func (c *QuotaClient) Delete(res *m.Quota) error {
ri, err := res.AsInstance()
if err != nil {
return err
}
return c.client.Delete(ri)
}
// Create - creates a resource
func (c *QuotaClient) Create(res *m.Quota, opts ...v1.CreateOption) (*m.Quota, error) {
ri, err := res.AsInstance()
if err != nil {
return nil, err
}
cri, err := c.client.Create(ri, opts...)
if err != nil {
return nil, err
}
created := &m.Quota{}
err = created.FromInstance(cri)
if err != nil {
return nil, err
}
return created, err
}
// Update - updates a resource
func (c *QuotaClient) Update(res *m.Quota, opts ...v1.UpdateOption) (*m.Quota, error) {
ri, err := res.AsInstance()
if err != nil {
return nil, err
}
resource, err := c.client.Update(ri, opts...)
if err != nil {
return nil, err
}
updated := &m.Quota{}
// Updates the resource in place
err = updated.FromInstance(resource)
if err != nil {
return nil, err
}
return updated, nil
}