Skip to content

Commit

Permalink
Initial autogeneration of Spanner instance in Terraform. (#2892)
Browse files Browse the repository at this point in the history
Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and nat-henderson committed Jan 22, 2019
1 parent c66e686 commit 738bdb9
Show file tree
Hide file tree
Showing 10 changed files with 534 additions and 356 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
}
3 changes: 1 addition & 2 deletions google/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
GeneratedRedisResourcesMap,
GeneratedResourceManagerResourcesMap,
GeneratedSourceRepoResourcesMap,
GeneratedSpannerResourcesMap,
GeneratedStorageResourcesMap,
GeneratedMonitoringResourcesMap,
map[string]*schema.Resource{
Expand Down Expand Up @@ -197,11 +198,9 @@ func ResourceMapWithErrors() (map[string]*schema.Resource, error) {
"google_kms_crypto_key": resourceKmsCryptoKey(),
"google_kms_crypto_key_iam_binding": ResourceIamBindingWithImport(IamKmsCryptoKeySchema, NewKmsCryptoKeyIamUpdater, CryptoIdParseFunc),
"google_kms_crypto_key_iam_member": ResourceIamMemberWithImport(IamKmsCryptoKeySchema, NewKmsCryptoKeyIamUpdater, CryptoIdParseFunc),
"google_spanner_instance": resourceSpannerInstance(),
"google_spanner_instance_iam_binding": ResourceIamBindingWithImport(IamSpannerInstanceSchema, NewSpannerInstanceIamUpdater, SpannerInstanceIdParseFunc),
"google_spanner_instance_iam_member": ResourceIamMemberWithImport(IamSpannerInstanceSchema, NewSpannerInstanceIamUpdater, SpannerInstanceIdParseFunc),
"google_spanner_instance_iam_policy": ResourceIamPolicyWithImport(IamSpannerInstanceSchema, NewSpannerInstanceIamUpdater, SpannerInstanceIdParseFunc),
"google_spanner_database": resourceSpannerDatabase(),
"google_spanner_database_iam_binding": ResourceIamBindingWithImport(IamSpannerDatabaseSchema, NewSpannerDatabaseIamUpdater, SpannerDatabaseIdParseFunc),
"google_spanner_database_iam_member": ResourceIamMemberWithImport(IamSpannerDatabaseSchema, NewSpannerDatabaseIamUpdater, SpannerDatabaseIdParseFunc),
"google_spanner_database_iam_policy": ResourceIamPolicyWithImport(IamSpannerDatabaseSchema, NewSpannerDatabaseIamUpdater, SpannerDatabaseIdParseFunc),
Expand Down
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 738bdb9

Please sign in to comment.