-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
set auditconfigs in state for iam policy resources (#4447)
Signed-off-by: Modular Magician <[email protected]>
- Loading branch information
1 parent
5c2c12f
commit f491302
Showing
2 changed files
with
74 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,7 +2,6 @@ package google | |
|
||
import ( | ||
"fmt" | ||
"reflect" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
|
@@ -18,45 +17,50 @@ func TestAccFolderIamPolicy_basic(t *testing.T) { | |
org := getTestOrgFromEnv(t) | ||
parent := "organizations/" + org | ||
|
||
policy1 := &resourceManagerV2Beta1.Policy{ | ||
Bindings: []*resourceManagerV2Beta1.Binding{ | ||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckGoogleFolderIamPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Role: "roles/viewer", | ||
Members: []string{ | ||
"user:[email protected]", | ||
}, | ||
Config: testAccFolderIamPolicy_basic(folderDisplayName, parent, "roles/viewer", "user:[email protected]"), | ||
}, | ||
}, | ||
} | ||
policy2 := &resourceManagerV2Beta1.Policy{ | ||
Bindings: []*resourceManagerV2Beta1.Binding{ | ||
{ | ||
Role: "roles/editor", | ||
Members: []string{ | ||
"user:[email protected]", | ||
}, | ||
ResourceName: "google_folder_iam_policy.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
{ | ||
Role: "roles/viewer", | ||
Members: []string{ | ||
"user:[email protected]", | ||
}, | ||
Config: testAccFolderIamPolicy_basic2(folderDisplayName, parent, "roles/editor", "user:[email protected]", "roles/viewer", "user:[email protected]"), | ||
}, | ||
{ | ||
ResourceName: "google_folder_iam_policy.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
} | ||
}) | ||
} | ||
|
||
func TestAccFolderIamPolicy_auditConfigs(t *testing.T) { | ||
t.Parallel() | ||
|
||
folderDisplayName := "tf-test-" + acctest.RandString(10) | ||
org := getTestOrgFromEnv(t) | ||
parent := "organizations/" + org | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testAccCheckGoogleFolderIamPolicyDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccFolderIamPolicy_basic(folderDisplayName, parent, "roles/viewer", "user:[email protected]"), | ||
Check: testAccCheckGoogleFolderIamPolicy("google_folder_iam_policy.test", policy1), | ||
Config: testAccFolderIamPolicy_auditConfigs(folderDisplayName, parent, "roles/viewer", "user:[email protected]"), | ||
}, | ||
{ | ||
Config: testAccFolderIamPolicy_basic2(folderDisplayName, parent, "roles/editor", "user:[email protected]", "roles/viewer", "user:[email protected]"), | ||
Check: testAccCheckGoogleFolderIamPolicy("google_folder_iam_policy.test", policy2), | ||
ResourceName: "google_folder_iam_policy.test", | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
|
@@ -80,40 +84,6 @@ func testAccCheckGoogleFolderIamPolicyDestroy(s *terraform.State) error { | |
return nil | ||
} | ||
|
||
func testAccCheckGoogleFolderIamPolicy(n string, policy *resourceManagerV2Beta1.Policy) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No ID is set") | ||
} | ||
|
||
config := testAccProvider.Meta().(*Config) | ||
|
||
p, err := config.clientResourceManagerV2Beta1.Folders.GetIamPolicy(rs.Primary.ID, &resourceManagerV2Beta1.GetIamPolicyRequest{}).Do() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if !reflect.DeepEqual(p.Bindings, policy.Bindings) { | ||
return fmt.Errorf("Incorrect iam policy bindings. Expected '%v', got '%v'", policy.Bindings, p.Bindings) | ||
} | ||
|
||
if _, ok = rs.Primary.Attributes["etag"]; !ok { | ||
return fmt.Errorf("Etag should be set.") | ||
} | ||
|
||
if rs.Primary.Attributes["etag"] != p.Etag { | ||
return fmt.Errorf("Incorrect etag value. Expected '%s', got '%s'", p.Etag, rs.Primary.Attributes["etag"]) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
// Confirm that a folder has an IAM policy with at least 1 binding | ||
func testAccFolderExistingPolicy(org, fname string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
|
@@ -176,3 +146,46 @@ resource "google_folder_iam_policy" "test" { | |
} | ||
`, folder, parent, role, member, role2, member2) | ||
} | ||
|
||
func testAccFolderIamPolicy_auditConfigs(folder, parent, role, member string) string { | ||
return fmt.Sprintf(` | ||
resource "google_folder" "permissiontest" { | ||
display_name = "%s" | ||
parent = "%s" | ||
} | ||
data "google_iam_policy" "test" { | ||
binding { | ||
role = "%s" | ||
members = ["%s"] | ||
} | ||
audit_config { | ||
service = "cloudkms.googleapis.com" | ||
audit_log_configs { | ||
log_type = "DATA_READ" | ||
exempted_members = ["%s"] | ||
} | ||
audit_log_configs { | ||
log_type = "DATA_WRITE" | ||
} | ||
} | ||
audit_config { | ||
service = "cloudsql.googleapis.com" | ||
audit_log_configs { | ||
log_type = "DATA_READ" | ||
exempted_members = ["%s"] | ||
} | ||
audit_log_configs { | ||
log_type = "DATA_WRITE" | ||
} | ||
} | ||
} | ||
resource "google_folder_iam_policy" "test" { | ||
folder = "${google_folder.permissiontest.name}" | ||
policy_data = "${data.google_iam_policy.test.policy_data}" | ||
} | ||
`, folder, parent, role, member, member, member) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters