From fbb1f6edecc2727c666a06d6a63e902f8767aff6 Mon Sep 17 00:00:00 2001 From: Modular Magician Date: Wed, 26 Aug 2020 02:40:23 +0000 Subject: [PATCH] add IAM resources for disk & regionDisk (#3888) Co-authored-by: Edward Sun Signed-off-by: Modular Magician --- .changelog/3888.txt | 18 ++ google/iam_compute_disk.go | 200 ++++++++++++ google/iam_compute_disk_generated_test.go | 240 ++++++++++++++ google/iam_compute_region_disk.go | 200 ++++++++++++ .../iam_compute_region_disk_generated_test.go | 305 ++++++++++++++++++ google/provider.go | 10 +- website/docs/r/compute_disk_iam.html.markdown | 150 +++++++++ .../r/compute_region_disk_iam.html.markdown | 150 +++++++++ website/google.erb | 8 + 9 files changed, 1279 insertions(+), 2 deletions(-) create mode 100644 .changelog/3888.txt create mode 100644 google/iam_compute_disk.go create mode 100644 google/iam_compute_disk_generated_test.go create mode 100644 google/iam_compute_region_disk.go create mode 100644 google/iam_compute_region_disk_generated_test.go create mode 100644 website/docs/r/compute_disk_iam.html.markdown create mode 100644 website/docs/r/compute_region_disk_iam.html.markdown diff --git a/.changelog/3888.txt b/.changelog/3888.txt new file mode 100644 index 00000000000..b6c5de16d3a --- /dev/null +++ b/.changelog/3888.txt @@ -0,0 +1,18 @@ +```release-note:new-resource +google_compute_region_disk_iam_binding +``` +```release-note:new-resource +google_compute_region_disk_iam_policy +``` +```release-note:new-resource +google_compute_region_disk_iam_member +``` +```release-note:new-resource +google_compute_disk_iam_binding +``` +```release-note:new-resource +google_compute_disk_iam_policy +``` +```release-note:new-resource +google_compute_disk_iam_member +``` diff --git a/google/iam_compute_disk.go b/google/iam_compute_disk.go new file mode 100644 index 00000000000..926200014d2 --- /dev/null +++ b/google/iam_compute_disk.go @@ -0,0 +1,200 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- +package google + +import ( + "fmt" + + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "google.golang.org/api/cloudresourcemanager/v1" +) + +var ComputeDiskIamSchema = map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "zone": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: compareSelfLinkOrResourceName, + }, +} + +type ComputeDiskIamUpdater struct { + project string + zone string + name string + d *schema.ResourceData + Config *Config +} + +func ComputeDiskIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { + values := make(map[string]string) + + project, _ := getProject(d, config) + if project != "" { + d.Set("project", project) + } + values["project"] = project + zone, _ := getZone(d, config) + if zone != "" { + d.Set("zone", zone) + } + values["zone"] = zone + if v, ok := d.GetOk("name"); ok { + values["name"] = v.(string) + } + + // We may have gotten either a long or short name, so attempt to parse long name if possible + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/zones/(?P[^/]+)/disks/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Get("name").(string)) + if err != nil { + return nil, err + } + + for k, v := range m { + values[k] = v + } + + u := &ComputeDiskIamUpdater{ + project: values["project"], + zone: values["zone"], + name: values["name"], + d: d, + Config: config, + } + + d.Set("project", u.project) + d.Set("zone", u.zone) + d.Set("name", u.GetResourceId()) + + return u, nil +} + +func ComputeDiskIdParseFunc(d *schema.ResourceData, config *Config) error { + values := make(map[string]string) + + project, _ := getProject(d, config) + if project != "" { + values["project"] = project + } + + zone, _ := getZone(d, config) + if zone != "" { + values["zone"] = zone + } + + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/zones/(?P[^/]+)/disks/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return err + } + + for k, v := range m { + values[k] = v + } + + u := &ComputeDiskIamUpdater{ + project: values["project"], + zone: values["zone"], + name: values["name"], + d: d, + Config: config, + } + d.Set("name", u.GetResourceId()) + d.SetId(u.GetResourceId()) + return nil +} + +func (u *ComputeDiskIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { + url, err := u.qualifyDiskUrl("getIamPolicy") + if err != nil { + return nil, err + } + + project, err := getProject(u.d, u.Config) + if err != nil { + return nil, err + } + var obj map[string]interface{} + + policy, err := sendRequest(u.Config, "GET", project, url, obj) + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + out := &cloudresourcemanager.Policy{} + err = Convert(policy, out) + if err != nil { + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) + } + + return out, nil +} + +func (u *ComputeDiskIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { + json, err := ConvertToMap(policy) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + obj["policy"] = json + + url, err := u.qualifyDiskUrl("setIamPolicy") + if err != nil { + return err + } + project, err := getProject(u.d, u.Config) + if err != nil { + return err + } + + _, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate)) + if err != nil { + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + return nil +} + +func (u *ComputeDiskIamUpdater) qualifyDiskUrl(methodIdentifier string) (string, error) { + urlTemplate := fmt.Sprintf("{{ComputeBasePath}}%s/%s", fmt.Sprintf("projects/%s/zones/%s/disks/%s", u.project, u.zone, u.name), methodIdentifier) + url, err := replaceVars(u.d, u.Config, urlTemplate) + if err != nil { + return "", err + } + return url, nil +} + +func (u *ComputeDiskIamUpdater) GetResourceId() string { + return fmt.Sprintf("projects/%s/zones/%s/disks/%s", u.project, u.zone, u.name) +} + +func (u *ComputeDiskIamUpdater) GetMutexKey() string { + return fmt.Sprintf("iam-compute-disk-%s", u.GetResourceId()) +} + +func (u *ComputeDiskIamUpdater) DescribeResource() string { + return fmt.Sprintf("compute disk %q", u.GetResourceId()) +} diff --git a/google/iam_compute_disk_generated_test.go b/google/iam_compute_disk_generated_test.go new file mode 100644 index 00000000000..4e974974fc6 --- /dev/null +++ b/google/iam_compute_disk_generated_test.go @@ -0,0 +1,240 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccComputeDiskIamBindingGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "role": "roles/viewer", + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeDiskIamBinding_basicGenerated(context), + }, + { + ResourceName: "google_compute_disk_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/disks/%s roles/viewer", getTestProjectFromEnv(), getTestZoneFromEnv(), fmt.Sprintf("tf-test-test-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccComputeDiskIamBinding_updateGenerated(context), + }, + { + ResourceName: "google_compute_disk_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/disks/%s roles/viewer", getTestProjectFromEnv(), getTestZoneFromEnv(), fmt.Sprintf("tf-test-test-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccComputeDiskIamMemberGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "role": "roles/viewer", + } + + vcrTest(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: testAccComputeDiskIamMember_basicGenerated(context), + }, + { + ResourceName: "google_compute_disk_iam_member.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/disks/%s roles/viewer user:admin@hashicorptest.com", getTestProjectFromEnv(), getTestZoneFromEnv(), fmt.Sprintf("tf-test-test-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccComputeDiskIamPolicyGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "role": "roles/viewer", + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeDiskIamPolicy_basicGenerated(context), + }, + { + ResourceName: "google_compute_disk_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/disks/%s", getTestProjectFromEnv(), getTestZoneFromEnv(), fmt.Sprintf("tf-test-test-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccComputeDiskIamPolicy_emptyBinding(context), + }, + { + ResourceName: "google_compute_disk_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/zones/%s/disks/%s", getTestProjectFromEnv(), getTestZoneFromEnv(), fmt.Sprintf("tf-test-test-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccComputeDiskIamMember_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk" "default" { + name = "tf-test-test-disk%{random_suffix}" + type = "pd-ssd" + zone = "us-central1-a" + image = "debian-8-jessie-v20170523" + labels = { + environment = "dev" + } + physical_block_size_bytes = 4096 +} + +resource "google_compute_disk_iam_member" "foo" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + role = "%{role}" + member = "user:admin@hashicorptest.com" +} +`, context) +} + +func testAccComputeDiskIamPolicy_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk" "default" { + name = "tf-test-test-disk%{random_suffix}" + type = "pd-ssd" + zone = "us-central1-a" + image = "debian-8-jessie-v20170523" + labels = { + environment = "dev" + } + physical_block_size_bytes = 4096 +} + +data "google_iam_policy" "foo" { + binding { + role = "%{role}" + members = ["user:admin@hashicorptest.com"] + } +} + +resource "google_compute_disk_iam_policy" "foo" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + policy_data = data.google_iam_policy.foo.policy_data +} +`, context) +} + +func testAccComputeDiskIamPolicy_emptyBinding(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk" "default" { + name = "tf-test-test-disk%{random_suffix}" + type = "pd-ssd" + zone = "us-central1-a" + image = "debian-8-jessie-v20170523" + labels = { + environment = "dev" + } + physical_block_size_bytes = 4096 +} + +data "google_iam_policy" "foo" { +} + +resource "google_compute_disk_iam_policy" "foo" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + policy_data = data.google_iam_policy.foo.policy_data +} +`, context) +} + +func testAccComputeDiskIamBinding_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk" "default" { + name = "tf-test-test-disk%{random_suffix}" + type = "pd-ssd" + zone = "us-central1-a" + image = "debian-8-jessie-v20170523" + labels = { + environment = "dev" + } + physical_block_size_bytes = 4096 +} + +resource "google_compute_disk_iam_binding" "foo" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + role = "%{role}" + members = ["user:admin@hashicorptest.com"] +} +`, context) +} + +func testAccComputeDiskIamBinding_updateGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_disk" "default" { + name = "tf-test-test-disk%{random_suffix}" + type = "pd-ssd" + zone = "us-central1-a" + image = "debian-8-jessie-v20170523" + labels = { + environment = "dev" + } + physical_block_size_bytes = 4096 +} + +resource "google_compute_disk_iam_binding" "foo" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + role = "%{role}" + members = ["user:admin@hashicorptest.com", "user:paddy@hashicorp.com"] +} +`, context) +} diff --git a/google/iam_compute_region_disk.go b/google/iam_compute_region_disk.go new file mode 100644 index 00000000000..3227375c7dc --- /dev/null +++ b/google/iam_compute_region_disk.go @@ -0,0 +1,200 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- +package google + +import ( + "fmt" + + "github.com/hashicorp/errwrap" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "google.golang.org/api/cloudresourcemanager/v1" +) + +var ComputeRegionDiskIamSchema = map[string]*schema.Schema{ + "project": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "region": { + Type: schema.TypeString, + Computed: true, + Optional: true, + ForceNew: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + DiffSuppressFunc: compareSelfLinkOrResourceName, + }, +} + +type ComputeRegionDiskIamUpdater struct { + project string + region string + name string + d *schema.ResourceData + Config *Config +} + +func ComputeRegionDiskIamUpdaterProducer(d *schema.ResourceData, config *Config) (ResourceIamUpdater, error) { + values := make(map[string]string) + + project, _ := getProject(d, config) + if project != "" { + d.Set("project", project) + } + values["project"] = project + region, _ := getRegion(d, config) + if region != "" { + d.Set("region", region) + } + values["region"] = region + if v, ok := d.GetOk("name"); ok { + values["name"] = v.(string) + } + + // We may have gotten either a long or short name, so attempt to parse long name if possible + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/regions/(?P[^/]+)/disks/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Get("name").(string)) + if err != nil { + return nil, err + } + + for k, v := range m { + values[k] = v + } + + u := &ComputeRegionDiskIamUpdater{ + project: values["project"], + region: values["region"], + name: values["name"], + d: d, + Config: config, + } + + d.Set("project", u.project) + d.Set("region", u.region) + d.Set("name", u.GetResourceId()) + + return u, nil +} + +func ComputeRegionDiskIdParseFunc(d *schema.ResourceData, config *Config) error { + values := make(map[string]string) + + project, _ := getProject(d, config) + if project != "" { + values["project"] = project + } + + region, _ := getRegion(d, config) + if region != "" { + values["region"] = region + } + + m, err := getImportIdQualifiers([]string{"projects/(?P[^/]+)/regions/(?P[^/]+)/disks/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config, d.Id()) + if err != nil { + return err + } + + for k, v := range m { + values[k] = v + } + + u := &ComputeRegionDiskIamUpdater{ + project: values["project"], + region: values["region"], + name: values["name"], + d: d, + Config: config, + } + d.Set("name", u.GetResourceId()) + d.SetId(u.GetResourceId()) + return nil +} + +func (u *ComputeRegionDiskIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { + url, err := u.qualifyRegionDiskUrl("getIamPolicy") + if err != nil { + return nil, err + } + + project, err := getProject(u.d, u.Config) + if err != nil { + return nil, err + } + var obj map[string]interface{} + + policy, err := sendRequest(u.Config, "GET", project, url, obj) + if err != nil { + return nil, errwrap.Wrapf(fmt.Sprintf("Error retrieving IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + out := &cloudresourcemanager.Policy{} + err = Convert(policy, out) + if err != nil { + return nil, errwrap.Wrapf("Cannot convert a policy to a resource manager policy: {{err}}", err) + } + + return out, nil +} + +func (u *ComputeRegionDiskIamUpdater) SetResourceIamPolicy(policy *cloudresourcemanager.Policy) error { + json, err := ConvertToMap(policy) + if err != nil { + return err + } + + obj := make(map[string]interface{}) + obj["policy"] = json + + url, err := u.qualifyRegionDiskUrl("setIamPolicy") + if err != nil { + return err + } + project, err := getProject(u.d, u.Config) + if err != nil { + return err + } + + _, err = sendRequestWithTimeout(u.Config, "POST", project, url, obj, u.d.Timeout(schema.TimeoutCreate)) + if err != nil { + return errwrap.Wrapf(fmt.Sprintf("Error setting IAM policy for %s: {{err}}", u.DescribeResource()), err) + } + + return nil +} + +func (u *ComputeRegionDiskIamUpdater) qualifyRegionDiskUrl(methodIdentifier string) (string, error) { + urlTemplate := fmt.Sprintf("{{ComputeBasePath}}%s/%s", fmt.Sprintf("projects/%s/regions/%s/disks/%s", u.project, u.region, u.name), methodIdentifier) + url, err := replaceVars(u.d, u.Config, urlTemplate) + if err != nil { + return "", err + } + return url, nil +} + +func (u *ComputeRegionDiskIamUpdater) GetResourceId() string { + return fmt.Sprintf("projects/%s/regions/%s/disks/%s", u.project, u.region, u.name) +} + +func (u *ComputeRegionDiskIamUpdater) GetMutexKey() string { + return fmt.Sprintf("iam-compute-regiondisk-%s", u.GetResourceId()) +} + +func (u *ComputeRegionDiskIamUpdater) DescribeResource() string { + return fmt.Sprintf("compute regiondisk %q", u.GetResourceId()) +} diff --git a/google/iam_compute_region_disk_generated_test.go b/google/iam_compute_region_disk_generated_test.go new file mode 100644 index 00000000000..afa31e35cc3 --- /dev/null +++ b/google/iam_compute_region_disk_generated_test.go @@ -0,0 +1,305 @@ +// ---------------------------------------------------------------------------- +// +// *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +// +// ---------------------------------------------------------------------------- +// +// This file is automatically generated by Magic Modules and manual +// changes will be clobbered when the file is regenerated. +// +// Please read more about how to change this file in +// .github/CONTRIBUTING.md. +// +// ---------------------------------------------------------------------------- + +package google + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" +) + +func TestAccComputeRegionDiskIamBindingGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "role": "roles/viewer", + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeRegionDiskIamBinding_basicGenerated(context), + }, + { + ResourceName: "google_compute_region_disk_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/regions/%s/disks/%s roles/viewer", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + // Test Iam Binding update + Config: testAccComputeRegionDiskIamBinding_updateGenerated(context), + }, + { + ResourceName: "google_compute_region_disk_iam_binding.foo", + ImportStateId: fmt.Sprintf("projects/%s/regions/%s/disks/%s roles/viewer", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccComputeRegionDiskIamMemberGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "role": "roles/viewer", + } + + vcrTest(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: testAccComputeRegionDiskIamMember_basicGenerated(context), + }, + { + ResourceName: "google_compute_region_disk_iam_member.foo", + ImportStateId: fmt.Sprintf("projects/%s/regions/%s/disks/%s roles/viewer user:admin@hashicorptest.com", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccComputeRegionDiskIamPolicyGenerated(t *testing.T) { + t.Parallel() + + context := map[string]interface{}{ + "random_suffix": randString(t, 10), + "role": "roles/viewer", + } + + vcrTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccComputeRegionDiskIamPolicy_basicGenerated(context), + }, + { + ResourceName: "google_compute_region_disk_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/regions/%s/disks/%s", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccComputeRegionDiskIamPolicy_emptyBinding(context), + }, + { + ResourceName: "google_compute_region_disk_iam_policy.foo", + ImportStateId: fmt.Sprintf("projects/%s/regions/%s/disks/%s", getTestProjectFromEnv(), getTestRegionFromEnv(), fmt.Sprintf("tf-test-my-region-disk%s", context["random_suffix"])), + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccComputeRegionDiskIamMember_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_region_disk" "regiondisk" { + name = "tf-test-my-region-disk%{random_suffix}" + snapshot = google_compute_snapshot.snapdisk.id + type = "pd-ssd" + region = "us-central1" + physical_block_size_bytes = 4096 + + replica_zones = ["us-central1-a", "us-central1-f"] +} + +resource "google_compute_disk" "disk" { + name = "tf-test-my-disk%{random_suffix}" + image = "debian-cloud/debian-9" + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_snapshot" "snapdisk" { + name = "tf-test-my-snapshot%{random_suffix}" + source_disk = google_compute_disk.disk.name + zone = "us-central1-a" +} + +resource "google_compute_region_disk_iam_member" "foo" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + role = "%{role}" + member = "user:admin@hashicorptest.com" +} +`, context) +} + +func testAccComputeRegionDiskIamPolicy_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_region_disk" "regiondisk" { + name = "tf-test-my-region-disk%{random_suffix}" + snapshot = google_compute_snapshot.snapdisk.id + type = "pd-ssd" + region = "us-central1" + physical_block_size_bytes = 4096 + + replica_zones = ["us-central1-a", "us-central1-f"] +} + +resource "google_compute_disk" "disk" { + name = "tf-test-my-disk%{random_suffix}" + image = "debian-cloud/debian-9" + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_snapshot" "snapdisk" { + name = "tf-test-my-snapshot%{random_suffix}" + source_disk = google_compute_disk.disk.name + zone = "us-central1-a" +} + +data "google_iam_policy" "foo" { + binding { + role = "%{role}" + members = ["user:admin@hashicorptest.com"] + } +} + +resource "google_compute_region_disk_iam_policy" "foo" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + policy_data = data.google_iam_policy.foo.policy_data +} +`, context) +} + +func testAccComputeRegionDiskIamPolicy_emptyBinding(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_region_disk" "regiondisk" { + name = "tf-test-my-region-disk%{random_suffix}" + snapshot = google_compute_snapshot.snapdisk.id + type = "pd-ssd" + region = "us-central1" + physical_block_size_bytes = 4096 + + replica_zones = ["us-central1-a", "us-central1-f"] +} + +resource "google_compute_disk" "disk" { + name = "tf-test-my-disk%{random_suffix}" + image = "debian-cloud/debian-9" + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_snapshot" "snapdisk" { + name = "tf-test-my-snapshot%{random_suffix}" + source_disk = google_compute_disk.disk.name + zone = "us-central1-a" +} + +data "google_iam_policy" "foo" { +} + +resource "google_compute_region_disk_iam_policy" "foo" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + policy_data = data.google_iam_policy.foo.policy_data +} +`, context) +} + +func testAccComputeRegionDiskIamBinding_basicGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_region_disk" "regiondisk" { + name = "tf-test-my-region-disk%{random_suffix}" + snapshot = google_compute_snapshot.snapdisk.id + type = "pd-ssd" + region = "us-central1" + physical_block_size_bytes = 4096 + + replica_zones = ["us-central1-a", "us-central1-f"] +} + +resource "google_compute_disk" "disk" { + name = "tf-test-my-disk%{random_suffix}" + image = "debian-cloud/debian-9" + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_snapshot" "snapdisk" { + name = "tf-test-my-snapshot%{random_suffix}" + source_disk = google_compute_disk.disk.name + zone = "us-central1-a" +} + +resource "google_compute_region_disk_iam_binding" "foo" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + role = "%{role}" + members = ["user:admin@hashicorptest.com"] +} +`, context) +} + +func testAccComputeRegionDiskIamBinding_updateGenerated(context map[string]interface{}) string { + return Nprintf(` +resource "google_compute_region_disk" "regiondisk" { + name = "tf-test-my-region-disk%{random_suffix}" + snapshot = google_compute_snapshot.snapdisk.id + type = "pd-ssd" + region = "us-central1" + physical_block_size_bytes = 4096 + + replica_zones = ["us-central1-a", "us-central1-f"] +} + +resource "google_compute_disk" "disk" { + name = "tf-test-my-disk%{random_suffix}" + image = "debian-cloud/debian-9" + size = 50 + type = "pd-ssd" + zone = "us-central1-a" +} + +resource "google_compute_snapshot" "snapdisk" { + name = "tf-test-my-snapshot%{random_suffix}" + source_disk = google_compute_disk.disk.name + zone = "us-central1-a" +} + +resource "google_compute_region_disk_iam_binding" "foo" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + role = "%{role}" + members = ["user:admin@hashicorptest.com", "user:paddy@hashicorp.com"] +} +`, context) +} diff --git a/google/provider.go b/google/provider.go index cfac4198db6..dcfdd8c946f 100644 --- a/google/provider.go +++ b/google/provider.go @@ -616,8 +616,8 @@ func Provider() terraform.ResourceProvider { } // Generated resources: 155 -// Generated IAM resources: 63 -// Total generated resources: 218 +// Generated IAM resources: 69 +// Total generated resources: 224 func ResourceMap() map[string]*schema.Resource { resourceMap, _ := ResourceMapWithErrors() return resourceMap @@ -682,6 +682,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_compute_region_disk_resource_policy_attachment": resourceComputeRegionDiskResourcePolicyAttachment(), "google_compute_disk_resource_policy_attachment": resourceComputeDiskResourcePolicyAttachment(), "google_compute_disk": resourceComputeDisk(), + "google_compute_disk_iam_binding": ResourceIamBinding(ComputeDiskIamSchema, ComputeDiskIamUpdaterProducer, ComputeDiskIdParseFunc), + "google_compute_disk_iam_member": ResourceIamMember(ComputeDiskIamSchema, ComputeDiskIamUpdaterProducer, ComputeDiskIdParseFunc), + "google_compute_disk_iam_policy": ResourceIamPolicy(ComputeDiskIamSchema, ComputeDiskIamUpdaterProducer, ComputeDiskIdParseFunc), "google_compute_firewall": resourceComputeFirewall(), "google_compute_forwarding_rule": resourceComputeForwardingRule(), "google_compute_global_address": resourceComputeGlobalAddress(), @@ -708,6 +711,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { "google_compute_node_template": resourceComputeNodeTemplate(), "google_compute_region_autoscaler": resourceComputeRegionAutoscaler(), "google_compute_region_disk": resourceComputeRegionDisk(), + "google_compute_region_disk_iam_binding": ResourceIamBinding(ComputeRegionDiskIamSchema, ComputeRegionDiskIamUpdaterProducer, ComputeRegionDiskIdParseFunc), + "google_compute_region_disk_iam_member": ResourceIamMember(ComputeRegionDiskIamSchema, ComputeRegionDiskIamUpdaterProducer, ComputeRegionDiskIdParseFunc), + "google_compute_region_disk_iam_policy": ResourceIamPolicy(ComputeRegionDiskIamSchema, ComputeRegionDiskIamUpdaterProducer, ComputeRegionDiskIdParseFunc), "google_compute_region_url_map": resourceComputeRegionUrlMap(), "google_compute_region_health_check": resourceComputeRegionHealthCheck(), "google_compute_resource_policy": resourceComputeResourcePolicy(), diff --git a/website/docs/r/compute_disk_iam.html.markdown b/website/docs/r/compute_disk_iam.html.markdown new file mode 100644 index 00000000000..676997eda45 --- /dev/null +++ b/website/docs/r/compute_disk_iam.html.markdown @@ -0,0 +1,150 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +subcategory: "Compute Engine" +layout: "google" +page_title: "Google: google_compute_disk_iam" +sidebar_current: "docs-google-compute-disk-iam" +description: |- + Collection of resources to manage IAM policy for Compute Engine Disk +--- + +# IAM policy for Compute Engine Disk +Three different resources help you manage your IAM policy for Compute Engine Disk. Each of these resources serves a different use case: + +* `google_compute_disk_iam_policy`: Authoritative. Sets the IAM policy for the disk and replaces any existing policy already attached. +* `google_compute_disk_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 disk are preserved. +* `google_compute_disk_iam_member`: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the disk are preserved. + +~> **Note:** `google_compute_disk_iam_policy` **cannot** be used in conjunction with `google_compute_disk_iam_binding` and `google_compute_disk_iam_member` or they will fight over what your policy should be. + +~> **Note:** `google_compute_disk_iam_binding` resources **can be** used in conjunction with `google_compute_disk_iam_member` resources **only if** they do not grant privilege to the same role. + + + +## google\_compute\_disk\_iam\_policy + +```hcl +data "google_iam_policy" "admin" { + binding { + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] + } +} + +resource "google_compute_disk_iam_policy" "policy" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + policy_data = data.google_iam_policy.admin.policy_data +} +``` + +## google\_compute\_disk\_iam\_binding + +```hcl +resource "google_compute_disk_iam_binding" "binding" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] +} +``` + +## google\_compute\_disk\_iam\_member + +```hcl +resource "google_compute_disk_iam_member" "member" { + project = google_compute_disk.default.project + zone = google_compute_disk.default.zone + name = google_compute_disk.default.name + role = "roles/viewer" + member = "user:jane@example.com" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Used to find the parent resource to bind the IAM policy to +* `zone` - (Optional) A reference to the zone where the disk resides. Used to find the parent resource to bind the IAM policy to. If not specified, + the value will be parsed from the identifier of the parent resource. If no zone is provided in the parent identifier and no + zone is specified, it is taken from the provider configuration. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used. + +* `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_disk_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_disk_iam_policy`) The policy data generated by + a `google_iam_policy` data source. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are +exported: + +* `etag` - (Computed) The etag of the IAM policy. + +## Import + +For all import syntaxes, the "resource in question" can take any of the following forms: + +* projects/{{project}}/zones/{{zone}}/disks/{{name}} +* {{project}}/{{zone}}/{{name}} +* {{zone}}/{{name}} +* {{name}} + +Any variables not passed in the import command will be taken from the provider configuration. + +Compute Engine disk IAM resources can be imported using the resource identifiers, role, and member. + +IAM member imports use space-delimited identifiers: the resource in question, the role, and the member identity, e.g. +``` +$ terraform import google_compute_disk_iam_member.editor "projects/{{project}}/zones/{{zone}}/disks/{{disk}} roles/viewer user:jane@example.com" +``` + +IAM binding imports use space-delimited identifiers: the resource in question and the role, e.g. +``` +$ terraform import google_compute_disk_iam_binding.editor "projects/{{project}}/zones/{{zone}}/disks/{{disk}} roles/viewer" +``` + +IAM policy imports use the identifier of the resource in question, e.g. +``` +$ terraform import google_compute_disk_iam_policy.editor projects/{{project}}/zones/{{zone}}/disks/{{disk}} +``` + +-> **Custom Roles**: If you're importing a IAM resource with a custom role, make sure to use the + full name of the custom role, e.g. `[projects/my-project|organizations/my-org]/roles/my-custom-role`. + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override). diff --git a/website/docs/r/compute_region_disk_iam.html.markdown b/website/docs/r/compute_region_disk_iam.html.markdown new file mode 100644 index 00000000000..2dde3c9b977 --- /dev/null +++ b/website/docs/r/compute_region_disk_iam.html.markdown @@ -0,0 +1,150 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** AUTO GENERATED CODE *** AUTO GENERATED CODE *** +# +# ---------------------------------------------------------------------------- +# +# This file is automatically generated by Magic Modules and manual +# changes will be clobbered when the file is regenerated. +# +# Please read more about how to change this file in +# .github/CONTRIBUTING.md. +# +# ---------------------------------------------------------------------------- +subcategory: "Compute Engine" +layout: "google" +page_title: "Google: google_compute_region_disk_iam" +sidebar_current: "docs-google-compute-region-disk-iam" +description: |- + Collection of resources to manage IAM policy for Compute Engine RegionDisk +--- + +# IAM policy for Compute Engine RegionDisk +Three different resources help you manage your IAM policy for Compute Engine RegionDisk. Each of these resources serves a different use case: + +* `google_compute_region_disk_iam_policy`: Authoritative. Sets the IAM policy for the regiondisk and replaces any existing policy already attached. +* `google_compute_region_disk_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 regiondisk are preserved. +* `google_compute_region_disk_iam_member`: Non-authoritative. Updates the IAM policy to grant a role to a new member. Other members for the role for the regiondisk are preserved. + +~> **Note:** `google_compute_region_disk_iam_policy` **cannot** be used in conjunction with `google_compute_region_disk_iam_binding` and `google_compute_region_disk_iam_member` or they will fight over what your policy should be. + +~> **Note:** `google_compute_region_disk_iam_binding` resources **can be** used in conjunction with `google_compute_region_disk_iam_member` resources **only if** they do not grant privilege to the same role. + + + +## google\_compute\_region\_disk\_iam\_policy + +```hcl +data "google_iam_policy" "admin" { + binding { + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] + } +} + +resource "google_compute_region_disk_iam_policy" "policy" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + policy_data = data.google_iam_policy.admin.policy_data +} +``` + +## google\_compute\_region\_disk\_iam\_binding + +```hcl +resource "google_compute_region_disk_iam_binding" "binding" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + role = "roles/viewer" + members = [ + "user:jane@example.com", + ] +} +``` + +## google\_compute\_region\_disk\_iam\_member + +```hcl +resource "google_compute_region_disk_iam_member" "member" { + project = google_compute_region_disk.regiondisk.project + region = google_compute_region_disk.regiondisk.region + name = google_compute_region_disk.regiondisk.name + role = "roles/viewer" + member = "user:jane@example.com" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) Used to find the parent resource to bind the IAM policy to +* `region` - (Optional) A reference to the region where the disk resides. Used to find the parent resource to bind the IAM policy to. If not specified, + the value will be parsed from the identifier of the parent resource. If no region is provided in the parent identifier and no + region is specified, it is taken from the provider configuration. + +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the project will be parsed from the identifier of the parent resource. If no project is provided in the parent identifier and no project is specified, the provider project is used. + +* `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_region_disk_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_region_disk_iam_policy`) The policy data generated by + a `google_iam_policy` data source. + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are +exported: + +* `etag` - (Computed) The etag of the IAM policy. + +## Import + +For all import syntaxes, the "resource in question" can take any of the following forms: + +* projects/{{project}}/regions/{{region}}/disks/{{name}} +* {{project}}/{{region}}/{{name}} +* {{region}}/{{name}} +* {{name}} + +Any variables not passed in the import command will be taken from the provider configuration. + +Compute Engine regiondisk IAM resources can be imported using the resource identifiers, role, and member. + +IAM member imports use space-delimited identifiers: the resource in question, the role, and the member identity, e.g. +``` +$ terraform import google_compute_region_disk_iam_member.editor "projects/{{project}}/regions/{{region}}/disks/{{region_disk}} roles/viewer user:jane@example.com" +``` + +IAM binding imports use space-delimited identifiers: the resource in question and the role, e.g. +``` +$ terraform import google_compute_region_disk_iam_binding.editor "projects/{{project}}/regions/{{region}}/disks/{{region_disk}} roles/viewer" +``` + +IAM policy imports use the identifier of the resource in question, e.g. +``` +$ terraform import google_compute_region_disk_iam_policy.editor projects/{{project}}/regions/{{region}}/disks/{{region_disk}} +``` + +-> **Custom Roles**: If you're importing a IAM resource with a custom role, make sure to use the + full name of the custom role, e.g. `[projects/my-project|organizations/my-org]/roles/my-custom-role`. + +## User Project Overrides + +This resource supports [User Project Overrides](https://www.terraform.io/docs/providers/google/guides/provider_reference.html#user_project_override). diff --git a/website/google.erb b/website/google.erb index c17047f2702..da68bbc993c 100644 --- a/website/google.erb +++ b/website/google.erb @@ -1277,6 +1277,10 @@ google_compute_disk +
  • + google_compute_disk_iam +
  • +
  • google_compute_disk_resource_policy_attachment
  • @@ -1409,6 +1413,10 @@ google_compute_region_disk +
  • + google_compute_region_disk_iam +
  • +
  • google_compute_region_disk_resource_policy_attachment