From b907b5a0c05077c61defcbda1750c8da319c0b10 Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Thu, 2 May 2019 12:03:57 -0400 Subject: [PATCH 1/7] adding new resource to allow iam bindings on GCE instances --- google/field_helpers.go | 4 + google/iam_compute_instance.go | 147 +++++++++ google/provider.go | 3 + google/resource_compute_instance_iam_test.go | 306 +++++++++++++++++++ 4 files changed, 460 insertions(+) create mode 100644 google/iam_compute_instance.go create mode 100644 google/resource_compute_instance_iam_test.go diff --git a/google/field_helpers.go b/google/field_helpers.go index 06ea3ff1..a822c4ea 100644 --- a/google/field_helpers.go +++ b/google/field_helpers.go @@ -62,6 +62,10 @@ func ParseMachineTypesFieldValue(machineType string, d TerraformResourceData, co return parseZonalFieldValue("machineTypes", machineType, "project", "zone", d, config, false) } +func ParseInstanceFieldValue(instance string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) { + return parseZonalFieldValue("instances", instance, "project", "zone", d, config, false) +} + func ParseInstanceGroupFieldValue(instanceGroup string, d TerraformResourceData, config *Config) (*ZonalFieldValue, error) { return parseZonalFieldValue("instanceGroups", instanceGroup, "project", "zone", d, config, false) } diff --git a/google/iam_compute_instance.go b/google/iam_compute_instance.go new file mode 100644 index 00000000..c6dafce8 --- /dev/null +++ b/google/iam_compute_instance.go @@ -0,0 +1,147 @@ +package google + +import ( + "fmt" + "strings" + + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform/helper/schema" + "google.golang.org/api/cloudresourcemanager/v1" + "google.golang.org/api/compute/v1" +) + +var IamComputeInstanceSchema = map[string]*schema.Schema{ + "instance_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + + "zone": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, +} + +type ComputeInstanceIamUpdater struct { + project string + zone string + resourceId string + Config *Config +} + +func NewComputeInstanceIamUpdater(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { + project, err := getProject(d, config) + if err != nil { + return nil, err + } + + zone, err := getZone(d, config) + if err != nil { + return nil, err + } + + return &ComputeInstanceIamUpdater{ + project: project, + zone: zone, + resourceId: d.Get("instance_name").(string), + Config: config, + }, nil +} + +func ComputeInstanceIdParseFunc(d *schema.ResourceData, config *Config) error { + parts := strings.Split(d.Id(), "/") + var fv *ZonalFieldValue + if len(parts) == 3 { + // {project}/{zone}/{name} syntax + fv = &ZonalFieldValue{ + Project: parts[0], + Zone: parts[1], + Name: parts[2], + resourceType: "instances", + } + } else if len(parts) == 2 { + // /{zone}/{name} syntax + project, err := getProject(d, config) + if err != nil { + return err + } + fv = &ZonalFieldValue{ + Project: project, + Zone: parts[0], + Name: parts[1], + resourceType: "instances", + } + } else { + // We either have a name or a full self link, so use the field helper + var err error + fv, err = ParseInstanceFieldValue(d.Id(), d, config) + if err != nil { + return err + } + } + + d.Set("project", fv.Project) + d.Set("zone", fv.Zone) + d.Set("instance_name", fv.Name) + + // Explicitly set the id so imported resources have the same ID format as non-imported ones. + d.SetId(fv.RelativeLink()) + return nil +} + +func (u *ComputeInstanceIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { + p, err := u.Config.clientCompute.Instances.GetIamPolicy(u.project, u.zone, u.resourceId).Do() + + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + cloudResourcePolicy, err := computeToResourceManagerPolicy(p) + + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + return cloudResourcePolicy, nil +} + +func (u *ComputeInstanceIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { + computePolicy, err := resourceManagerToComputePolicy(policy) + + if err != nil { + return errwrap.Wrapf(fmt.Sprintf("Invalid IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + req := &compute.ZoneSetPolicyRequest{ + Policy: computePolicy, + } + _, err = u.Config.clientCompute.Instances.SetIamPolicy(u.project, u.zone, u.resourceId, req).Do() + + if err != nil { + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + return nil +} + +func (u *ComputeInstanceIamUpdater) GetResourceId() string { + return fmt.Sprintf("projects/%s/zones/%s/instances/%s", u.project, u.zone, u.resourceId) +} + +func (u *ComputeInstanceIamUpdater) GetMutexKey() string { + return fmt.Sprintf("iam-compute-Instance-%s-%s-%s", u.project, u.zone, u.resourceId) +} + +func (u *ComputeInstanceIamUpdater) DescribeResource() string { + return fmt.Sprintf("Compute Instance %s/%s/%s", u.project, u.zone, u.resourceId) +} diff --git a/google/provider.go b/google/provider.go index 1635cf23..029ea8df 100644 --- a/google/provider.go +++ b/google/provider.go @@ -173,6 +173,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_compute_instance_from_template": resourceComputeInstanceFromTemplate(), "google_compute_instance_group": resourceComputeInstanceGroup(), "google_compute_instance_group_manager": resourceComputeInstanceGroupManager(), + "google_compute_instance_iam_binding": ResourceIamBindingWithImport(IamComputeInstanceSchema, NewComputeInstanceIamUpdater, ComputeInstanceIdParseFunc), + "google_compute_instance_iam_member": ResourceIamMemberWithImport(IamComputeInstanceSchema, NewComputeInstanceIamUpdater, ComputeInstanceIdParseFunc), + "google_compute_instance_iam_policy": ResourceIamPolicyWithImport(IamComputeInstanceSchema, NewComputeInstanceIamUpdater, ComputeInstanceIdParseFunc), "google_compute_instance_template": resourceComputeInstanceTemplate(), "google_compute_network_peering": resourceComputeNetworkPeering(), "google_compute_project_metadata": resourceComputeProjectMetadata(), diff --git a/google/resource_compute_instance_iam_test.go b/google/resource_compute_instance_iam_test.go new file mode 100644 index 00000000..586eee6c --- /dev/null +++ b/google/resource_compute_instance_iam_test.go @@ -0,0 +1,306 @@ +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccComputeInstanceIamBinding(t *testing.T) { + t.Parallel() + + project := getTestProjectFromEnv() + account := acctest.RandomWithPrefix("tf-test") + role := "roles/compute.osLogin" + region := getTestRegionFromEnv() + zone := getTestZoneFromEnv() + subnetwork := fmt.Sprintf("tf-test-net-%s", acctest.RandString(10)) + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetwork, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_binding.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetwork, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_binding.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccComputeInstanceIamMember(t *testing.T) { + t.Parallel() + + project := getTestProjectFromEnv() + account := acctest.RandomWithPrefix("tf-test") + role := "roles/compute.osLogin" + region := getTestRegionFromEnv() + zone := getTestZoneFromEnv() + subnetwork := fmt.Sprintf("tf-test-net-%s", acctest.RandString(10)) + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + // Test Iam Member creation (no update for member, no need to test) + Config: testAccComputeInstanceIamMember_basic(project, account, region, zone, subnetwork, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_member.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s serviceAccount:%s@%s.iam.gserviceaccount.com", project, zone, instanceName, role, account, project), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccComputeInstanceIamPolicy(t *testing.T) { + t.Parallel() + + project := getTestProjectFromEnv() + account := acctest.RandomWithPrefix("tf-test") + role := "roles/compute.osLogin" + region := getTestRegionFromEnv() + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + subnetwork := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceIamPolicy_basic(project, account, region, zone, subnetwork, instanceName, role), + }, + // Test a few import formats + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s", project, zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("%s/%s", zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccComputeInstanceIamMember_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { + return fmt.Sprintf(` +resource "google_service_account" "test_account" { + account_id = "%s" + display_name = "Iam Testing Account" +} + +resource "google_compute_network" "network" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "subnetwork" { + name = "%s" + region = "%s" + ip_cidr_range = "10.1.0.0/16" + network = "${google_compute_network.network.name}" +} + +resource "google_compute_instance" "test_vm" { + project = "%s" + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + network_interface { + subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + } +} + +resource "google_compute_instance_iam_member" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + member = "serviceAccount:${google_service_account.test_account.email}" +} +`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +} + +func testAccComputeInstanceIamPolicy_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { + return fmt.Sprintf(` +resource "google_service_account" "test_account" { + account_id = "%s" + display_name = "Iam Testing Account" +} + +resource "google_compute_network" "network" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "subnetwork" { + name = "%s" + region = "%s" + ip_cidr_range = "10.1.0.0/16" + network = "${google_compute_network.network.name}" +} + +resource "google_compute_instance" "test_vm" { + project = "%s" + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + network_interface { + subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + } +} + +data "google_iam_policy" "foo" { + binding { + role = "%s" + members = ["serviceAccount:${google_service_account.test_account.email}"] + } +} + +resource "google_compute_instance_iam_policy" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + policy_data = "${data.google_iam_policy.foo.policy_data}" +} +`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +} + +func testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { + return fmt.Sprintf(` + resource "google_service_account" "test_account" { + account_id = "%s" + display_name = "Iam Testing Account" + } + + resource "google_compute_network" "network" { + name = "%s" + auto_create_subnetworks = false + } + + resource "google_compute_subnetwork" "subnetwork" { + name = "%s" + region = "%s" + ip_cidr_range = "10.1.0.0/16" + network = "${google_compute_network.network.name}" + } + + resource "google_compute_instance" "test_vm" { + project = "%s" + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + network_interface { + subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + } + } + + resource "google_compute_instance_iam_binding" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + members = ["serviceAccount:${google_service_account.test_account.email}"] + } + `, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +} + +func testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetworkName, instanceName, roleId string) string { + return fmt.Sprintf(` + resource "google_service_account" "test_account" { + account_id = "%s" + display_name = "Iam Testing Account" + } + + resource "google_service_account" "test_account_2" { + account_id = "%s-2" + display_name = "Iam Testing Account" + } + + resource "google_compute_network" "network" { + name = "%s" + auto_create_subnetworks = false + } + + resource "google_compute_subnetwork" "subnetwork" { + name = "%s" + region = "%s" + ip_cidr_range = "10.1.0.0/16" + network = "${google_compute_network.network.name}" + } + + resource "google_compute_instance" "test_vm" { + project = "%s" + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + network_interface { + subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + } + } + + resource "google_compute_instance_iam_binding" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + members = [ + "serviceAccount:${google_service_account.test_account.email}", + "serviceAccount:${google_service_account.test_account_2.email}" + ] + } + `, account, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +} From 79593d8fc6d59d0e2cb3d4697ad9aeb0917062c3 Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Mon, 6 May 2019 14:53:58 -0400 Subject: [PATCH 2/7] Update resource_compute_instance_iam_test.go fixing whitespace/tabbing issue --- google/resource_compute_instance_iam_test.go | 194 +++++++++---------- 1 file changed, 97 insertions(+), 97 deletions(-) diff --git a/google/resource_compute_instance_iam_test.go b/google/resource_compute_instance_iam_test.go index 586eee6c..f81331cd 100644 --- a/google/resource_compute_instance_iam_test.go +++ b/google/resource_compute_instance_iam_test.go @@ -135,8 +135,8 @@ resource "google_compute_subnetwork" "subnetwork" { } resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" + project = "%s" + zone = "%s" name = "%s" machine_type = "n1-standard-1" boot_disk { @@ -150,7 +150,7 @@ resource "google_compute_instance" "test_vm" { } resource "google_compute_instance_iam_member" "foo" { - project = "${google_compute_instance.test_vm.project}" + project = "${google_compute_instance.test_vm.project}" zone = "${google_compute_instance.test_vm.zone}" instance_name = "${google_compute_instance.test_vm.name}" role = "%s" @@ -175,13 +175,13 @@ resource "google_compute_subnetwork" "subnetwork" { name = "%s" region = "%s" ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" + network = "${google_compute_network.network.name}" } resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" + project = "%s" + zone = "%s" + name = "%s" machine_type = "n1-standard-1" boot_disk { initialize_params { @@ -194,113 +194,113 @@ resource "google_compute_instance" "test_vm" { } data "google_iam_policy" "foo" { - binding { - role = "%s" - members = ["serviceAccount:${google_service_account.test_account.email}"] - } + binding { + role = "%s" + members = ["serviceAccount:${google_service_account.test_account.email}"] + } } resource "google_compute_instance_iam_policy" "foo" { - project = "${google_compute_instance.test_vm.project}" + project = "${google_compute_instance.test_vm.project}" zone = "${google_compute_instance.test_vm.zone}" instance_name = "${google_compute_instance.test_vm.name}" - policy_data = "${data.google_iam_policy.foo.policy_data}" + policy_data = "${data.google_iam_policy.foo.policy_data}" } `, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) } func testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { return fmt.Sprintf(` - resource "google_service_account" "test_account" { - account_id = "%s" - display_name = "Iam Testing Account" - } +resource "google_service_account" "test_account" { + account_id = "%s" + display_name = "Iam Testing Account" +} - resource "google_compute_network" "network" { - name = "%s" - auto_create_subnetworks = false - } +resource "google_compute_network" "network" { + name = "%s" + auto_create_subnetworks = false +} - resource "google_compute_subnetwork" "subnetwork" { - name = "%s" - region = "%s" - ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" - } +resource "google_compute_subnetwork" "subnetwork" { + name = "%s" + region = "%s" + ip_cidr_range = "10.1.0.0/16" + network = "${google_compute_network.network.name}" +} - resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" - machine_type = "n1-standard-1" - boot_disk { - initialize_params { - image = "debian-cloud/debian-9" - } - } - network_interface { - subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" - } - } +resource "google_compute_instance" "test_vm" { + project = "%s" + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + network_interface { + subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + } +} - resource "google_compute_instance_iam_binding" "foo" { - project = "${google_compute_instance.test_vm.project}" - zone = "${google_compute_instance.test_vm.zone}" - instance_name = "${google_compute_instance.test_vm.name}" - role = "%s" - members = ["serviceAccount:${google_service_account.test_account.email}"] - } - `, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +resource "google_compute_instance_iam_binding" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + members = ["serviceAccount:${google_service_account.test_account.email}"] +} +`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) } func testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetworkName, instanceName, roleId string) string { return fmt.Sprintf(` - resource "google_service_account" "test_account" { - account_id = "%s" - display_name = "Iam Testing Account" - } - - resource "google_service_account" "test_account_2" { - account_id = "%s-2" - display_name = "Iam Testing Account" - } - - resource "google_compute_network" "network" { - name = "%s" - auto_create_subnetworks = false - } - - resource "google_compute_subnetwork" "subnetwork" { - name = "%s" - region = "%s" - ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" - } - - resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" - machine_type = "n1-standard-1" - boot_disk { - initialize_params { - image = "debian-cloud/debian-9" - } - } - network_interface { - subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" - } - } - - resource "google_compute_instance_iam_binding" "foo" { - project = "${google_compute_instance.test_vm.project}" - zone = "${google_compute_instance.test_vm.zone}" - instance_name = "${google_compute_instance.test_vm.name}" - role = "%s" - members = [ - "serviceAccount:${google_service_account.test_account.email}", - "serviceAccount:${google_service_account.test_account_2.email}" - ] - } - `, account, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +resource "google_service_account" "test_account" { + account_id = "%s" + display_name = "Iam Testing Account" +} + +resource "google_service_account" "test_account_2" { + account_id = "%s-2" + display_name = "Iam Testing Account" +} + +resource "google_compute_network" "network" { + name = "%s" + auto_create_subnetworks = false +} + +resource "google_compute_subnetwork" "subnetwork" { + name = "%s" + region = "%s" + ip_cidr_range = "10.1.0.0/16" + network = "${google_compute_network.network.name}" +} + +resource "google_compute_instance" "test_vm" { + project = "%s" + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + network_interface { + subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + } +} + +resource "google_compute_instance_iam_binding" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + members = [ + "serviceAccount:${google_service_account.test_account.email}", + "serviceAccount:${google_service_account.test_account_2.email}" + ] +} +`, account, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) } From 0e4bdb83c595c5dcc2446deda9c0f6e162272073 Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Mon, 6 May 2019 17:12:58 -0400 Subject: [PATCH 3/7] adding documentation for gce instance iam --- .../docs/r/compute_instance_iam.html.markdown | 126 ++++++++++++++++++ website/google.erb | 12 ++ 2 files changed, 138 insertions(+) create mode 100644 website/docs/r/compute_instance_iam.html.markdown diff --git a/website/docs/r/compute_instance_iam.html.markdown b/website/docs/r/compute_instance_iam.html.markdown new file mode 100644 index 00000000..e99533bd --- /dev/null +++ b/website/docs/r/compute_instance_iam.html.markdown @@ -0,0 +1,126 @@ +--- +layout: "google" +page_title: "Google: google_compute_instance_iam" +sidebar_current: "docs-google-compute-instance-iam" +description: |- + Collection of resources to manage IAM policy for a GCE instance. +--- + +# IAM policy for GCE instance + +~> **Warning:** These resources are in beta, and should be used with the terraform-provider-google-beta provider. +See [Provider Versions](https://terraform.io/docs/providers/google/provider_versions.html) for more details on beta resources. + +Three different resources help you manage your IAM policy for GCE instance. Each of these resources serves a different use case: + +* `google_compute_instance_iam_policy`: Authoritative. Sets the IAM policy for the instance and replaces any existing policy already attached. +* `google_compute_instance_iam_binding`: Authoritative for a given role. Updates the IAM policy to grant a role to a list of members. Other roles within the IAM policy for the instance are preserved. +* `google_compute_instance_iam_member`: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the instance are preserved. + +~> **Note:** `google_compute_instance_iam_policy` **cannot** be used in conjunction with `google_compute_instance_iam_binding` and `google_compute_instance_iam_member` or they will fight over what your policy should be. + +~> **Note:** `google_compute_instance_iam_binding` resources **can be** used in conjunction with `google_compute_instance_iam_member` resources **only if** they do not grant privilege to the same role. + +## google\_compute\_instance\_iam\_policy + +```hcl +data "google_iam_policy" "admin" { + binding { + role = "roles/editor" + + members = [ + "user:jane@example.com", + ] + } +} + +resource "google_compute_instance_iam_policy" "instance" { + instance_name = "your-instance-id" + policy_data = "${data.google_iam_policy.admin.policy_data}" +} +``` + +## google\_compute\_instance\_iam\_binding + +```hcl +resource "google_compute_instance_iam_binding" "instance" { + instance_name = "your-instance-id" + role = "roles/compute.networkUser" + + members = [ + "user:jane@example.com", + ] +} +``` + +## google\compute\_instance\_iam\_member + +```hcl +resource "google_compute_instance_iam_member" "instance" { + instance_name = "your-instance-id" + role = "roles/compute.networkUser" + member = "user:jane@example.com" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `instance_name` - (Required) The name of the instance. + +* `member/members` - (Required) Identities that will be granted the privilege in `role`. + Each entry can have one of the following values: + * **allUsers**: A special identifier that represents anyone who is on the internet; with or without a Google account. + * **allAuthenticatedUsers**: A special identifier that represents anyone who is authenticated with a Google account or a service account. + * **user:{emailid}**: An email address that represents a specific Google account. For example, alice@gmail.com or joe@example.com. + * **serviceAccount:{emailid}**: An email address that represents a service account. For example, my-other-app@appspot.gserviceaccount.com. + * **group:{emailid}**: An email address that represents a Google group. For example, admins@example.com. + * **domain:{domain}**: A G Suite domain (primary, instead of alias) name that represents all the users of that domain. For example, google.com or example.com. + +* `role` - (Required) The role that should be applied. Only one + `google_compute_instance_iam_binding` can be used per role. Note that custom roles must be of the format + `[projects|organizations]/{parent-name}/roles/{role-name}`. + +* `policy_data` - (Required only by `google_compute_instance_iam_policy`) The policy data generated by + a `google_iam_policy` data source. + +* `project` - (Optional) The ID of the project in which the resource belongs. If it + is not provided, the provider project is used. + +* `zone` - (Optional) The zone of the instance. If + unspecified, this defaults to the zone configured in the provider. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are +exported: + +* `etag` - (Computed) The etag of the instance's IAM policy. + +## Import + +For all import syntaxes, the "resource in question" can take any of the following forms: + +* full self link or relative link (projects/{{project}}/zones/{{zone}}/instances/{{name}}) +* {{project}}/{{zone}}/{{name}} +* {{zone}}/{{name}} (project is taken from provider project) +* {{name}} (project and zone are taken from provider project) + +IAM member imports use space-delimited identifiers; the resource in question, the role, and the member identity, e.g. + +``` +$ terraform import google_compute_instance_iam_member.instance "project-name/zone-name/instance-name roles/compute.networkUser user:foo@example.com" +``` + +IAM binding imports use space-delimited identifiers; the resource in question and the role, e.g. + +``` +$ terraform import google_compute_instance_iam_binding.instance "project-name/zone-name/instance-name roles/compute.networkUser" +``` + +IAM policy imports use the identifier of the resource in question, e.g. + +``` +$ terraform import google_compute_instance_iam_policy.instance project-name/zone-name/instance-name +``` diff --git a/website/google.erb b/website/google.erb index caea73e7..11f5d0c6 100644 --- a/website/google.erb +++ b/website/google.erb @@ -390,6 +390,18 @@ google_compute_instance + > + google_compute_instance_iam_binding + + + > + google_compute_instance_iam_member + + + > + google_compute_instance_iam_policy + + > google_compute_instance_from_template From 2e84d558e20ff97eb429eb0c8c326bc13c3a9015 Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Tue, 7 May 2019 15:55:10 -0400 Subject: [PATCH 4/7] fixing documentation and making tests more specific --- google/resource_compute_instance_iam_test.go | 274 +++++++----------- .../docs/r/compute_instance_iam.html.markdown | 17 +- 2 files changed, 106 insertions(+), 185 deletions(-) diff --git a/google/resource_compute_instance_iam_test.go b/google/resource_compute_instance_iam_test.go index f81331cd..aeb4724a 100644 --- a/google/resource_compute_instance_iam_test.go +++ b/google/resource_compute_instance_iam_test.go @@ -12,11 +12,8 @@ func TestAccComputeInstanceIamBinding(t *testing.T) { t.Parallel() project := getTestProjectFromEnv() - account := acctest.RandomWithPrefix("tf-test") role := "roles/compute.osLogin" - region := getTestRegionFromEnv() zone := getTestZoneFromEnv() - subnetwork := fmt.Sprintf("tf-test-net-%s", acctest.RandString(10)) instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) resource.Test(t, resource.TestCase{ @@ -24,7 +21,7 @@ func TestAccComputeInstanceIamBinding(t *testing.T) { Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetwork, instanceName, role), + Config: testAccComputeInstanceIamBinding_basic(zone, instanceName, role), }, { ResourceName: "google_compute_instance_iam_binding.foo", @@ -34,7 +31,7 @@ func TestAccComputeInstanceIamBinding(t *testing.T) { }, { // Test Iam Binding update - Config: testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetwork, instanceName, role), + Config: testAccComputeInstanceIamBinding_update(zone, instanceName, role), }, { ResourceName: "google_compute_instance_iam_binding.foo", @@ -50,23 +47,21 @@ func TestAccComputeInstanceIamMember(t *testing.T) { t.Parallel() project := getTestProjectFromEnv() - account := acctest.RandomWithPrefix("tf-test") role := "roles/compute.osLogin" - region := getTestRegionFromEnv() zone := getTestZoneFromEnv() - subnetwork := fmt.Sprintf("tf-test-net-%s", acctest.RandString(10)) instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { // Test Iam Member creation (no update for member, no need to test) - Config: testAccComputeInstanceIamMember_basic(project, account, region, zone, subnetwork, instanceName, role), + Config: testAccComputeInstanceIamMember_basic(zone, instanceName, role), }, { ResourceName: "google_compute_instance_iam_member.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s serviceAccount:%s@%s.iam.gserviceaccount.com", project, zone, instanceName, role, account, project), + ImportStateId: fmt.Sprintf("%s/%s/%s %s user:admin@hashicorptest.com", project, zone, instanceName, role), ImportState: true, ImportStateVerify: true, }, @@ -78,19 +73,16 @@ func TestAccComputeInstanceIamPolicy(t *testing.T) { t.Parallel() project := getTestProjectFromEnv() - account := acctest.RandomWithPrefix("tf-test") role := "roles/compute.osLogin" - region := getTestRegionFromEnv() zone := getTestZoneFromEnv() instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - subnetwork := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, Steps: []resource.TestStep{ { - Config: testAccComputeInstanceIamPolicy_basic(project, account, region, zone, subnetwork, instanceName, role), + Config: testAccComputeInstanceIamPolicy_basic(zone, instanceName, role), }, // Test a few import formats { @@ -115,192 +107,124 @@ func TestAccComputeInstanceIamPolicy(t *testing.T) { }) } -func testAccComputeInstanceIamMember_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { +func testAccComputeInstanceIamMember_basic(zone, instanceName, roleId string) string { return fmt.Sprintf(` -resource "google_service_account" "test_account" { - account_id = "%s" - display_name = "Iam Testing Account" -} + resource "google_compute_instance" "test_vm" { + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" -resource "google_compute_network" "network" { - name = "%s" - auto_create_subnetworks = false -} + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } -resource "google_compute_subnetwork" "subnetwork" { - name = "%s" - region = "%s" - ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" -} - -resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" - machine_type = "n1-standard-1" - boot_disk { - initialize_params { - image = "debian-cloud/debian-9" + network_interface { + network = "default" } } - network_interface { - subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + + resource "google_compute_instance_iam_member" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + member = "user:admin@hashicorptest.com" } + +`, zone, instanceName, roleId) } -resource "google_compute_instance_iam_member" "foo" { - project = "${google_compute_instance.test_vm.project}" - zone = "${google_compute_instance.test_vm.zone}" - instance_name = "${google_compute_instance.test_vm.name}" - role = "%s" - member = "serviceAccount:${google_service_account.test_account.email}" -} -`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) -} - -func testAccComputeInstanceIamPolicy_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { +func testAccComputeInstanceIamPolicy_basic(zone, instanceName, roleId string) string { return fmt.Sprintf(` -resource "google_service_account" "test_account" { - account_id = "%s" - display_name = "Iam Testing Account" -} + resource "google_compute_instance" "test_vm" { + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" -resource "google_compute_network" "network" { - name = "%s" - auto_create_subnetworks = false -} + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } -resource "google_compute_subnetwork" "subnetwork" { - name = "%s" - region = "%s" - ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" -} - -resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" - machine_type = "n1-standard-1" - boot_disk { - initialize_params { - image = "debian-cloud/debian-9" + network_interface { + network = "default" } } - network_interface { - subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" - } -} -data "google_iam_policy" "foo" { - binding { - role = "%s" - members = ["serviceAccount:${google_service_account.test_account.email}"] - } -} - -resource "google_compute_instance_iam_policy" "foo" { - project = "${google_compute_instance.test_vm.project}" - zone = "${google_compute_instance.test_vm.zone}" - instance_name = "${google_compute_instance.test_vm.name}" - policy_data = "${data.google_iam_policy.foo.policy_data}" -} -`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) -} - -func testAccComputeInstanceIamBinding_basic(project, account, region, zone, subnetworkName, instanceName, roleId string) string { - return fmt.Sprintf(` -resource "google_service_account" "test_account" { - account_id = "%s" - display_name = "Iam Testing Account" -} - -resource "google_compute_network" "network" { - name = "%s" - auto_create_subnetworks = false -} - -resource "google_compute_subnetwork" "subnetwork" { - name = "%s" - region = "%s" - ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" -} - -resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" - machine_type = "n1-standard-1" - boot_disk { - initialize_params { - image = "debian-cloud/debian-9" + data "google_iam_policy" "foo" { + binding { + role = "%s" + members = ["user:admin@hashicorptest.com"] } } - network_interface { - subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + + resource "google_compute_instance_iam_policy" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + policy_data = "${data.google_iam_policy.foo.policy_data}" } + +`, zone, instanceName, roleId) } -resource "google_compute_instance_iam_binding" "foo" { - project = "${google_compute_instance.test_vm.project}" - zone = "${google_compute_instance.test_vm.zone}" - instance_name = "${google_compute_instance.test_vm.name}" - role = "%s" - members = ["serviceAccount:${google_service_account.test_account.email}"] -} -`, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) -} - -func testAccComputeInstanceIamBinding_update(project, account, region, zone, subnetworkName, instanceName, roleId string) string { +func testAccComputeInstanceIamBinding_basic(zone, instanceName, roleId string) string { return fmt.Sprintf(` -resource "google_service_account" "test_account" { - account_id = "%s" - display_name = "Iam Testing Account" -} + resource "google_compute_instance" "test_vm" { + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" -resource "google_service_account" "test_account_2" { - account_id = "%s-2" - display_name = "Iam Testing Account" -} + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } -resource "google_compute_network" "network" { - name = "%s" - auto_create_subnetworks = false -} - -resource "google_compute_subnetwork" "subnetwork" { - name = "%s" - region = "%s" - ip_cidr_range = "10.1.0.0/16" - network = "${google_compute_network.network.name}" -} - -resource "google_compute_instance" "test_vm" { - project = "%s" - zone = "%s" - name = "%s" - machine_type = "n1-standard-1" - boot_disk { - initialize_params { - image = "debian-cloud/debian-9" + network_interface { + network = "default" } } - network_interface { - subnetwork ="${google_compute_subnetwork.subnetwork.self_link}" + + resource "google_compute_instance_iam_binding" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + members = ["user:admin@hashicorptest.com"] } + +`, zone, instanceName, roleId) } -resource "google_compute_instance_iam_binding" "foo" { - project = "${google_compute_instance.test_vm.project}" - zone = "${google_compute_instance.test_vm.zone}" - instance_name = "${google_compute_instance.test_vm.name}" - role = "%s" - members = [ - "serviceAccount:${google_service_account.test_account.email}", - "serviceAccount:${google_service_account.test_account_2.email}" - ] -} -`, account, account, subnetworkName, subnetworkName, region, project, zone, instanceName, roleId) +func testAccComputeInstanceIamBinding_update(zone, instanceName, roleId string) string { + return fmt.Sprintf(` + resource "google_compute_instance" "test_vm" { + zone = "%s" + name = "%s" + machine_type = "n1-standard-1" + + boot_disk { + initialize_params { + image = "debian-cloud/debian-9" + } + } + + network_interface { + network = "default" + } + } + + resource "google_compute_instance_iam_binding" "foo" { + project = "${google_compute_instance.test_vm.project}" + zone = "${google_compute_instance.test_vm.zone}" + instance_name = "${google_compute_instance.test_vm.name}" + role = "%s" + members = ["user:admin@hashicorptest.com", "user:paddy@hashicorp.com"] + } + +`, zone, instanceName, roleId) } diff --git a/website/docs/r/compute_instance_iam.html.markdown b/website/docs/r/compute_instance_iam.html.markdown index e99533bd..a247f934 100644 --- a/website/docs/r/compute_instance_iam.html.markdown +++ b/website/docs/r/compute_instance_iam.html.markdown @@ -8,9 +8,6 @@ description: |- # IAM policy for GCE instance -~> **Warning:** These resources are in beta, and should be used with the terraform-provider-google-beta provider. -See [Provider Versions](https://terraform.io/docs/providers/google/provider_versions.html) for more details on beta resources. - Three different resources help you manage your IAM policy for GCE instance. Each of these resources serves a different use case: * `google_compute_instance_iam_policy`: Authoritative. Sets the IAM policy for the instance and replaces any existing policy already attached. @@ -26,7 +23,7 @@ Three different resources help you manage your IAM policy for GCE instance. Each ```hcl data "google_iam_policy" "admin" { binding { - role = "roles/editor" + role = "roles/compute.osLogin" members = [ "user:jane@example.com", @@ -35,8 +32,8 @@ data "google_iam_policy" "admin" { } resource "google_compute_instance_iam_policy" "instance" { - instance_name = "your-instance-id" - policy_data = "${data.google_iam_policy.admin.policy_data}" + instance_name = "your-instance-name" + policy_data = "${data.google_iam_policy.admin.policy_data}" } ``` @@ -44,8 +41,8 @@ resource "google_compute_instance_iam_policy" "instance" { ```hcl resource "google_compute_instance_iam_binding" "instance" { - instance_name = "your-instance-id" - role = "roles/compute.networkUser" + instance_name = "your-instance-name" + role = "roles/compute.osLoginr" members = [ "user:jane@example.com", @@ -57,8 +54,8 @@ resource "google_compute_instance_iam_binding" "instance" { ```hcl resource "google_compute_instance_iam_member" "instance" { - instance_name = "your-instance-id" - role = "roles/compute.networkUser" + instance_name = "your-instance-name" + role = "roles/compute.osLogin" member = "user:jane@example.com" } ``` From 9ee9dab9c99db992e6b0e73a9bd06fd98644b097 Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Tue, 7 May 2019 15:57:58 -0400 Subject: [PATCH 5/7] convert to spaces --- google/resource_compute_instance_iam_test.go | 184 +++++++++---------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/google/resource_compute_instance_iam_test.go b/google/resource_compute_instance_iam_test.go index aeb4724a..10093a73 100644 --- a/google/resource_compute_instance_iam_test.go +++ b/google/resource_compute_instance_iam_test.go @@ -1,114 +1,114 @@ package google import ( - "fmt" - "testing" + "fmt" + "testing" - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" ) func TestAccComputeInstanceIamBinding(t *testing.T) { - t.Parallel() + t.Parallel() - project := getTestProjectFromEnv() - role := "roles/compute.osLogin" - zone := getTestZoneFromEnv() - instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + project := getTestProjectFromEnv() + role := "roles/compute.osLogin" + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccComputeInstanceIamBinding_basic(zone, instanceName, role), - }, - { - ResourceName: "google_compute_instance_iam_binding.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), - ImportState: true, - ImportStateVerify: true, - }, - { - // Test Iam Binding update - Config: testAccComputeInstanceIamBinding_update(zone, instanceName, role), - }, - { - ResourceName: "google_compute_instance_iam_binding.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceIamBinding_basic(zone, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_binding.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccComputeInstanceIamBinding_update(zone, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_binding.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } func TestAccComputeInstanceIamMember(t *testing.T) { - t.Parallel() + t.Parallel() - project := getTestProjectFromEnv() - role := "roles/compute.osLogin" - zone := getTestZoneFromEnv() - instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + project := getTestProjectFromEnv() + role := "roles/compute.osLogin" + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - // Test Iam Member creation (no update for member, no need to test) - Config: testAccComputeInstanceIamMember_basic(zone, instanceName, role), - }, - { - ResourceName: "google_compute_instance_iam_member.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s user:admin@hashicorptest.com", project, zone, instanceName, role), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + // Test Iam Member creation (no update for member, no need to test) + Config: testAccComputeInstanceIamMember_basic(zone, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_member.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s user:admin@hashicorptest.com", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } func TestAccComputeInstanceIamPolicy(t *testing.T) { - t.Parallel() + t.Parallel() - project := getTestProjectFromEnv() - role := "roles/compute.osLogin" - zone := getTestZoneFromEnv() - instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + project := getTestProjectFromEnv() + role := "roles/compute.osLogin" + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccComputeInstanceIamPolicy_basic(zone, instanceName, role), - }, - // Test a few import formats - { - ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instanceName), - ImportState: true, - ImportStateVerify: true, - }, - { - ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s", project, zone, instanceName), - ImportState: true, - ImportStateVerify: true, - }, - { - ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s/%s", zone, instanceName), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceIamPolicy_basic(zone, instanceName, role), + }, + // Test a few import formats + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s", project, zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("%s/%s", zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } func testAccComputeInstanceIamMember_basic(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" @@ -137,7 +137,7 @@ func testAccComputeInstanceIamMember_basic(zone, instanceName, roleId string) st } func testAccComputeInstanceIamPolicy_basic(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" @@ -172,7 +172,7 @@ func testAccComputeInstanceIamPolicy_basic(zone, instanceName, roleId string) st } func testAccComputeInstanceIamBinding_basic(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" @@ -201,7 +201,7 @@ func testAccComputeInstanceIamBinding_basic(zone, instanceName, roleId string) s } func testAccComputeInstanceIamBinding_update(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" From fef7cffaef9f896d6bac8c30e481c0c6174e43aa Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Tue, 7 May 2019 17:02:26 -0400 Subject: [PATCH 6/7] gofmt --- google/resource_compute_instance_iam_test.go | 184 +++++++++---------- 1 file changed, 92 insertions(+), 92 deletions(-) diff --git a/google/resource_compute_instance_iam_test.go b/google/resource_compute_instance_iam_test.go index 10093a73..aeb4724a 100644 --- a/google/resource_compute_instance_iam_test.go +++ b/google/resource_compute_instance_iam_test.go @@ -1,114 +1,114 @@ package google import ( - "fmt" - "testing" + "fmt" + "testing" - "github.com/hashicorp/terraform/helper/acctest" - "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" ) func TestAccComputeInstanceIamBinding(t *testing.T) { - t.Parallel() + t.Parallel() - project := getTestProjectFromEnv() - role := "roles/compute.osLogin" - zone := getTestZoneFromEnv() - instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + project := getTestProjectFromEnv() + role := "roles/compute.osLogin" + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccComputeInstanceIamBinding_basic(zone, instanceName, role), - }, - { - ResourceName: "google_compute_instance_iam_binding.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), - ImportState: true, - ImportStateVerify: true, - }, - { - // Test Iam Binding update - Config: testAccComputeInstanceIamBinding_update(zone, instanceName, role), - }, - { - ResourceName: "google_compute_instance_iam_binding.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceIamBinding_basic(zone, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_binding.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccComputeInstanceIamBinding_update(zone, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_binding.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } func TestAccComputeInstanceIamMember(t *testing.T) { - t.Parallel() + t.Parallel() - project := getTestProjectFromEnv() - role := "roles/compute.osLogin" - zone := getTestZoneFromEnv() - instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + project := getTestProjectFromEnv() + role := "roles/compute.osLogin" + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - // Test Iam Member creation (no update for member, no need to test) - Config: testAccComputeInstanceIamMember_basic(zone, instanceName, role), - }, - { - ResourceName: "google_compute_instance_iam_member.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s %s user:admin@hashicorptest.com", project, zone, instanceName, role), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + // Test Iam Member creation (no update for member, no need to test) + Config: testAccComputeInstanceIamMember_basic(zone, instanceName, role), + }, + { + ResourceName: "google_compute_instance_iam_member.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s %s user:admin@hashicorptest.com", project, zone, instanceName, role), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } func TestAccComputeInstanceIamPolicy(t *testing.T) { - t.Parallel() + t.Parallel() - project := getTestProjectFromEnv() - role := "roles/compute.osLogin" - zone := getTestZoneFromEnv() - instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) + project := getTestProjectFromEnv() + role := "roles/compute.osLogin" + zone := getTestZoneFromEnv() + instanceName := fmt.Sprintf("tf-test-instance-%s", acctest.RandString(10)) - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccComputeInstanceIamPolicy_basic(zone, instanceName, role), - }, - // Test a few import formats - { - ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instanceName), - ImportState: true, - ImportStateVerify: true, - }, - { - ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s/%s/%s", project, zone, instanceName), - ImportState: true, - ImportStateVerify: true, - }, - { - ResourceName: "google_compute_instance_iam_policy.foo", - ImportStateId: fmt.Sprintf("%s/%s", zone, instanceName), - ImportState: true, - ImportStateVerify: true, - }, - }, - }) + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeInstanceIamPolicy_basic(zone, instanceName, role), + }, + // Test a few import formats + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/instances/%s", project, zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("%s/%s/%s", project, zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + { + ResourceName: "google_compute_instance_iam_policy.foo", + ImportStateId: fmt.Sprintf("%s/%s", zone, instanceName), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) } func testAccComputeInstanceIamMember_basic(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" @@ -137,7 +137,7 @@ func testAccComputeInstanceIamMember_basic(zone, instanceName, roleId string) st } func testAccComputeInstanceIamPolicy_basic(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" @@ -172,7 +172,7 @@ func testAccComputeInstanceIamPolicy_basic(zone, instanceName, roleId string) st } func testAccComputeInstanceIamBinding_basic(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" @@ -201,7 +201,7 @@ func testAccComputeInstanceIamBinding_basic(zone, instanceName, roleId string) s } func testAccComputeInstanceIamBinding_update(zone, instanceName, roleId string) string { - return fmt.Sprintf(` + return fmt.Sprintf(` resource "google_compute_instance" "test_vm" { zone = "%s" name = "%s" From 459436fd20b5d98e3f587a7ad041a5bc8d49361b Mon Sep 17 00:00:00 2001 From: Richard Hsu Date: Wed, 8 May 2019 12:11:48 -0400 Subject: [PATCH 7/7] fixing typos and roles --- website/docs/r/compute_instance_iam.html.markdown | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/website/docs/r/compute_instance_iam.html.markdown b/website/docs/r/compute_instance_iam.html.markdown index a247f934..9c54414e 100644 --- a/website/docs/r/compute_instance_iam.html.markdown +++ b/website/docs/r/compute_instance_iam.html.markdown @@ -42,7 +42,7 @@ resource "google_compute_instance_iam_policy" "instance" { ```hcl resource "google_compute_instance_iam_binding" "instance" { instance_name = "your-instance-name" - role = "roles/compute.osLoginr" + role = "roles/compute.osLogin" members = [ "user:jane@example.com", @@ -107,13 +107,13 @@ For all import syntaxes, the "resource in question" can take any of the followin IAM member imports use space-delimited identifiers; the resource in question, the role, and the member identity, e.g. ``` -$ terraform import google_compute_instance_iam_member.instance "project-name/zone-name/instance-name roles/compute.networkUser user:foo@example.com" +$ terraform import google_compute_instance_iam_member.instance "project-name/zone-name/instance-name roles/compute.osLogin user:foo@example.com" ``` IAM binding imports use space-delimited identifiers; the resource in question and the role, e.g. ``` -$ terraform import google_compute_instance_iam_binding.instance "project-name/zone-name/instance-name roles/compute.networkUser" +$ terraform import google_compute_instance_iam_binding.instance "project-name/zone-name/instance-name roles/compute.osLogin" ``` IAM policy imports use the identifier of the resource in question, e.g.