-
Notifications
You must be signed in to change notification settings - Fork 55
/
common_account_aws.go
93 lines (74 loc) · 2.28 KB
/
common_account_aws.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
package commons
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/spotinst/spotinst-sdk-go/service/account/providers/aws"
)
const (
AccountAWSResourceName ResourceName = "spotinst_account_aws"
)
var AccountAWSResource *AccountAWSTerraformResource
type AccountAWSTerraformResource struct {
GenericResource
}
type AWSAccountWrapper struct {
account *aws.Account
}
func NewAccountAWSResource(fieldsMap map[FieldName]*GenericField) *AccountAWSTerraformResource {
return &AccountAWSTerraformResource{
GenericResource: GenericResource{
resourceName: AccountAWSResourceName,
fields: NewGenericFields(fieldsMap),
},
}
}
func (res *AccountAWSTerraformResource) OnCreate(
resourceData *schema.ResourceData,
meta interface{}) (*aws.Account, error) {
if res.fields == nil || res.fields.fieldsMap == nil || len(res.fields.fieldsMap) == 0 {
return nil, fmt.Errorf("resource fields are nil or empty, cannot create")
}
accountWrapper := NewAccountWrapper()
for _, field := range res.fields.fieldsMap {
if field.onCreate == nil {
continue
}
log.Printf(string(ResourceFieldOnCreate), field.resourceAffinity, field.fieldNameStr)
if err := field.onCreate(accountWrapper, resourceData, meta); err != nil {
return nil, err
}
}
return accountWrapper.GetAccount(), nil
}
func (res *AccountAWSTerraformResource) OnRead(
account *aws.Account,
resourceData *schema.ResourceData,
meta interface{}) error {
if res.fields == nil || res.fields.fieldsMap == nil || len(res.fields.fieldsMap) == 0 {
return fmt.Errorf("resource fields are nil or empty, cannot read")
}
accountWrapper := NewAccountWrapper()
accountWrapper.SetAccount(account)
for _, field := range res.fields.fieldsMap {
if field.onRead == nil {
continue
}
log.Printf(string(ResourceFieldOnRead), field.resourceAffinity, field.fieldNameStr)
if err := field.onRead(accountWrapper, resourceData, meta); err != nil {
return err
}
}
return nil
}
func NewAccountWrapper() *AWSAccountWrapper {
return &AWSAccountWrapper{
account: &aws.Account{},
}
}
func (accountWrapper *AWSAccountWrapper) GetAccount() *aws.Account {
return accountWrapper.account
}
func (accountWrapper *AWSAccountWrapper) SetAccount(account *aws.Account) {
accountWrapper.account = account
}