github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/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 conn := meta.(*providerConfiguration).Conn 99 100 stmtSQL := fmt.Sprintf("SELECT USER FROM mysql.user WHERE USER='%s'", 101 d.Get("user").(string)) 102 103 log.Println("Executing statement:", stmtSQL) 104 105 rows, _, err := conn.Query(stmtSQL) 106 log.Println("Returned rows:", len(rows)) 107 if err != nil { 108 return err 109 } 110 if len(rows) == 0 { 111 d.SetId("") 112 } 113 return nil 114 } 115 116 func DeleteUser(d *schema.ResourceData, meta interface{}) error { 117 conn := meta.(*providerConfiguration).Conn 118 119 stmtSQL := fmt.Sprintf("DROP USER '%s'@'%s'", 120 d.Get("user").(string), 121 d.Get("host").(string)) 122 123 log.Println("Executing statement:", stmtSQL) 124 125 _, _, err := conn.Query(stmtSQL) 126 if err == nil { 127 d.SetId("") 128 } 129 return err 130 }