This repository has been archived by the owner on May 15, 2023. It is now read-only.
/
service_account.go
138 lines (116 loc) · 4.66 KB
/
service_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
package google
import (
"fmt"
"reflect"
)
const ServiceAccountAssetType string = "iam.googleapis.com/ServiceAccount"
func resourceConverterServiceAccount() ResourceConverter {
return ResourceConverter{
AssetType: ServiceAccountAssetType,
Convert: GetServiceAccountCaiObject,
}
}
func GetServiceAccountCaiObject(d TerraformResourceData, config *Config) ([]Asset, error) {
name, err := assetName(d, config, "//iam.googleapis.com/projects/{{project}}/serviceAccounts/{{unique_id}}")
if err != nil {
return []Asset{}, err
}
if obj, err := GetServiceAccountApiObject(d, config); err == nil {
return []Asset{{
Name: name,
Type: ServiceAccountAssetType,
Resource: &AssetResource{
Version: "v1",
DiscoveryDocumentURI: "https://iam.googleapis.com/$discovery/rest",
DiscoveryName: "ServiceAccount",
Data: obj,
},
}}, nil
} else {
return []Asset{}, err
}
}
func GetServiceAccountApiObject(d TerraformResourceData, config *Config) (map[string]interface{}, error) {
obj := make(map[string]interface{})
project, err := getProject(d, config)
if err != nil {
return nil, err
}
descriptionProp, err := expandServiceAccountDescription(d.Get("description"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) {
obj["description"] = descriptionProp
}
emailProp, err := expandServiceAccountDescription(d.Get("email"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("email"); !isEmptyValue(reflect.ValueOf(emailProp)) && (ok || !reflect.DeepEqual(v, emailProp)) {
obj["email"] = emailProp
}
displayNameProp, err := expandServiceAccountDisplayName(d.Get("display_name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("display_name"); !isEmptyValue(reflect.ValueOf(displayNameProp)) && (ok || !reflect.DeepEqual(v, displayNameProp)) {
obj["displayName"] = displayNameProp
}
nameProp, err := expandServiceAccountName(d.Get("name"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("name"); !isEmptyValue(reflect.ValueOf(nameProp)) && (ok || !reflect.DeepEqual(v, nameProp)) {
obj["name"] = nameProp
}
disabledProp, err := expandServiceAccountDisabled(d.Get("disabled"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("disabled"); !isEmptyValue(reflect.ValueOf(disabledProp)) && (ok || !reflect.DeepEqual(v, disabledProp)) {
obj["disabled"] = disabledProp
}
uniqueIdProp, err := expandServiceAccountUniqueId(d.Get("unique_id"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("unique_id"); !isEmptyValue(reflect.ValueOf(uniqueIdProp)) && (ok || !reflect.DeepEqual(v, uniqueIdProp)) {
obj["uniqueId"] = uniqueIdProp
}
projectProp, err := expandServiceAccountProject(d.Get("project"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("project"); !isEmptyValue(reflect.ValueOf(projectProp)) && (ok || !reflect.DeepEqual(v, projectProp)) {
obj["projectId"] = projectProp
}
accountIdProp, err := expandServiceAccountId(d.Get("account_id"), d, config)
if err != nil {
return nil, err
} else if v, ok := d.GetOkExists("account_id"); !isEmptyValue(reflect.ValueOf(accountIdProp)) && (ok || !reflect.DeepEqual(v, accountIdProp)) {
accountId := accountIdProp
if _, ok := obj["email"]; !ok {
// Generating email when the service account is being created (email not present)
obj["email"] = fmt.Sprintf("%s@%s.iam.gserviceaccount.com", accountId, project)
}
}
return obj, nil
}
func expandServiceAccountId(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountDescription(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountDisplayName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountName(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountEmail(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountDisabled(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountUniqueId(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}
func expandServiceAccountProject(v interface{}, d TerraformResourceData, config *Config) (interface{}, error) {
return v, nil
}