Add Google Spanner Support (google_spanner_instance) (#270)

This commit is contained in:
Nicki Watt 2017-08-14 17:30:41 +01:00 committed by Vincent Roseberry
parent 9c159c6a96
commit 04b3ffb8ea
11 changed files with 13038 additions and 0 deletions

View File

@ -27,6 +27,7 @@ import (
"google.golang.org/api/pubsub/v1"
"google.golang.org/api/servicemanagement/v1"
"google.golang.org/api/sourcerepo/v1"
"google.golang.org/api/spanner/v1"
"google.golang.org/api/sqladmin/v1beta4"
"google.golang.org/api/storage/v1"
)
@ -45,6 +46,7 @@ type Config struct {
clientDns *dns.Service
clientPubsub *pubsub.Service
clientResourceManager *cloudresourcemanager.Service
clientSpanner *spanner.Service
clientSourceRepo *sourcerepo.Service
clientStorage *storage.Service
clientSqlAdmin *sqladmin.Service
@ -215,6 +217,13 @@ func (c *Config) loadAndValidate() error {
}
c.clientSourceRepo.UserAgent = userAgent
log.Printf("[INFO] Instantiating Google Cloud Spanner Client...")
c.clientSpanner, err = spanner.New(client)
if err != nil {
return err
}
c.clientSpanner.UserAgent = userAgent
return nil
}

View File

@ -0,0 +1,59 @@
package google
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)
func TestAccGoogleSpannerInstance_importInstance(t *testing.T) {
resourceName := "google_spanner_instance.basic"
instanceName := fmt.Sprintf("span-itest-%s", acctest.RandString(10))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpannerInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccSpannerInstance_basic(instanceName),
},
resource.TestStep{
ResourceName: resourceName,
ImportStateId: instanceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
func TestAccGoogleSpannerInstance_importProjectInstance(t *testing.T) {
resourceName := "google_spanner_instance.basic"
instanceName := fmt.Sprintf("span-itest-%s", acctest.RandString(10))
var projectId = multiEnvSearch([]string{"GOOGLE_PROJECT", "GCLOUD_PROJECT", "CLOUDSDK_CORE_PROJECT"})
if projectId == "" {
t.Skip("Unable to locate projectId via environment variables ... skipping ")
return
}
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpannerInstanceDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccSpannerInstance_basicWithProject(projectId, instanceName),
},
resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

View File

@ -111,6 +111,7 @@ func Provider() terraform.ResourceProvider {
"google_dns_managed_zone": resourceDnsManagedZone(),
"google_dns_record_set": resourceDnsRecordSet(),
"google_sourcerepo_repository": resourceSourceRepoRepository(),
"google_spanner_instance": resourceSpannerInstance(),
"google_sql_database": resourceSqlDatabase(),
"google_sql_database_instance": resourceSqlDatabaseInstance(),
"google_sql_user": resourceSqlUser(),

View File

@ -0,0 +1,345 @@
package google
import (
"fmt"
"log"
"net/http"
"regexp"
"strings"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/api/googleapi"
"google.golang.org/api/spanner/v1"
)
func resourceSpannerInstance() *schema.Resource {
return &schema.Resource{
Create: resourceSpannerInstanceCreate,
Read: resourceSpannerInstanceRead,
Update: resourceSpannerInstanceUpdate,
Delete: resourceSpannerInstanceDelete,
Importer: &schema.ResourceImporter{
State: resourceSpannerInstanceImportState,
},
Schema: map[string]*schema.Schema{
"config": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < 6 && len(value) > 30 {
errors = append(errors, fmt.Errorf(
"%q must be between 6 and 30 characters in length", k))
}
if !regexp.MustCompile("^[a-z0-9-]+$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q can only contain lowercase letters, numbers and hyphens", k))
}
if !regexp.MustCompile("^[a-z]").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must start with a letter", k))
}
if !regexp.MustCompile("[a-z0-9]$").MatchString(value) {
errors = append(errors, fmt.Errorf(
"%q must end with a number or a letter", k))
}
return
},
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (ws []string, errors []error) {
value := v.(string)
if len(value) < 4 && len(value) > 30 {
errors = append(errors, fmt.Errorf(
"%q must be between 4 and 30 characters in length", k))
}
return
},
},
"num_nodes": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 1,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"state": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceSpannerInstanceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
cir := &spanner.CreateInstanceRequest{
Instance: &spanner.Instance{},
}
if v, ok := d.GetOk("name"); ok {
cir.InstanceId = v.(string)
} else {
cir.InstanceId = genSpannerInstanceName()
d.Set("name", cir.InstanceId)
}
if v, ok := d.GetOk("labels"); ok {
cir.Instance.Labels = convertStringMap(v.(map[string]interface{}))
}
id, err := buildSpannerInstanceId(d, config)
if err != nil {
return err
}
cir.Instance.Config = id.instanceConfigUri(d.Get("config").(string))
cir.Instance.DisplayName = d.Get("display_name").(string)
cir.Instance.NodeCount = int64(d.Get("num_nodes").(int))
op, err := config.clientSpanner.Projects.Instances.Create(
id.parentProjectUri(), cir).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict {
return fmt.Errorf("Error, the name %s is not unique within project %s", id.Instance, id.Project)
}
return fmt.Errorf("Error, failed to create instance %s: %s", id.terraformId(), err)
}
d.SetId(id.terraformId())
// Wait until it's created
timeoutMins := int(d.Timeout(schema.TimeoutCreate).Minutes())
waitErr := spannerInstanceOperationWait(config, op, "Creating Spanner instance", timeoutMins)
if waitErr != nil {
// The resource didn't actually create
d.SetId("")
return waitErr
}
log.Printf("[INFO] Spanner instance %s has been created", id.terraformId())
return resourceSpannerInstanceRead(d, meta)
}
func resourceSpannerInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id, err := buildSpannerInstanceId(d, config)
if err != nil {
return err
}
instance, err := config.clientSpanner.Projects.Instances.Get(
id.instanceUri()).Do()
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("Spanner instance %s", id.terraformId()))
}
d.Set("config", extractInstanceConfigFromUri(instance.Config))
d.Set("labels", instance.Labels)
d.Set("display_name", instance.DisplayName)
d.Set("num_nodes", instance.NodeCount)
d.Set("state", instance.State)
return nil
}
func resourceSpannerInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
log.Printf("[INFO] About to update Spanner Instance %s ", d.Id())
uir := &spanner.UpdateInstanceRequest{
Instance: &spanner.Instance{},
}
id, err := buildSpannerInstanceId(d, config)
if err != nil {
return err
}
fieldMask := []string{}
if d.HasChange("num_nodes") {
fieldMask = append(fieldMask, "nodeCount")
uir.Instance.NodeCount = int64(d.Get("num_nodes").(int))
}
if d.HasChange("display_name") {
fieldMask = append(fieldMask, "displayName")
uir.Instance.DisplayName = d.Get("display_name").(string)
}
if d.HasChange("labels") {
fieldMask = append(fieldMask, "labels")
uir.Instance.Labels = convertStringMap(d.Get("labels").(map[string]interface{}))
}
uir.FieldMask = strings.Join(fieldMask, ",")
op, err := config.clientSpanner.Projects.Instances.Patch(
id.instanceUri(), uir).Do()
if err != nil {
return err
}
// Wait until it's updated
timeoutMins := int(d.Timeout(schema.TimeoutUpdate).Minutes())
err = spannerInstanceOperationWait(config, op, "Update Spanner Instance", timeoutMins)
if err != nil {
return err
}
log.Printf("[INFO] Spanner Instance %s has been updated ", id.terraformId())
return resourceSpannerInstanceRead(d, meta)
}
func resourceSpannerInstanceDelete(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
id, err := buildSpannerInstanceId(d, config)
if err != nil {
return err
}
_, err = config.clientSpanner.Projects.Instances.Delete(
id.instanceUri()).Do()
if err != nil {
return fmt.Errorf("Error, failed to delete Spanner Instance %s in project %s: %s", id.Instance, id.Project, err)
}
d.SetId("")
return nil
}
func resourceSpannerInstanceImportState(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
id, err := importSpannerInstanceId(d.Id())
if err != nil {
return nil, err
}
if id.Project != "" {
d.Set("project", id.Project)
} else {
project, err := getProject(d, config)
if err != nil {
return nil, err
}
id.Project = project
}
d.Set("name", id.Instance)
d.SetId(id.terraformId())
return []*schema.ResourceData{d}, nil
}
func buildSpannerInstanceId(d *schema.ResourceData, config *Config) (*spannerInstanceId, error) {
project, err := getProject(d, config)
if err != nil {
return nil, err
}
return &spannerInstanceId{
Project: project,
Instance: d.Get("name").(string),
}, nil
}
func extractInstanceConfigFromUri(configUri string) string {
return extractLastResourceFromUri(configUri)
}
func extractInstanceNameFromUri(nameUri string) string {
return extractLastResourceFromUri(nameUri)
}
func extractLastResourceFromUri(uri string) string {
rUris := strings.Split(uri, "/")
return rUris[len(rUris)-1]
}
func genSpannerInstanceName() string {
return resource.PrefixedUniqueId("tfgen-spanid-")[:30]
}
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 importSpannerInstanceId(id string) (*spannerInstanceId, error) {
if !regexp.MustCompile("^[a-z0-9-]+$").Match([]byte(id)) &&
!regexp.MustCompile("^[a-z0-9-]+/[a-z0-9-]+$").Match([]byte(id)) {
return nil, fmt.Errorf("Invalid spanner instance specifier. " +
"Expecting either {projectId}/{instanceId} OR " +
"{instanceId} (where project is to be derived from that specified in provider)")
}
parts := strings.Split(id, "/")
if len(parts) == 1 {
log.Printf("[INFO] Spanner instance import format of {instanceId} specified: %s", id)
return &spannerInstanceId{Instance: parts[0]}, nil
}
log.Printf("[INFO] Spanner instance import format of {projectId}/{instanceId} specified: %s", id)
return extractSpannerInstanceId(id)
}
func extractSpannerInstanceId(id string) (*spannerInstanceId, error) {
if !regexp.MustCompile("^[a-z0-9-]+/[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
}
func convertStringMap(v map[string]interface{}) map[string]string {
m := make(map[string]string)
for k, val := range v {
m[k] = val.(string)
}
return m
}

View File

@ -0,0 +1,390 @@
package google
import (
"fmt"
"net/http"
"regexp"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"google.golang.org/api/googleapi"
"google.golang.org/api/spanner/v1"
"strings"
)
// Unit Tests
func TestExtractInstanceConfigFromUri_withFullPath(t *testing.T) {
actual := extractInstanceConfigFromUri("projects/project123/instanceConfigs/conf987")
expected := "conf987"
expectEquals(t, expected, actual)
}
func TestExtractInstanceConfigFromUri_withNoPath(t *testing.T) {
actual := extractInstanceConfigFromUri("conf987")
expected := "conf987"
expectEquals(t, expected, actual)
}
func TestExtractInstanceNameFromUri_withFullPath(t *testing.T) {
actual := extractInstanceNameFromUri("projects/project123/instances/instance456")
expected := "instance456"
expectEquals(t, expected, actual)
}
func TestExtractInstanceNameFromUri_withNoPath(t *testing.T) {
actual := extractInstanceConfigFromUri("instance456")
expected := "instance456"
expectEquals(t, expected, actual)
}
func TestSpannerInstanceId_instanceUri(t *testing.T) {
id := spannerInstanceId{
Project: "project123",
Instance: "instance456",
}
actual := id.instanceUri()
expected := "projects/project123/instances/instance456"
expectEquals(t, expected, actual)
}
func TestSpannerInstanceId_instanceConfigUri(t *testing.T) {
id := spannerInstanceId{
Project: "project123",
Instance: "instance456",
}
actual := id.instanceConfigUri("conf987")
expected := "projects/project123/instanceConfigs/conf987"
expectEquals(t, expected, actual)
}
func TestSpannerInstanceId_parentProjectUri(t *testing.T) {
id := spannerInstanceId{
Project: "project123",
Instance: "instance456",
}
actual := id.parentProjectUri()
expected := "projects/project123"
expectEquals(t, expected, actual)
}
func TestGenSpannerInstanceName(t *testing.T) {
s := genSpannerInstanceName()
if len(s) != 30 {
t.Fatalf("Expected a 30 char ID to be generated, instead found %d chars", len(s))
}
}
func TestImportSpannerInstanceId(t *testing.T) {
sid, e := importSpannerInstanceId("instance456")
if e != nil {
t.Errorf("Error should have been nil")
}
expectEquals(t, "", sid.Project)
expectEquals(t, "instance456", sid.Instance)
}
func TestImportSpannerInstanceId_projectAndInstance(t *testing.T) {
sid, e := importSpannerInstanceId("project123/instance456")
if e != nil {
t.Errorf("Error should have been nil")
}
expectEquals(t, "project123", sid.Project)
expectEquals(t, "instance456", sid.Instance)
}
func TestImportSpannerInstanceId_invalidLeadingSlash(t *testing.T) {
sid, e := importSpannerInstanceId("/instance456")
expectInvalidSpannerInstanceImport(t, sid, e)
}
func TestImportSpannerInstanceId_invalidTrailingSlash(t *testing.T) {
sid, e := importSpannerInstanceId("project123/")
expectInvalidSpannerInstanceImport(t, sid, e)
}
func TestImportSpannerInstanceId_invalidSingleSlash(t *testing.T) {
sid, e := importSpannerInstanceId("/")
expectInvalidSpannerInstanceImport(t, sid, e)
}
func TestImportSpannerInstanceId_invalidMultiSlash(t *testing.T) {
sid, e := importSpannerInstanceId("project123/instance456/db789")
expectInvalidSpannerInstanceImport(t, sid, e)
}
func expectInvalidSpannerInstanceImport(t *testing.T, sid *spannerInstanceId, e error) {
if sid != nil {
t.Errorf("Expected spannerInstanceId to be nil")
return
}
if e == nil {
t.Errorf("Expected an Error but did not get one")
return
}
if !strings.HasPrefix(e.Error(), "Invalid spanner instance specifier") {
t.Errorf("Expecting Error starting with 'Invalid spanner instance specifier'")
}
}
func expectEquals(t *testing.T, expected, actual string) {
if actual != expected {
t.Fatalf("Expected %s, but got %s", expected, actual)
}
}
// Acceptance Tests
func TestAccSpannerInstance_basic(t *testing.T) {
var instance spanner.Instance
rnd := acctest.RandString(10)
idName := fmt.Sprintf("spanner-test-%s", rnd)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpannerInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccSpannerInstance_basic(idName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSpannerInstanceExists("google_spanner_instance.basic", &instance),
resource.TestCheckResourceAttr("google_spanner_instance.basic", "name", idName),
resource.TestCheckResourceAttr("google_spanner_instance.basic", "display_name", idName+"-dname"),
resource.TestCheckResourceAttr("google_spanner_instance.basic", "num_nodes", "1"),
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "state"),
),
},
},
})
}
func TestAccSpannerInstance_basicWithAutogenName(t *testing.T) {
var instance spanner.Instance
rnd := acctest.RandString(10)
displayName := fmt.Sprintf("spanner-test-%s-dname", rnd)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpannerInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccSpannerInstance_basicWithAutogenName(displayName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSpannerInstanceExists("google_spanner_instance.basic", &instance),
resource.TestCheckResourceAttr("google_spanner_instance.basic", "display_name", displayName),
resource.TestCheckResourceAttrSet("google_spanner_instance.basic", "name"),
),
},
},
})
}
func TestAccSpannerInstance_duplicateNameError(t *testing.T) {
var instance spanner.Instance
rnd := acctest.RandString(10)
idName := fmt.Sprintf("spanner-test-%s", rnd)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpannerInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccSpannerInstance_duplicateNameError_part1(idName),
Check: resource.ComposeTestCheckFunc(
testAccCheckSpannerInstanceExists("google_spanner_instance.basic1", &instance),
),
},
{
Config: testAccSpannerInstance_duplicateNameError_part2(idName),
ExpectError: regexp.MustCompile(
fmt.Sprintf("Error, the name %s is not unique within project", idName)),
},
},
})
}
func TestAccSpannerInstance_update(t *testing.T) {
var instance spanner.Instance
rnd := acctest.RandString(10)
dName1 := fmt.Sprintf("spanner-dname1-%s", rnd)
dName2 := fmt.Sprintf("spanner-dname2-%s", rnd)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckSpannerInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testAccSpannerInstance_update(dName1, 1, false),
Check: resource.ComposeTestCheckFunc(
testAccCheckSpannerInstanceExists("google_spanner_instance.updater", &instance),
resource.TestCheckResourceAttr("google_spanner_instance.updater", "display_name", dName1),
resource.TestCheckResourceAttr("google_spanner_instance.updater", "num_nodes", "1"),
resource.TestCheckResourceAttr("google_spanner_instance.updater", "labels.%", "1"),
),
},
{
Config: testAccSpannerInstance_update(dName2, 2, true),
Check: resource.ComposeTestCheckFunc(
testAccCheckSpannerInstanceExists("google_spanner_instance.updater", &instance),
resource.TestCheckResourceAttr("google_spanner_instance.updater", "display_name", dName2),
resource.TestCheckResourceAttr("google_spanner_instance.updater", "num_nodes", "2"),
resource.TestCheckResourceAttr("google_spanner_instance.updater", "labels.%", "2"),
),
},
},
})
}
func testAccCheckSpannerInstanceDestroy(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
for _, rs := range s.RootModule().Resources {
if rs.Type != "google_spanner_instance" {
continue
}
if rs.Primary.ID == "" {
return fmt.Errorf("Unable to verify delete of spanner instance, ID is empty")
}
instanceName := rs.Primary.Attributes["name"]
project, err := getTestProject(rs.Primary, config)
if err != nil {
return err
}
id := spannerInstanceId{
Project: project,
Instance: instanceName,
}
_, err = config.clientSpanner.Projects.Instances.Get(
id.instanceUri()).Do()
if err != nil {
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusNotFound {
return nil
}
return fmt.Errorf("Error make GCP platform call to verify spanner instance deleted: %s", err.Error())
}
return fmt.Errorf("Spanner instance not destroyed - still exists")
}
return nil
}
func testAccCheckSpannerInstanceExists(n string, instance *spanner.Instance) resource.TestCheckFunc {
return func(s *terraform.State) error {
config := testAccProvider.Meta().(*Config)
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Terraform resource Not found: %s", n)
}
if rs.Primary.ID == "" {
return fmt.Errorf("No ID is set for Spanner instance")
}
id, err := extractSpannerInstanceId(rs.Primary.ID)
if err != nil {
return err
}
found, err := config.clientSpanner.Projects.Instances.Get(
id.instanceUri()).Do()
if err != nil {
return err
}
fName := extractInstanceNameFromUri(found.Name)
if fName != extractInstanceNameFromUri(rs.Primary.ID) {
return fmt.Errorf("Spanner instance %s not found, found %s instead", rs.Primary.ID, fName)
}
*instance = *found
return nil
}
}
func testAccSpannerInstance_basic(name string) string {
return fmt.Sprintf(`
resource "google_spanner_instance" "basic" {
name = "%s"
config = "regional-us-central1"
display_name = "%s-dname"
num_nodes = 1
}
`, name, name)
}
func testAccSpannerInstance_basicWithProject(project, name string) string {
return fmt.Sprintf(`
resource "google_spanner_instance" "basic" {
project = "%s"
name = "%s"
config = "regional-us-central1"
display_name = "%s-dname"
num_nodes = 1
}
`, project, name, name)
}
func testAccSpannerInstance_basicWithAutogenName(name string) string {
return fmt.Sprintf(`
resource "google_spanner_instance" "basic" {
config = "regional-us-central1"
display_name = "%s"
num_nodes = 1
}
`, name)
}
func testAccSpannerInstance_duplicateNameError_part1(name string) string {
return fmt.Sprintf(`
resource "google_spanner_instance" "basic1" {
name = "%s"
config = "regional-us-central1"
display_name = "%s-dname"
num_nodes = 1
}
`, name, name)
}
func testAccSpannerInstance_duplicateNameError_part2(name string) string {
return fmt.Sprintf(`
%s
resource "google_spanner_instance" "basic2" {
name = "%s"
config = "regional-us-central1"
display_name = "%s-dname"
num_nodes = 1
}
`, testAccSpannerInstance_duplicateNameError_part1(name), name, name)
}
func testAccSpannerInstance_update(name string, nodes int, addLabel bool) string {
extraLabel := ""
if addLabel {
extraLabel = "\"key2\" = \"value2\""
}
return fmt.Sprintf(`
resource "google_spanner_instance" "updater" {
config = "regional-us-central1"
display_name = "%s"
num_nodes = %d
labels {
"key1" = "value1"
%s
}
}
`, name, nodes, extraLabel)
}

