Skip to content

Commit

Permalink
renames + comments
Browse files Browse the repository at this point in the history
  • Loading branch information
danawillow committed Oct 25, 2019
1 parent 894a9b9 commit 0c0a13b
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 11 deletions.
12 changes: 8 additions & 4 deletions third_party/terraform/resources/resource_iam_binding.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,10 @@ func resourceIamBindingCreateUpdate(newUpdaterFunc newResourceIamUpdaterFunc, en

binding := getResourceIamBinding(d)
modifyF := func(ep *cloudresourcemanager.Policy) error {
cleaned := removeAllBindingsWithRoleAndCondition(ep.Bindings, binding.Role, binding.Condition)
cleaned := filterBindingsWithRoleAndCondition(ep.Bindings, binding.Role, binding.Condition)
ep.Bindings = append(cleaned, binding)
<% unless version == 'ga' -%>
ep.Version = 3
ep.Version = iamPolicyVersion
<% end -%>
return nil
}
Expand Down Expand Up @@ -201,7 +201,11 @@ func iamBindingImport(newUpdaterFunc newResourceIamUpdaterFunc, resourceIdParser
d.SetId(d.Id() + "/" + role)

<% unless version == 'ga' -%>
// Read the upstream policy so we can set the full condition.
// Since condition titles can have any character in them, we can't separate them from any other
// field the user might set in import (like the condition description and expression). So, we
// have the user just specify the title and then read the upstream policy to set the full
// condition. We can't rely on the read fn to do this for us because it looks for a match of the
// full condition.
updater, err := newUpdaterFunc(d, config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -253,7 +257,7 @@ func resourceIamBindingDelete(newUpdaterFunc newResourceIamUpdaterFunc, enableBa

binding := getResourceIamBinding(d)
modifyF := func(p *cloudresourcemanager.Policy) error {
p.Bindings = removeAllBindingsWithRoleAndCondition(p.Bindings, binding.Role, binding.Condition)
p.Bindings = filterBindingsWithRoleAndCondition(p.Bindings, binding.Role, binding.Condition)
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion third_party/terraform/resources/resource_iam_member.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func resourceIamMemberCreate(newUpdaterFunc newResourceIamUpdaterFunc, enableBat
// Merge the bindings together
ep.Bindings = mergeBindings(append(ep.Bindings, memberBind))
<% unless version == 'ga' -%>
ep.Version = 3
ep.Version = iamPolicyVersion
<% end -%>
return nil
}
Expand Down
2 changes: 1 addition & 1 deletion third_party/terraform/resources/resource_iam_policy.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ func setIamPolicyData(d *schema.ResourceData, updater ResourceIamUpdater) error
return fmt.Errorf("'policy_data' is not valid for %s: %s", updater.DescribeResource(), err)
}
<% unless version == 'ga' -%>
policy.Version = 3
policy.Version = iamPolicyVersion
<% end -%>

err = updater.SetResourceIamPolicy(policy)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ func testAccCheckGoogleServiceAccountIam(account string, numBindings int) resour
<% if version == 'ga' -%>
p, err := config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(serviceAccountCanonicalId(account)).Do()
<% else -%>
p, err := config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(serviceAccountCanonicalId(account)).OptionsRequestedPolicyVersion(3).Do()
p, err := config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(serviceAccountCanonicalId(account)).OptionsRequestedPolicyVersion(iamPolicyVersion).Do()
<% end -%>
if err != nil {
return err
Expand Down
3 changes: 2 additions & 1 deletion third_party/terraform/utils/iam.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
)

const maxBackoffSeconds = 30
const iamPolicyVersion = 3

// These types are implemented per GCP resource type and specify how to do per-resource IAM operations.
// They are used in the generic Terraform IAM resource definitions
Expand Down Expand Up @@ -186,7 +187,7 @@ type iamBindingKey struct {
}

// Removes a single role+condition binding from a list of Bindings
func removeAllBindingsWithRoleAndCondition(b []*cloudresourcemanager.Binding, role string, condition *cloudresourcemanager.Expr) []*cloudresourcemanager.Binding {
func filterBindingsWithRoleAndCondition(b []*cloudresourcemanager.Binding, role string, condition *cloudresourcemanager.Expr) []*cloudresourcemanager.Binding {
bMap := createIamBindingsMap(b)
key := iamBindingKey{role, conditionKeyFromCondition(condition)}
delete(bMap, key)
Expand Down
2 changes: 1 addition & 1 deletion third_party/terraform/utils/iam_service_account.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (u *ServiceAccountIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager
<% if version == 'ga' -%>
p, err := u.Config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(u.serviceAccountId).Do()
<% else -%>
p, err := u.Config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(u.serviceAccountId).OptionsRequestedPolicyVersion(3).Do()
p, err := u.Config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(u.serviceAccountId).OptionsRequestedPolicyVersion(iamPolicyVersion).Do()
<% end -%>

if err != nil {
Expand Down
4 changes: 2 additions & 2 deletions third_party/terraform/utils/iam_test.go.erb
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ func TestIamMergeBindings(t *testing.T) {
}
}

func TestIamRemoveAllBindingsWithRoleAndCondition(t *testing.T) {
func TestIamFilterBindingsWithRoleAndCondition(t *testing.T) {
testCases := []struct {
input []*cloudresourcemanager.Binding
role string
Expand Down Expand Up @@ -365,7 +365,7 @@ func TestIamRemoveAllBindingsWithRoleAndCondition(t *testing.T) {
}

for _, tc := range testCases {
got := removeAllBindingsWithRoleAndCondition(tc.input, tc.role, &cloudresourcemanager.Expr{Title: tc.conditionTitle})
got := filterBindingsWithRoleAndCondition(tc.input, tc.role, &cloudresourcemanager.Expr{Title: tc.conditionTitle})
if !compareBindings(got, tc.expect) {
t.Errorf("Got unexpected value for removeAllBindingsWithRole(%s, %s).\nActual: %s\nExpected: %s",
debugPrintBindings(tc.input), tc.role, debugPrintBindings(got), debugPrintBindings(tc.expect))
Expand Down

0 comments on commit 0c0a13b

Please sign in to comment.