This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_yandex_iam_service_account.go
224 lines (174 loc) · 5.75 KB
/
resource_yandex_iam_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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package yandex
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"google.golang.org/genproto/protobuf/field_mask"
"github.com/yandex-cloud/go-genproto/yandex/cloud/iam/v1"
)
const yandexIAMServiceAccountDefaultTimeout = 1 * time.Minute
func resourceYandexIAMServiceAccount() *schema.Resource {
return &schema.Resource{
Create: resourceYandexIAMServiceAccountCreate,
Read: resourceYandexIAMServiceAccountRead,
Update: resourceYandexIAMServiceAccountUpdate,
Delete: resourceYandexIAMServiceAccountDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(yandexIAMServiceAccountDefaultTimeout),
Update: schema.DefaultTimeout(yandexIAMServiceAccountDefaultTimeout),
Delete: schema.DefaultTimeout(yandexIAMServiceAccountDefaultTimeout),
},
SchemaVersion: 0,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ForceNew: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceYandexIAMServiceAccountCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
folderID, err := getFolderID(d, config)
if err != nil {
return fmt.Errorf("Error getting folder ID while creating service account: %s", err)
}
// Lock cloud to prevent out IAM changes:
// SA create operation adds 'resource-manager.clouds.member' role
unlock, err := lockCloudByFolderID(config, folderID)
if err != nil {
return fmt.Errorf("could not lock cloud to prevent IAM changes: %s", err)
}
defer unlock()
req := iam.CreateServiceAccountRequest{
FolderId: folderID,
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}
ctx, cancel := context.WithTimeout(config.Context(), d.Timeout(schema.TimeoutCreate))
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.IAM().ServiceAccount().Create(ctx, &req))
if err != nil {
return fmt.Errorf("Error while requesting API to create service account: %s", err)
}
protoMetadata, err := op.Metadata()
if err != nil {
return fmt.Errorf("Error while get service account create operation metadata: %s", err)
}
md, ok := protoMetadata.(*iam.CreateServiceAccountMetadata)
if !ok {
return fmt.Errorf("could not get Service Account ID from create operation metadata")
}
d.SetId(md.ServiceAccountId)
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("Error while waiting operation to create service account: %s", err)
}
if _, err := op.Response(); err != nil {
return fmt.Errorf("Service account creation failed: %s", err)
}
return resourceYandexIAMServiceAccountRead(d, meta)
}
func resourceYandexIAMServiceAccountRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
sa, err := config.sdk.IAM().ServiceAccount().Get(config.Context(), &iam.GetServiceAccountRequest{
ServiceAccountId: d.Id(),
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", d.Get("name").(string)))
}
createdAt, err := getTimestamp(sa.CreatedAt)
if err != nil {
return err
}
d.Set("created_at", createdAt)
d.Set("name", sa.Name)
d.Set("folder_id", sa.FolderId)
d.Set("description", sa.Description)
return nil
}
func resourceYandexIAMServiceAccountUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
d.Partial(true)
req := &iam.UpdateServiceAccountRequest{
ServiceAccountId: d.Id(),
UpdateMask: &field_mask.FieldMask{},
}
if d.HasChange("name") {
req.Name = d.Get("name").(string)
req.UpdateMask.Paths = append(req.UpdateMask.Paths, "name")
}
if d.HasChange("description") {
req.Description = d.Get("description").(string)
req.UpdateMask.Paths = append(req.UpdateMask.Paths, "description")
}
ctx, cancel := context.WithTimeout(config.Context(), d.Timeout(schema.TimeoutUpdate))
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.IAM().ServiceAccount().Update(ctx, req))
if err != nil {
return fmt.Errorf("Error while requesting API to update Service Account %q: %s", d.Id(), err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("Error updating Service Account %q: %s", d.Id(), err)
}
for _, v := range req.UpdateMask.Paths {
d.SetPartial(v)
}
d.Partial(false)
return resourceYandexIAMServiceAccountRead(d, meta)
}
func resourceYandexIAMServiceAccountDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
folderID, err := getFolderID(d, config)
if err != nil {
return fmt.Errorf("Error getting folder ID while deleting service account: %s", err)
}
// Lock cloud to prevent out IAM changes:
// SA delete operation removes 'resource-manager.clouds.member' role
unlock, err := lockCloudByFolderID(config, folderID)
if err != nil {
return fmt.Errorf("could not lock cloud to prevent IAM changes: %s", err)
}
defer unlock()
log.Printf("[DEBUG] Deleting Service Account %q", d.Id())
req := &iam.DeleteServiceAccountRequest{
ServiceAccountId: d.Id(),
}
ctx, cancel := context.WithTimeout(config.Context(), d.Timeout(schema.TimeoutDelete))
defer cancel()
op, err := config.sdk.WrapOperation(config.sdk.IAM().ServiceAccount().Delete(ctx, req))
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Service Account %q", d.Get("name").(string)))
}
err = op.Wait(ctx)
if err != nil {
return err
}
resp, err := op.Response()
if err != nil {
return err
}
log.Printf("[DEBUG] Finished deleting Service Account %q: %#v", d.Id(), resp)
return nil
}