-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_profiles.go
More file actions
54 lines (42 loc) · 1.07 KB
/
cmd_profiles.go
File metadata and controls
54 lines (42 loc) · 1.07 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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"net/http"
"sort"
"github.com/urfave/cli/v3"
)
type DeploymentTemplates []DeploymentTemplate
func listProfiles(ctx context.Context, cmd *cli.Command) error {
region := cmd.String("region")
if !isRegionValid(region) {
return fmt.Errorf("region %q is not a known Elastic Cloud region", region)
}
deploymentTemplatesURL := fmt.Sprintf("https://api.elastic-cloud.com/api/v1/deployments/templates?region=%s", region)
resp, err := http.Get(deploymentTemplatesURL)
if err != nil {
return err
}
defer func() {
_ = resp.Body.Close()
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return err
}
var deploymentTemplates DeploymentTemplates
err = json.Unmarshal(body, &deploymentTemplates)
if err != nil {
return err
}
sort.Slice(deploymentTemplates, func(i, j int) bool {
return deploymentTemplates[i].ID < deploymentTemplates[j].ID
})
fmt.Fprintf(cmd.Writer, "Profiles for %q:\n", region)
for _, dt := range deploymentTemplates {
fmt.Fprintf(cmd.Writer, "%s\n", dt.ID)
}
return nil
}