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
/
iam_service_account.go
105 lines (82 loc) · 2.66 KB
/
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
package yandex
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/access"
)
var IamServiceAccountSchema = map[string]*schema.Schema{
"service_account_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
}
type ServiceAccountIamUpdater struct {
serviceAccountID string
Config *Config
}
func newServiceAccountIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) {
return &ServiceAccountIamUpdater{
serviceAccountID: d.Get("service_account_id").(string),
Config: config,
}, nil
}
func serviceAccountIDParseFunc(d *schema.ResourceData, _ *Config) error {
d.Set("service_account_id", d.Id())
return nil
}
func (u *ServiceAccountIamUpdater) GetResourceIamPolicy() (*Policy, error) {
bindings, err := getServiceAccountAccessBindings(u.Config, u.GetResourceID())
if err != nil {
return nil, err
}
return &Policy{bindings}, nil
}
func (u *ServiceAccountIamUpdater) SetResourceIamPolicy(policy *Policy) error {
req := &access.SetAccessBindingsRequest{
ResourceId: u.serviceAccountID,
AccessBindings: policy.Bindings,
}
ctx, cancel := context.WithTimeout(u.Config.Context(), yandexIAMServiceAccountDefaultTimeout)
defer cancel()
op, err := u.Config.sdk.WrapOperation(u.Config.sdk.IAM().ServiceAccount().SetAccessBindings(ctx, req))
if err != nil {
return fmt.Errorf("Error setting IAM policy for %s: %s", u.DescribeResource(), err)
}
err = op.Wait(ctx)
if err != nil {
return fmt.Errorf("Error setting IAM policy for %s: %s", u.DescribeResource(), err)
}
return nil
}
func (u *ServiceAccountIamUpdater) GetResourceID() string {
return u.serviceAccountID
}
func (u *ServiceAccountIamUpdater) GetMutexKey() string {
return fmt.Sprintf("iam-service-account-%s", u.serviceAccountID)
}
func (u *ServiceAccountIamUpdater) DescribeResource() string {
return fmt.Sprintf("service account '%s'", u.serviceAccountID)
}
func getServiceAccountAccessBindings(config *Config, serviceAccountID string) ([]*access.AccessBinding, error) {
bindings := []*access.AccessBinding{}
pageToken := ""
ctx := config.Context()
for {
resp, err := config.sdk.IAM().ServiceAccount().ListAccessBindings(ctx, &access.ListAccessBindingsRequest{
ResourceId: serviceAccountID,
PageSize: defaultListSize,
PageToken: pageToken,
})
if err != nil {
return nil, fmt.Errorf("Error retrieving IAM access bindings for service account %s: %s", serviceAccountID, err)
}
bindings = append(bindings, resp.AccessBindings...)
if resp.NextPageToken == "" {
break
}
pageToken = resp.NextPageToken
}
return bindings, nil
}