forked from openshift-online/ocm-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sync_addons.go
211 lines (188 loc) · 5.47 KB
/
sync_addons.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
/*
Copyright (c) 2019 Red Hat, 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.
*/
// This example shows how to update the collection of add-ons from an external source. To simplify
// things that external source is a JSON document embedded in this source file, but it could be an
// external file or an external collection of files.
package main
import (
"context"
"fmt"
"os"
sdk "github.com/openshift-online/ocm-sdk-go"
cmv1 "github.com/openshift-online/ocm-sdk-go/clustersmgmt/v1"
"github.com/openshift-online/ocm-sdk-go/logging"
)
func main() {
// Create a context:
ctx := context.Background()
// Create a logger that has the debug level enabled:
logger, err := logging.NewGoLoggerBuilder().
Debug(true).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build logger: %v\n", err)
os.Exit(1)
}
// Create the connection, and remember to close it:
token := os.Getenv("OCM_TOKEN")
connection, err := sdk.NewConnectionBuilder().
Logger(logger).
URL("https://clusters-service.apps-crc.testing").
Insecure(true).
Tokens(token).
BuildContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build connection: %v\n", err)
os.Exit(1)
}
defer connection.Close()
// Get the client for the collection of add-ons:
collection := connection.ClustersMgmt().V1().Addons()
// Load the sets of add-ons from the JSON file and from the API:
fileIndex, err := loadJSON(ctx, fileData)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't load JSON data: %v\n", err)
os.Exit(1)
}
apiIndex, err := loadAPI(ctx, collection)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't load API data: %v\n", err)
os.Exit(1)
}
// Add to the API the items that are in the file but not in the API:
for id, fileItem := range fileIndex {
_, ok := apiIndex[id]
if !ok {
apiItem, err := cmv1.NewAddOn().
Copy(fileItem).
Enabled(true).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build add-on: %v\n", err)
os.Exit(1)
}
_, err = collection.Add().
Body(apiItem).
SendContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't add add-on: %v\n", err)
os.Exit(1)
}
}
}
// Update in the API the items that are in the file and in the API:
for id, fileItem := range fileIndex {
_, ok := apiIndex[id]
if ok {
apiItem, err := cmv1.NewAddOn().
Name(fileItem.Name()).
Description(fileItem.Description()).
Icon(fileItem.Icon()).
Enabled(true).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build patch: %v\n", err)
os.Exit(1)
}
_, err = collection.Addon(id).Update().
Body(apiItem).
SendContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't update add-on: %v\n", err)
os.Exit(1)
}
}
}
// Disable in the API the items that are in the API but not in the file:
for id := range apiIndex {
_, ok := fileIndex[id]
if !ok {
apiItem, err := cmv1.NewAddOn().
Enabled(false).
Build()
if err != nil {
fmt.Fprintf(os.Stderr, "Can't build patch: %v\n", err)
os.Exit(1)
}
_, err = collection.Addon(id).Update().
Body(apiItem).
SendContext(ctx)
if err != nil {
fmt.Fprintf(os.Stderr, "Can't update add-on: %v\n", err)
os.Exit(1)
}
}
}
}
// loadJSON loads the add-ons from the JSON document and returns a map where the key is the
// identifier of the add-on and the value is the add-on object.
func loadJSON(ctx context.Context, data []byte) (result map[string]*cmv1.AddOn, err error) {
// Load the list of add-ons:
items, err := cmv1.UnmarshalAddOnList(data)
if err != nil {
return
}
// Populate the map:
result = map[string]*cmv1.AddOn{}
for _, item := range items {
result[item.ID()] = item
}
return
}
// loadAPI loads the add-ons from the API and returns a map where the key is the identifier of the
// add-on and the value is the add-on object.
func loadAPI(ctx context.Context, collection *cmv1.AddOnsClient) (result map[string]*cmv1.AddOn,
err error) {
// Retrieve the list of add-ons using pages of ten items, till we get a page that has less
// items than requests, as that marks the end of the collection:
size := 10
page := 1
for {
// Retrieve the page:
var response *cmv1.AddOnsListResponse
response, err = collection.List().
Size(size).
Page(page).
SendContext(ctx)
if err != nil {
return
}
// Process the page:
result = map[string]*cmv1.AddOn{}
response.Items().Each(func(item *cmv1.AddOn) bool {
result[item.ID()] = item
return true
})
// Break the loop if the size of the page is less than requested, otherwise go to
// the next page:
if response.Size() < size {
break
}
page++
}
return
}
var fileData = []byte(`[
{
"id": "black",
"name": "Black add-on",
"description": "Very black add-on",
"icon": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg=="
},
{
"id": "white",
"name": "White add-on",
"description": "Very white add-on",
"icon": "iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAAAAAA6fptVAAAACklEQVQI12NgAAAAAgAB4iG8MwAAAABJRU5ErkJggg=="
}
]`)