diff --git a/google/config.go b/google/config.go index 6aaa9aaaafa..3ddf7d39f9f 100644 --- a/google/config.go +++ b/google/config.go @@ -71,7 +71,7 @@ type Config struct { clientDataflow *dataflow.Service clientDns *dns.Service clientDnsBeta *dnsBeta.Service - clientFile *file.Service + clientFilestore *file.Service clientKms *cloudkms.Service clientLogging *cloudlogging.Service clientPubsub *pubsub.Service @@ -350,11 +350,11 @@ func (c *Config) loadAndValidate() error { } c.clientDataproc.UserAgent = userAgent - c.clientFile, err = file.New(client) + c.clientFilestore, err = file.New(client) if err != nil { return err } - c.clientFile.UserAgent = userAgent + c.clientFilestore.UserAgent = userAgent log.Printf("[INFO] Instantiating Google Cloud IoT Core Client...") c.clientCloudIoT, err = cloudiot.New(client) diff --git a/google/filestore_operation.go b/google/filestore_operation.go new file mode 100644 index 00000000000..05ce0592d19 --- /dev/null +++ b/google/filestore_operation.go @@ -0,0 +1,71 @@ +package google + +import ( + "fmt" + "log" + "time" + + "github.com/hashicorp/terraform/helper/resource" + file "google.golang.org/api/file/v1beta1" +) + +type FilestoreOperationWaiter struct { + Service *file.ProjectsLocationsService + Op *file.Operation +} + +func (w *FilestoreOperationWaiter) RefreshFunc() resource.StateRefreshFunc { + return func() (interface{}, string, error) { + op, err := w.Service.Operations.Get(w.Op.Name).Do() + + if err != nil { + return nil, "", err + } + + log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name) + + return op, fmt.Sprint(op.Done), nil + } +} + +func (w *FilestoreOperationWaiter) Conf() *resource.StateChangeConf { + return &resource.StateChangeConf{ + Pending: []string{"false"}, + Target: []string{"true"}, + Refresh: w.RefreshFunc(), + } +} + +func filestoreOperationWait(service *file.Service, op *file.Operation, project, activity string) error { + return filestoreOperationWaitTime(service, op, project, activity, 4) +} + +func filestoreOperationWaitTime(service *file.Service, op *file.Operation, project, activity string, timeoutMin int) error { + if op.Done { + if op.Error != nil { + return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message) + } + return nil + } + + w := &FilestoreOperationWaiter{ + Service: service.Projects.Locations, + Op: op, + } + + state := w.Conf() + state.Delay = 10 * time.Second + state.Timeout = time.Duration(timeoutMin) * time.Minute + state.MinTimeout = 2 * time.Second + opRaw, err := state.WaitForState() + if err != nil { + return fmt.Errorf("Error waiting for %s: %s", activity, err) + } + + op = opRaw.(*file.Operation) + if op.Error != nil { + return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message) + } + + return nil +} diff --git a/google/provider.go b/google/provider.go index 20be30b2252..333d4c7f607 100644 --- a/google/provider.go +++ b/google/provider.go @@ -103,6 +103,7 @@ func Provider() terraform.ResourceProvider { GeneratedBinaryAuthorizationResourcesMap, GeneratedComputeResourcesMap, GeneratedContainerAnalysisResourcesMap, + GeneratedFilestoreResourcesMap, GeneratedRedisResourcesMap, GeneratedResourceManagerResourcesMap, map[string]*schema.Resource{ diff --git a/google/provider_filestore_gen.go b/google/provider_filestore_gen.go new file mode 100644 index 00000000000..7bf114b2fff --- /dev/null +++ b/google/provider_filestore_gen.go @@ -0,0 +1,21 @@ +// ---------------------------------------------------------------------------- +// +// *** 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 "github.com/hashicorp/terraform/helper/schema" + +var GeneratedFilestoreResourcesMap = map[string]*schema.Resource{ + "google_filestore_instance": resourceFilestoreInstance(), +} diff --git a/google/resource_filestore_instance.go b/google/resource_filestore_instance.go new file mode 100644 index 00000000000..6489d452bf7 --- /dev/null +++ b/google/resource_filestore_instance.go @@ -0,0 +1,603 @@ +// ---------------------------------------------------------------------------- +// +// *** 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" + "log" + "reflect" + "strconv" + "strings" + "time" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/hashicorp/terraform/helper/validation" + filestore "google.golang.org/api/file/v1beta1" +) + +func resourceFilestoreInstance() *schema.Resource { + return &schema.Resource{ + Create: resourceFilestoreInstanceCreate, + Read: resourceFilestoreInstanceRead, + Update: resourceFilestoreInstanceUpdate, + Delete: resourceFilestoreInstanceDelete, + + Importer: &schema.ResourceImporter{ + State: resourceFilestoreInstanceImport, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(300 * time.Second), + Update: schema.DefaultTimeout(240 * time.Second), + Delete: schema.DefaultTimeout(240 * time.Second), + }, + + Schema: map[string]*schema.Schema{ + "file_shares": { + Type: schema.TypeList, + Required: true, + MaxItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "capacity_gb": { + Type: schema.TypeInt, + Required: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + }, + }, + }, + "name": { + Type: schema.TypeString, + Required: true, + }, + "networks": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + MinItems: 1, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "modes": { + Type: schema.TypeList, + Required: true, + ForceNew: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + "network": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "reserved_ip_range": { + Type: schema.TypeString, + Computed: true, + Optional: true, + }, + "ip_addresses": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{ + Type: schema.TypeString, + }, + }, + }, + }, + }, + "tier": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{"TIER_UNSPECIFIED", "STANDARD", "PREMIUM"}, false), + }, + "zone": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "labels": { + Type: schema.TypeMap, + Optional: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "create_time": { + Type: schema.TypeString, + Computed: true, + }, + "etag": { + Type: schema.TypeString, + Computed: true, + }, + "project": { + Type: schema.TypeString, + Optional: true, + Computed: true, + ForceNew: true, + }, + }, + } +} + +func resourceFilestoreInstanceCreate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + obj := make(map[string]interface{}) + descriptionProp, err := expandFilestoreInstanceDescription(d.Get("description"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(descriptionProp)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + tierProp, err := expandFilestoreInstanceTier(d.Get("tier"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("tier"); !isEmptyValue(reflect.ValueOf(tierProp)) && (ok || !reflect.DeepEqual(v, tierProp)) { + obj["tier"] = tierProp + } + labelsProp, err := expandFilestoreInstanceLabels(d.Get("labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(labelsProp)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + fileSharesProp, err := expandFilestoreInstanceFileShares(d.Get("file_shares"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("file_shares"); !isEmptyValue(reflect.ValueOf(fileSharesProp)) && (ok || !reflect.DeepEqual(v, fileSharesProp)) { + obj["fileShares"] = fileSharesProp + } + networksProp, err := expandFilestoreInstanceNetworks(d.Get("networks"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("networks"); !isEmptyValue(reflect.ValueOf(networksProp)) && (ok || !reflect.DeepEqual(v, networksProp)) { + obj["networks"] = networksProp + } + + url, err := replaceVars(d, config, "https://file.googleapis.com/v1beta1/projects/{{project}}/locations/{{zone}}/instances?instanceId={{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Creating new Instance: %#v", obj) + res, err := sendRequest(config, "POST", url, obj) + if err != nil { + return fmt.Errorf("Error creating Instance: %s", err) + } + + // Store the ID now + id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{name}}") + if err != nil { + return fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + project, err := getProject(d, config) + if err != nil { + return err + } + op := &filestore.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + waitErr := filestoreOperationWaitTime( + config.clientFilestore, op, project, "Creating Instance", + int(d.Timeout(schema.TimeoutCreate).Minutes())) + + if waitErr != nil { + // The resource didn't actually create + d.SetId("") + return fmt.Errorf("Error waiting to create Instance: %s", waitErr) + } + + log.Printf("[DEBUG] Finished creating Instance %q: %#v", d.Id(), res) + + return resourceFilestoreInstanceRead(d, meta) +} + +func resourceFilestoreInstanceRead(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + url, err := replaceVars(d, config, "https://file.googleapis.com/v1beta1/projects/{{project}}/locations/{{zone}}/instances/{{name}}") + if err != nil { + return err + } + + res, err := sendRequest(config, "GET", url, nil) + if err != nil { + return handleNotFoundError(err, d, fmt.Sprintf("FilestoreInstance %q", d.Id())) + } + + if err := d.Set("name", flattenFilestoreInstanceName(res["name"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("description", flattenFilestoreInstanceDescription(res["description"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("create_time", flattenFilestoreInstanceCreateTime(res["createTime"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("tier", flattenFilestoreInstanceTier(res["tier"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("labels", flattenFilestoreInstanceLabels(res["labels"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("file_shares", flattenFilestoreInstanceFileShares(res["fileShares"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("networks", flattenFilestoreInstanceNetworks(res["networks"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + if err := d.Set("etag", flattenFilestoreInstanceEtag(res["etag"])); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + project, err := getProject(d, config) + if err != nil { + return err + } + if err := d.Set("project", project); err != nil { + return fmt.Errorf("Error reading Instance: %s", err) + } + + return nil +} + +func resourceFilestoreInstanceUpdate(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + obj := make(map[string]interface{}) + descriptionProp, err := expandFilestoreInstanceDescription(d.Get("description"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("description"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, descriptionProp)) { + obj["description"] = descriptionProp + } + tierProp, err := expandFilestoreInstanceTier(d.Get("tier"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("tier"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, tierProp)) { + obj["tier"] = tierProp + } + labelsProp, err := expandFilestoreInstanceLabels(d.Get("labels"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("labels"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, labelsProp)) { + obj["labels"] = labelsProp + } + fileSharesProp, err := expandFilestoreInstanceFileShares(d.Get("file_shares"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("file_shares"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, fileSharesProp)) { + obj["fileShares"] = fileSharesProp + } + networksProp, err := expandFilestoreInstanceNetworks(d.Get("networks"), d, config) + if err != nil { + return err + } else if v, ok := d.GetOkExists("networks"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, networksProp)) { + obj["networks"] = networksProp + } + + url, err := replaceVars(d, config, "https://file.googleapis.com/v1beta1/projects/{{project}}/locations/{{zone}}/instances/{{name}}") + if err != nil { + return err + } + + log.Printf("[DEBUG] Updating Instance %q: %#v", d.Id(), obj) + updateMask := []string{} + + if d.HasChange("description") { + updateMask = append(updateMask, "description") + } + + if d.HasChange("tier") { + updateMask = append(updateMask, "tier") + } + + if d.HasChange("labels") { + updateMask = append(updateMask, "labels") + } + + if d.HasChange("file_shares") { + updateMask = append(updateMask, "fileShares") + } + + if d.HasChange("networks") { + updateMask = append(updateMask, "networks") + } + // updateMask is a URL parameter but not present in the schema, so replaceVars + // won't set it + url, err = addQueryParams(url, map[string]string{"updateMask": strings.Join(updateMask, ",")}) + if err != nil { + return err + } + res, err := sendRequest(config, "PATCH", url, obj) + + if err != nil { + return fmt.Errorf("Error updating Instance %q: %s", d.Id(), err) + } + + project, err := getProject(d, config) + if err != nil { + return err + } + op := &filestore.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = filestoreOperationWaitTime( + config.clientFilestore, op, project, "Updating Instance", + int(d.Timeout(schema.TimeoutUpdate).Minutes())) + + if err != nil { + return err + } + + return resourceFilestoreInstanceRead(d, meta) +} + +func resourceFilestoreInstanceDelete(d *schema.ResourceData, meta interface{}) error { + config := meta.(*Config) + + url, err := replaceVars(d, config, "https://file.googleapis.com/v1beta1/projects/{{project}}/locations/{{zone}}/instances/{{name}}") + if err != nil { + return err + } + + var obj map[string]interface{} + log.Printf("[DEBUG] Deleting Instance %q", d.Id()) + res, err := sendRequest(config, "DELETE", url, obj) + if err != nil { + return handleNotFoundError(err, d, "Instance") + } + + project, err := getProject(d, config) + if err != nil { + return err + } + op := &filestore.Operation{} + err = Convert(res, op) + if err != nil { + return err + } + + err = filestoreOperationWaitTime( + config.clientFilestore, op, project, "Deleting Instance", + int(d.Timeout(schema.TimeoutDelete).Minutes())) + + if err != nil { + return err + } + + log.Printf("[DEBUG] Finished deleting Instance %q: %#v", d.Id(), res) + return nil +} + +func resourceFilestoreInstanceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + parseImportId([]string{"projects/(?P[^/]+)/locations/(?P[^/]+)/instances/(?P[^/]+)", "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", "(?P[^/]+)"}, d, config) + + // Replace import id for the resource id + id, err := replaceVars(d, config, "{{project}}/{{zone}}/{{name}}") + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } + d.SetId(id) + + return []*schema.ResourceData{d}, nil +} + +func flattenFilestoreInstanceName(v interface{}) interface{} { + if v == nil { + return v + } + return NameFromSelfLinkStateFunc(v) +} + +func flattenFilestoreInstanceDescription(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceCreateTime(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceTier(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceLabels(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceFileShares(v interface{}) interface{} { + if v == nil { + return v + } + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + transformed = append(transformed, map[string]interface{}{ + "name": flattenFilestoreInstanceFileSharesName(original["name"]), + "capacity_gb": flattenFilestoreInstanceFileSharesCapacityGb(original["capacityGb"]), + }) + } + return transformed +} +func flattenFilestoreInstanceFileSharesName(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceFileSharesCapacityGb(v interface{}) interface{} { + // Handles the string fixed64 format + if strVal, ok := v.(string); ok { + if intVal, err := strconv.ParseInt(strVal, 10, 64); err == nil { + return intVal + } // let terraform core handle it if we can't convert the string to an int. + } + return v +} + +func flattenFilestoreInstanceNetworks(v interface{}) interface{} { + if v == nil { + return v + } + l := v.([]interface{}) + transformed := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + transformed = append(transformed, map[string]interface{}{ + "network": flattenFilestoreInstanceNetworksNetwork(original["network"]), + "modes": flattenFilestoreInstanceNetworksModes(original["modes"]), + "reserved_ip_range": flattenFilestoreInstanceNetworksReservedIpRange(original["reservedIpRange"]), + "ip_addresses": flattenFilestoreInstanceNetworksIpAddresses(original["ipAddresses"]), + }) + } + return transformed +} +func flattenFilestoreInstanceNetworksNetwork(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceNetworksModes(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceNetworksReservedIpRange(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceNetworksIpAddresses(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceEtag(v interface{}) interface{} { + return v +} + +func flattenFilestoreInstanceZone(v interface{}) interface{} { + return v +} + +func expandFilestoreInstanceDescription(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceTier(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceLabels(v interface{}, d *schema.ResourceData, config *Config) (map[string]string, error) { + if v == nil { + return map[string]string{}, nil + } + m := make(map[string]string) + for k, val := range v.(map[string]interface{}) { + m[k] = val.(string) + } + return m, nil +} + +func expandFilestoreInstanceFileShares(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + l := v.([]interface{}) + req := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedName, err := expandFilestoreInstanceFileSharesName(original["name"], d, config) + if err != nil { + return nil, err + } + transformed["name"] = transformedName + transformedCapacityGb, err := expandFilestoreInstanceFileSharesCapacityGb(original["capacity_gb"], d, config) + if err != nil { + return nil, err + } + transformed["capacityGb"] = transformedCapacityGb + req = append(req, transformed) + } + return req, nil +} + +func expandFilestoreInstanceFileSharesName(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceFileSharesCapacityGb(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceNetworks(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + l := v.([]interface{}) + req := make([]interface{}, 0, len(l)) + for _, raw := range l { + original := raw.(map[string]interface{}) + transformed := make(map[string]interface{}) + + transformedNetwork, err := expandFilestoreInstanceNetworksNetwork(original["network"], d, config) + if err != nil { + return nil, err + } + transformed["network"] = transformedNetwork + transformedModes, err := expandFilestoreInstanceNetworksModes(original["modes"], d, config) + if err != nil { + return nil, err + } + transformed["modes"] = transformedModes + transformedReservedIpRange, err := expandFilestoreInstanceNetworksReservedIpRange(original["reserved_ip_range"], d, config) + if err != nil { + return nil, err + } + transformed["reservedIpRange"] = transformedReservedIpRange + transformedIpAddresses, err := expandFilestoreInstanceNetworksIpAddresses(original["ip_addresses"], d, config) + if err != nil { + return nil, err + } + transformed["ipAddresses"] = transformedIpAddresses + req = append(req, transformed) + } + return req, nil +} + +func expandFilestoreInstanceNetworksNetwork(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceNetworksModes(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceNetworksReservedIpRange(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} + +func expandFilestoreInstanceNetworksIpAddresses(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { + return v, nil +} diff --git a/google/resource_filestore_instance_test.go b/google/resource_filestore_instance_test.go new file mode 100644 index 00000000000..266baacdebd --- /dev/null +++ b/google/resource_filestore_instance_test.go @@ -0,0 +1,149 @@ +package google + +import ( + "fmt" + "strings" + "testing" + + "github.com/hashicorp/terraform/helper/acctest" + "github.com/hashicorp/terraform/helper/resource" + "github.com/hashicorp/terraform/terraform" +) + +func TestAccFilestoreInstance_basic(t *testing.T) { + t.Parallel() + + name := acctest.RandomWithPrefix("tf-test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckFilestoreInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccFilestoreInstance_basic(name), + }, + resource.TestStep{ + ResourceName: "google_filestore_instance.instance", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccFilestoreInstance_update(t *testing.T) { + t.Parallel() + + name := acctest.RandomWithPrefix("tf-test") + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckFilestoreInstanceDestroy, + Steps: []resource.TestStep{ + resource.TestStep{ + Config: testAccFilestoreInstance_update(name), + }, + resource.TestStep{ + ResourceName: "google_filestore_instance.instance", + ImportState: true, + ImportStateVerify: true, + }, + resource.TestStep{ + Config: testAccFilestoreInstance_update2(name), + }, + resource.TestStep{ + ResourceName: "google_filestore_instance.instance", + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckFilestoreInstanceDestroy(s *terraform.State) error { + config := testAccProvider.Meta().(*Config) + + for _, rs := range s.RootModule().Resources { + if rs.Type != "google_filestore_instance" { + continue + } + + redisIdParts := strings.Split(rs.Primary.ID, "/") + if len(redisIdParts) != 3 { + return fmt.Errorf("Unexpected resource ID %s, expected {project}/{region}/{name}", rs.Primary.ID) + } + + project, region, inst := redisIdParts[0], redisIdParts[1], redisIdParts[2] + + name := fmt.Sprintf("projects/%s/locations/%s/instances/%s", project, region, inst) + _, err := config.clientFilestore.Projects.Locations.Get(name).Do() + if err == nil { + return fmt.Errorf("Filestore instance still exists") + } + } + + return nil +} + +func testAccFilestoreInstance_basic(name string) string { + return fmt.Sprintf(` +resource "google_filestore_instance" "instance" { + name = "tf-instance-%s" + zone = "us-central1-b" + file_shares { + capacity_gb = 2560 + name = "share" + } + networks { + network = "default" + modes = ["MODE_IPV4"] + } + labels { + foo = "bar" + } + tier = "PREMIUM" +} +`, name) +} + +func testAccFilestoreInstance_update(name string) string { + return fmt.Sprintf(` +resource "google_filestore_instance" "instance" { + name = "tf-instance-%s" + zone = "us-central1-b" + file_shares { + capacity_gb = 2660 + name = "share" + } + networks { + network = "default" + modes = ["MODE_IPV4"] + } + labels { + baz = "qux" + } + tier = "PREMIUM" + description = "An instance created during testing." +} +`, name) +} + +func testAccFilestoreInstance_update2(name string) string { + return fmt.Sprintf(` +resource "google_filestore_instance" "instance" { + name = "tf-instance-%s" + zone = "us-central1-b" + file_shares { + capacity_gb = 2760 + name = "share" + } + networks { + network = "default" + modes = ["MODE_IPV4"] + } + tier = "PREMIUM" + description = "A modified instance created during testing." +}`, name) +} diff --git a/google/resource_redis_instance.go b/google/resource_redis_instance.go index 885059d96af..051bd4587ed 100644 --- a/google/resource_redis_instance.go +++ b/google/resource_redis_instance.go @@ -208,17 +208,6 @@ func resourceRedisInstanceCreate(d *schema.ResourceData, meta interface{}) error } else if v, ok := d.GetOkExists("tier"); !isEmptyValue(reflect.ValueOf(tierProp)) && (ok || !reflect.DeepEqual(v, tierProp)) { obj["tier"] = tierProp } - regionProp, err := expandRedisInstanceRegion(d.Get("region"), d, config) - if err != nil { - return err - } else if v, ok := d.GetOkExists("region"); !isEmptyValue(reflect.ValueOf(regionProp)) && (ok || !reflect.DeepEqual(v, regionProp)) { - obj["region"] = regionProp - } - - obj, err = resourceRedisInstanceEncoder(d, meta, obj) - if err != nil { - return err - } url, err := replaceVars(d, config, "https://redis.googleapis.com/v1beta1/projects/{{project}}/locations/{{region}}/instances?instanceId={{name}}") if err != nil { @@ -276,11 +265,6 @@ func resourceRedisInstanceRead(d *schema.ResourceData, meta interface{}) error { return handleNotFoundError(err, d, fmt.Sprintf("RedisInstance %q", d.Id())) } - res, err = resourceRedisInstanceDecoder(d, meta, res) - if err != nil { - return err - } - if err := d.Set("alternative_location_id", flattenRedisInstanceAlternativeLocationId(res["alternativeLocationId"])); err != nil { return fmt.Errorf("Error reading Instance: %s", err) } @@ -410,14 +394,6 @@ func resourceRedisInstanceUpdate(d *schema.ResourceData, meta interface{}) error } else if v, ok := d.GetOkExists("tier"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, tierProp)) { obj["tier"] = tierProp } - regionProp, err := expandRedisInstanceRegion(d.Get("region"), d, config) - if err != nil { - return err - } else if v, ok := d.GetOkExists("region"); !isEmptyValue(reflect.ValueOf(v)) && (ok || !reflect.DeepEqual(v, regionProp)) { - obj["region"] = regionProp - } - - obj, err = resourceRedisInstanceEncoder(d, meta, obj) url, err := replaceVars(d, config, "https://redis.googleapis.com/v1beta1/projects/{{project}}/locations/{{region}}/instances/{{name}}") if err != nil { @@ -672,22 +648,3 @@ func expandRedisInstanceReservedIpRange(v interface{}, d *schema.ResourceData, c func expandRedisInstanceTier(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { return v, nil } - -func expandRedisInstanceRegion(v interface{}, d *schema.ResourceData, config *Config) (interface{}, error) { - return v, nil -} - -func resourceRedisInstanceEncoder(d *schema.ResourceData, meta interface{}, obj map[string]interface{}) (map[string]interface{}, error) { - delete(obj, "region") - return obj, nil -} - -func resourceRedisInstanceDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) { - config := meta.(*Config) - region, err := getRegion(d, config) - if err != nil { - return nil, err - } - res["region"] = region - return res, nil -} diff --git a/website/docs/r/filestore_instance.html.markdown b/website/docs/r/filestore_instance.html.markdown new file mode 100644 index 00000000000..527c325c6eb --- /dev/null +++ b/website/docs/r/filestore_instance.html.markdown @@ -0,0 +1,155 @@ +--- +# ---------------------------------------------------------------------------- +# +# *** 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. +# +# ---------------------------------------------------------------------------- +layout: "google" +page_title: "Google: google_filestore_instance" +sidebar_current: "docs-google-filestore-instance" +description: |- + A Google Cloud Filestore instance. +--- + +# google\_filestore\_instance + +A Google Cloud Filestore instance. + +To get more information about Instance, see: + +* [API documentation](https://cloud.google.com/filestore/docs/reference/rest/v1beta1/projects.locations.instances/create) +* How-to Guides + * [Official Documentation](https://cloud.google.com/filestore/docs/creating-instances) + * [Use with Kubernetes](https://cloud.google.com/filestore/docs/accessing-fileshares) + * [Copying Data In/Out](https://cloud.google.com/filestore/docs/copying-data) + +## Example Usage + +### Basic Usage +```hcl + resource "google_file_instance" "instance" { + name = "test-instance" + zone = "us-central1-b" + file_shares { + capacity_gb = 2660 + name = "share1" + } + networks { + network = "default" + modes = ["MODE_IPV4"] + } + tier = "PREMIUM" + } +``` + +## Argument Reference + +The following arguments are supported: + + +* `name` - + (Required) + The resource name of the instance. + +* `tier` - + (Required) + The service tier of the instance. + +* `file_shares` - + (Required) + File system shares on the instance. For this version, only a + single file share is supported. Structure is documented below. + +* `networks` - + (Required) + VPC networks to which the instance is connected. For this version, + only a single network is supported. Structure is documented below. + +* `zone` - + (Required) + The name of the Filestore zone of the instance. + + +The `file_shares` block supports: + +* `name` - + (Required) + The name of the fileshare (16 characters or less) + +* `capacity_gb` - + (Required) + File share capacity in GB. + +The `networks` block supports: + +* `network` - + (Required) + The name of the GCE VPC network to which the + instance is connected. + +* `modes` - + (Required) + IP versions for which the instance has + IP addresses assigned. + +* `reserved_ip_range` - + (Optional) + A /29 CIDR block that identifies the range of IP + addresses reserved for this instance. + +* `ip_addresses` - + A list of IPv4 or IPv6 addresses. + +- - - + + +* `description` - + (Optional) + A description of the instance. + +* `labels` - + (Optional) + Resource labels to represent user-provided metadata. +* `project` - (Optional) The ID of the project in which the resource belongs. + If it is not provided, the provider project is used. + + +## Attributes Reference + +In addition to the arguments listed above, the following computed attributes are exported: + + +* `create_time` - + Creation timestamp in RFC3339 text format. + +* `etag` - + Server-specified ETag for the instance resource to prevent + simultaneous updates from overwriting each other. + + +## Timeouts + +This resource provides the following +[Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - Default is 5 minutes. +- `update` - Default is 4 minutes. +- `delete` - Default is 4 minutes. + +## Import + +Instance can be imported using any of these accepted formats: + +``` +$ terraform import google_filestore_instance.default projects/{{project}}/locations/{{zone}}/instances/{{name}} +$ terraform import google_filestore_instance.default {{project}}/{{zone}}/{{name}} +$ terraform import google_filestore_instance.default {{name}} +``` diff --git a/website/google.erb b/website/google.erb index f92dbef93ae..d5c89174be1 100644 --- a/website/google.erb +++ b/website/google.erb @@ -534,6 +534,15 @@ + > + Google Filestore Resources + + + > Google PubSub Resources