github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/google/resource_sql_database.go (about) 1 package google 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 8 "google.golang.org/api/sqladmin/v1beta4" 9 ) 10 11 func resourceSqlDatabase() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceSqlDatabaseCreate, 14 Read: resourceSqlDatabaseRead, 15 Delete: resourceSqlDatabaseDelete, 16 17 Schema: map[string]*schema.Schema{ 18 "name": &schema.Schema{ 19 Type: schema.TypeString, 20 Required: true, 21 ForceNew: true, 22 }, 23 "instance": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 "self_link": &schema.Schema{ 29 Type: schema.TypeString, 30 Computed: true, 31 }, 32 }, 33 } 34 } 35 36 func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error { 37 config := meta.(*Config) 38 39 database_name := d.Get("name").(string) 40 instance_name := d.Get("instance").(string) 41 project := config.Project 42 43 db := &sqladmin.Database{ 44 Name: database_name, 45 Instance: instance_name, 46 } 47 48 op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name, 49 db).Do() 50 51 if err != nil { 52 return fmt.Errorf("Error, failed to insert "+ 53 "database %s into instance %s: %s", database_name, 54 instance_name, err) 55 } 56 57 err = sqladminOperationWait(config, op, "Insert Database") 58 59 if err != nil { 60 return fmt.Errorf("Error, failure waiting for insertion of %s "+ 61 "into %s: %s", database_name, instance_name, err) 62 } 63 64 return resourceSqlDatabaseRead(d, meta) 65 } 66 67 func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { 68 config := meta.(*Config) 69 70 database_name := d.Get("name").(string) 71 instance_name := d.Get("instance").(string) 72 project := config.Project 73 74 db, err := config.clientSqlAdmin.Databases.Get(project, instance_name, 75 database_name).Do() 76 77 if err != nil { 78 return fmt.Errorf("Error, failed to get"+ 79 "database %s in instance %s: %s", database_name, 80 instance_name, err) 81 } 82 83 d.Set("self_link", db.SelfLink) 84 d.SetId(instance_name + ":" + database_name) 85 86 return nil 87 } 88 89 func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error { 90 config := meta.(*Config) 91 92 database_name := d.Get("name").(string) 93 instance_name := d.Get("instance").(string) 94 project := config.Project 95 96 op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name, 97 database_name).Do() 98 99 if err != nil { 100 return fmt.Errorf("Error, failed to delete"+ 101 "database %s in instance %s: %s", database_name, 102 instance_name, err) 103 } 104 105 err = sqladminOperationWait(config, op, "Delete Database") 106 107 if err != nil { 108 return fmt.Errorf("Error, failure waiting for deletion of %s "+ 109 "in %s: %s", database_name, instance_name, err) 110 } 111 112 return nil 113 }