Skip to content

Commit

Permalink
Initial addition of generated spanner database to 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 7, 2019
1 parent 43d40f4 commit ee5c7bf
Show file tree
Hide file tree
Showing 6 changed files with 494 additions and 115 deletions.
44 changes: 43 additions & 1 deletion google/iam_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ func NewSpannerDatabaseIamUpdater(d *schema.ResourceData, config *Config) (Resou
}

func SpannerDatabaseIdParseFunc(d *schema.ResourceData, config *Config) error {
_, err := resourceSpannerDatabaseImport("database")(d, config)
_, err := resourceSpannerDatabaseImport(d, config)
return err
}

Expand Down Expand Up @@ -130,3 +130,45 @@ func spannerToResourceManagerPolicy(p *spanner.Policy) (*cloudresourcemanager.Po
}
return out, nil
}

func buildSpannerDatabaseId(d *schema.ResourceData, config *Config) (*spannerDatabaseId, error) {
project, err := getProject(d, config)
if err != nil {
return nil, err
}
database, ok := d.GetOk("name")
if !ok {
database = d.Get("database")
}

dbName := database.(string)
instanceName := d.Get("instance").(string)

return &spannerDatabaseId{
Project: project,
Instance: instanceName,
Database: dbName,
}, nil
}

type spannerDatabaseId struct {
Project string
Instance string
Database string
}

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

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

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

func (s spannerDatabaseId) databaseUri() string {
return fmt.Sprintf("%s/databases/%s", s.parentInstanceUri(), s.Database)
}
3 changes: 3 additions & 0 deletions google/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package google

import (
"fmt"
"log"
"regexp"
"strconv"
"strings"
Expand All @@ -23,10 +24,12 @@ func parseImportId(idRegexes []string, d TerraformResourceData, config *Config)
}

if fieldValues := re.FindStringSubmatch(d.Id()); fieldValues != nil {
log.Printf("[DEBUG] matching ID %s to regex %s.", d.Id(), idFormat)
// Starting at index 1, the first match is the full string.
for i := 1; i < len(fieldValues); i++ {
fieldName := re.SubexpNames()[i]
fieldValue := fieldValues[i]
log.Printf("[DEBUG] importing %s = %s", fieldName, fieldValue)
// Because we do not know at this point whether 'fieldName'
// corresponds to a TypeString or a TypeInteger in the resource
// schema, we need to determine the type in an unintutitive way.
Expand Down
22 changes: 22 additions & 0 deletions google/provider_spanner_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ----------------------------------------------------------------------------
//
// *** 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 GeneratedSpannerResourcesMap = map[string]*schema.Resource{
"google_spanner_instance_config": resourceSpannerInstanceConfig(),
"google_spanner_database": resourceSpannerDatabase(),
}
Loading

0 comments on commit ee5c7bf

Please sign in to comment.