View File

@ -0,0 +1,62 @@
package google
import (
"fmt"
"log"
"time"
"github.com/hashicorp/terraform/helper/resource"
"google.golang.org/api/spanner/v1"
)
type SpannerInstanceOperationWaiter struct {
Service *spanner.Service
Op *spanner.Operation
}
func (w *SpannerInstanceOperationWaiter) Conf() *resource.StateChangeConf {
return &resource.StateChangeConf{
Pending: []string{"false"},
Target: []string{"true"},
Refresh: w.RefreshFunc(),
}
}
func (w *SpannerInstanceOperationWaiter) RefreshFunc() resource.StateRefreshFunc {
return func() (interface{}, string, error) {
op, err := w.Service.Projects.Instances.Operations.Get(w.Op.Name).Do()
if err != nil {
return nil, "", err
}
log.Printf("[DEBUG] Got %v while polling for operation %s's 'done' status", op.Done, w.Op.Name)
return op, fmt.Sprint(op.Done), nil
}
}
func spannerInstanceOperationWait(config *Config, op *spanner.Operation, activity string, timeoutMin int) error {
w := &SpannerInstanceOperationWaiter{
Service: config.clientSpanner,
Op: op,
}
state := w.Conf()
state.Delay = 10 * time.Second
state.Timeout = time.Duration(timeoutMin) * time.Minute
state.MinTimeout = 2 * time.Second
opRaw, err := state.WaitForState()
if err != nil {
return fmt.Errorf("Error waiting for %s: %s", activity, err)
}
op = opRaw.(*spanner.Operation)
if op.Error != nil {
return fmt.Errorf("Error code %v, message: %s", op.Error.Code, op.Error.Message)
}
return nil
}

