-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccounts.go
164 lines (131 loc) · 3.64 KB
/
accounts.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
160
161
162
163
164
package main
import (
"flag"
"fmt"
"os"
"strings"
"github.com/mitchellh/cli"
"github.com/pr8kerl/organizer/aws"
)
// List Account
type ListAccountsCommand struct {
All bool
Ui cli.Ui
}
func listAccountsCmdFactory() (cli.Command, error) {
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
return &ListAccountsCommand{
All: false,
Ui: ui,
}, nil
}
func (c *ListAccountsCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("list accounts", flag.ContinueOnError)
cmdFlags.BoolVar(&c.All, "all", false, "show all accounts including inactive")
cmdFlags.Usage = func() { c.Ui.Output(c.Help()); os.Exit(1) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
org, err := aws.NewOrganization()
if err != nil {
fmt.Printf("error: could not initialize organization: %s\n", err)
return 1
}
if c.All {
err = org.PrintAccounts()
if err != nil {
fmt.Printf("error: could not list accounts: %s\n", err)
return 1
}
} else {
err = org.PrintActiveAccounts()
if err != nil {
fmt.Printf("error: could not list accounts: %s\n", err)
return 1
}
}
return 0
}
func (c *ListAccountsCommand) Help() string {
helpText := `usage: organizer list accounts [<args>]
List organization accounts
Options:
-all show all accounts regardless of state. default is to show only active accounts.
`
return strings.TrimSpace(helpText)
}
func (c *ListAccountsCommand) Synopsis() string {
return "list all accounts for an organization"
}
// Create Account
type CreateAccountCommand struct {
AccountName string
AccountEmail string
Ui cli.Ui
}
func createAccountCmdFactory() (cli.Command, error) {
ui := &cli.BasicUi{
Reader: os.Stdin,
Writer: os.Stdout,
ErrorWriter: os.Stderr,
}
return &CreateAccountCommand{
Ui: ui,
}, nil
}
func (c *CreateAccountCommand) Run(args []string) int {
cmdFlags := flag.NewFlagSet("create account", flag.ContinueOnError)
cmdFlags.StringVar(&c.AccountName, "name", "", "the account name to use")
cmdFlags.StringVar(&c.AccountEmail, "email", "", "the account email address to use")
cmdFlags.Usage = func() { c.Ui.Output(c.Help()); os.Exit(1) }
if err := cmdFlags.Parse(args); err != nil {
return 1
}
if len(c.AccountName) == 0 {
c.Ui.Error("error: missing create account --name parameter.")
cmdFlags.Usage()
return 1
}
if len(c.AccountEmail) == 0 {
c.Ui.Error("error: missing create account --email parameter.")
cmdFlags.Usage()
return 1
}
fmt.Printf("create account: %s, %s\n", c.AccountName, c.AccountEmail)
org, err := aws.NewOrganization()
if err != nil {
fmt.Printf("error: could not initialize organization: %s\n", err)
return 1
}
status, err := org.CreateAccount(c.AccountName, c.AccountEmail)
if err != nil {
fmt.Printf("error: could not create account: %s\n", err)
return 1
}
account, err := org.WaitForAccountStatus(status)
if err != nil {
fmt.Printf("error: could not get account status: %s\n", err)
return 1
}
fmt.Printf("created account %s successfully id: %s\n", c.AccountName, *account.AccountId)
fmt.Printf("ACCOUNT_NAME=%s\n", *account.AccountName)
fmt.Printf("ACCOUNT_ID=%s\n", *account.AccountId)
return 0
}
func (c *CreateAccountCommand) Help() string {
helpText := `
usage: organizer create account --name <account alias> --email <account email address>
create an organization aws account.
options:
-name=<account name> the account name or account alias used to identify the account
-email=<email addr> the account email address
`
return strings.TrimSpace(helpText)
}
func (c *CreateAccountCommand) Synopsis() string {
return "create an account for an organization"
}