|
| 1 | +package google |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "reflect" |
| 6 | + "sort" |
| 7 | + "testing" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/acctest" |
| 10 | + "github.com/hashicorp/terraform/helper/resource" |
| 11 | + "github.com/hashicorp/terraform/terraform" |
| 12 | +) |
| 13 | + |
| 14 | +func TestAccBillingAccountIam(t *testing.T) { |
| 15 | + t.Parallel() |
| 16 | + |
| 17 | + billing := getTestBillingAccountFromEnv(t) |
| 18 | + account := acctest.RandomWithPrefix("tf-test") |
| 19 | + role := "roles/billing.viewer" |
| 20 | + resource.Test(t, resource.TestCase{ |
| 21 | + PreCheck: func() { testAccPreCheck(t) }, |
| 22 | + Providers: testAccProviders, |
| 23 | + Steps: []resource.TestStep{ |
| 24 | + { |
| 25 | + // Test Iam Binding creation |
| 26 | + Config: testAccBillingAccountIamBinding_basic(account, billing, role), |
| 27 | + Check: testAccCheckGoogleBillingAccountIamBindingExists("foo", role, []string{ |
| 28 | + fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()), |
| 29 | + }), |
| 30 | + }, |
| 31 | + { |
| 32 | + ResourceName: "google_billing_account_iam_binding.foo", |
| 33 | + ImportStateId: fmt.Sprintf("%s roles/billing.viewer", billing), |
| 34 | + ImportState: true, |
| 35 | + ImportStateVerify: true, |
| 36 | + }, |
| 37 | + { |
| 38 | + // Test Iam Binding update |
| 39 | + Config: testAccBillingAccountIamBinding_update(account, billing, role), |
| 40 | + Check: testAccCheckGoogleBillingAccountIamBindingExists("foo", role, []string{ |
| 41 | + fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()), |
| 42 | + fmt.Sprintf("serviceAccount:%s-2@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()), |
| 43 | + }), |
| 44 | + }, |
| 45 | + { |
| 46 | + ResourceName: "google_billing_account_iam_binding.foo", |
| 47 | + ImportStateId: fmt.Sprintf("%s roles/billing.viewer", billing), |
| 48 | + ImportState: true, |
| 49 | + ImportStateVerify: true, |
| 50 | + }, |
| 51 | + { |
| 52 | + // Test Iam Member creation (no update for member, no need to test) |
| 53 | + Config: testAccBillingAccountIamMember_basic(account, billing, role), |
| 54 | + Check: testAccCheckGoogleBillingAccountIamMemberExists("foo", "roles/billing.viewer", |
| 55 | + fmt.Sprintf("serviceAccount:%s@%s.iam.gserviceaccount.com", account, getTestProjectFromEnv()), |
| 56 | + ), |
| 57 | + }, |
| 58 | + { |
| 59 | + ResourceName: "google_billing_account_iam_member.foo", |
| 60 | + ImportStateId: fmt.Sprintf("%s roles/billing.viewer serviceAccount:%s@%s.iam.gserviceaccount.com", billing, account, getTestProjectFromEnv()), |
| 61 | + ImportState: true, |
| 62 | + ImportStateVerify: true, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }) |
| 66 | +} |
| 67 | + |
| 68 | +func testAccCheckGoogleBillingAccountIamBindingExists(bindingResourceName, role string, members []string) resource.TestCheckFunc { |
| 69 | + return func(s *terraform.State) error { |
| 70 | + bindingRs, ok := s.RootModule().Resources["google_billing_account_iam_binding."+bindingResourceName] |
| 71 | + if !ok { |
| 72 | + return fmt.Errorf("Not found: %s", bindingResourceName) |
| 73 | + } |
| 74 | + |
| 75 | + config := testAccProvider.Meta().(*Config) |
| 76 | + p, err := config.clientBilling.BillingAccounts.GetIamPolicy("billingAccounts/" + bindingRs.Primary.Attributes["billing_account_id"]).Do() |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + for _, binding := range p.Bindings { |
| 82 | + if binding.Role == role { |
| 83 | + sort.Strings(members) |
| 84 | + sort.Strings(binding.Members) |
| 85 | + |
| 86 | + if reflect.DeepEqual(members, binding.Members) { |
| 87 | + return nil |
| 88 | + } |
| 89 | + |
| 90 | + return fmt.Errorf("Binding found but expected members is %v, got %v", members, binding.Members) |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + return fmt.Errorf("No binding for role %q", role) |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +func testAccCheckGoogleBillingAccountIamMemberExists(n, role, member string) resource.TestCheckFunc { |
| 99 | + return func(s *terraform.State) error { |
| 100 | + rs, ok := s.RootModule().Resources["google_billing_account_iam_member."+n] |
| 101 | + if !ok { |
| 102 | + return fmt.Errorf("Not found: %s", n) |
| 103 | + } |
| 104 | + |
| 105 | + config := testAccProvider.Meta().(*Config) |
| 106 | + p, err := config.clientBilling.BillingAccounts.GetIamPolicy("billingAccounts/" + rs.Primary.Attributes["billing_account_id"]).Do() |
| 107 | + if err != nil { |
| 108 | + return err |
| 109 | + } |
| 110 | + |
| 111 | + for _, binding := range p.Bindings { |
| 112 | + if binding.Role == role { |
| 113 | + for _, m := range binding.Members { |
| 114 | + if m == member { |
| 115 | + return nil |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + return fmt.Errorf("Missing member %q, got %v", member, binding.Members) |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return fmt.Errorf("No binding for role %q", role) |
| 124 | + } |
| 125 | +} |
| 126 | + |
| 127 | +func testAccBillingAccountIamBinding_basic(account, billingAccountId, role string) string { |
| 128 | + return fmt.Sprintf(` |
| 129 | +resource "google_service_account" "test-account" { |
| 130 | + account_id = "%s" |
| 131 | + display_name = "Iam Testing Account" |
| 132 | +} |
| 133 | +
|
| 134 | +resource "google_billing_account_iam_binding" "foo" { |
| 135 | + billing_account_id = "%s" |
| 136 | + role = "%s" |
| 137 | + members = ["serviceAccount:${google_service_account.test-account.email}"] |
| 138 | +} |
| 139 | +`, account, billingAccountId, role) |
| 140 | +} |
| 141 | + |
| 142 | +func testAccBillingAccountIamBinding_update(account, billingAccountId, role string) string { |
| 143 | + return fmt.Sprintf(` |
| 144 | +resource "google_service_account" "test-account" { |
| 145 | + account_id = "%s" |
| 146 | + display_name = "Iam Testing Account" |
| 147 | +} |
| 148 | +
|
| 149 | +resource "google_service_account" "test-account-2" { |
| 150 | + account_id = "%s-2" |
| 151 | + display_name = "Iam Testing Account" |
| 152 | +} |
| 153 | +
|
| 154 | +resource "google_billing_account_iam_binding" "foo" { |
| 155 | + billing_account_id = "%s" |
| 156 | + role = "%s" |
| 157 | + members = [ |
| 158 | + "serviceAccount:${google_service_account.test-account.email}", |
| 159 | + "serviceAccount:${google_service_account.test-account-2.email}" |
| 160 | + ] |
| 161 | +} |
| 162 | +`, account, account, billingAccountId, role) |
| 163 | +} |
| 164 | + |
| 165 | +func testAccBillingAccountIamMember_basic(account, billingAccountId, role string) string { |
| 166 | + return fmt.Sprintf(` |
| 167 | +resource "google_service_account" "test-account" { |
| 168 | + account_id = "%s" |
| 169 | + display_name = "Iam Testing Account" |
| 170 | +} |
| 171 | +
|
| 172 | +resource "google_billing_account_iam_member" "foo" { |
| 173 | + billing_account_id = "%s" |
| 174 | + role = "%s" |
| 175 | + member = "serviceAccount:${google_service_account.test-account.email}" |
| 176 | +} |
| 177 | +`, account, billingAccountId, role) |
| 178 | +} |
0 commit comments