2695
vendor/google.golang.org/api/spanner/v1/spanner-api.json generated vendored Normal file

File diff suppressed because one or more lines are too long

9389
vendor/google.golang.org/api/spanner/v1/spanner-gen.go generated vendored Normal file

File diff suppressed because it is too large Load Diff

6
vendor/vendor.json vendored
View File

@ -965,6 +965,12 @@
"revision": "295e4bb0ade057ae2cfb9876ab0b54635dbfcea4",
"revisionTime": "2017-07-18T13:06:16Z"
},
{
"checksumSHA1": "/UUpe1f/F+BPolZJZOa3EWLA6Gc=",
"path": "google.golang.org/api/spanner/v1",
"revision": "bb4e0b1b53539118e8d3a704d31cb69a81a9c140",
"revisionTime": "2017-07-05T20:32:08Z"
},
{
"checksumSHA1": "moKPpECJZQR/mANGD26E7Pbxn4I=",
"path": "google.golang.org/api/sqladmin/v1beta4",

View File

@ -0,0 +1,73 @@
---
layout: "google"
page_title: "Google: google_spanner_instance"
sidebar_current: "docs-google-spanner-instance"
description: |-
Creates and manages a Google Spanner Instance.
---
# google\_spanner\_instance
Creates and manages a Google Spanner Instance. For more information, see the [official documentation](https://cloud.google.com/spanner/), or the [JSON API](https://cloud.google.com/spanner/docs/reference/rest/v1/projects.instances).
## Example Usage
Example creating a Spanner instance.
```hcl
resource "google_spanner_instance" "main" {
config = "regional-europe-west1"
display_name = "main-instance"
name = "main-instance"
num_nodes = 1
}
```
## Argument Reference
The following arguments are supported:
* `config` - (Required) The name of the instance's configuration (similar but not
quite the same as a region) which defines defines the geographic placement and
replication of your databases in this instance. It determines where your data
is stored. Values are typically of the form `regional-europe-west1` , `us-central` etc.
In order to obtain a valid list please consult the
[Configuration section of the docs](https://cloud.google.com/spanner/docs/instances).
* `display_name` - (Required) The descriptive name for this instance as it appears
in UIs. Can be updated, however should be kept globally unique to avoid confusion.
- - -
* `name` - (Optional, Computed) The unique name (ID) of the instance. If the name is left
blank, Terraform will randomly generate one when the instance is first
created.
* `num_nodes` - (Optional, Computed) The number of nodes allocated to this instance.
Defaults to `1`. This can be updated after creation.
* `project` - (Optional) The project in which the resource belongs. If it
is not provided, the provider project is used.
* `labels` - (Optional) A mapping (key/value pairs) of labels to assign to the instance.
## Attributes Reference
In addition to the arguments listed above, the following computed attributes are
exported:
* `state` - The current state of the instance.
## Import
Instances can be imported using their `name` and optionally
the `project` in which it is defined (Often used when the project is different
to that defined in the provider), The format is thus either `{instanceId}` or
`{projectId}/{instanceId}`. e.g.
```
$ terraform import google_spanner_instance.master instance123
$ terraform import google_spanner_instance.master project123/instance456
```

View File

@ -279,6 +279,15 @@
</ul>
</li>
<li<%= sidebar_current("docs-google-spanner") %>>
<a href="#">Google Spanner Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-google-spanner-instance") %>>
<a href="/docs/providers/google/r/spanner_instance.html">google_spanner_instance</a>
</li>
</ul>
</li>
<li<%= sidebar_current("docs-google-sql") %>>
<a href="#">Google SQL Resources</a>
<ul class="nav nav-visible">