github.com/koding/terraform@v0.6.4-0.20170608090606-5d7e0339779d/builtin/providers/google/resource_sql_user.go (about)

     1  package google
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"google.golang.org/api/sqladmin/v1beta4"
    10  )
    11  
    12  func resourceSqlUser() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceSqlUserCreate,
    15  		Read:   resourceSqlUserRead,
    16  		Update: resourceSqlUserUpdate,
    17  		Delete: resourceSqlUserDelete,
    18  		Importer: &schema.ResourceImporter{
    19  			State: schema.ImportStatePassthrough,
    20  		},
    21  
    22  		SchemaVersion: 1,
    23  		MigrateState:  resourceSqlUserMigrateState,
    24  
    25  		Schema: map[string]*schema.Schema{
    26  			"host": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"instance": &schema.Schema{
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  
    38  			"name": &schema.Schema{
    39  				Type:     schema.TypeString,
    40  				Required: true,
    41  				ForceNew: true,
    42  			},
    43  
    44  			"password": &schema.Schema{
    45  				Type:      schema.TypeString,
    46  				Required:  true,
    47  				Sensitive: true,
    48  			},
    49  
    50  			"project": &schema.Schema{
    51  				Type:     schema.TypeString,
    52  				Optional: true,
    53  				ForceNew: true,
    54  			},
    55  		},
    56  	}
    57  }
    58  
    59  func resourceSqlUserCreate(d *schema.ResourceData, meta interface{}) error {
    60  	config := meta.(*Config)
    61  
    62  	project, err := getProject(d, config)
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	name := d.Get("name").(string)
    68  	instance := d.Get("instance").(string)
    69  	password := d.Get("password").(string)
    70  	host := d.Get("host").(string)
    71  
    72  	user := &sqladmin.User{
    73  		Name:     name,
    74  		Instance: instance,
    75  		Password: password,
    76  		Host:     host,
    77  	}
    78  
    79  	mutexKV.Lock(instanceMutexKey(project, instance))
    80  	defer mutexKV.Unlock(instanceMutexKey(project, instance))
    81  	op, err := config.clientSqlAdmin.Users.Insert(project, instance,
    82  		user).Do()
    83  
    84  	if err != nil {
    85  		return fmt.Errorf("Error, failed to insert "+
    86  			"user %s into instance %s: %s", name, instance, err)
    87  	}
    88  
    89  	d.SetId(fmt.Sprintf("%s/%s", instance, name))
    90  
    91  	err = sqladminOperationWait(config, op, "Insert User")
    92  
    93  	if err != nil {
    94  		return fmt.Errorf("Error, failure waiting for insertion of %s "+
    95  			"into %s: %s", name, instance, err)
    96  	}
    97  
    98  	return resourceSqlUserRead(d, meta)
    99  }
   100  
   101  func resourceSqlUserRead(d *schema.ResourceData, meta interface{}) error {
   102  	config := meta.(*Config)
   103  
   104  	project, err := getProject(d, config)
   105  	if err != nil {
   106  		return err
   107  	}
   108  
   109  	instanceAndName := strings.SplitN(d.Id(), "/", 2)
   110  	if len(instanceAndName) != 2 {
   111  		return fmt.Errorf(
   112  			"Wrong number of arguments when specifying imported id. Expected: 2.  Saw: %d. Expected Input: $INSTANCENAME/$SQLUSERNAME Input: %s",
   113  			len(instanceAndName),
   114  			d.Id())
   115  	}
   116  
   117  	instance := instanceAndName[0]
   118  	name := instanceAndName[1]
   119  
   120  	users, err := config.clientSqlAdmin.Users.List(project, instance).Do()
   121  
   122  	if err != nil {
   123  		return handleNotFoundError(err, d, fmt.Sprintf("SQL User %q in instance %q", name, instance))
   124  	}
   125  
   126  	var user *sqladmin.User
   127  	for _, currentUser := range users.Items {
   128  		if currentUser.Name == name {
   129  			user = currentUser
   130  			break
   131  		}
   132  	}
   133  
   134  	if user == nil {
   135  		log.Printf("[WARN] Removing SQL User %q because it's gone", d.Get("name").(string))
   136  		d.SetId("")
   137  
   138  		return nil
   139  	}
   140  
   141  	d.Set("host", user.Host)
   142  	d.Set("instance", user.Instance)
   143  	d.Set("name", user.Name)
   144  	return nil
   145  }
   146  
   147  func resourceSqlUserUpdate(d *schema.ResourceData, meta interface{}) error {
   148  	config := meta.(*Config)
   149  
   150  	if d.HasChange("password") {
   151  		project, err := getProject(d, config)
   152  		if err != nil {
   153  			return err
   154  		}
   155  
   156  		name := d.Get("name").(string)
   157  		instance := d.Get("instance").(string)
   158  		host := d.Get("host").(string)
   159  		password := d.Get("password").(string)
   160  
   161  		user := &sqladmin.User{
   162  			Name:     name,
   163  			Instance: instance,
   164  			Password: password,
   165  			Host:     host,
   166  		}
   167  
   168  		mutexKV.Lock(instanceMutexKey(project, instance))
   169  		defer mutexKV.Unlock(instanceMutexKey(project, instance))
   170  		op, err := config.clientSqlAdmin.Users.Update(project, instance, host, name,
   171  			user).Do()
   172  
   173  		if err != nil {
   174  			return fmt.Errorf("Error, failed to update"+
   175  				"user %s into user %s: %s", name, instance, err)
   176  		}
   177  
   178  		err = sqladminOperationWait(config, op, "Insert User")
   179  
   180  		if err != nil {
   181  			return fmt.Errorf("Error, failure waiting for update of %s "+
   182  				"in %s: %s", name, instance, err)
   183  		}
   184  
   185  		return resourceSqlUserRead(d, meta)
   186  	}
   187  
   188  	return nil
   189  }
   190  
   191  func resourceSqlUserDelete(d *schema.ResourceData, meta interface{}) error {
   192  	config := meta.(*Config)
   193  
   194  	project, err := getProject(d, config)
   195  	if err != nil {
   196  		return err
   197  	}
   198  
   199  	name := d.Get("name").(string)
   200  	instance := d.Get("instance").(string)
   201  	host := d.Get("host").(string)
   202  
   203  	mutexKV.Lock(instanceMutexKey(project, instance))
   204  	defer mutexKV.Unlock(instanceMutexKey(project, instance))
   205  	op, err := config.clientSqlAdmin.Users.Delete(project, instance, host, name).Do()
   206  
   207  	if err != nil {
   208  		return fmt.Errorf("Error, failed to delete"+
   209  			"user %s in instance %s: %s", name,
   210  			instance, err)
   211  	}
   212  
   213  	err = sqladminOperationWait(config, op, "Delete User")
   214  
   215  	if err != nil {
   216  		return fmt.Errorf("Error, failure waiting for deletion of %s "+
   217  			"in %s: %s", name, instance, err)
   218  	}
   219  
   220  	return nil
   221  }