mirror of
https://github.com/letic/terraform-provider-google.git
synced 2024-09-07 01:26:43 +00:00
224 lines
5.8 KiB
Go
224 lines
5.8 KiB
Go
package google
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/googleapi"
|
|
"google.golang.org/api/spanner/v1"
|
|
)
|
|
|
|
const (
|
|
spannerDatabaseNameFormat = "^[a-z][a-z0-9_-]*[a-z0-9]$"
|
|
)
|
|
|
|
func resourceSpannerDatabase() *schema.Resource {
|
|
return &schema.Resource{
|
|
Create: resourceSpannerDatabaseCreate,
|
|
Read: resourceSpannerDatabaseRead,
|
|
Delete: resourceSpannerDatabaseDelete,
|
|
Importer: &schema.ResourceImporter{
|
|
State: resourceSpannerDatabaseImport,
|
|
},
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
"instance": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"name": &schema.Schema{
|
|
Type: schema.TypeString,
|
|
Required: true,
|
|
ForceNew: true,
|
|
ValidateFunc: validateResourceSpannerDatabaseName,
|
|
},
|
|
|
|
"project": {
|
|
Type: schema.TypeString,
|
|
Optional: true,
|
|
Computed: true,
|
|
ForceNew: true,
|
|
},
|
|
|
|
"ddl": &schema.Schema{
|
|
Type: schema.TypeList,
|
|
Optional: true,
|
|
ForceNew: true,
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
},
|
|
|
|
"state": {
|
|
Type: schema.TypeString,
|
|
Computed: true,
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func resourceSpannerDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
id, err := buildSpannerDatabaseId(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
cdr := &spanner.CreateDatabaseRequest{}
|
|
cdr.CreateStatement = fmt.Sprintf("CREATE DATABASE `%s`", id.Database)
|
|
if v, ok := d.GetOk("ddl"); ok {
|
|
cdr.ExtraStatements = convertStringArr(v.([]interface{}))
|
|
}
|
|
|
|
op, err := config.clientSpanner.Projects.Instances.Databases.Create(
|
|
id.parentInstanceUri(), cdr).Do()
|
|
if err != nil {
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == http.StatusConflict {
|
|
return fmt.Errorf("Error, A database with name %s already exists in this instance", id.Database)
|
|
}
|
|
return fmt.Errorf("Error, failed to create database %s: %s", id.Database, err)
|
|
}
|
|
|
|
d.SetId(id.terraformId())
|
|
|
|
// Wait until it's created
|
|
timeoutMins := int(d.Timeout(schema.TimeoutCreate).Minutes())
|
|
waitErr := spannerDatabaseOperationWait(config, op, "Creating Spanner database", timeoutMins)
|
|
if waitErr != nil {
|
|
// The resource didn't actually create
|
|
d.SetId("")
|
|
return waitErr
|
|
}
|
|
|
|
log.Printf("[INFO] Spanner database %s has been created", id.terraformId())
|
|
return resourceSpannerDatabaseRead(d, meta)
|
|
}
|
|
|
|
func resourceSpannerDatabaseRead(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
id, err := buildSpannerDatabaseId(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
db, err := config.clientSpanner.Projects.Instances.Databases.Get(
|
|
id.databaseUri()).Do()
|
|
if err != nil {
|
|
return handleNotFoundError(err, d, fmt.Sprintf("Spanner database %q", id.databaseUri()))
|
|
}
|
|
|
|
d.Set("state", db.State)
|
|
d.Set("project", id.Project)
|
|
return nil
|
|
}
|
|
|
|
func resourceSpannerDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
|
|
config := meta.(*Config)
|
|
|
|
id, err := buildSpannerDatabaseId(d, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
_, err = config.clientSpanner.Projects.Instances.Databases.DropDatabase(
|
|
id.databaseUri()).Do()
|
|
if err != nil {
|
|
return fmt.Errorf("Error, failed to delete Spanner Database %s: %s", id.databaseUri(), err)
|
|
}
|
|
|
|
d.SetId("")
|
|
return nil
|
|
}
|
|
|
|
func resourceSpannerDatabaseImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
|
|
config := meta.(*Config)
|
|
err := parseImportId([]string{
|
|
"projects/(?P<project>[^/]+)/instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)",
|
|
"instances/(?P<instance>[^/]+)/databases/(?P<name>[^/]+)",
|
|
"(?P<project>[^/]+)/(?P<instance>[^/]+)/(?P<name>[^/]+)",
|
|
"(?P<instance>[^/]+)/(?P<name>[^/]+)",
|
|
}, d, config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error constructing id: %s", err)
|
|
}
|
|
|
|
id, err := buildSpannerDatabaseId(d, config)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("Error constructing id: %s", err)
|
|
}
|
|
|
|
d.SetId(id.terraformId())
|
|
|
|
return []*schema.ResourceData{d}, nil
|
|
}
|
|
|
|
func buildSpannerDatabaseId(d *schema.ResourceData, config *Config) (*spannerDatabaseId, error) {
|
|
project, err := getProject(d, config)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
dbName := d.Get("name").(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)
|
|
}
|
|
|
|
func extractSpannerDatabaseId(id string) (*spannerDatabaseId, error) {
|
|
if !regexp.MustCompile(fmt.Sprintf("^%s/[a-z0-9-]+/%s$", ProjectRegex, spannerDatabaseNameFormat)).Match([]byte(id)) {
|
|
return nil, fmt.Errorf("Invalid spanner id format, expecting {projectId}/{instanceId}/{databaseId}")
|
|
}
|
|
parts := strings.Split(id, "/")
|
|
return &spannerDatabaseId{
|
|
Project: parts[0],
|
|
Instance: parts[1],
|
|
Database: parts[2],
|
|
}, nil
|
|
}
|
|
|
|
func validateResourceSpannerDatabaseName(v interface{}, k string) (ws []string, errors []error) {
|
|
value := v.(string)
|
|
|
|
if len(value) < 2 && len(value) > 30 {
|
|
errors = append(errors, fmt.Errorf(
|
|
"%q must be between 2 and 30 characters in length", k))
|
|
}
|
|
|
|
if !regexp.MustCompile(spannerDatabaseNameFormat).MatchString(value) {
|
|
errors = append(errors, fmt.Errorf("database name %q must match regexp %q", value, spannerDatabaseNameFormat))
|
|
}
|
|
return
|
|
}
|