2015-10-23 16:10:41 +02:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
2015-11-13 21:36:03 +01:00
|
|
|
"log"
|
2015-10-23 16:10:41 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
|
2015-11-13 21:36:03 +01:00
|
|
|
"google.golang.org/api/googleapi"
|
2015-10-23 16:10:41 +02:00
|
|
|
"google.golang.org/api/sqladmin/v1beta4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceSqlDatabase() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceSqlDatabaseCreate,
|
|
|
|
Read: resourceSqlDatabaseRead,
|
|
|
|
Delete: resourceSqlDatabaseDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"instance": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
"self_link": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
database_name := d.Get("name").(string)
|
|
|
|
instance_name := d.Get("instance").(string)
|
|
|
|
project := config.Project
|
|
|
|
|
|
|
|
db := &sqladmin.Database{
|
|
|
|
Name: database_name,
|
|
|
|
Instance: instance_name,
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name,
|
|
|
|
db).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to insert "+
|
|
|
|
"database %s into instance %s: %s", database_name,
|
|
|
|
instance_name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqladminOperationWait(config, op, "Insert Database")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failure waiting for insertion of %s "+
|
|
|
|
"into %s: %s", database_name, instance_name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceSqlDatabaseRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
database_name := d.Get("name").(string)
|
|
|
|
instance_name := d.Get("instance").(string)
|
|
|
|
project := config.Project
|
|
|
|
|
|
|
|
db, err := config.clientSqlAdmin.Databases.Get(project, instance_name,
|
|
|
|
database_name).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
2015-11-13 21:36:03 +01:00
|
|
|
if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 {
|
|
|
|
log.Printf("[WARN] Removing SQL Database %q because it's gone", d.Get("name").(string))
|
|
|
|
// The resource doesn't exist anymore
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-10-23 16:10:41 +02:00
|
|
|
return fmt.Errorf("Error, failed to get"+
|
|
|
|
"database %s in instance %s: %s", database_name,
|
|
|
|
instance_name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Set("self_link", db.SelfLink)
|
|
|
|
d.SetId(instance_name + ":" + database_name)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
database_name := d.Get("name").(string)
|
|
|
|
instance_name := d.Get("instance").(string)
|
|
|
|
project := config.Project
|
|
|
|
|
|
|
|
op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name,
|
|
|
|
database_name).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to delete"+
|
|
|
|
"database %s in instance %s: %s", database_name,
|
|
|
|
instance_name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqladminOperationWait(config, op, "Delete Database")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failure waiting for deletion of %s "+
|
|
|
|
"in %s: %s", database_name, instance_name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|