@@ -5,14 +5,10 @@ import (
5
5
"errors"
6
6
"fmt"
7
7
8
- "github.com/aws/aws-sdk-go/aws/session"
9
-
10
- "github.com/aws/aws-sdk-go/service/sts"
11
8
"github.com/spf13/cobra"
12
9
13
10
"github.com/santiago-labs/telophasecli/cmd/runner"
14
11
"github.com/santiago-labs/telophasecli/lib/awsorgs"
15
- "github.com/santiago-labs/telophasecli/lib/awssess"
16
12
"github.com/santiago-labs/telophasecli/lib/ymlparser"
17
13
"github.com/santiago-labs/telophasecli/resource"
18
14
"github.com/santiago-labs/telophasecli/resourceoperation"
@@ -23,6 +19,7 @@ var orgFile string
23
19
func init () {
24
20
rootCmd .AddCommand (accountProvision )
25
21
accountProvision .Flags ().StringVar (& orgFile , "org" , "organization.yml" , "Path to the organization.yml file" )
22
+ accountProvision .Flags ().BoolVar (& useTUI , "tui" , false , "use the TUI for diff" )
26
23
}
27
24
28
25
func isValidAccountArg (arg string ) bool {
@@ -51,7 +48,6 @@ var accountProvision = &cobra.Command{
51
48
return fmt .Errorf ("invalid color specified: %s" , args [0 ])
52
49
},
53
50
Run : func (cmd * cobra.Command , args []string ) {
54
- orgClient := awsorgs .New ()
55
51
56
52
var consoleUI runner.ConsoleUI
57
53
if useTUI {
@@ -60,48 +56,61 @@ var accountProvision = &cobra.Command{
60
56
consoleUI = runner .NewSTDOut ()
61
57
}
62
58
63
- ctx := context .Background ()
64
- if args [0 ] == "import" {
65
- if err := importOrgV2 (orgClient ); err != nil {
66
- panic (fmt .Sprintf ("error importing organization: %s" , err ))
67
- }
68
- }
59
+ go processOrg (consoleUI , args [0 ])
60
+ consoleUI .Start ()
69
61
70
- rootAWSGroup , err := ymlparser .ParseOrganizationV2 (orgFile )
71
- if err != nil {
72
- panic (fmt .Sprintf ("error: %s" , err ))
73
- }
74
- if args [0 ] == "diff" {
75
- orgV2Diff (ctx , consoleUI , orgClient , rootAWSGroup , resourceoperation .Diff )
62
+ },
63
+ }
64
+
65
+ func processOrg (consoleUI runner.ConsoleUI , cmd string ) {
66
+ orgClient := awsorgs .New ()
67
+ ctx := context .Background ()
68
+ mgmtAcct , err := orgClient .FetchManagementAccount (ctx )
69
+ if err != nil {
70
+ panic (err )
71
+ }
72
+ if cmd == "import" {
73
+ consoleUI .Print ("Importing AWS Organization" , * mgmtAcct )
74
+ if err := importOrgV2 (ctx , consoleUI , orgClient , mgmtAcct ); err != nil {
75
+ consoleUI .Print (fmt .Sprintf ("error importing organization: %s" , err ), * mgmtAcct )
76
76
}
77
+ }
78
+
79
+ rootAWSGroup , err := ymlparser .ParseOrganizationV2 (orgFile )
80
+ if err != nil {
81
+ consoleUI .Print (fmt .Sprintf ("error parsing organization: %s" , err ), * mgmtAcct )
82
+ }
83
+ if cmd == "diff" {
84
+ consoleUI .Print ("Diffing AWS Organization" , * mgmtAcct )
85
+ orgV2Diff (ctx , consoleUI , orgClient , rootAWSGroup , mgmtAcct , resourceoperation .Diff )
86
+ }
77
87
78
- if args [0 ] == "deploy" {
79
- operations := orgV2Diff (ctx , consoleUI , orgClient , rootAWSGroup , resourceoperation .Deploy )
88
+ if cmd == "deploy" {
89
+ consoleUI .Print ("Diffing AWS Organization" , * mgmtAcct )
90
+ operations := orgV2Diff (ctx , consoleUI , orgClient , rootAWSGroup , mgmtAcct , resourceoperation .Deploy )
80
91
81
- for _ , op := range operations {
82
- err := op .Call (ctx )
83
- if err != nil {
84
- panic (fmt .Sprintf ("error: %s" , err ))
85
- }
92
+ for _ , op := range operations {
93
+ err := op .Call (ctx )
94
+ if err != nil {
95
+ panic (fmt .Sprintf ("error: %s" , err ))
86
96
}
87
97
}
88
- },
98
+ }
99
+
100
+ consoleUI .Print ("Done." , * mgmtAcct )
89
101
}
90
102
91
103
func orgV2Diff (
92
104
ctx context.Context ,
93
105
outputUI runner.ConsoleUI ,
94
106
orgClient awsorgs.Client ,
95
107
rootAWSGroup * resource.AccountGroup ,
108
+ mgmtAcct * resource.Account ,
96
109
operation int ,
97
110
) []resourceoperation.ResourceOperation {
98
111
99
112
var operations []resourceoperation.ResourceOperation
100
113
if rootAWSGroup != nil {
101
- mgmtAcct , err := orgClient .FetchManagementAccount (ctx )
102
- if err != nil {
103
- panic (err )
104
- }
105
114
operations = append (operations , resourceoperation .CollectOrganizationUnitOps (
106
115
ctx , outputUI , orgClient , rootAWSGroup , operation ,
107
116
)... )
@@ -116,21 +125,7 @@ func orgV2Diff(
116
125
return operations
117
126
}
118
127
119
- func currentAccountID () (string , error ) {
120
- stsClient := sts .New (session .Must (awssess .DefaultSession ()))
121
- caller , err := stsClient .GetCallerIdentity (& sts.GetCallerIdentityInput {})
122
- if err != nil {
123
- return "" , err
124
- }
125
-
126
- return * caller .Account , nil
127
- }
128
-
129
- func importOrgV2 (orgClient awsorgs.Client ) error {
130
- managingAccountID , err := currentAccountID ()
131
- if err != nil {
132
- return err
133
- }
128
+ func importOrgV2 (ctx context.Context , consoleUI runner.ConsoleUI , orgClient awsorgs.Client , mgmtAcct * resource.Account ) error {
134
129
135
130
rootId , err := orgClient .GetRootId ()
136
131
if err != nil {
@@ -140,12 +135,12 @@ func importOrgV2(orgClient awsorgs.Client) error {
140
135
return fmt .Errorf ("no root ID found" )
141
136
}
142
137
143
- rootGroup , err := orgClient .FetchGroupAndDescendents (context . TODO () , rootId , managingAccountID )
138
+ rootGroup , err := orgClient .FetchGroupAndDescendents (ctx , rootId , mgmtAcct . AccountID )
144
139
if err != nil {
145
140
return err
146
141
}
147
142
org := resource.AccountGroup {
148
- Name : rootGroup .Name ,
143
+ GroupName : rootGroup .GroupName ,
149
144
ChildGroups : rootGroup .ChildGroups ,
150
145
Accounts : rootGroup .Accounts ,
151
146
}
@@ -154,5 +149,6 @@ func importOrgV2(orgClient awsorgs.Client) error {
154
149
return err
155
150
}
156
151
152
+ consoleUI .Print (fmt .Sprintf ("Successfully wrote file to: %s" , orgFile ), * mgmtAcct )
157
153
return nil
158
154
}
0 commit comments