From 6ac947454ce07a4fdb61104b43ad0f1716442faf Mon Sep 17 00:00:00 2001 From: emily Date: Fri, 7 Dec 2018 18:58:36 +0000 Subject: [PATCH] Fix spanner database iam binding import --- google-beta/iam_spanner_database.go | 15 ++---- google-beta/resource_spanner_database.go | 61 +++++++++++------------- 2 files changed, 30 insertions(+), 46 deletions(-) diff --git a/google-beta/iam_spanner_database.go b/google-beta/iam_spanner_database.go index 51994c16eb..be9c7f86be 100644 --- a/google-beta/iam_spanner_database.go +++ b/google-beta/iam_spanner_database.go @@ -6,7 +6,7 @@ import ( "github.com/hashicorp/errwrap" "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/cloudresourcemanager/v1" - spanner "google.golang.org/api/spanner/v1" + "google.golang.org/api/spanner/v1" ) var IamSpannerDatabaseSchema = map[string]*schema.Schema{ @@ -51,17 +51,8 @@ func NewSpannerDatabaseIamUpdater(d *schema.ResourceData, config *Config) (Resou } func SpannerDatabaseIdParseFunc(d *schema.ResourceData, config *Config) error { - id, err := extractSpannerDatabaseId(d.Id()) - if err != nil { - return err - } - d.Set("instance", id.Instance) - d.Set("project", id.Project) - d.Set("database", id.Database) - - // Explicitly set the id so imported resources have the same ID format as non-imported ones. - d.SetId(id.terraformId()) - return nil + _, err := resourceSpannerDatabaseImport("database")(d, config) + return err } func (u *SpannerDatabaseIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) { diff --git a/google-beta/resource_spanner_database.go b/google-beta/resource_spanner_database.go index 0579d40732..d1130c4c32 100644 --- a/google-beta/resource_spanner_database.go +++ b/google-beta/resource_spanner_database.go @@ -2,12 +2,10 @@ package google import ( "fmt" + "github.com/hashicorp/terraform/helper/schema" "log" "net/http" "regexp" - "strings" - - "github.com/hashicorp/terraform/helper/schema" "google.golang.org/api/googleapi" "google.golang.org/api/spanner/v1" @@ -23,7 +21,7 @@ func resourceSpannerDatabase() *schema.Resource { Read: resourceSpannerDatabaseRead, Delete: resourceSpannerDatabaseDelete, Importer: &schema.ResourceImporter{ - State: resourceSpannerDatabaseImport, + State: resourceSpannerDatabaseImport("name"), }, Schema: map[string]*schema.Schema{ @@ -137,26 +135,28 @@ func resourceSpannerDatabaseDelete(d *schema.ResourceData, meta interface{}) err return nil } -func resourceSpannerDatabaseImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - config := meta.(*Config) - err := parseImportId([]string{ - "projects/(?P[^/]+)/instances/(?P[^/]+)/databases/(?P[^/]+)", - "instances/(?P[^/]+)/databases/(?P[^/]+)", - "(?P[^/]+)/(?P[^/]+)/(?P[^/]+)", - "(?P[^/]+)/(?P[^/]+)", - }, d, config) - if err != nil { - return nil, fmt.Errorf("Error constructing id: %s", err) - } +func resourceSpannerDatabaseImport(databaseField string) schema.StateFunc { + return func(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { + config := meta.(*Config) + err := parseImportId([]string{ + fmt.Sprintf("projects/(?P[^/]+)/instances/(?P[^/]+)/databases/(?P<%s>[^/]+)", databaseField), + fmt.Sprintf("instances/(?P[^/]+)/databases/(?P<%s>[^/]+)", databaseField), + fmt.Sprintf("(?P[^/]+)/(?P[^/]+)/(?P<%s>[^/]+)", databaseField), + fmt.Sprintf("(?P[^/]+)/(?P<%s>[^/]+)", databaseField), + }, d, config) + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } - id, err := buildSpannerDatabaseId(d, config) - if err != nil { - return nil, fmt.Errorf("Error constructing id: %s", err) - } + id, err := buildSpannerDatabaseId(d, config) + if err != nil { + return nil, fmt.Errorf("Error constructing id: %s", err) + } - d.SetId(id.terraformId()) + d.SetId(id.terraformId()) - return []*schema.ResourceData{d}, nil + return []*schema.ResourceData{d}, nil + } } func buildSpannerDatabaseId(d *schema.ResourceData, config *Config) (*spannerDatabaseId, error) { @@ -164,7 +164,12 @@ func buildSpannerDatabaseId(d *schema.ResourceData, config *Config) (*spannerDat if err != nil { return nil, err } - dbName := d.Get("name").(string) + database, ok := d.GetOk("name") + if !ok { + database = d.Get("database") + } + + dbName := database.(string) instanceName := d.Get("instance").(string) return &spannerDatabaseId{ @@ -196,18 +201,6 @@ func (s spannerDatabaseId) databaseUri() string { return fmt.Sprintf("%s/databases/%s", s.parentInstanceUri(), s.Database) } -func extractSpannerDatabaseId(id string) (*spannerDatabaseId, error) { - if !regexp.MustCompile(fmt.Sprintf("^%s/[a-z0-9-]+/%s$", ProjectRegex, spannerDatabaseNameFormat)).Match([]byte(id)) { - return nil, fmt.Errorf("Invalid spanner id format, expecting {projectId}/{instanceId}/{databaseId}") - } - parts := strings.Split(id, "/") - return &spannerDatabaseId{ - Project: parts[0], - Instance: parts[1], - Database: parts[2], - }, nil -} - func validateResourceSpannerDatabaseName(v interface{}, k string) (ws []string, errors []error) { value := v.(string)