-
Notifications
You must be signed in to change notification settings - Fork 5
/
accounts.go
100 lines (84 loc) · 3.06 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
package main
import (
"fmt"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/organizations"
"github.com/aws/aws-sdk-go/service/organizations/organizationsiface"
"github.com/jinzhu/gorm"
)
func printErr(err error) {
if aerr, ok := err.(awserr.Error); ok {
switch aerr.Code() {
case organizations.ErrCodeAccessDeniedException:
fmt.Println(organizations.ErrCodeAccessDeniedException, aerr.Error())
case organizations.ErrCodeAWSOrganizationsNotInUseException:
fmt.Println(organizations.ErrCodeAWSOrganizationsNotInUseException, aerr.Error())
case organizations.ErrCodeConcurrentModificationException:
fmt.Println(organizations.ErrCodeConcurrentModificationException, aerr.Error())
case organizations.ErrCodeConstraintViolationException:
fmt.Println(organizations.ErrCodeConstraintViolationException, aerr.Error())
case organizations.ErrCodeInvalidInputException:
fmt.Println(organizations.ErrCodeInvalidInputException, aerr.Error())
case organizations.ErrCodeFinalizingOrganizationException:
fmt.Println(organizations.ErrCodeFinalizingOrganizationException, aerr.Error())
case organizations.ErrCodeServiceException:
fmt.Println(organizations.ErrCodeServiceException, aerr.Error())
case organizations.ErrCodeTooManyRequestsException:
fmt.Println(organizations.ErrCodeTooManyRequestsException, aerr.Error())
default:
fmt.Println(aerr.Error())
}
} else {
// Print the error, cast err to awserr.Error to get the Code and
// Message from an error.
fmt.Println(err.Error())
}
}
type accountManager struct {
svc organizationsiface.OrganizationsAPI
}
func (am accountManager) CreateAccount(acctName string, email string, db *gorm.DB) (*organizations.CreateAccountOutput, error) {
// follows this example
// https://docs.aws.amazon.com/sdk-for-go/api/service/organizations/#example_Organizations_CreateAccount_shared00
input := &organizations.CreateAccountInput{
AccountName: aws.String(acctName),
Email: aws.String(email),
}
err := input.Validate()
if err != nil {
return nil, err
}
result, err := am.svc.CreateAccount(input)
if err != nil {
printErr(err)
return result, err
}
fmt.Println(result)
requestID := result.CreateAccountStatus.Id
db.Create(&serviceInstance{InstanceID: acctName, RequestID: *requestID})
return result, err
}
func (am accountManager) GetAccountStatus(caRequestID string) (*organizations.CreateAccountStatus, error) {
input := &organizations.DescribeCreateAccountStatusInput{}
input.SetCreateAccountRequestId(caRequestID)
err := input.Validate()
if err != nil {
return nil, err
}
result, err := am.svc.DescribeCreateAccountStatus(input)
if err != nil {
printErr(err)
return nil, err
}
fmt.Println(result)
return result.CreateAccountStatus, nil
}
func newAccountManager() (accountManager, error) {
// cache session, per
// https://docs.aws.amazon.com/sdk-for-go/v1/developer-guide/sessions.html
sess, err := session.NewSession()
svc := organizations.New(sess)
return accountManager{svc}, err
}