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