-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
clouds.go
159 lines (136 loc) · 3.92 KB
/
clouds.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
158
159
package cmd
import (
"errors"
"fmt"
"strings"
"github.com/eezhee/eezhee/pkg/core"
"github.com/eezhee/eezhee/pkg/digitalocean"
"github.com/eezhee/eezhee/pkg/linode"
"github.com/eezhee/eezhee/pkg/vultr"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
func init() {
rootCmd.AddCommand(cloudsCmd)
cloudsCmd.AddCommand(listCloudsCmd)
cloudsCmd.AddCommand(digitaloceanApiKeyCmd)
cloudsCmd.AddCommand(linodeApiKeyCmd)
cloudsCmd.AddCommand(vultrApiKeyCmd)
}
var cloudsCmd = &cobra.Command{
Use: "clouds",
Short: "Details about cloud providers supported",
Long: `List or config cloud providers used by eezhee`,
}
// list which providers we support
var listCloudsCmd = &cobra.Command{
Use: "list",
Short: "List supported cloud providers",
Long: `List supported cloud providers and if they are configured"`,
Run: func(cmd *cobra.Command, args []string) {
fmt.Println("Enabled Clouds:")
// go through each cloud and see if enabled
numEnabled := 0
clouds := []string{"digitalocean", "linode", "vultr"}
for _, cloud := range clouds {
_, err := GetManager(cloud)
if err == nil {
fmt.Println(" ", cloud)
// _, err := manager.ListVMs()
numEnabled = numEnabled + 1
}
// else {
// fmt.Println(" ", cloud, " (not configured)")
// }
}
if numEnabled == 0 {
fmt.Println("no clouds enabled. use `eezhee clouds [digitalocean|linode|vultr] [api_key]` to set")
}
},
}
var digitaloceanApiKeyCmd = &cobra.Command{
Use: "digitalocean [api_key]",
Aliases: []string{"do"},
Short: "Set digitalocean api key ",
Long: `Set digitalocean api key eezhee should use`,
Args: validateArguments,
Run: saveApiKey,
}
var linodeApiKeyCmd = &cobra.Command{
Use: "linode [api_key]",
Short: "Set linode api key ",
Long: `Set linode api key eezhee should use`,
Args: validateArguments,
Run: saveApiKey,
}
var vultrApiKeyCmd = &cobra.Command{
Use: "vultr [api_key]",
Short: "Set vultr api key ",
Long: `Set vultr api key eezhee should use`,
Args: validateArguments,
Run: saveApiKey,
}
// validateArguments will make sure api key is valid
func validateArguments(cmd *cobra.Command, args []string) (err error) {
// make sure only one argument
if len(args) < 1 {
return errors.New("no api key provided")
} else if len(args) > 1 {
return errors.New("too many arguments specified. only an api key is required")
}
// get api key user provided
apiKey := args[0]
// need to know which cloud
var manager core.VMManager
if strings.HasPrefix(cmd.Use, "digitalocean") || strings.HasPrefix(cmd.Use, "do") {
manager, err = digitalocean.NewManager(apiKey)
if err != nil {
return err
}
} else if strings.HasPrefix(cmd.Use, "linode") {
manager, err = linode.NewManager(apiKey)
if err != nil {
return err
}
} else if strings.HasPrefix(cmd.Use, "vultr") {
manager, err = vultr.NewManager(apiKey)
if err != nil {
return err
}
} else {
// cobra will make sure this never is allowed
// ie this code should never be called
log.Error("invalid cloud name")
}
// validate api key
_, err = manager.ListVMs()
if err != nil {
return errors.New("invalid api key specified")
}
return nil
}
// save the api key provided
func saveApiKey(cmd *cobra.Command, args []string) {
// get api key user provided
apiKey := args[0]
// store api key in app config
// need to know which cloud
if strings.HasPrefix(cmd.Use, "digitalocean") || strings.HasPrefix(cmd.Use, "do") {
AppConfig.DigitalOceanAPIKey = apiKey
} else if strings.HasPrefix(cmd.Use, "linode") {
AppConfig.LinodeAPIKey = apiKey
} else if strings.HasPrefix(cmd.Use, "vultr") {
AppConfig.VultrAPIKey = apiKey
} else {
// cobra will make sure this never is allowed
// ie this code should never be called
log.Error("invalid cloud name")
}
// now save to disk
err := AppConfig.Save()
if err != nil {
log.Error("could not save api key to config file")
return
}
log.Info("api key added to config")
}