-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathresource_account.go
147 lines (132 loc) · 4.02 KB
/
resource_account.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
package aviatrix
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/AviatrixSystems/go-aviatrix/goaviatrix"
)
func resourceAccount() *schema.Resource {
return &schema.Resource{
Create: resourceAccountCreate,
Read: resourceAccountRead,
Update: resourceAccountUpdate,
Delete: resourceAccountDelete,
Schema: map[string]*schema.Schema{
"account_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"account_password": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"account_email": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"cloud_type": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"aws_account_number": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"aws_iam": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"aws_role_arn": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"aws_role_ec2": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"aws_access_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
"aws_secret_key": &schema.Schema{
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceAccountCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
account := &goaviatrix.Account{
AccountName: d.Get("account_name").(string),
AccountPassword: d.Get("account_password").(string),
AccountEmail: d.Get("account_email").(string),
CloudType: d.Get("cloud_type").(int),
AwsAccountNumber: d.Get("aws_account_number").(string),
AwsIam: d.Get("aws_iam").(string),
AwsRoleArn: d.Get("aws_role_arn").(string),
AwsRoleEc2: d.Get("aws_role_ec2").(string),
AwsAccessKey: d.Get("aws_access_key").(string),
AwsSecretKey: d.Get("aws_secret_key").(string),
}
d.SetId(account.AccountName)
log.Printf("[INFO] Creating Aviatrix account: %#v", account)
err := client.CreateAccount(account)
if err != nil {
return fmt.Errorf("Failed to create Aviatrix Account: %s", err)
}
//return nil
return resourceAccountRead(d, meta)
}
func resourceAccountRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
account := &goaviatrix.Account{
AccountName: d.Get("account_name").(string),
}
log.Printf("[INFO] Looking for Aviatrix account: %#v", account)
acc, err := client.GetAccount(account)
if err != nil {
return fmt.Errorf("Aviatrix Account: %s", err)
}
if acc != nil {
d.Set("account_name", acc.AccountName)
d.Set("account_email", acc.AccountEmail)
d.Set("cloud_type", acc.CloudType)
d.Set("aws_account_number", acc.AwsAccountNumber)
d.Set("aws_access_key", acc.AwsAccessKey)
d.Set("aws_secret_key", acc.AwsSecretKey)
d.SetId(acc.AccountName)
} else {
d.SetId("")
}
return nil
}
func resourceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
account := &goaviatrix.Account{
AccountName: d.Get("account_name").(string),
CloudType: d.Get("cloud_type").(int),
AwsAccountNumber: d.Get("aws_account_number").(string),
AwsAccessKey: d.Get("aws_access_key").(string),
AwsSecretKey: d.Get("aws_secret_key").(string),
}
log.Printf("[INFO] Updating Aviatrix account: %#v", account)
err := client.UpdateAccount(account)
if err != nil {
return fmt.Errorf("Failed to update Aviatrix Account: %s", err)
}
d.SetId(account.AccountName)
return resourceAccountRead(d, meta)
}
func resourceAccountDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*goaviatrix.Client)
account := &goaviatrix.Account{
AccountName: d.Get("account_name").(string),
}
log.Printf("[INFO] Deleting Aviatrix account: %#v", account)
err := client.DeleteAccount(account)
if err != nil {
return fmt.Errorf("Failed to delete Aviatrix Account: %s", err)
}
return nil
}