2016-01-13 22:33:08 +01:00
|
|
|
package google
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2017-05-22 22:43:11 +02:00
|
|
|
"strings"
|
2016-01-13 22:33:08 +01:00
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"google.golang.org/api/sqladmin/v1beta4"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceSqlUser() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceSqlUserCreate,
|
|
|
|
Read: resourceSqlUserRead,
|
|
|
|
Update: resourceSqlUserUpdate,
|
|
|
|
Delete: resourceSqlUserDelete,
|
2017-05-22 22:43:11 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
|
|
|
|
|
|
|
SchemaVersion: 1,
|
|
|
|
MigrateState: resourceSqlUserMigrateState,
|
2016-01-13 22:33:08 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2016-04-10 23:34:15 +02:00
|
|
|
"host": &schema.Schema{
|
2016-01-13 22:33:08 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"instance": &schema.Schema{
|
2016-01-13 22:33:08 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
2016-04-10 23:34:15 +02:00
|
|
|
ForceNew: true,
|
2016-01-13 22:33:08 +01:00
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"name": &schema.Schema{
|
2016-01-13 22:33:08 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2016-04-10 23:34:15 +02:00
|
|
|
"password": &schema.Schema{
|
2017-05-22 22:43:11 +02:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
Sensitive: true,
|
2016-01-13 22:33:08 +01:00
|
|
|
},
|
2016-04-10 18:59:57 +02:00
|
|
|
|
|
|
|
"project": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2016-01-13 22:33:08 +01:00
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:33:08 +01:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
instance := d.Get("instance").(string)
|
|
|
|
password := d.Get("password").(string)
|
|
|
|
host := d.Get("host").(string)
|
|
|
|
|
|
|
|
user := &sqladmin.User{
|
|
|
|
Name: name,
|
|
|
|
Instance: instance,
|
|
|
|
Password: password,
|
|
|
|
Host: host,
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientSqlAdmin.Users.Insert(project, instance,
|
|
|
|
user).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to insert "+
|
|
|
|
"user %s into instance %s: %s", name, instance, err)
|
|
|
|
}
|
|
|
|
|
2017-05-22 22:43:11 +02:00
|
|
|
d.SetId(fmt.Sprintf("%s/%s", instance, name))
|
|
|
|
|
2016-01-13 22:33:08 +01:00
|
|
|
err = sqladminOperationWait(config, op, "Insert User")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failure waiting for insertion of %s "+
|
|
|
|
"into %s: %s", name, instance, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceSqlUserRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-05-22 22:43:11 +02:00
|
|
|
instanceAndName := strings.SplitN(d.Id(), "/", 2)
|
|
|
|
if len(instanceAndName) != 2 {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Wrong number of arguments when specifying imported id. Expected: 2. Saw: %d. Expected Input: $INSTANCENAME/$SQLUSERNAME Input: %s",
|
|
|
|
len(instanceAndName),
|
|
|
|
d.Id())
|
|
|
|
}
|
|
|
|
|
|
|
|
instance := instanceAndName[0]
|
|
|
|
name := instanceAndName[1]
|
2016-01-13 22:33:08 +01:00
|
|
|
|
|
|
|
users, err := config.clientSqlAdmin.Users.List(project, instance).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
2017-05-10 01:00:47 +02:00
|
|
|
return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
|
2016-01-13 22:33:08 +01:00
|
|
|
}
|
|
|
|
|
2017-05-22 22:43:11 +02:00
|
|
|
var user *sqladmin.User
|
|
|
|
for _, currentUser := range users.Items {
|
|
|
|
if currentUser.Name == name {
|
|
|
|
user = currentUser
|
2016-01-13 22:33:08 +01:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-22 22:43:11 +02:00
|
|
|
if user == nil {
|
2016-01-13 22:33:08 +01:00
|
|
|
log.Printf("[WARN] Removing SQL User %q because it's gone", d.Get("name").(string))
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-05-22 22:43:11 +02:00
|
|
|
d.Set("host", user.Host)
|
|
|
|
d.Set("instance", user.Instance)
|
|
|
|
d.Set("name", user.Name)
|
2016-01-13 22:33:08 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
|
|
|
if d.HasChange("password") {
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:33:08 +01:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
instance := d.Get("instance").(string)
|
|
|
|
host := d.Get("host").(string)
|
|
|
|
password := d.Get("password").(string)
|
|
|
|
|
|
|
|
user := &sqladmin.User{
|
|
|
|
Name: name,
|
|
|
|
Instance: instance,
|
|
|
|
Password: password,
|
|
|
|
Host: host,
|
|
|
|
}
|
|
|
|
|
|
|
|
op, err := config.clientSqlAdmin.Users.Update(project, instance, host, name,
|
|
|
|
user).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to update"+
|
|
|
|
"user %s into user %s: %s", name, instance, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqladminOperationWait(config, op, "Insert User")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failure waiting for update of %s "+
|
|
|
|
"in %s: %s", name, instance, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceSqlUserRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
config := meta.(*Config)
|
|
|
|
|
2016-04-10 18:59:57 +02:00
|
|
|
project, err := getProject(d, config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-01-13 22:33:08 +01:00
|
|
|
name := d.Get("name").(string)
|
|
|
|
instance := d.Get("instance").(string)
|
|
|
|
host := d.Get("host").(string)
|
|
|
|
|
|
|
|
op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failed to delete"+
|
|
|
|
"user %s in instance %s: %s", name,
|
|
|
|
instance, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = sqladminOperationWait(config, op, "Delete User")
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error, failure waiting for deletion of %s "+
|
|
|
|
"in %s: %s", name, instance, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|