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.go
64 lines (46 loc) · 1.78 KB
/
iam.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
package yandex
import (
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
type ResourceIamUpdater interface {
// Fetch the existing IAM policy attached to a resource.
GetResourceIamPolicy() (*Policy, error)
// Replaces the existing IAM Policy attached to a resource.
SetResourceIamPolicy(policy *Policy) error
// A mutex guards against concurrent call to the SetResourceIamPolicy method.
// The mutex key should be made of the resource type and resource id.
// For example: `iam-folder-{id}`.
GetMutexKey() string
// Returns the unique resource identifier.
GetResourceID() string
// Textual description of this resource to be used in error message.
// The description should include the unique resource identifier.
DescribeResource() string
}
type newResourceIamUpdaterFunc func(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error)
type iamPolicyModifyFunc func(p *Policy) error
type resourceIDParserFunc func(d *schema.ResourceData, config *Config) error
func iamPolicyReadModifyWrite(updater ResourceIamUpdater, modify iamPolicyModifyFunc) error {
mutexKey := updater.GetMutexKey()
mutexKV.Lock(mutexKey)
defer mutexKV.Unlock(mutexKey)
log.Printf("[DEBUG]: Retrieving policy for %s\n", updater.DescribeResource())
p, err := updater.GetResourceIamPolicy()
if err != nil {
return err
}
log.Printf("[DEBUG]: Retrieved policy for %s: %+v\n", updater.DescribeResource(), p)
err = modify(p)
if err != nil {
return err
}
log.Printf("[DEBUG]: Setting policy for %s to %+v\n", updater.DescribeResource(), p)
err = updater.SetResourceIamPolicy(p)
if err != nil {
return fmt.Errorf("Error applying IAM policy for %s: %s", updater.DescribeResource(), err)
}
log.Printf("[DEBUG]: Set policy for %s", updater.DescribeResource())
return nil
}