github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/google/resource_sql_user.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 resourceSqlUser() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceSqlUserCreate, 16 Read: resourceSqlUserRead, 17 Update: resourceSqlUserUpdate, 18 Delete: resourceSqlUserDelete, 19 20 Schema: map[string]*schema.Schema{ 21 "host": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 ForceNew: true, 25 }, 26 27 "instance": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 ForceNew: true, 31 }, 32 33 "name": &schema.Schema{ 34 Type: schema.TypeString, 35 Required: true, 36 ForceNew: true, 37 }, 38 39 "password": &schema.Schema{ 40 Type: schema.TypeString, 41 Required: true, 42 }, 43 44 "project": &schema.Schema{ 45 Type: schema.TypeString, 46 Optional: true, 47 ForceNew: true, 48 }, 49 }, 50 } 51 } 52 53 func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error { 54 config := meta.(*Config) 55 56 project, err := getProject(d, config) 57 if err != nil { 58 return err 59 } 60 61 name := d.Get("name").(string) 62 instance := d.Get("instance").(string) 63 password := d.Get("password").(string) 64 host := d.Get("host").(string) 65 66 user := &sqladmin.User{ 67 Name: name, 68 Instance: instance, 69 Password: password, 70 Host: host, 71 } 72 73 op, err := config.clientSqlAdmin.Users.Insert(project, instance, 74 user).Do() 75 76 if err != nil { 77 return fmt.Errorf("Error, failed to insert "+ 78 "user %s into instance %s: %s", name, instance, err) 79 } 80 81 err = sqladminOperationWait(config, op, "Insert User") 82 83 if err != nil { 84 return fmt.Errorf("Error, failure waiting for insertion of %s "+ 85 "into %s: %s", name, instance, err) 86 } 87 88 return resourceSqlUserRead(d, meta) 89 } 90 91 func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error { 92 config := meta.(*Config) 93 94 project, err := getProject(d, config) 95 if err != nil { 96 return err 97 } 98 99 name := d.Get("name").(string) 100 instance := d.Get("instance").(string) 101 102 users, err := config.clientSqlAdmin.Users.List(project, instance).Do() 103 104 if err != nil { 105 if gerr, ok := err.(*googleapi.Error); ok && gerr.Code == 404 { 106 log.Printf("[WARN] Removing SQL User %q because it's gone", d.Get("name").(string)) 107 d.SetId("") 108 109 return nil 110 } 111 112 return fmt.Errorf("Error, failed to get user %s in instance %s: %s", name, instance, err) 113 } 114 115 found := false 116 for _, user := range users.Items { 117 if user.Name == name { 118 found = true 119 break 120 } 121 } 122 123 if !found { 124 log.Printf("[WARN] Removing SQL User %q because it's gone", d.Get("name").(string)) 125 d.SetId("") 126 127 return nil 128 } 129 130 d.SetId(name) 131 132 return nil 133 } 134 135 func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error { 136 config := meta.(*Config) 137 138 if d.HasChange("password") { 139 project, err := getProject(d, config) 140 if err != nil { 141 return err 142 } 143 144 name := d.Get("name").(string) 145 instance := d.Get("instance").(string) 146 host := d.Get("host").(string) 147 password := d.Get("password").(string) 148 149 user := &sqladmin.User{ 150 Name: name, 151 Instance: instance, 152 Password: password, 153 Host: host, 154 } 155 156 op, err := config.clientSqlAdmin.Users.Update(project, instance, host, name, 157 user).Do() 158 159 if err != nil { 160 return fmt.Errorf("Error, failed to update"+ 161 "user %s into user %s: %s", name, instance, err) 162 } 163 164 err = sqladminOperationWait(config, op, "Insert User") 165 166 if err != nil { 167 return fmt.Errorf("Error, failure waiting for update of %s "+ 168 "in %s: %s", name, instance, err) 169 } 170 171 return resourceSqlUserRead(d, meta) 172 } 173 174 return nil 175 } 176 177 func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error { 178 config := meta.(*Config) 179 180 project, err := getProject(d, config) 181 if err != nil { 182 return err 183 } 184 185 name := d.Get("name").(string) 186 instance := d.Get("instance").(string) 187 host := d.Get("host").(string) 188 189 op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do() 190 191 if err != nil { 192 return fmt.Errorf("Error, failed to delete"+ 193 "user %s in instance %s: %s", name, 194 instance, err) 195 } 196 197 err = sqladminOperationWait(config, op, "Delete User") 198 199 if err != nil { 200 return fmt.Errorf("Error, failure waiting for deletion of %s "+ 201 "in %s: %s", name, instance, err) 202 } 203 204 return nil 205 }