Skip to content

Commit

Permalink
Initial addition of generated spanner database to terraform. (hashico…
Browse files Browse the repository at this point in the history
…rp#323)

Signed-off-by: Modular Magician <magic-modules@google.com>
  • Loading branch information
modular-magician authored and nat-henderson committed Jan 16, 2019
1 parent d11f9ca commit 17199f0
Show file tree
Hide file tree
Showing 8 changed files with 361 additions and 254 deletions.
25 changes: 23 additions & 2 deletions google-beta/iam_spanner_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ func NewSpannerDatabaseIamUpdater(d *schema.ResourceData, config *Config) (Resou
}

func SpannerDatabaseIdParseFunc(d *schema.ResourceData, config *Config) error {
_, err := resourceSpannerDatabaseImport("database")(d, config)
return err
return parseImportId([]string{"(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<database>[^/]+)"}, d, config)
}

func (u *SpannerDatabaseIamUpdater) GetResourceIamPolicy() (*cloudresourcemanager.Policy, error) {
Expand Down Expand Up @@ -130,3 +129,25 @@ func spannerToResourceManagerPolicy(p *spanner.Policy) (*cloudresourcemanager.Po
}
return out, 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-beta/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
21 changes: 21 additions & 0 deletions google-beta/provider_spanner_gen.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// ----------------------------------------------------------------------------
//
// *** 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_database": resourceSpannerDatabase(),
}
Loading

0 comments on commit 17199f0

Please sign in to comment.