github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/google/resource_sql_database.go (about) 1 package google 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 9 "google.golang.org/api/googleapi" 10 "google.golang.org/api/sqladmin/v1beta4" 11 ) 12 13 func resourceSqlDatabase() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceSqlDatabaseCreate, 16 Read: resourceSqlDatabaseRead, 17 Delete: resourceSqlDatabaseDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "instance": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "project": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 ForceNew: true, 36 }, 37 38 "self_link": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 }, 43 } 44 } 45 46 func resourceSqlDatabaseCreate(d *schema.ResourceData, meta interface{}) error { 47 config := meta.(*Config) 48 49 project, err := getProject(d, config) 50 if err != nil { 51 return err 52 } 53 54 database_name := d.Get("name").(string) 55 instance_name := d.Get("instance").(string) 56 57 db := &sqladmin.Database{ 58 Name: database_name, 59 Instance: instance_name, 60 } 61 62 op, err := config.clientSqlAdmin.Databases.Insert(project, instance_name, 63 db).Do() 64 65 if err != nil { 66 return fmt.Errorf("Error, failed to insert "+ 67 "database %s into instance %s: %s", database_name, 68 instance_name, err) 69 } 70 71 err = sqladminOperationWait(config, op, "Insert Database") 72 73 if err != nil { 74 return fmt.Errorf("Error, failure waiting for insertion of %s "+ 75 "into %s: %s", database_name, instance_name, err) 76 } 77 78 return resourceSqlDatabaseRead(d, meta) 79 } 80 81 func resourceSqlDatabaseRead(d *schema.ResourceData, meta interface{}) error { 82 config := meta.(*Config) 83 84 project, err := getProject(d, config) 85 if err != nil { 86 return err 87 } 88 89 database_name := d.Get("name").(string) 90 instance_name := d.Get("instance").(string) 91 92 db, err := config.clientSqlAdmin.Databases.Get(project, instance_name, 93 database_name).Do() 94 95 if err != nil { 96 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 97 log.Printf("[WARN] Removing SQL Database %q because it's gone", d.Get("name").(string)) 98 // The resource doesn't exist anymore 99 d.SetId("") 100 101 return nil 102 } 103 104 return fmt.Errorf("Error, failed to get"+ 105 "database %s in instance %s: %s", database_name, 106 instance_name, err) 107 } 108 109 d.Set("self_link", db.SelfLink) 110 d.SetId(instance_name + ":" + database_name) 111 112 return nil 113 } 114 115 func resourceSqlDatabaseDelete(d *schema.ResourceData, meta interface{}) error { 116 config := meta.(*Config) 117 118 project, err := getProject(d, config) 119 if err != nil { 120 return err 121 } 122 123 database_name := d.Get("name").(string) 124 instance_name := d.Get("instance").(string) 125 126 op, err := config.clientSqlAdmin.Databases.Delete(project, instance_name, 127 database_name).Do() 128 129 if err != nil { 130 return fmt.Errorf("Error, failed to delete"+ 131 "database %s in instance %s: %s", database_name, 132 instance_name, err) 133 } 134 135 err = sqladminOperationWait(config, op, "Delete Database") 136 137 if err != nil { 138 return fmt.Errorf("Error, failure waiting for deletion of %s "+ 139 "in %s: %s", database_name, instance_name, err) 140 } 141 142 return nil 143 }