|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/hashicorp/terraform/helper/acctest" |
| 6 | + "github.com/hashicorp/terraform/helper/resource" |
| 7 | + "github.com/hashicorp/terraform/terraform" |
| 8 | + "reflect" |
| 9 | + "sort" |
| 10 | + "testing" |
| 11 | +) |
| 12 | + |
| 13 | +func TestAccGoogleServiceAccountIamBinding(t *testing.T) { |
| 14 | + t.Parallel() |
| 15 | + |
| 16 | + account := acctest.RandomWithPrefix("tf-test") |
| 17 | + |
| 18 | + resource.Test(t, resource.TestCase{ |
| 19 | + PreCheck: func() { testAccPreCheck(t) }, |
| 20 | + Providers: testAccProviders, |
| 21 | + Steps: []resource.TestStep{ |
| 22 | + { |
| 23 | + Config: testAccGoogleServiceAccountIamBinding_basic(account), |
| 24 | + Check: testAccCheckGoogleServiceAccountIam(account, "roles/viewer", []string{ |
| 25 | + fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()), |
| 26 | + }), |
| 27 | + }, |
| 28 | + { |
| 29 | + ResourceName: "google_service_account_iam_binding.foo", |
| 30 | + ImportStateId: fmt.Sprintf("%s %s", getServiceAccountCanonicalId(account), "roles/viewer"), |
| 31 | + ImportState: true, |
| 32 | + }, |
| 33 | + }, |
| 34 | + }) |
| 35 | +} |
| 36 | + |
| 37 | +func TestAccGoogleServiceAccountIamMember(t *testing.T) { |
| 38 | + t.Parallel() |
| 39 | + |
| 40 | + account := acctest.RandomWithPrefix("tf-test") |
| 41 | + identity := fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()) |
| 42 | + |
| 43 | + resource.Test(t, resource.TestCase{ |
| 44 | + PreCheck: func() { testAccPreCheck(t) }, |
| 45 | + Providers: testAccProviders, |
| 46 | + Steps: []resource.TestStep{ |
| 47 | + { |
| 48 | + Config: testAccGoogleServiceAccountIamMember_basic(account), |
| 49 | + Check: testAccCheckGoogleServiceAccountIam(account, "roles/editor", []string{identity}), |
| 50 | + }, |
| 51 | + { |
| 52 | + ResourceName: "google_service_account_iam_member.foo", |
| 53 | + ImportStateId: fmt.Sprintf("%s %s %s", getServiceAccountCanonicalId(account), "roles/editor", identity), |
| 54 | + ImportState: true, |
| 55 | + }, |
| 56 | + }, |
| 57 | + }) |
| 58 | +} |
| 59 | + |
| 60 | +func TestAccGoogleServiceAccountIamPolicy(t *testing.T) { |
| 61 | + t.Parallel() |
| 62 | + |
| 63 | + account := acctest.RandomWithPrefix("tf-test") |
| 64 | + |
| 65 | + resource.Test(t, resource.TestCase{ |
| 66 | + PreCheck: func() { testAccPreCheck(t) }, |
| 67 | + Providers: testAccProviders, |
| 68 | + Steps: []resource.TestStep{ |
| 69 | + { |
| 70 | + Config: testAccGoogleServiceAccountIamPolicy_basic(account), |
| 71 | + Check: testAccCheckGoogleServiceAccountIam(account, "roles/owner", []string{ |
| 72 | + fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()), |
| 73 | + }), |
| 74 | + }, |
| 75 | + { |
| 76 | + ResourceName: "google_service_account_iam_policy.foo", |
| 77 | + ImportStateId: getServiceAccountCanonicalId(account), |
| 78 | + ImportState: true, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }) |
| 82 | +} |
| 83 | + |
| 84 | +func testAccCheckGoogleServiceAccountIam(account, role string, members []string) resource.TestCheckFunc { |
| 85 | + return func(s *terraform.State) error { |
| 86 | + config := testAccProvider.Meta().(*Config) |
| 87 | + p, err := config.clientIAM.Projects.ServiceAccounts.GetIamPolicy(getServiceAccountCanonicalId(account)).Do() |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + |
| 92 | + for _, binding := range p.Bindings { |
| 93 | + if binding.Role == role { |
| 94 | + sort.Strings(members) |
| 95 | + sort.Strings(binding.Members) |
| 96 | + |
| 97 | + if reflect.DeepEqual(members, binding.Members) { |
| 98 | + return nil |
| 99 | + } |
| 100 | + |
| 101 | + return fmt.Errorf("Binding found but expected members is %v, got %v", members, binding.Members) |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + return fmt.Errorf("No binding for role %q", role) |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func getServiceAccountCanonicalId(account string) string { |
| 110 | + return fmt.Sprintf("projects/%s/serviceAccounts/%s@%s.iam.gserviceaccount.com", getTestProjectFromEnv(), account, getTestProjectFromEnv()) |
| 111 | +} |
| 112 | + |
| 113 | +func testAccGoogleServiceAccountIamBinding_basic(account string) string { |
| 114 | + return fmt.Sprintf(` |
| 115 | +resource "google_service_account" "test_account" { |
| 116 | + account_id = "%s" |
| 117 | + display_name = "Iam Testing Account" |
| 118 | +} |
| 119 | +
|
| 120 | +resource "google_service_account_iam_binding" "foo" { |
| 121 | + service_account_id = "${google_service_account.test_account.id}" |
| 122 | + role = "roles/viewer" |
| 123 | + members = ["serviceAccount:${google_service_account.test_account.email}"] |
| 124 | +} |
| 125 | +`, account) |
| 126 | +} |
| 127 | + |
| 128 | +func testAccGoogleServiceAccountIamMember_basic(account string) string { |
| 129 | + return fmt.Sprintf(` |
| 130 | +resource "google_service_account" "test_account" { |
| 131 | + account_id = "%s" |
| 132 | + display_name = "Iam Testing Account" |
| 133 | +} |
| 134 | +
|
| 135 | +resource "google_service_account_iam_member" "foo" { |
| 136 | + service_account_id = "${google_service_account.test_account.id}" |
| 137 | + role = "roles/editor" |
| 138 | + member = "serviceAccount:${google_service_account.test_account.email}" |
| 139 | +} |
| 140 | +`, account) |
| 141 | +} |
| 142 | + |
| 143 | +func testAccGoogleServiceAccountIamPolicy_basic(account string) string { |
| 144 | + return fmt.Sprintf(` |
| 145 | +resource "google_service_account" "test_account" { |
| 146 | + account_id = "%s" |
| 147 | + display_name = "Iam Testing Account" |
| 148 | +} |
| 149 | +
|
| 150 | +data "google_iam_policy" "foo" { |
| 151 | + binding { |
| 152 | + role = "roles/owner" |
| 153 | +
|
| 154 | + members = ["serviceAccount:${google_service_account.test_account.email}"] |
| 155 | + } |
| 156 | +} |
| 157 | +
|
| 158 | +resource "google_service_account_iam_policy" "foo" { |
| 159 | + service_account_id = "${google_service_account.test_account.id}" |
| 160 | + policy_data = "${data.google_iam_policy.foo.policy_data}" |
| 161 | +} |
| 162 | +`, account) |
| 163 | +} |
0 commit comments