-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmd_downscale.go
More file actions
67 lines (51 loc) · 1.71 KB
/
cmd_downscale.go
File metadata and controls
67 lines (51 loc) · 1.71 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
package main
import (
"context"
"fmt"
"strings"
"github.com/urfave/cli/v3"
)
func downscale(ctx context.Context, cmd *cli.Command) error {
deployment := cmd.String("deployment")
region := cmd.String("region")
profile := cmd.String("profile")
username := cmd.String("username")
password := cmd.String("password")
headroomPercent := cmd.Float64("headroom-pct")
recommendZoneChange := cmd.Bool("recommend-zone-change")
exitCode := cmd.Bool("exit-code")
if !isRegionValid(region) {
return fmt.Errorf("region %q is not a known Elastic Cloud region", region)
}
regionParts := strings.Split(region, "-")
if len(regionParts) != 2 {
return fmt.Errorf(`invalid region, expected format "<provider>-<region>", e.g. "azure-westeurope"`)
}
provider := regionParts[0]
providerRegion := regionParts[1]
tierDiskSizes, err := getTierSizes(region, profile)
if err != nil {
return err
}
verbosef(cmd, "%s", tierDiskSizes)
usernamePassword := ""
if username != "" && password != "" {
usernamePassword = fmt.Sprintf("%s:%s@", username, password)
}
deploymentURL := fmt.Sprintf("https://%s%s.es.%s.%s.elastic-cloud.com", usernamePassword, deployment, providerRegion, provider)
allocations, err := getAllocationInformation(deploymentURL)
if err != nil {
return err
}
recommendations := calcDownscaleRecommendation(allocations, tierDiskSizes, headroomPercent, recommendZoneChange)
fmt.Fprintf(cmd.Writer, "%s", recommendations)
if exitCode && recommendations.IsDownscalingRecommended() {
return cli.Exit("Downscaling for at least one tier is recommended", 2)
}
return nil
}
func verbosef(cmd *cli.Command, format string, args ...any) {
if cmd.Bool("verbose") {
fmt.Fprintf(cmd.Writer, format, args...)
}
}