-
Notifications
You must be signed in to change notification settings - Fork 0
/
server_plan_cmd.go
76 lines (61 loc) · 2.12 KB
/
server_plan_cmd.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
// Copyright (c) 2017-2021 Ingram Micro Inc.
package cmd
import (
"github.com/ingrammicro/cio/api/cloud"
"github.com/ingrammicro/cio/utils"
"github.com/ingrammicro/cio/utils/format"
"github.com/urfave/cli"
)
// WireUpServerPlan prepares common resources to send request to Concerto API
func WireUpServerPlan(c *cli.Context) (ds *cloud.ServerPlanService, f format.Formatter) {
f = format.GetFormatter()
config, err := utils.GetConcertoConfig()
if err != nil {
f.PrintFatal("Couldn't wire up config", err)
}
hcs, err := utils.NewHTTPConcertoService(config)
if err != nil {
f.PrintFatal("Couldn't wire up concerto service", err)
}
ds, err = cloud.NewServerPlanService(hcs)
if err != nil {
f.PrintFatal("Couldn't wire up server plan service", err)
}
return ds, f
}
// ServerPlanList subcommand function
func ServerPlanList(c *cli.Context) error {
debugCmdFuncInfo(c)
serverPlanSvc, formatter := WireUpServerPlan(c)
checkRequiredFlags(c, []string{"cloud-provider-id", "realm-id"}, formatter)
serverPlans, err := serverPlanSvc.ListServerPlans(c.String("cloud-provider-id"), c.String("realm-id"))
if err != nil {
formatter.PrintFatal("Couldn't receive serverPlan data", err)
}
cloudProvidersMap := LoadCloudProvidersMapping(c)
locationsMap := LoadLocationsMapping(c)
for id, sp := range serverPlans {
serverPlans[id].CloudProviderName = cloudProvidersMap[sp.CloudProviderID]
serverPlans[id].LocationName = locationsMap[sp.LocationID]
}
if err = formatter.PrintList(serverPlans); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}
// ServerPlanShow subcommand function
func ServerPlanShow(c *cli.Context) error {
debugCmdFuncInfo(c)
serverPlanSvc, formatter := WireUpServerPlan(c)
checkRequiredFlags(c, []string{"id"}, formatter)
serverPlan, err := serverPlanSvc.GetServerPlan(c.String("id"))
if err != nil {
formatter.PrintFatal("Couldn't receive serverPlan data", err)
}
locationsMap := LoadLocationsMapping(c)
serverPlan.LocationName = locationsMap[serverPlan.LocationID]
if err = formatter.PrintItem(*serverPlan); err != nil {
formatter.PrintFatal(PrintFormatError, err)
}
return nil
}