terraform-provider-google/google/resource_bigtable_instance.go

205 lines
4.3 KiB
Go
Raw Normal View History

2017-06-15 17:41:05 +00:00
package google
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"cloud.google.com/go/bigtable"
"golang.org/x/net/context"
)
func resourceBigtableInstance() *schema.Resource {
return &schema.Resource{
Create: resourceBigtableInstanceCreate,
Read: resourceBigtableInstanceRead,
Delete: resourceBigtableInstanceDestroy,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"display_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"num_nodes": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Default: 3,
ValidateFunc: IntAtLeast(3),
2017-06-15 17:41:05 +00:00
},
"storage_type": {
Type: schema.TypeString,
Optional: true,
2017-06-15 17:41:05 +00:00
ForceNew: true,
Default: "SSD",
2017-06-15 17:41:05 +00:00
ValidateFunc: validation.StringInSlice([]string{"SSD", "HDD"}, false),
},
"zone": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"project": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
},
}
}
func resourceBigtableInstanceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
name := d.Get("name").(string)
displayName, ok := d.GetOk("display_name")
if !ok {
displayName = name
}
var storageType bigtable.StorageType
switch value := d.Get("storage_type"); value {
case "HDD":
storageType = bigtable.HDD
case "SSD":
storageType = bigtable.SSD
}
instanceConf := &bigtable.InstanceConf{
InstanceId: name,
DisplayName: displayName.(string),
ClusterId: d.Get("cluster_id").(string),
NumNodes: int32(d.Get("num_nodes").(int)),
2017-06-15 17:41:05 +00:00
StorageType: storageType,
Zone: d.Get("zone").(string),
2017-06-15 17:41:05 +00:00
}
c, err := config.bigtableClientFactory.NewInstanceAdminClient(project)
2017-06-15 17:41:05 +00:00
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
err = c.CreateInstance(ctx, instanceConf)
if err != nil {
return fmt.Errorf("Error creating instance. %s", err)
}
d.SetId(name)
return resourceBigtableInstanceRead(d, meta)
}
func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
c, err := config.bigtableClientFactory.NewInstanceAdminClient(project)
2017-06-15 17:41:05 +00:00
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
instances, err := c.Instances(ctx)
if err != nil {
return fmt.Errorf("Error retrieving instances. %s", err)
}
var instanceInfo *bigtable.InstanceInfo
name := d.Id()
2017-06-15 17:41:05 +00:00
found := false
for _, i := range instances {
if i.Name == name {
instanceInfo = i
found = true
break
}
}
if !found {
return fmt.Errorf("Error retrieving instance. Could not find %s.", name)
}
d.Set("name", instanceInfo.Name)
d.Set("display_name", instanceInfo.DisplayName)
return nil
}
func resourceBigtableInstanceDestroy(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
c, err := config.bigtableClientFactory.NewInstanceAdminClient(project)
2017-06-15 17:41:05 +00:00
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
name := d.Id()
err = c.DeleteInstance(ctx, name)
if err != nil {
return fmt.Errorf("Error deleting instance. %s", err)
}
d.SetId("")
return nil
}
// IntAtLeast returns a SchemaValidateFunc which tests if the provided value
// is of type int and is above min (inclusive)
func IntAtLeast(min int) schema.SchemaValidateFunc {
return func(i interface{}, k string) (s []string, es []error) {
v, ok := i.(int)
if !ok {
es = append(es, fmt.Errorf("expected type of %s to be int", k))
return
}
if v < min {
es = append(es, fmt.Errorf("expected %s to be at least %d, got %d", k, min, v))
return
}
return
}
}