-
Notifications
You must be signed in to change notification settings - Fork 14
/
template.go
163 lines (140 loc) · 4.43 KB
/
template.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
package v2
import (
"context"
"time"
apiv2 "github.com/exoscale/egoscale/v2/api"
"github.com/exoscale/egoscale/v2/oapi"
)
// Template represents a Compute instance template.
type Template struct {
BootMode *string
Build *string
Checksum *string `req-for:"create"`
CreatedAt *time.Time
DefaultUser *string
Description *string
Family *string
ID *string `req-for:"delete"`
Name *string `req-for:"create"`
PasswordEnabled *bool `req-for:"create"`
SSHKeyEnabled *bool `req-for:"create"`
Size *int64
URL *string `req-for:"create"`
Version *string
Visibility *string
Zone *string
}
// ListTemplatesOpt represents an ListTemplates operation option.
type ListTemplatesOpt func(params *oapi.ListTemplatesParams)
// ListTemplatesWithFamily sets a family filter to list Templates with.
func ListTemplatesWithFamily(v string) ListTemplatesOpt {
return func(p *oapi.ListTemplatesParams) {
if v != "" {
p.Family = &v
}
}
}
// ListTemplatesWithVisibility sets a visibility filter to list Templates with (default: "public").
func ListTemplatesWithVisibility(v string) ListTemplatesOpt {
return func(p *oapi.ListTemplatesParams) {
if v != "" {
p.Visibility = (*oapi.ListTemplatesParamsVisibility)(&v)
}
}
}
func templateFromAPI(t *oapi.Template, zone string) *Template {
return &Template{
BootMode: (*string)(t.BootMode),
Build: t.Build,
Checksum: t.Checksum,
CreatedAt: t.CreatedAt,
DefaultUser: t.DefaultUser,
Description: t.Description,
Family: t.Family,
ID: t.Id,
Name: t.Name,
PasswordEnabled: t.PasswordEnabled,
SSHKeyEnabled: t.SshKeyEnabled,
Size: t.Size,
URL: t.Url,
Version: t.Version,
Visibility: (*string)(t.Visibility),
Zone: &zone,
}
}
// DeleteTemplate deletes a Template.
func (c *Client) DeleteTemplate(ctx context.Context, zone string, template *Template) error {
if err := validateOperationParams(template, "delete"); err != nil {
return err
}
resp, err := c.DeleteTemplateWithResponse(apiv2.WithZone(ctx, zone), *template.ID)
if err != nil {
return err
}
_, err = oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return err
}
return nil
}
// GetTemplate returns the Template corresponding to the specified ID.
func (c *Client) GetTemplate(ctx context.Context, zone, id string) (*Template, error) {
resp, err := c.GetTemplateWithResponse(apiv2.WithZone(ctx, zone), id)
if err != nil {
return nil, err
}
return templateFromAPI(resp.JSON200, zone), nil
}
// ListTemplates returns the list of existing Templates.
func (c *Client) ListTemplates(ctx context.Context, zone string, opts ...ListTemplatesOpt) ([]*Template, error) {
list := make([]*Template, 0)
defaultVisibility := oapi.TemplateVisibilityPublic
params := oapi.ListTemplatesParams{
Visibility: (*oapi.ListTemplatesParamsVisibility)(&defaultVisibility),
}
for _, opt := range opts {
opt(¶ms)
}
resp, err := c.ListTemplatesWithResponse(apiv2.WithZone(ctx, zone), ¶ms)
if err != nil {
return nil, err
}
if resp.JSON200.Templates != nil {
for i := range *resp.JSON200.Templates {
list = append(list, templateFromAPI(&(*resp.JSON200.Templates)[i], zone))
}
}
return list, nil
}
// RegisterTemplate registers a new Template.
func (c *Client) RegisterTemplate(ctx context.Context, zone string, template *Template) (*Template, error) {
if err := validateOperationParams(template, "create"); err != nil {
return nil, err
}
resp, err := c.RegisterTemplateWithResponse(
apiv2.WithZone(ctx, zone),
oapi.RegisterTemplateJSONRequestBody{
BootMode: (*oapi.RegisterTemplateJSONBodyBootMode)(template.BootMode),
Checksum: *template.Checksum,
DefaultUser: template.DefaultUser,
Description: template.Description,
Name: *template.Name,
PasswordEnabled: *template.PasswordEnabled,
SshKeyEnabled: *template.SSHKeyEnabled,
Url: *template.URL,
})
if err != nil {
return nil, err
}
res, err := oapi.NewPoller().
WithTimeout(c.timeout).
WithInterval(c.pollInterval).
Poll(ctx, c.OperationPoller(zone, *resp.JSON200.Id))
if err != nil {
return nil, err
}
return c.GetTemplate(ctx, zone, *res.(*oapi.Reference).Id)
}