From 17c7549bdc5f7b5cefe25a0847a09abfe3210596 Mon Sep 17 00:00:00 2001 From: Sam Levenick Date: Mon, 30 Sep 2019 21:22:03 +0000 Subject: [PATCH] Upstream network_peering import Signed-off-by: Modular Magician --- google/provider.go | 1 - google/resource_bigtable_gc_policy.go | 231 ------------------ google/resource_bigtable_gc_policy_test.go | 199 --------------- google/resource_compute_network_peering.go | 20 +- .../resource_compute_network_peering_test.go | 5 + .../docs/r/bigtable_gc_policy.html.markdown | 98 -------- .../r/compute_network_peering.html.markdown | 7 + 7 files changed, 31 insertions(+), 530 deletions(-) delete mode 100644 google/resource_bigtable_gc_policy.go delete mode 100644 google/resource_bigtable_gc_policy_test.go delete mode 100644 website/docs/r/bigtable_gc_policy.html.markdown diff --git a/google/provider.go b/google/provider.go index db66f50d1b0..7a9ee41c342 100644 --- a/google/provider.go +++ b/google/provider.go @@ -559,7 +559,6 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) { map[string]*schema.Resource{ "google_app_engine_application": resourceAppEngineApplication(), "google_bigquery_table": resourceBigQueryTable(), - "google_bigtable_gc_policy": resourceBigtableGCPolicy(), "google_bigtable_instance": resourceBigtableInstance(), "google_bigtable_instance_iam_binding": ResourceIamBinding(IamBigtableInstanceSchema, NewBigtableInstanceUpdater, BigtableInstanceIdParseFunc), "google_bigtable_instance_iam_member": ResourceIamMember(IamBigtableInstanceSchema, NewBigtableInstanceUpdater, BigtableInstanceIdParseFunc), diff --git a/google/resource_bigtable_gc_policy.go b/google/resource_bigtable_gc_policy.go deleted file mode 100644 index d77f9ea1cac..00000000000 --- a/google/resource_bigtable_gc_policy.go +++ /dev/null @@ -1,231 +0,0 @@ -package google - -import ( - "context" - "fmt" - "log" - "time" - - "cloud.google.com/go/bigtable" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" -) - -const ( - GCPolicyModeIntersection = "INTERSECTION" - GCPolicyModeUnion = "UNION" -) - -func resourceBigtableGCPolicy() *schema.Resource { - return &schema.Resource{ - Create: resourceBigtableGCPolicyCreate, - Read: resourceBigtableGCPolicyRead, - Delete: resourceBigtableGCPolicyDestroy, - - Schema: map[string]*schema.Schema{ - "instance_name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "table": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "column_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - - "mode": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{GCPolicyModeIntersection, GCPolicyModeUnion}, false), - }, - - "max_age": { - Type: schema.TypeList, - Optional: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "days": { - Type: schema.TypeInt, - Required: true, - }, - }, - }, - }, - - "max_version": { - Type: schema.TypeList, - Optional: true, - ForceNew: true, - Elem: &schema.Resource{ - Schema: map[string]*schema.Schema{ - "number": { - Type: schema.TypeInt, - Required: true, - }, - }, - }, - }, - - "project": { - Type: schema.TypeString, - Optional: true, - Computed: true, - ForceNew: true, - }, - }, - } -} - -func resourceBigtableGCPolicyCreate(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - ctx := context.Background() - - project, err := getProject(d, config) - if err != nil { - return err - } - - instanceName := d.Get("instance_name").(string) - c, err := config.bigtableClientFactory.NewAdminClient(project, instanceName) - if err != nil { - return fmt.Errorf("Error starting admin client. %s", err) - } - - defer c.Close() - - gcPolicy, err := generateBigtableGCPolicy(d) - if err != nil { - return err - } - - tableName := d.Get("table").(string) - columnFamily := d.Get("column_family").(string) - - if err := c.SetGCPolicy(ctx, tableName, columnFamily, gcPolicy); err != nil { - return err - } - - table, err := c.TableInfo(ctx, tableName) - if err != nil { - return fmt.Errorf("Error retrieving table. Could not find %s in %s. %s", tableName, instanceName, err) - } - - for _, i := range table.FamilyInfos { - if i.Name == columnFamily { - d.SetId(i.GCPolicy) - } - } - - return resourceBigtableGCPolicyRead(d, meta) -} - -func resourceBigtableGCPolicyRead(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - ctx := context.Background() - - project, err := getProject(d, config) - if err != nil { - return err - } - - instanceName := d.Get("instance_name").(string) - c, err := config.bigtableClientFactory.NewAdminClient(project, instanceName) - if err != nil { - return fmt.Errorf("Error starting admin client. %s", err) - } - - defer c.Close() - - name := d.Get("table").(string) - ti, err := c.TableInfo(ctx, name) - if err != nil { - log.Printf("[WARN] Removing %s because it's gone", name) - d.SetId("") - return fmt.Errorf("Error retrieving table. Could not find %s in %s. %s", name, instanceName, err) - } - - for _, fi := range ti.FamilyInfos { - if fi.Name == name { - d.SetId(fi.GCPolicy) - break - } - } - - d.Set("project", project) - - return nil -} - -func resourceBigtableGCPolicyDestroy(d *schema.ResourceData, meta interface{}) error { - config := meta.(*Config) - ctx := context.Background() - - project, err := getProject(d, config) - if err != nil { - return err - } - - instanceName := d.Get("instance_name").(string) - c, err := config.bigtableClientFactory.NewAdminClient(project, instanceName) - if err != nil { - return fmt.Errorf("Error starting admin client. %s", err) - } - - defer c.Close() - - if err := c.SetGCPolicy(ctx, d.Get("table").(string), d.Get("column_family").(string), bigtable.NoGcPolicy()); err != nil { - return err - } - - d.SetId("") - - return nil -} - -func generateBigtableGCPolicy(d *schema.ResourceData) (bigtable.GCPolicy, error) { - var policies []bigtable.GCPolicy - mode := d.Get("mode").(string) - ma, aok := d.GetOk("max_age") - mv, vok := d.GetOk("max_version") - - if !aok && !vok { - return bigtable.NoGcPolicy(), nil - } - - if mode == "" && aok && vok { - return nil, fmt.Errorf("If multiple policies are set, mode can't be empty") - } - - if aok { - l, _ := ma.([]interface{}) - d, _ := l[0].(map[string]interface{})["days"].(int) - - policies = append(policies, bigtable.MaxAgePolicy(time.Duration(d)*time.Hour*24)) - } - - if vok { - l, _ := mv.([]interface{}) - n, _ := l[0].(map[string]interface{})["number"].(int) - - policies = append(policies, bigtable.MaxVersionsPolicy(n)) - } - - switch mode { - case GCPolicyModeUnion: - return bigtable.UnionPolicy(policies...), nil - case GCPolicyModeIntersection: - return bigtable.IntersectionPolicy(policies...), nil - } - - return policies[0], nil -} diff --git a/google/resource_bigtable_gc_policy_test.go b/google/resource_bigtable_gc_policy_test.go deleted file mode 100644 index 46cee1b5096..00000000000 --- a/google/resource_bigtable_gc_policy_test.go +++ /dev/null @@ -1,199 +0,0 @@ -package google - -import ( - "context" - "fmt" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" -) - -func TestAccBigtableGCPolicy_basic(t *testing.T) { - t.Parallel() - - instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - tableName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - familyName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckBigtableGCPolicyDestroy, - Steps: []resource.TestStep{ - { - Config: testAccBigtableGCPolicy(instanceName, tableName, familyName), - Check: resource.ComposeTestCheckFunc( - testAccBigtableGCPolicyExists( - "google_bigtable_gc_policy.policy"), - ), - }, - }, - }) -} - -func TestAccBigtableGCPolicy_union(t *testing.T) { - t.Parallel() - - instanceName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - tableName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - familyName := fmt.Sprintf("tf-test-%s", acctest.RandString(10)) - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckBigtableGCPolicyDestroy, - Steps: []resource.TestStep{ - { - Config: testAccBigtableGCPolicyUnion(instanceName, tableName, familyName), - Check: resource.ComposeTestCheckFunc( - testAccBigtableGCPolicyExists( - "google_bigtable_gc_policy.policy"), - ), - }, - }, - }) -} - -func testAccCheckBigtableGCPolicyDestroy(s *terraform.State) error { - var ctx = context.Background() - for _, rs := range s.RootModule().Resources { - if rs.Type != "google_bigtable_gc_policy" { - continue - } - - config := testAccProvider.Meta().(*Config) - c, err := config.bigtableClientFactory.NewAdminClient(config.Project, rs.Primary.Attributes["instance_name"]) - if err != nil { - // The instance is already gone - return nil - } - - table, err := c.TableInfo(ctx, rs.Primary.Attributes["name"]) - if err != nil { - // The table is already gone - return nil - } - - for _, i := range table.FamilyInfos { - if i.Name == rs.Primary.Attributes["column_family"] { - if i.GCPolicy != "" { - return fmt.Errorf("GC Policy still present. Found %s in %s.", i.GCPolicy, rs.Primary.Attributes["column_family"]) - } - } - } - - c.Close() - } - - return nil -} - -func testAccBigtableGCPolicyExists(n string) resource.TestCheckFunc { - var ctx = context.Background() - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No ID is set") - } - config := testAccProvider.Meta().(*Config) - c, err := config.bigtableClientFactory.NewAdminClient(config.Project, rs.Primary.Attributes["instance_name"]) - if err != nil { - return fmt.Errorf("Error starting admin client. %s", err) - } - - defer c.Close() - - table, err := c.TableInfo(ctx, rs.Primary.Attributes["table"]) - if err != nil { - return fmt.Errorf("Error retrieving table. Could not find %s in %s.", rs.Primary.Attributes["table"], rs.Primary.Attributes["instance_name"]) - } - - for _, i := range table.FamilyInfos { - if i.Name == rs.Primary.Attributes["column_family"] { - return nil - } - } - - return fmt.Errorf("Error retrieving gc policy. Could not find policy in family %s", rs.Primary.Attributes["column_family"]) - } -} - -func testAccBigtableGCPolicy(instanceName, tableName, family string) string { - return fmt.Sprintf(` -resource "google_bigtable_instance" "instance" { - name = "%s" - - cluster { - cluster_id = "%s" - zone = "us-central1-b" - } - - instance_type = "DEVELOPMENT" -} - -resource "google_bigtable_table" "table" { - name = "%s" - instance_name = "${google_bigtable_instance.instance.name}" - - column_family { - family = "%s" - } -} - -resource "google_bigtable_gc_policy" "policy" { - instance_name = "${google_bigtable_instance.instance.name}" - table = "${google_bigtable_table.table.name}" - column_family = "%s" - - max_age { - days = 3 - } -} -`, instanceName, instanceName, tableName, family, family) -} - -func testAccBigtableGCPolicyUnion(instanceName, tableName, family string) string { - return fmt.Sprintf(` -resource "google_bigtable_instance" "instance" { - name = "%s" - - cluster { - cluster_id = "%s" - zone = "us-central1-b" - } - - instance_type = "DEVELOPMENT" -} - -resource "google_bigtable_table" "table" { - name = "%s" - instance_name = "${google_bigtable_instance.instance.name}" - - column_family { - family = "%s" - } -} - -resource "google_bigtable_gc_policy" "policy" { - instance_name = "${google_bigtable_instance.instance.name}" - table = "${google_bigtable_table.table.name}" - column_family = "%s" - - mode = "UNION" - - max_age { - days = 3 - } - - max_version { - number = 10 - } -} -`, instanceName, instanceName, tableName, family, family) -} diff --git a/google/resource_compute_network_peering.go b/google/resource_compute_network_peering.go index 593b3063201..e5181881a1b 100644 --- a/google/resource_compute_network_peering.go +++ b/google/resource_compute_network_peering.go @@ -18,7 +18,9 @@ func resourceComputeNetworkPeering() *schema.Resource { Create: resourceComputeNetworkPeeringCreate, Read: resourceComputeNetworkPeeringRead, Delete: resourceComputeNetworkPeeringDelete, - + Importer: &schema.ResourceImporter{ + State: resourceComputeNetworkPeeringImporter, + }, Schema: map[string]*schema.Schema{ "name": { Type: schema.TypeString, @@ -61,6 +63,22 @@ func resourceComputeNetworkPeering() *schema.Resource { } } +func resourceComputeNetworkPeeringImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + if err := parseImportId([]string{"(?P[^/]+)/(?P[^/]+)"}, d, config); err != nil { + return nil, err + } + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + func resourceComputeNetworkPeeringCreate(d *schema.ResourceData, meta interface{}) error { config := meta.(*Config) networkFieldValue, err := ParseNetworkFieldValue(d.Get("network").(string), d, config) diff --git a/google/resource_compute_network_peering_test.go b/google/resource_compute_network_peering_test.go index 4de93ae9dbc..c3a8df33c1c 100644 --- a/google/resource_compute_network_peering_test.go +++ b/google/resource_compute_network_peering_test.go @@ -29,6 +29,11 @@ func TestAccComputeNetworkPeering_basic(t *testing.T) { testAccCheckComputeNetworkPeeringAutoCreateRoutes(true, &peering_beta), ), }, + { + ResourceName: "google_compute_network_peering.foo", + ImportState: true, + ImportStateVerify: true, + }, }, }) diff --git a/website/docs/r/bigtable_gc_policy.html.markdown b/website/docs/r/bigtable_gc_policy.html.markdown deleted file mode 100644 index 342cef57124..00000000000 --- a/website/docs/r/bigtable_gc_policy.html.markdown +++ /dev/null @@ -1,98 +0,0 @@ ---- -layout: "google" -page_title: "Google: google_bigtable_gc_policy" -sidebar_current: "docs-google-bigtable-gc-policy" -description: |- - Creates a Google Cloud Bigtable GC Policy inside a family. ---- - -# google_bigtable_gc_policy - -Creates a Google Cloud Bigtable GC Policy inside a family. For more information see -[the official documentation](https://cloud.google.com/bigtable/) and -[API](https://cloud.google.com/bigtable/docs/go/reference). - - -## Example Usage - -```hcl -resource "google_bigtable_instance" "instance" { - name = "tf-instance" - cluster_id = "tf-instance-cluster" - zone = "us-central1-b" - num_nodes = 3 - storage_type = "HDD" -} - -resource "google_bigtable_table" "table" { - name = "tf-table" - instance_name = "${google_bigtable_instance.instance.name}" - - column_family { - family = "name" - } -} - -resource "google_bigtable_gc_policy" "policy" { - instance_name = "${google_bigtable_instance.instance.name}" - table = "${google_bigtable_table.table.name}" - column_family = "name" - - max_age { - days = 7 - } -} -``` - -Multiple conditions is also supported. `UNION` when any of its sub-policies apply (OR). `INTERSECTION` when all its sub-policies apply (AND) -``` -resource "google_bigtable_gc_policy" "policy" { - instance_name = "${google_bigtable_instance.instance.name}" - table = "${google_bigtable_table.table.name}" - column_family = "name" - - mode = "UNION" - - max_age { - days = 7 - } - - max_version { - number = 10 - } -} -``` - -## Argument Reference - -The following arguments are supported: - -* `name` - (Required) The name of the table. - -* `instance_name` - (Required) The name of the Bigtable instance. - -* `family` - (Required) The name of the column family. - -* `project` - (Optional) The ID of the project in which the resource belongs. If it is not provided, the provider project is used. - -* `mode` - (Optional) If multiple policies are set, you should choose between `UNION` OR `INTERSECTION`. - -* `max_age` - (Optional) GC policy that applies to all cells older than the given age. - -* `max_version` - (Optional) GC policy that applies to all versions of a cell except for the most recent. - ------ - -`max_age` supports the following arguments: - -* `days` - (Required) Number of days before applying GC policy. - ------ - -`max_version` supports the following arguments: - -* `number` - (Required) Number of version before applying the GC policy. - -## Attributes Reference - -Only the arguments listed above are exposed as attributes. diff --git a/website/docs/r/compute_network_peering.html.markdown b/website/docs/r/compute_network_peering.html.markdown index e649c0ee2ac..5d5d7cf3046 100644 --- a/website/docs/r/compute_network_peering.html.markdown +++ b/website/docs/r/compute_network_peering.html.markdown @@ -64,3 +64,10 @@ exported: * `state` - State for the peering. * `state_details` - Details about the current state of the peering. + +## Import +VPC Peering Networks can be imported using the name of the network the peering exists in and the name of the peering network + +``` +$ terraform import google_compute_network_peering.peering_network network-name/peering-network-name +``` \ No newline at end of file