-
Notifications
You must be signed in to change notification settings - Fork 63
/
get.go
157 lines (127 loc) · 4.15 KB
/
get.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
// SPDX-License-Identifier: BSD-3-Clause
// Copyright (c) 2024, Unikraft GmbH and The KraftKit Authors.
// Licensed under the BSD-3-Clause License (the "License").
// You may not use this file except in compliance with the License.
package get
import (
"context"
"encoding/json"
"fmt"
"github.com/MakeNowJust/heredoc"
"github.com/spf13/cobra"
"gopkg.in/yaml.v2"
kraftcloud "sdk.kraft.cloud"
"kraftkit.sh/cmdfactory"
"kraftkit.sh/config"
"kraftkit.sh/internal/cli/kraft/cloud/utils"
"kraftkit.sh/internal/tableprinter"
"kraftkit.sh/iostreams"
"kraftkit.sh/log"
)
type GetOptions struct {
Auth *config.AuthConfig `noattribute:"true"`
Client kraftcloud.KraftCloud `noattribute:"true"`
Metro string `noattribute:"true"`
Output string `long:"output" short:"o" usage:"Output format" default:"list"`
Policy string `long:"policy" short:"p" usage:"Get a policy instead of a configuration"`
Token string `noattribute:"true"`
}
func NewCmd() *cobra.Command {
cmd, err := cmdfactory.New(&GetOptions{}, cobra.Command{
Short: "Get an autoscale configuration or policy",
Use: "get [FLAGS] UUID|NAME",
Args: cobra.ExactArgs(1),
Aliases: []string{"gt"},
Long: "Get an autoscale configuration or policy of a service group.",
Example: heredoc.Doc(`
# Get an autoscale configuration by UUID of a service group
$ kraft cloud scale get fd1684ea-7970-4994-92d6-61dcc7905f2b
# Get an autoscale configuration by name of a service group
$ kraft cloud scale get my-service-group
# Get an autoscale policy by UUID of a service group
$ kraft cloud scale get fd1684ea-7970-4994-92d6-61dcc7905f2b --policy my-policy
# Get an autoscale policy by name of a service group
$ kraft cloud scale get my-service-group --policy my-policy
`),
Annotations: map[string]string{
cmdfactory.AnnotationHelpGroup: "kraftcloud-scale",
},
})
if err != nil {
panic(err)
}
return cmd
}
func (opts *GetOptions) Pre(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
return fmt.Errorf("specify a service group NAME or UUID")
}
err := utils.PopulateMetroToken(cmd, &opts.Metro, &opts.Token)
if err != nil {
return fmt.Errorf("could not populate metro and token: %w", err)
}
return nil
}
func (opts *GetOptions) Run(ctx context.Context, args []string) error {
var err error
if opts.Auth == nil {
opts.Auth, err = config.GetKraftCloudAuthConfig(ctx, opts.Token)
if err != nil {
return fmt.Errorf("could not retrieve credentials: %w", err)
}
}
if opts.Client == nil {
opts.Client = kraftcloud.NewClient(
kraftcloud.WithToken(config.GetKraftCloudTokenAuthConfig(*opts.Auth)),
)
}
id := args[0]
// Look up the configuration by name
if !utils.IsUUID(id) {
confResp, err := opts.Client.Services().WithMetro(opts.Metro).Get(ctx, id)
if err != nil {
return fmt.Errorf("could not get configuration: %w", err)
}
conf, err := confResp.FirstOrErr()
if err != nil {
return fmt.Errorf("could not get configuration: %w", err)
}
id = conf.UUID
}
if len(opts.Policy) > 0 {
policyResp, err := opts.Client.Autoscale().WithMetro(opts.Metro).GetPolicy(ctx, id, opts.Policy)
if err != nil {
return fmt.Errorf("could not get configuration: %w", err)
}
policy, err := policyResp.FirstOrErr()
if err != nil {
return fmt.Errorf("could not get configuration: %w", err)
}
err = iostreams.G(ctx).StartPager()
if err != nil {
log.G(ctx).Errorf("error starting pager: %v", err)
}
defer iostreams.G(ctx).StopPager()
var b []byte
switch opts.Output {
case string(tableprinter.OutputFormatJSON):
b, err = json.Marshal(&policy)
if err != nil {
return fmt.Errorf("could not marshal policy: %w", err)
}
default:
b, err = yaml.Marshal(&policy)
if err != nil {
return fmt.Errorf("could not marshal policy: %w", err)
}
}
fmt.Fprintf(iostreams.G(ctx).Out, "%s\n", b)
return nil
} else {
resp, err := opts.Client.Autoscale().WithMetro(opts.Metro).GetConfigurations(ctx, id)
if err != nil {
return fmt.Errorf("could not get configuration: %w", err)
}
return utils.PrintAutoscaleConfiguration(ctx, opts.Output, *resp)
}
}