-
Notifications
You must be signed in to change notification settings - Fork 13
/
APIService.go
225 lines (189 loc) · 4.97 KB
/
APIService.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
219
220
221
222
223
224
225
/*
* This file is automatically generated
*/
package management
import (
"encoding/json"
apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
"github.com/Axway/agent-sdk/pkg/util/log"
)
var (
APIServiceCtx log.ContextField = "apiService"
_APIServiceGVK = apiv1.GroupVersionKind{
GroupKind: apiv1.GroupKind{
Group: "management",
Kind: "APIService",
},
APIVersion: "v1alpha1",
}
APIServiceScopes = []string{"Environment"}
)
const (
APIServiceResourceName = "apiservices"
ApiServiceComplianceSubResourceName = "compliance"
ApiServiceDetailsSubResourceName = "details"
ApiServiceStatusSubResourceName = "status"
)
func APIServiceGVK() apiv1.GroupVersionKind {
return _APIServiceGVK
}
func init() {
apiv1.RegisterGVK(_APIServiceGVK, APIServiceScopes[0], APIServiceResourceName)
log.RegisterContextField(APIServiceCtx)
}
// APIService Resource
type APIService struct {
apiv1.ResourceMeta
// GENERATE: The following code has been modified after code generation
//
// Compliance ApiServiceCompliance `json:"compliance"`
Compliance *ApiServiceCompliance `json:"compliance,omitempty"`
Details ApiServiceDetails `json:"details"`
Owner *apiv1.Owner `json:"owner"`
Spec ApiServiceSpec `json:"spec"`
// Status ApiServiceStatus `json:"status"`
Status *apiv1.ResourceStatus `json:"status"`
}
// NewAPIService creates an empty *APIService
func NewAPIService(name, scopeName string) *APIService {
return &APIService{
ResourceMeta: apiv1.ResourceMeta{
Name: name,
GroupVersionKind: _APIServiceGVK,
Metadata: apiv1.Metadata{
Scope: apiv1.MetadataScope{
Name: scopeName,
Kind: APIServiceScopes[0],
},
},
},
}
}
// APIServiceFromInstanceArray converts a []*ResourceInstance to a []*APIService
func APIServiceFromInstanceArray(fromArray []*apiv1.ResourceInstance) ([]*APIService, error) {
newArray := make([]*APIService, 0)
for _, item := range fromArray {
res := &APIService{}
err := res.FromInstance(item)
if err != nil {
return make([]*APIService, 0), err
}
newArray = append(newArray, res)
}
return newArray, nil
}
// AsInstance converts a APIService to a ResourceInstance
func (res *APIService) AsInstance() (*apiv1.ResourceInstance, error) {
meta := res.ResourceMeta
meta.GroupVersionKind = APIServiceGVK()
res.ResourceMeta = meta
m, err := json.Marshal(res)
if err != nil {
return nil, err
}
instance := apiv1.ResourceInstance{}
err = json.Unmarshal(m, &instance)
if err != nil {
return nil, err
}
return &instance, nil
}
// FromInstance converts a ResourceInstance to a APIService
func (res *APIService) FromInstance(ri *apiv1.ResourceInstance) error {
if ri == nil {
res = nil
return nil
}
var err error
rawResource := ri.GetRawResource()
if rawResource == nil {
rawResource, err = json.Marshal(ri)
if err != nil {
return err
}
}
err = json.Unmarshal(rawResource, res)
return err
}
// MarshalJSON custom marshaller to handle sub resources
func (res *APIService) MarshalJSON() ([]byte, error) {
m, err := json.Marshal(&res.ResourceMeta)
if err != nil {
return nil, err
}
var out map[string]interface{}
err = json.Unmarshal(m, &out)
if err != nil {
return nil, err
}
out["compliance"] = res.Compliance
out["details"] = res.Details
out["owner"] = res.Owner
out["spec"] = res.Spec
out["status"] = res.Status
return json.Marshal(out)
}
// UnmarshalJSON custom unmarshaller to handle sub resources
func (res *APIService) UnmarshalJSON(data []byte) error {
var err error
aux := &apiv1.ResourceInstance{}
err = json.Unmarshal(data, aux)
if err != nil {
return err
}
res.ResourceMeta = aux.ResourceMeta
res.Owner = aux.Owner
// ResourceInstance holds the spec as a map[string]interface{}.
// Convert it to bytes, then convert to the spec type for the resource.
sr, err := json.Marshal(aux.Spec)
if err != nil {
return err
}
err = json.Unmarshal(sr, &res.Spec)
if err != nil {
return err
}
// marshalling subresource Compliance
if v, ok := aux.SubResources["compliance"]; ok {
sr, err = json.Marshal(v)
if err != nil {
return err
}
delete(aux.SubResources, "compliance")
err = json.Unmarshal(sr, &res.Compliance)
if err != nil {
return err
}
}
// marshalling subresource Details
if v, ok := aux.SubResources["details"]; ok {
sr, err = json.Marshal(v)
if err != nil {
return err
}
delete(aux.SubResources, "details")
err = json.Unmarshal(sr, &res.Details)
if err != nil {
return err
}
}
// marshalling subresource Status
if v, ok := aux.SubResources["status"]; ok {
sr, err = json.Marshal(v)
if err != nil {
return err
}
delete(aux.SubResources, "status")
// err = json.Unmarshal(sr, &res.Status)
res.Status = &apiv1.ResourceStatus{}
err = json.Unmarshal(sr, res.Status)
if err != nil {
return err
}
}
return nil
}
// PluralName returns the plural name of the resource
func (res *APIService) PluralName() string {
return APIServiceResourceName
}