This repository has been archived by the owner on Jun 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
/
imagecatalogs.go
184 lines (151 loc) · 6.47 KB
/
imagecatalogs.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
package cli
import (
"strconv"
"time"
log "github.com/Sirupsen/logrus"
"github.com/hortonworks/cb-cli/cli/cloud"
"github.com/hortonworks/cb-cli/cli/utils"
"github.com/hortonworks/cb-cli/client_cloudbreak/v1imagecatalogs"
"github.com/hortonworks/cb-cli/models_cloudbreak"
"github.com/urfave/cli"
)
var imagecatalogHeader []string = []string{"Name", "Default", "URL"}
type imagecatalogOut struct {
Name string `json:"Name" yaml:"Name"`
Default bool `json:"Default" yaml:"Default"`
URL string `json:"URL" yaml:"URL"`
}
func (r *imagecatalogOut) DataAsStringArray() []string {
return []string{r.Name, strconv.FormatBool(r.Default), r.URL}
}
var imageHeader []string = []string{"Date", "Description", "Version", "ImageID"}
type imageOut struct {
Date string `json:"Date" yaml:"Date"`
Description string `json:"Description" yaml:"Description"`
Version string `json:"Version" yaml:"Version"`
ImageID string `json:"ImageID" yaml:"ImageID"`
}
func (r *imageOut) DataAsStringArray() []string {
return []string{r.Date, r.Description, r.Version, r.ImageID}
}
func CreateImagecatalogFromUrl(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
log.Infof("[CreateImagecatalogFromUrl] creating imagecatalog from a URL")
cbClient := NewCloudbreakHTTPClientFromContext(c)
createImagecatalogImpl(
cbClient.Cloudbreak.V1imagecatalogs,
c.String(FlName.Name),
c.Bool(FlPublicOptional.Name),
c.String(FlURL.Name))
}
type imagecatalogClient interface {
PostPrivateImageCatalog(params *v1imagecatalogs.PostPrivateImageCatalogParams) (*v1imagecatalogs.PostPrivateImageCatalogOK, error)
PostPublicImageCatalog(params *v1imagecatalogs.PostPublicImageCatalogParams) (*v1imagecatalogs.PostPublicImageCatalogOK, error)
}
func createImagecatalogImpl(client imagecatalogClient, name string, public bool, imagecatalogURL string) {
defer utils.TimeTrack(time.Now(), "create imagecatalog")
imagecatalogRequest := &models_cloudbreak.ImageCatalogRequest{
Name: &name,
URL: &imagecatalogURL,
}
var ic *models_cloudbreak.ImageCatalogResponse
if public {
log.Infof("[createImagecatalogImpl] sending create public imagecatalog request")
resp, err := client.PostPublicImageCatalog(v1imagecatalogs.NewPostPublicImageCatalogParams().WithBody(imagecatalogRequest))
if err != nil {
utils.LogErrorAndExit(err)
}
ic = resp.Payload
} else {
log.Infof("[createImagecatalogImpl] sending create private imagecatalog request")
resp, err := client.PostPrivateImageCatalog(v1imagecatalogs.NewPostPrivateImageCatalogParams().WithBody(imagecatalogRequest))
if err != nil {
utils.LogErrorAndExit(err)
}
ic = resp.Payload
}
log.Infof("[createImagecatalogImpl] imagecatalog created: %s (id: %d)", ic.Name, ic.ID)
}
func ListImagecatalogs(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list imagecatalogs")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listImagecatalogsImpl(cbClient.Cloudbreak.V1imagecatalogs, output.WriteList)
}
type getPublicsImagecatalogsClient interface {
GetPublicsImageCatalogs(*v1imagecatalogs.GetPublicsImageCatalogsParams) (*v1imagecatalogs.GetPublicsImageCatalogsOK, error)
}
func listImagecatalogsImpl(client getPublicsImagecatalogsClient, writer func([]string, []utils.Row)) {
log.Infof("[listImagecatalogsImpl] sending imagecatalog list request")
imagecatalogResp, err := client.GetPublicsImageCatalogs(v1imagecatalogs.NewGetPublicsImageCatalogsParams())
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, ic := range imagecatalogResp.Payload {
tableRows = append(tableRows, &imagecatalogOut{*ic.Name, ic.UsedAsDefault, *ic.URL})
}
writer(imagecatalogHeader, tableRows)
}
func DeleteImagecatalog(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "delete imagecatalog")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[DeleteImagecatalog] sending delete imagecatalog request with name: %s", name)
if err := cbClient.Cloudbreak.V1imagecatalogs.DeletePublicImageCatalogByName(v1imagecatalogs.NewDeletePublicImageCatalogByNameParams().WithName(name)); err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[DeleteImagecatalog] imagecatalog deleted, name: %s", name)
}
func SetDefaultImagecatalog(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "set default imagecatalog")
cbClient := NewCloudbreakHTTPClientFromContext(c)
name := c.String(FlName.Name)
log.Infof("[SetDefautlImagecatalog] sending set default imagecatalog request with name: %s", name)
if _, err := cbClient.Cloudbreak.V1imagecatalogs.PutSetDefaultImageCatalogByName(v1imagecatalogs.NewPutSetDefaultImageCatalogByNameParams().WithName(name)); err != nil {
utils.LogErrorAndExit(err)
}
log.Infof("[SetDefaultImagecatalog] imagecatalog is set as default, name: %s", name)
}
func ListAwsImages(c *cli.Context) {
cloud.SetProviderType(cloud.AWS)
listImages(c)
}
func ListAzureImages(c *cli.Context) {
cloud.SetProviderType(cloud.AZURE)
listImages(c)
}
func ListGcpImages(c *cli.Context) {
cloud.SetProviderType(cloud.GCP)
listImages(c)
}
func ListOpenstackImages(c *cli.Context) {
cloud.SetProviderType(cloud.OPENSTACK)
listImages(c)
}
func listImages(c *cli.Context) {
checkRequiredFlagsAndArguments(c)
defer utils.TimeTrack(time.Now(), "list available images")
cbClient := NewCloudbreakHTTPClientFromContext(c)
output := utils.Output{Format: c.String(FlOutputOptional.Name)}
listImagesImpl(cbClient.Cloudbreak.V1imagecatalogs, output.WriteList, c.String(FlImageCatalog.Name))
}
type getPublicImagesClient interface {
GetPublicImagesByProviderAndCustomImageCatalog(*v1imagecatalogs.GetPublicImagesByProviderAndCustomImageCatalogParams) (*v1imagecatalogs.GetPublicImagesByProviderAndCustomImageCatalogOK, error)
}
func listImagesImpl(client getPublicImagesClient, writer func([]string, []utils.Row), imagecatalog string) {
log.Infof("[listImagesImpl] sending list images request")
provider := cloud.GetProvider().GetName()
imageResp, err := client.GetPublicImagesByProviderAndCustomImageCatalog(v1imagecatalogs.NewGetPublicImagesByProviderAndCustomImageCatalogParams().WithName(imagecatalog).WithPlatform(*provider))
if err != nil {
utils.LogErrorAndExit(err)
}
tableRows := []utils.Row{}
for _, i := range imageResp.Payload.BaseImages {
tableRows = append(tableRows, &imageOut{i.Date, i.Description, i.Version, i.UUID})
}
writer(imageHeader, tableRows)
}