Skip to content

Commit

Permalink
Fix spanner database iam binding import
Browse files Browse the repository at this point in the history
  • Loading branch information
emilymye authored and modular-magician committed Dec 7, 2018
1 parent a8eca9f commit 6ac9474
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 46 deletions.
15 changes: 3 additions & 12 deletions google-beta/iam_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -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) {
Expand Down
61 changes: 27 additions & 34 deletions google-beta/resource_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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{
Expand Down Expand Up @@ -137,34 +135,41 @@ 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<project>[^/]+)/instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)",
"instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<name>[^/]+)",
"(?P<instance>[^/]+)/(?P<name>[^/]+)",
}, 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<project>[^/]+)/instances/(?P<instance>[^/]+)/databases/(?P<%s>[^/]+)", databaseField),
fmt.Sprintf("instances/(?P<instance>[^/]+)/databases/(?P<%s>[^/]+)", databaseField),
fmt.Sprintf("(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<%s>[^/]+)", databaseField),
fmt.Sprintf("(?P<instance>[^/]+)/(?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) {
project, err := getProject(d, config)
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{
Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit 6ac9474

Please sign in to comment.