hashicorp / terraform-provider-mysql

Terraform MySQL provider – This Terraform provider is archived per our provider archiving process: https://terraform.io/docs/internals/archiving.html
https://www.terraform.io/docs/providers/mysql/
Mozilla Public License 2.0
61 stars 187 forks source link

Order of creating users and privileges #42

Closed hfm closed 6 years ago

hfm commented 6 years ago

When attempting to apply mysql_user and mysql_grant resources at once, it creates user but fails to give privileges to users.

Terraform Version

Terraform v0.11.8

Affected Resource(s)

Terraform Configuration Files

provider "mysql" {
  endpoint = "0.0.0.0:33060"
  username = "root"
}

resource "mysql_user" "hfm" {
  user = "hfm"
  host = "%"
  plaintext_password = "samplehfmpassword"
}

resource "mysql_grant" "hfm" {
  user = "hfm"
  host = "%"
  database = "sample"
  privileges = ["ALL"]
}

docker-compose.yml

version: '3'
services:
  mysql:
    image: mysql:5.7
    ports:
      - 33060:3306
    environment:
      MYSQL_DATABASE: 'sample'
      MYSQL_ALLOW_EMPTY_PASSWORD: 'yes'

Debug Output

https://gist.github.com/hfm/97e431f9bfe9eb14bd6e3674fe257ea6

Expected Behavior

Terraform should generate and display a plan successfully.

Actual Behavior

Receiving the following error message:

Error: Error applying plan:

1 error(s) occurred:

* mysql_grant.hfm: 1 error(s) occurred:

* mysql_grant.hfm: Error 1133: Can't find any matching row in the user table

Steps to Reproduce

  1. docker-compose up -d
  2. terraform apply
joestump commented 6 years ago

This is because there's no graph dependency between the two resources. If you change your mysql_grant to look like below it should work.

resource "mysql_grant" "hfm" {
  user = "${mysql_user.hfm.user}"
  host = "%"
  database = "sample"
  privileges = ["ALL"]
}

This will cause mysql_grant.hfm to become dependent on mysql_user.hfm.