-
Notifications
You must be signed in to change notification settings - Fork 37
Expand file tree
/
Copy pathcreate.go
More file actions
239 lines (215 loc) · 7.98 KB
/
create.go
File metadata and controls
239 lines (215 loc) · 7.98 KB
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
package create
import (
"context"
"encoding/json"
"fmt"
"github.com/goccy/go-yaml"
"github.com/stackitcloud/stackit-cli/internal/cmd/params"
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
"github.com/stackitcloud/stackit-cli/internal/pkg/examples"
"github.com/stackitcloud/stackit-cli/internal/pkg/flags"
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
"github.com/stackitcloud/stackit-cli/internal/pkg/print"
"github.com/stackitcloud/stackit-cli/internal/pkg/projectname"
"github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/client"
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/spf13/cobra"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
"github.com/stackitcloud/stackit-sdk-go/services/dns/wait"
)
const (
nameFlag = "name"
dnsNameFlag = "dns-name"
defaultTTLFlag = "default-ttl"
primaryFlag = "primary"
aclFlag = "acl"
typeFlag = "type"
retryTimeFlag = "retry-time"
refreshTimeFlag = "refresh-time"
negativeCacheFlag = "negative-cache"
isReverseZoneFlag = "is-reverse-zone"
expireTimeFlag = "expire-time"
descriptionFlag = "description"
contactEmailFlag = "contact-email"
)
type inputModel struct {
*globalflags.GlobalFlagModel
Name *string
DnsName *string
DefaultTTL *int64
Primaries *[]string
Acl *string
Type *dns.CreateZonePayloadTypes
RetryTime *int64
RefreshTime *int64
NegativeCache *int64
IsReverseZone *bool
ExpireTime *int64
Description *string
ContactEmail *string
}
func NewCmd(params *params.CmdParams) *cobra.Command {
cmd := &cobra.Command{
Use: "create",
Short: "Creates a DNS zone",
Long: "Creates a DNS zone.",
Args: args.NoArgs,
Example: examples.Build(
examples.NewExample(
`Create a DNS zone with name "my-zone" and DNS name "www.my-zone.com"`,
"$ stackit dns zone create --name my-zone --dns-name www.my-zone.com"),
examples.NewExample(
`Create a DNS zone with name "my-zone", DNS name "www.my-zone.com" and default time to live of 1000ms`,
"$ stackit dns zone create --name my-zone --dns-name www.my-zone.com --default-ttl 1000"),
),
RunE: func(cmd *cobra.Command, _ []string) error {
ctx := context.Background()
model, err := parseInput(params.Printer, cmd)
if err != nil {
return err
}
// Configure API client
apiClient, err := client.ConfigureClient(params.Printer, params.CliVersion)
if err != nil {
return err
}
projectLabel, err := projectname.GetProjectName(ctx, params.Printer, params.CliVersion, cmd)
if err != nil {
params.Printer.Debug(print.ErrorLevel, "get project name: %v", err)
projectLabel = model.ProjectId
}
if !model.AssumeYes {
prompt := fmt.Sprintf("Are you sure you want to create a zone for project %q?", projectLabel)
err = params.Printer.PromptForConfirmation(prompt)
if err != nil {
return err
}
}
// Call API
req := buildRequest(ctx, model, apiClient)
resp, err := req.Execute()
if err != nil {
return fmt.Errorf("create DNS zone: %w", err)
}
zoneId := *resp.Zone.Id
// Wait for async operation, if async mode not enabled
if !model.Async {
s := spinner.New(params.Printer)
s.Start("Creating zone")
_, err = wait.CreateZoneWaitHandler(ctx, apiClient, model.ProjectId, zoneId).WaitWithContext(ctx)
if err != nil {
return fmt.Errorf("wait for DNS zone creation: %w", err)
}
s.Stop()
}
return outputResult(params.Printer, model, projectLabel, resp)
},
}
configureFlags(cmd)
return cmd
}
func configureFlags(cmd *cobra.Command) {
var typeFlagOptions []string
for _, val := range dns.AllowedCreateZonePayloadTypesEnumValues {
typeFlagOptions = append(typeFlagOptions, string(val))
}
cmd.Flags().String(nameFlag, "", "User given name of the zone")
cmd.Flags().String(dnsNameFlag, "", "Fully qualified domain name of the DNS zone")
cmd.Flags().Int64(defaultTTLFlag, 1000, "Default time to live")
cmd.Flags().StringSlice(primaryFlag, []string{}, "Primary name server for secondary zone")
cmd.Flags().String(aclFlag, "", "Access control list")
cmd.Flags().Var(flags.EnumFlag(false, "", append(typeFlagOptions, "")...), typeFlag, fmt.Sprintf("Zone type, one of: %q", typeFlagOptions))
cmd.Flags().Int64(retryTimeFlag, 0, "Retry time")
cmd.Flags().Int64(refreshTimeFlag, 0, "Refresh time")
cmd.Flags().Int64(negativeCacheFlag, 0, "Negative cache")
cmd.Flags().Bool(isReverseZoneFlag, false, "Is reverse zone")
cmd.Flags().Int64(expireTimeFlag, 0, "Expire time")
cmd.Flags().String(descriptionFlag, "", "Description of the zone")
cmd.Flags().String(contactEmailFlag, "", "Contact email for the zone")
err := flags.MarkFlagsRequired(cmd, nameFlag, dnsNameFlag)
cobra.CheckErr(err)
}
func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
globalFlags := globalflags.Parse(p, cmd)
if globalFlags.ProjectId == "" {
return nil, &errors.ProjectIdError{}
}
var zoneType *dns.CreateZonePayloadTypes
if zoneTypeString := flags.FlagToStringPointer(p, cmd, typeFlag); zoneTypeString != nil && *zoneTypeString != "" {
zoneType = dns.CreateZonePayloadTypes(*zoneTypeString).Ptr()
}
model := inputModel{
GlobalFlagModel: globalFlags,
Name: flags.FlagToStringPointer(p, cmd, nameFlag),
DnsName: flags.FlagToStringPointer(p, cmd, dnsNameFlag),
DefaultTTL: flags.FlagToInt64Pointer(p, cmd, defaultTTLFlag),
Primaries: flags.FlagToStringSlicePointer(p, cmd, primaryFlag),
Acl: flags.FlagToStringPointer(p, cmd, aclFlag),
Type: zoneType,
RetryTime: flags.FlagToInt64Pointer(p, cmd, retryTimeFlag),
RefreshTime: flags.FlagToInt64Pointer(p, cmd, refreshTimeFlag),
NegativeCache: flags.FlagToInt64Pointer(p, cmd, negativeCacheFlag),
IsReverseZone: flags.FlagToBoolPointer(p, cmd, isReverseZoneFlag),
ExpireTime: flags.FlagToInt64Pointer(p, cmd, expireTimeFlag),
Description: flags.FlagToStringPointer(p, cmd, descriptionFlag),
ContactEmail: flags.FlagToStringPointer(p, cmd, contactEmailFlag),
}
if p.IsVerbosityDebug() {
modelStr, err := print.BuildDebugStrFromInputModel(model)
if err != nil {
p.Debug(print.ErrorLevel, "convert model to string for debugging: %v", err)
} else {
p.Debug(print.DebugLevel, "parsed input values: %s", modelStr)
}
}
return &model, nil
}
func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClient) dns.ApiCreateZoneRequest {
req := apiClient.CreateZone(ctx, model.ProjectId)
req = req.CreateZonePayload(dns.CreateZonePayload{
Name: model.Name,
DnsName: model.DnsName,
DefaultTTL: model.DefaultTTL,
Primaries: model.Primaries,
Acl: model.Acl,
Type: model.Type,
RetryTime: model.RetryTime,
RefreshTime: model.RefreshTime,
NegativeCache: model.NegativeCache,
IsReverseZone: model.IsReverseZone,
ExpireTime: model.ExpireTime,
Description: model.Description,
ContactEmail: model.ContactEmail,
})
return req
}
func outputResult(p *print.Printer, model *inputModel, projectLabel string, resp *dns.ZoneResponse) error {
if resp == nil {
return fmt.Errorf("dns zone response is empty")
}
switch model.OutputFormat {
case print.JSONOutputFormat:
details, err := json.MarshalIndent(resp, "", " ")
if err != nil {
return fmt.Errorf("marshal DNS zone: %w", err)
}
p.Outputln(string(details))
return nil
case print.YAMLOutputFormat:
details, err := yaml.MarshalWithOptions(resp, yaml.IndentSequence(true), yaml.UseJSONMarshaler())
if err != nil {
return fmt.Errorf("marshal DNS zone: %w", err)
}
p.Outputln(string(details))
return nil
default:
operationState := "Created"
if model.Async {
operationState = "Triggered creation of"
}
p.Outputf("%s zone for project %q. Zone ID: %s\n", operationState, projectLabel, utils.PtrString(resp.Zone.Id))
return nil
}
}