Skip to content

Commit

Permalink
Initial autogeneration of Spanner instance in Terraform.
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
nat-henderson authored and modular-magician committed Jan 18, 2019
1 parent 30fe927 commit 9cb665d
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 312 deletions.
34 changes: 34 additions & 0 deletions google/iam_spanner_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package google

import (
"fmt"
"regexp"
"strings"

"github.com/hashicorp/errwrap"
"github.com/hashicorp/terraform/helper/schema"
Expand Down Expand Up @@ -110,3 +112,35 @@ func (u *SpannerInstanceIamUpdater) GetMutexKey() string {
func (u *SpannerInstanceIamUpdater) DescribeResource() string {
return fmt.Sprintf("Spanner Instance: %s/%s", u.project, u.instance)
}

type spannerInstanceId struct {
Project string
Instance string
}

func (s spannerInstanceId) terraformId() string {
return fmt.Sprintf("%s/%s", s.Project, s.Instance)
}

func (s spannerInstanceId) parentProjectUri() string {
return fmt.Sprintf("projects/%s", s.Project)
}

func (s spannerInstanceId) instanceUri() string {
return fmt.Sprintf("%s/instances/%s", s.parentProjectUri(), s.Instance)
}

func (s spannerInstanceId) instanceConfigUri(c string) string {
return fmt.Sprintf("%s/instanceConfigs/%s", s.parentProjectUri(), c)
}

func extractSpannerInstanceId(id string) (*spannerInstanceId, error) {
if !regexp.MustCompile("^" + ProjectRegex + "/[a-z0-9-]+$").Match([]byte(id)) {
return nil, fmt.Errorf("Invalid spanner id format, expecting {projectId}/{instanceId}")
}
parts := strings.Split(id, "/")
return &spannerInstanceId{
Project: parts[0],
Instance: parts[1],
}, nil
}
1 change: 1 addition & 0 deletions google/provider_spanner_gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@ package google
import "github.com/hashicorp/terraform/helper/schema"

var GeneratedSpannerResourcesMap = map[string]*schema.Resource{
"google_spanner_instance": resourceSpannerInstance(),
"google_spanner_database": resourceSpannerDatabase(),
}
2 changes: 0 additions & 2 deletions google/resource_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,14 +239,12 @@ func resourceSpannerDatabaseEncoder(d *schema.ResourceData, meta interface{}, ob
func resourceSpannerDatabaseDecoder(d *schema.ResourceData, meta interface{}, res map[string]interface{}) (map[string]interface{}, error) {
config := meta.(*Config)
d.SetId(res["name"].(string))
log.Printf("[DEBUG] name = %s", res["name"])
if err := parseImportId([]string{"projects/(?P<project>[^/]+)/instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)"}, d, config); err != nil {
return nil, err
}
res["project"] = d.Get("project").(string)
res["instance"] = d.Get("instance").(string)
res["name"] = d.Get("name").(string)
log.Printf("[DEBUG] result %#v", res)
id, err := replaceVars(d, config, "{{instance}}/{{name}}")
if err != nil {
return nil, err
Expand Down
Loading

0 comments on commit 9cb665d

Please sign in to comment.