-
Notifications
You must be signed in to change notification settings - Fork 13
/
Category.go
154 lines (121 loc) · 3.32 KB
/
Category.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
/*
* 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 CategoryMergeFunc func(*m.Category, *m.Category) (*m.Category, error)
// CategoryMerge builds a merge option for an update operation
func CategoryMerge(f CategoryMergeFunc) v1.UpdateOption {
return v1.Merge(func(prev, new apiv1.Interface) (apiv1.Interface, error) {
p, n := &m.Category{}, &m.Category{}
switch t := prev.(type) {
case *m.Category:
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.Category:
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)
})
}
// CategoryClient - rest client for Category resources that have a defined resource scope
type CategoryClient struct {
client v1.Scoped
}
// NewCategoryClient - creates a client scoped to a particular resource
func NewCategoryClient(c v1.Base) (*CategoryClient, error) {
client, err := c.ForKind(m.CategoryGVK())
if err != nil {
return nil, err
}
return &CategoryClient{client}, nil
}
// List - gets a list of resources
func (c *CategoryClient) List(options ...v1.ListOptions) ([]*m.Category, error) {
riList, err := c.client.List(options...)
if err != nil {
return nil, err
}
result := make([]*m.Category, len(riList))
for i := range riList {
result[i] = &m.Category{}
err := result[i].FromInstance(riList[i])
if err != nil {
return nil, err
}
}
return result, nil
}
// Get - gets a resource by name
func (c *CategoryClient) Get(name string) (*m.Category, error) {
ri, err := c.client.Get(name)
if err != nil {
return nil, err
}
service := &m.Category{}
service.FromInstance(ri)
return service, nil
}
// Delete - deletes a resource
func (c *CategoryClient) Delete(res *m.Category) error {
ri, err := res.AsInstance()
if err != nil {
return err
}
return c.client.Delete(ri)
}
// Create - creates a resource
func (c *CategoryClient) Create(res *m.Category, opts ...v1.CreateOption) (*m.Category, 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.Category{}
err = created.FromInstance(cri)
if err != nil {
return nil, err
}
return created, err
}
// Update - updates a resource
func (c *CategoryClient) Update(res *m.Category, opts ...v1.UpdateOption) (*m.Category, 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.Category{}
// Updates the resource in place
err = updated.FromInstance(resource)
if err != nil {
return nil, err
}
return updated, nil
}