-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathversion.go
347 lines (297 loc) · 9.75 KB
/
version.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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
package main
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"os"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/marketplacecatalog"
"github.com/aws/aws-sdk-go-v2/service/marketplacecatalog/types"
"gopkg.in/yaml.v2"
)
//source data structure read from the version YAML file
type YAMLVersionData struct {
ID string `json:"id"`
Releasenotes string `json:"releasenotes"`
Upgradeinstructions string `json:"upgradeinstructions"`
Versiontitle string `json:"versiontitle"`
Creationdate time.Time `json:"creationdate"`
Sources []Sources `json:"sources"`
Deliveryoptions []Deliveryoptions `json:"deliveryoptions"`
}
type PlatformCompatibility struct {
Platform string `json:"platform"`
}
type Sources struct {
Type string `json:"type"`
ID string `json:"id"`
Images []string `json:"images"`
Compatibility ServicesCompatibility `json:"compatibility"`
}
type ServicesCompatibility struct {
Awsservices []string `json:"awsservices"`
}
type Instructions struct {
Usage string `json:"usage"`
}
type Deploymentresources struct {
Text string `json:"text"`
URL string `json:"url"`
}
type Recommendations struct {
Deploymentresources []Deploymentresources `json:"deploymentresources"`
}
type Deliveryoptions struct {
ID string `json:"id"`
Type string `json:"type"`
Sourceid string `json:"sourceid"`
Title string `json:"title"`
Shortdescription string `json:"shortdescription"`
Isrecommended bool `json:"isrecommended"`
Compatibility ServicesCompatibility `json:"compatibility"`
Instructions Instructions `json:"instructions"`
Recommendations Recommendations `json:"recommendations"`
Visibility string `json:"visibility"`
}
// destination data structure
type DstVersionData struct {
Version Version `json:"Version"`
DeliveryOptions []DeliveryOptions `json:"DeliveryOptions"`
}
type Version struct {
ReleaseNotes string `json:"ReleaseNotes"`
VersionTitle string `json:"VersionTitle"`
}
type DeploymentResources struct {
Name string `json:"Name"`
URL string `json:"Url"`
}
type EcrDeliveryOptionDetails struct {
DeploymentResources []DeploymentResources `json:"DeploymentResources"`
CompatibleServices []string `json:"CompatibleServices"`
ContainerImages []string `json:"ContainerImages"`
Description string `json:"Description"`
UsageInstructions string `json:"UsageInstructions"`
}
type Details struct {
EcrDeliveryOptionDetails EcrDeliveryOptionDetails `json:"EcrDeliveryOptionDetails"`
}
type DeliveryOptions struct {
Details Details `json:"Details"`
DeliveryOptionTitle string `json:"DeliveryOptionTitle"`
}
func (src YAMLVersionData) convertToDst() DstVersionData {
var dst DstVersionData
var deliveryOptions []DeliveryOptions
var version Version
// set version fields
version.ReleaseNotes = src.Releasenotes
version.VersionTitle = src.Versiontitle
// set delivery options fields
for _, deliveryOption := range src.Deliveryoptions {
var d DeliveryOptions
var details Details
d.DeliveryOptionTitle = deliveryOption.Title
// set EcrDeliveryOptionDetails fields
details.EcrDeliveryOptionDetails.Description = deliveryOption.Shortdescription
details.EcrDeliveryOptionDetails.UsageInstructions = deliveryOption.Instructions.Usage
details.EcrDeliveryOptionDetails.ContainerImages = src.Sources[0].Images
details.EcrDeliveryOptionDetails.CompatibleServices = deliveryOption.Compatibility.Awsservices
for _, deploymentResource := range deliveryOption.Recommendations.Deploymentresources {
details.EcrDeliveryOptionDetails.DeploymentResources = append(details.EcrDeliveryOptionDetails.DeploymentResources, DeploymentResources{
Name: deploymentResource.Text,
URL: deploymentResource.URL,
})
}
d.Details = details
deliveryOptions = append(deliveryOptions, d)
}
dst.Version = version
dst.DeliveryOptions = deliveryOptions
return dst
}
func getYAMLData(fileName string) (*YAMLVersionData, error) {
yamlFile, err := ioutil.ReadFile(fileName)
if err != nil {
return nil, err
}
var data YAMLVersionData
if err := yaml.Unmarshal(yamlFile, &data); err != nil {
return nil, err
}
return &data, nil
}
func dumpVersions(productName string) error {
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return err
}
svc := marketplacecatalog.NewFromConfig(cfg)
productTypes := []string{
"ServerProduct",
"ContainerProduct",
"DataProduct",
"MachinelearningProduct",
"SaaSProduct",
"ServiceProduct",
"SolutionProduct",
"SupportProduct",
}
var entityID *string
var lastErr error
for _, productType := range productTypes {
entityID, lastErr = getProductEntityID(svc, &productName, productType)
if lastErr == nil {
break
}
}
if lastErr != nil {
return fmt.Errorf("could not find product %s in any supported type: %v", productName, lastErr)
}
resp, err := svc.DescribeEntity(context.Background(), &marketplacecatalog.DescribeEntityInput{
EntityId: entityID,
Catalog: aws.String("AWSMarketplace"),
})
if err != nil {
return err
}
var details EntityDetails
if err := json.Unmarshal([]byte(*resp.Details), &details); err != nil {
return err
}
for _, version := range details.Versions {
fileName := getYamlFilePath(productName, "versions", version.VersionTitle)
data, err := yaml.Marshal(version)
if err != nil {
return err
}
// Check if file has changed before writing to it
if _, err := os.Stat(fileName); err == nil {
existingData, err := ioutil.ReadFile(fileName)
if err != nil {
return err
}
if bytes.Equal(existingData, data) {
fmt.Printf("Data for entity %s version %s has not changed\n", *entityID, version.VersionTitle)
continue
}
}
if err := ioutil.WriteFile(fileName, data, 0644); err != nil {
return err
}
fmt.Printf("Data written to %s\n", fileName)
}
return nil
}
func pushNewVersion(productName string, noOp bool, version string) error {
cfg, err := config.LoadDefaultConfig(context.Background())
if err != nil {
return errors.New("couldn't load default config")
}
svc := marketplacecatalog.NewFromConfig(cfg)
productTypes := []string{
"ServerProduct",
"ContainerProduct",
"DataProduct",
"MachinelearningProduct",
"SaaSProduct",
"ServiceProduct",
"SolutionProduct",
"SupportProduct",
}
var entityID *string
var lastErr error
var foundType string
for _, productType := range productTypes {
entityID, lastErr = getProductEntityID(svc, &productName, productType)
if lastErr == nil {
foundType = productType
break
}
}
if lastErr != nil {
return fmt.Errorf("could not find product %s in any supported type: %v", productName, lastErr)
}
srcVersionDetails, err := getYAMLData(getYamlFilePath(productName, "versions", version))
if err != nil {
return errors.New("could not read version details: " + err.Error())
}
dstVersionDetails := srcVersionDetails.convertToDst()
versionBytes, err := json.Marshal(dstVersionDetails)
if err != nil {
return err
}
if noOp {
changeSetJSON, _ := json.MarshalIndent(dstVersionDetails, "", " ")
fmt.Println(string(changeSetJSON))
return nil
}
entityTypeIdentifier, _ := getEntityTypeAndChangeType(foundType)
// Define the version change type based on product type
versionChangeType := "AddDeliveryOptions"
if foundType == "ServerProduct" {
versionChangeType = "CreateVersion"
}
// Create a changeset to update the product
change := types.Change{
ChangeType: aws.String(versionChangeType),
ChangeName: aws.String("AddNewVersion"),
Entity: &types.Entity{
Type: aws.String(entityTypeIdentifier),
Identifier: entityID,
},
Details: aws.String(string(versionBytes)),
}
changeSetInput := &marketplacecatalog.StartChangeSetInput{
Catalog: aws.String("AWSMarketplace"),
ChangeSet: []types.Change{
change,
},
ChangeSetName: aws.String(fmt.Sprintf("Push %s version %s", productName, version)),
}
if noOp {
changeSetJSON, _ := json.MarshalIndent(changeSetInput, "", " ")
fmt.Println(string(changeSetJSON))
return nil
}
_, err = svc.StartChangeSet(context.Background(), changeSetInput)
if err != nil {
return errors.New("could not start change set: " + err.Error())
}
fmt.Printf("Changeset created for product %s (%s) with entity ID %s\n", productName, foundType, *entityID)
return nil
}
func cloneProductVersion(productName, srcVersion, dstVersion string) error {
srcFilePath := getYamlFilePath(productName, "versions", srcVersion)
dstFilePath := getYamlFilePath(productName, "versions", dstVersion)
existingData, err := ioutil.ReadFile(dstFilePath)
if err == nil {
srcData, err := ioutil.ReadFile(srcFilePath)
if err != nil {
return fmt.Errorf("failed to read source file: %v", err)
}
if bytes.Equal(existingData, srcData) {
fmt.Printf("Data for product %s version %s has not changed\n", productName, srcVersion)
return nil
}
} else if !os.IsNotExist(err) {
return fmt.Errorf("failed to read destination file: %v", err)
}
input, err := ioutil.ReadFile(srcFilePath)
if err != nil {
return fmt.Errorf("failed to read source file: %v", err)
}
// Replace srcVersion with dstVersion inside the YAML content
output := bytes.Replace(input, []byte(srcVersion), []byte(dstVersion), -1)
err = ioutil.WriteFile(dstFilePath, output, 0644)
if err != nil {
return fmt.Errorf("failed to write destination file: %v", err)
}
fmt.Printf("Data written to %s\n", dstFilePath)
return nil
}