From b578b60b88fe84435dbf41da4230b3c2547cbe63 Mon Sep 17 00:00:00 2001 From: protomouse Date: Wed, 31 Aug 2016 10:50:08 +0200 Subject: [PATCH] use hashicorp/go-version to parse mysql server version --- builtin/providers/mysql/provider.go | 46 ++++++------------------ builtin/providers/mysql/resource_user.go | 11 +++--- 2 files changed, 16 insertions(+), 41 deletions(-) diff --git a/builtin/providers/mysql/provider.go b/builtin/providers/mysql/provider.go index c642bd4b6..a9027dc81 100644 --- a/builtin/providers/mysql/provider.go +++ b/builtin/providers/mysql/provider.go @@ -2,9 +2,9 @@ package mysql import ( "fmt" - "strconv" "strings" + "github.com/hashicorp/go-version" mysqlc "github.com/ziutek/mymysql/mysql" mysqlts "github.com/ziutek/mymysql/thrsafe" @@ -13,10 +13,8 @@ import ( ) type providerConfiguration struct { - Conn mysqlc.Conn - VersionMajor uint - VersionMinor uint - VersionPatch uint + Conn mysqlc.Conn + ServerVersion *version.Version } func Provider() terraform.ResourceProvider { @@ -88,16 +86,14 @@ func providerConfigure(d *schema.ResourceData) (interface{}, error) { return nil, err } - major, minor, patch, err := mysqlVersion(conn) + ver, err := serverVersion(conn) if err != nil { return nil, err } return &providerConfiguration{ - Conn: conn, - VersionMajor: major, - VersionMinor: minor, - VersionPatch: patch, + Conn: conn, + ServerVersion: ver, }, nil } @@ -107,36 +103,14 @@ func quoteIdentifier(in string) string { return fmt.Sprintf("`%s`", identQuoteReplacer.Replace(in)) } -func mysqlVersion(conn mysqlc.Conn) (uint, uint, uint, error) { +func serverVersion(conn mysqlc.Conn) (*version.Version, error) { rows, _, err := conn.Query("SELECT VERSION()") if err != nil { - return 0, 0, 0, err + return nil, err } if len(rows) == 0 { - return 0, 0, 0, fmt.Errorf("SELECT VERSION() returned an empty set") + return nil, fmt.Errorf("SELECT VERSION() returned an empty set") } - versionString := rows[0].Str(0) - version := strings.Split(versionString, ".") - invalidVersionErr := fmt.Errorf("Invalid major.minor.patch in %q", versionString) - if len(version) != 3 { - return 0, 0, 0, invalidVersionErr - } - - major, err := strconv.ParseUint(version[0], 10, 32) - if err != nil { - return 0, 0, 0, invalidVersionErr - } - - minor, err := strconv.ParseUint(version[1], 10, 32) - if err != nil { - return 0, 0, 0, invalidVersionErr - } - - patch, err := strconv.ParseUint(version[2], 10, 32) - if err != nil { - return 0, 0, 0, invalidVersionErr - } - - return uint(major), uint(minor), uint(patch), nil + return version.NewVersion(rows[0].Str(0)) } diff --git a/builtin/providers/mysql/resource_user.go b/builtin/providers/mysql/resource_user.go index 6f7d884c6..7cdf5b812 100644 --- a/builtin/providers/mysql/resource_user.go +++ b/builtin/providers/mysql/resource_user.go @@ -4,6 +4,8 @@ import ( "fmt" "log" + "github.com/hashicorp/go-version" + "github.com/hashicorp/terraform/helper/schema" ) @@ -69,15 +71,14 @@ func UpdateUser(d *schema.ResourceData, meta interface{}) error { var stmtSQL string /* ALTER USER syntax introduced in MySQL 5.7.6 deprecates SET PASSWORD (GH-8230) */ - if conf.VersionMajor > 5 || - (conf.VersionMajor == 5 && conf.VersionMinor > 7) || - (conf.VersionMajor == 5 && conf.VersionMinor == 7 && conf.VersionPatch >= 6) { - stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", + ver, _ := version.NewVersion("5.7.6") + if conf.ServerVersion.LessThan(ver) { + stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')", d.Get("user").(string), d.Get("host").(string), newpw.(string)) } else { - stmtSQL = fmt.Sprintf("SET PASSWORD FOR '%s'@'%s' = PASSWORD('%s')", + stmtSQL = fmt.Sprintf("ALTER USER '%s'@'%s' IDENTIFIED BY '%s'", d.Get("user").(string), d.Get("host").(string), newpw.(string))