github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/mysql/resource_user.go (about)

     1  package mysql
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/go-version"
     8  
     9  	"github.com/hashicorp/terraform/helper/schema"
    10  )
    11  
    12  func resourceUser() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: CreateUser,
    15  		Update: UpdateUser,
    16  		Read:   ReadUser,
    17  		Delete: DeleteUser,
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"user": &schema.Schema{
    21  				Type:     schema.TypeString,
    22  				Required: true,
    23  				ForceNew: true,
    24  			},
    25  
    26  			"host": &schema.Schema{
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				ForceNew: true,
    30  				Default:  "localhost",
    31  			},
    32  
    33  			"password": &schema.Schema{
    34  				Type:      schema.TypeString,
    35  				Optional:  true,
    36  				Sensitive: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func CreateUser(d *schema.ResourceData, meta interface{}) error {
    43  	conn := meta.(*providerConfiguration).Conn
    44  
    45  	stmtSQL := fmt.Sprintf("CREATE USER '%s'@'%s'",
    46  		d.Get("user").(string),
    47  		d.Get("host").(string))
    48  
    49  	password := d.Get("password").(string)
    50  	if password != "" {
    51  		stmtSQL = stmtSQL + fmt.Sprintf(" IDENTIFIED BY '%s'", password)
    52  	}
    53  
    54  	log.Println("Executing statement:", stmtSQL)
    55  	_, _, err := conn.Query(stmtSQL)
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	user := fmt.Sprintf("%s@%s", d.Get("user").(string), d.Get("host").(string))
    61  	d.SetId(user)
    62  
    63  	return nil
    64  }
    65  
    66  func UpdateUser(d *schema.ResourceData, meta interface{}) error {
    67  	conf := meta.(*providerConfiguration)
    68  
    69  	if d.HasChange("password") {
    70  		_, newpw := d.GetChange("password")
    71  		var stmtSQL string
    72  
    73  		/* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */
    74  		ver, _ := version.NewVersion("5.7.6")
    75  		if conf.ServerVersion.LessThan(ver) {
    76  			stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')",
    77  				d.Get("user").(string),
    78  				d.Get("host").(string),
    79  				newpw.(string))
    80  		} else {
    81  			stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'",
    82  				d.Get("user").(string),
    83  				d.Get("host").(string),
    84  				newpw.(string))
    85  		}
    86  
    87  		log.Println("Executing query:", stmtSQL)
    88  		_, _, err := conf.Conn.Query(stmtSQL)
    89  		if err != nil {
    90  			return err
    91  		}
    92  	}
    93  
    94  	return nil
    95  }
    96  
    97  func ReadUser(d *schema.ResourceData, meta interface{}) error {
    98  	// At this time, all attributes are supplied by the user
    99  	return nil
   100  }
   101  
   102  func DeleteUser(d *schema.ResourceData, meta interface{}) error {
   103  	conn := meta.(*providerConfiguration).Conn
   104  
   105  	stmtSQL := fmt.Sprintf("DROP USER '%s'@'%s'",
   106  		d.Get("user").(string),
   107  		d.Get("host").(string))
   108  
   109  	log.Println("Executing statement:", stmtSQL)
   110  
   111  	_, _, err := conn.Query(stmtSQL)
   112  	if err == nil {
   113  		d.SetId("")
   114  	}
   115  	return err
   116  }