Skip to content

Commit

Permalink
Introduce a mysql_role resource for creating MySQL roles (petoju#53)
Browse files Browse the repository at this point in the history
  • Loading branch information
Joe Stump authored Nov 7, 2018
1 parent b9ec6e7 commit e639b83
Show file tree
Hide file tree
Showing 11 changed files with 694 additions and 117 deletions.
47 changes: 33 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
dist: trusty

sudo: required

language: go
services:
- docker
- mysql

go:
- 1.10.x
- 1.10.x

env:
- DB=mysql:5.6 DB_EXTRA=''
- DB=mysql:5.7 DB_EXTRA=''
- DB=mysql:8.0 DB_EXTRA='mysqld --default-authentication-plugin=mysql_native_password'

services:
- docker

script:
- test -d /home/travis/gopath/src/github.com/terraform-providers || mv $(dirname $PWD) /home/travis/gopath/src/github.com/terraform-providers
- make test
- make vet
- make website-test
- export MYSQL_USERNAME=root
- export MYSQL_ENDPOINT=localhost:3306
- export MYSQL_PASSWORD=
- mysql -u root -e "INSTALL PLUGIN mysql_no_login SONAME 'mysql_no_login.so';"
- make testacc
- test -d /home/travis/gopath/src/github.com/terraform-providers || mv $(dirname $PWD) /home/travis/gopath/src/github.com/terraform-providers
- make test
- make vet
- make website-test
- sudo service mysql stop
- export MYSQL_HOST=127.0.0.1
- export MYSQL_PORT=3306
- export MYSQL_USERNAME=root
- export MYSQL_ENDPOINT="${MYSQL_HOST}:${MYSQL_PORT}"
- export MYSQL_PASSWORD=''
- docker pull $DB
- docker run -d -p ${MYSQL_HOST}:${MYSQL_PORT}:${MYSQL_PORT} -e MYSQL_ALLOW_EMPTY_PASSWORD=1 -e MYSQL_ROOT_PASSWORD='' $DB $DB_EXTRA
- docker ps -a
- |
while ! mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USERNAME} -e 'SELECT 1'; do
echo 'Waiting for MySQL...'
sleep 1;
done;
- mysql -h${MYSQL_HOST} -P${MYSQL_PORT} -u${MYSQL_USERNAME} -e "INSTALL PLUGIN mysql_no_login SONAME 'mysql_no_login.so';"
- make testacc

matrix:
fast_finish: true
allow_failures:
- go: tip
- go: tip
13 changes: 9 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,20 @@ BUG FIXES:

* Lazily connect to MySQL servers. ([#43](https://github.com/terraform-providers/terraform-provider-mysql/issues/43))
* Add retries to MySQL server connection logic. ([#43](https://github.com/terraform-providers/terraform-provider-mysql/issues/43))
* Migrated to Go modules for dependencies and `vendor/` management.
* Migrated to Go modules for dependencies and `vendor/` management. ([#44](https://github.com/terraform-providers/terraform-provider-mysql/issues/44))

IMPROVEMENTS:

* Provider now has a `tls` option that configures TSL for server connections. ([#43](https://github.com/terraform-providers/terraform-provider-mysql/issues/43))
* `r/mysql_user`: Added the `tls_option` attribute, which allows to restrict the MySQL users to a specific MySQL-TLS-Encryption. ([#26](https://github.com/terraform-providers/terraform-provider-mysql/issues/40))
* `r/mysql_grant`: Added the `tls_option` attribute, which allows to restrict the MySQL grant to a specific MySQL-TLS-Encryption. ([#26](https://github.com/terraform-providers/terraform-provider-mysql/issues/40))
* Provider now supports MySQL 8. ([#53](https://github.com/terraform-providers/terraform-provider-mysql/issues/53))
* Acceptance tests now ran against MySQL 5.6, 5.7, and 8.0.
* Provider now has a `tls` argument that configures TSL for server connections. ([#43](https://github.com/terraform-providers/terraform-provider-mysql/issues/43))
* `r/mysql_user`: Added the `tls_option` argument, which allows to restrict the MySQL users to a specific MySQL-TLS-Encryption. ([#26](https://github.com/terraform-providers/terraform-provider-mysql/issues/40))
* `r/mysql_grant`: Added the `tls_option` argument, which allows to restrict the MySQL grant to a specific MySQL-TLS-Encryption. ([#26](https://github.com/terraform-providers/terraform-provider-mysql/issues/40))
* `r/mysql_grant`: Added a `table` argument that allows `GRANT` statements to be scoped to a single table. ([#39](https://github.com/terraform-providers/terraform-provider-mysql/issues/30))
* `r/mysql_grant`: Added a `role` argument that allows `GRANT` assign privileges to roles. ([#53](https://github.com/terraform-providers/terraform-provider-mysql/issues/53))
* `r/mysql_grant`: Added a `roles` argument that allows `GRANT` assign roles to a user. ([#53](https://github.com/terraform-providers/terraform-provider-mysql/issues/53))
* `r/mysql_user_password`: Manages a PGP encrypted randomly assigned password for the given MySQL user. ([#50](https://github.com/terraform-providers/terraform-provider-mysql/issues/50))
* `r/mysql_role`: New resource for managing MySQL roles. ([#48](https://github.com/terraform-providers/terraform-provider-mysql/issues/48))

## 1.1.0 (March 28, 2018)

Expand Down
1 change: 1 addition & 0 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ func Provider() terraform.ResourceProvider {
ResourcesMap: map[string]*schema.Resource{
"mysql_database": resourceDatabase(),
"mysql_grant": resourceGrant(),
"mysql_role": resourceRole(),
"mysql_user": resourceUser(),
"mysql_user_password": resourceUserPassword(),
},
Expand Down
23 changes: 19 additions & 4 deletions mysql/resource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"strings"

"github.com/go-sql-driver/mysql"

"github.com/hashicorp/go-version"
"github.com/hashicorp/terraform/helper/schema"
)

Expand Down Expand Up @@ -118,12 +118,27 @@ func ReadDatabase(d *schema.ResourceData, meta interface{}) error {
// hunt for the default.
stmtSQL := "SHOW COLLATION WHERE `Charset` = ? AND `Default` = 'Yes'"
var empty interface{}
err := db.QueryRow(stmtSQL, defaultCharset).Scan(&defaultCollation, &empty, &empty, &empty, &empty, &empty)

requiredVersion, _ := version.NewVersion("8.0.0")
currentVersion, err := serverVersion(db)
if err != nil {
if err == sql.ErrNoRows {
return err
}

// MySQL 8 returns more data in a row.
var res error
if currentVersion.GreaterThan(requiredVersion) {
res = db.QueryRow(stmtSQL, defaultCharset).Scan(&defaultCollation, &empty, &empty, &empty, &empty, &empty, &empty)
} else {
res = db.QueryRow(stmtSQL, defaultCharset).Scan(&defaultCollation, &empty, &empty, &empty, &empty, &empty)
}

if res != nil {
if res == sql.ErrNoRows {
return fmt.Errorf("Charset %s has no default collation", defaultCharset)
}
return fmt.Errorf("Error getting default charset: %s, %s", err, defaultCharset)

return fmt.Errorf("Error getting default charset: %s, %s", res, defaultCharset)
}
}

Expand Down
Loading

0 comments on commit e639b83

Please sign in to comment.