-
Notifications
You must be signed in to change notification settings - Fork 6
/
image.go
197 lines (173 loc) · 5.56 KB
/
image.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
package resources
import (
"bufio"
"context"
"crypto/tls"
"crypto/x509"
"fmt"
"path/filepath"
"github.com/ionos-cloud/ionosctl/v6/internal/client"
"github.com/fatih/structs"
ionoscloud "github.com/ionos-cloud/sdk-go/v6"
"github.com/kardianos/ftps"
)
type Image struct {
ionoscloud.Image
}
type Images struct {
ionoscloud.Images
}
type ImageProperties struct {
ionoscloud.ImageProperties
}
// UploadProperties contains info needed to initialize an FTP connection to IONOS server and upload an image.
type UploadProperties struct {
ImageFileProperties
FTPServerProperties
}
type ImageFileProperties struct {
Path string // File name, server path (not local) and file extension included
DataBuffer *bufio.Reader
}
type FTPServerProperties struct {
Url string // Server URL without any directory path. Example: ftp-fkb.ionos.com
Port int
SkipVerify bool // Skip FTP server certificate verification. WARNING man-in-the-middle attack possible
ServerCertificate *x509.CertPool // If FTP server uses self signed certificates, put this in tlsConfig. IONOS FTP Servers in prod DON'T need this
Username string
Password string
}
// ImagesService is a wrapper around ionoscloud.Image
type ImagesService interface {
List(params ListQueryParams) (Images, *Response, error)
Get(imageId string, params QueryParams) (*Image, *Response, error)
Update(imageId string, imgProp ImageProperties, params QueryParams) (*Image, *Response, error)
Delete(imageId string, params QueryParams) (*Response, error)
}
type imagesService struct {
client *ionoscloud.APIClient
context context.Context
}
var _ ImagesService = &imagesService{}
func NewImageService(client *client.Client, ctx context.Context) ImagesService {
return &imagesService{
client: client.CloudClient,
context: ctx,
}
}
func FtpUpload(ctx context.Context, p UploadProperties) error {
tlsConfig := tls.Config{
InsecureSkipVerify: p.SkipVerify,
ServerName: p.Url,
RootCAs: p.ServerCertificate,
MaxVersion: tls.VersionTLS12,
}
dialOptions := ftps.DialOptions{
Host: p.Url,
Port: p.Port,
Username: p.Username,
Passowrd: p.Password,
ExplicitTLS: true,
TLSConfig: &tlsConfig,
}
c, err := ftps.Dial(ctx, dialOptions)
if err != nil {
return fmt.Errorf("dialing FTP server failed. Check username & password. FTP server doesn't support usage of JWT token: %w", err)
}
err = c.Chdir(filepath.Dir(p.Path))
if err != nil {
return fmt.Errorf("failed while changing directory within FTP server: %w", err)
}
files, err := c.List(ctx)
if err != nil {
return fmt.Errorf("failed while listing files within FTP server: %w", err)
}
// Check if there already exists an image with the given name at the location
desiredFileName := filepath.Base(p.Path)
var errExists error
for _, f := range files {
if f.Name == desiredFileName {
errExists = fmt.Errorf("%s already exists at %s. Please contact support at support@cloud.ionos.com to delete the old image. We're sorry for the inconvenience", desiredFileName, p.Url)
}
}
err = c.Upload(ctx, desiredFileName, p.DataBuffer)
if err != nil {
err = fmt.Errorf("failed while uploading %s to FTP server: %w", desiredFileName, err)
if errExists != nil {
err = fmt.Errorf("%w\nNote: %w", err, errExists)
}
return err
}
return c.Close()
}
func (s *imagesService) List(params ListQueryParams) (Images, *Response, error) {
req := s.client.ImagesApi.ImagesGet(s.context)
if !structs.IsZero(params) {
if params.Filters != nil {
for k, v := range *params.Filters {
for _, val := range v {
req = req.Filter(k, val)
}
}
}
if params.OrderBy != nil {
req = req.OrderBy(*params.OrderBy)
}
if params.MaxResults != nil {
req = req.MaxResults(*params.MaxResults)
}
if !structs.IsZero(params.QueryParams) {
if params.QueryParams.Depth != nil {
req = req.Depth(*params.QueryParams.Depth)
}
if params.QueryParams.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.QueryParams.Pretty)
}
}
}
images, resp, err := s.client.ImagesApi.ImagesGetExecute(req)
return Images{images}, &Response{*resp}, err
}
func (s *imagesService) Get(imageId string, params QueryParams) (*Image, *Response, error) {
req := s.client.ImagesApi.ImagesFindById(s.context, imageId)
if !structs.IsZero(params) {
if params.Depth != nil {
req = req.Depth(*params.Depth)
}
if params.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.Pretty)
}
}
image, resp, err := s.client.ImagesApi.ImagesFindByIdExecute(req)
return &Image{image}, &Response{*resp}, err
}
func (s *imagesService) Update(imageId string, imgProp ImageProperties, params QueryParams) (*Image, *Response, error) {
req := s.client.ImagesApi.ImagesPatch(s.context, imageId).Image(imgProp.ImageProperties)
if !structs.IsZero(params) {
if params.Depth != nil {
req = req.Depth(*params.Depth)
}
if params.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.Pretty)
}
}
image, resp, err := s.client.ImagesApi.ImagesPatchExecute(req)
return &Image{image}, &Response{*resp}, err
}
func (s *imagesService) Delete(imageId string, params QueryParams) (*Response, error) {
req := s.client.ImagesApi.ImagesDelete(s.context, imageId)
if !structs.IsZero(params) {
if params.Depth != nil {
req = req.Depth(*params.Depth)
}
if params.Pretty != nil {
// Currently not implemented
req = req.Pretty(*params.Pretty)
}
}
resp, err := s.client.ImagesApi.ImagesDeleteExecute(req)
return &Response{*resp}, err
}