diff --git a/google/iam_project.go b/google/iam_project.go index 192dab4a..476a458c 100644 --- a/google/iam_project.go +++ b/google/iam_project.go @@ -2,6 +2,7 @@ package google import ( "fmt" + "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/cloudresourcemanager/v1" @@ -65,7 +66,7 @@ func (u *ProjectIamUpdater) GetResourceId() string { } func (u *ProjectIamUpdater) GetMutexKey() string { - return fmt.Sprintf("iam-project-%s", u.resourceId) + return getProjectIamPolicyMutexKey(u.resourceId) } func (u *ProjectIamUpdater) DescribeResource() string { diff --git a/google/resource_google_project_iam_policy.go b/google/resource_google_project_iam_policy.go index 306037e8..1107cc53 100644 --- a/google/resource_google_project_iam_policy.go +++ b/google/resource_google_project_iam_policy.go @@ -61,6 +61,11 @@ func resourceGoogleProjectIamPolicyCreate(d *schema.ResourceData, meta interface if err != nil { return err } + + mutexKey := getProjectIamPolicyMutexKey(pid) + mutexKV.Lock(mutexKey) + defer mutexKV.Unlock(mutexKey) + // Get the policy in the template p, err := getResourceIamPolicy(d) if err != nil { @@ -153,6 +158,10 @@ func resourceGoogleProjectIamPolicyUpdate(d *schema.ResourceData, meta interface return err } + mutexKey := getProjectIamPolicyMutexKey(pid) + mutexKV.Lock(mutexKey) + defer mutexKV.Unlock(mutexKey) + // Get the policy in the template p, err := getResourceIamPolicy(d) if err != nil { @@ -220,6 +229,10 @@ func resourceGoogleProjectIamPolicyDelete(d *schema.ResourceData, meta interface return err } + mutexKey := getProjectIamPolicyMutexKey(pid) + mutexKV.Lock(mutexKey) + defer mutexKV.Unlock(mutexKey) + // Get the existing IAM policy from the API ep, err := getProjectIamPolicy(pid, config) if err != nil { @@ -400,3 +413,7 @@ func (b sortableBindings) Swap(i, j int) { func (b sortableBindings) Less(i, j int) bool { return b[i].Role < b[j].Role } + +func getProjectIamPolicyMutexKey(pid string) string { + return fmt.Sprintf("iam-project-%s", pid) +}