From 48ed39df5f012897a197ae00981f5ff88220ecad Mon Sep 17 00:00:00 2001 From: winebarrel Date: Wed, 19 Feb 2020 14:10:43 +0900 Subject: [PATCH 1/3] Add "mysql_tables" data source --- mysql/data_source_tables.go | 80 +++++++++++++++++++++++++++++++++++++ mysql/provider.go | 4 ++ 2 files changed, 84 insertions(+) create mode 100644 mysql/data_source_tables.go diff --git a/mysql/data_source_tables.go b/mysql/data_source_tables.go new file mode 100644 index 00000000..853aaf47 --- /dev/null +++ b/mysql/data_source_tables.go @@ -0,0 +1,80 @@ +package mysql + +import ( + "fmt" + "log" + + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" +) + +func dataSourceTables() *schema.Resource { + return &schema.Resource{ + Read: ShowTables, + Schema: map[string]*schema.Schema{ + "database": { + Type: schema.TypeString, + Required: true, + }, + "pattern": { + Type: schema.TypeString, + Optional: true, + }, + "tables": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + }, + } +} + +func ShowTables(d *schema.ResourceData, meta interface{}) error { + db, err := connectToMySQL(meta.(*MySQLConfiguration)) + + if err != nil { + return err + } + + database := d.Get("database").(string) + pattern := d.Get("pattern").(string) + + sql := fmt.Sprintf("SHOW TABLES FROM %s", quoteIdentifier(database)) + + if pattern != "" { + sql += fmt.Sprintf(" LIKE '%s'", pattern) + } + + log.Printf("[DEBUG] SQL: %s", sql) + + rows, err := db.Query(sql) + + if err != nil { + return err + } + + defer rows.Close() + + var tables []string + + for rows.Next() { + var table string + + err := rows.Scan(&table) + + if err != nil { + return err + } + + tables = append(tables, table) + } + + err = d.Set("tables", tables) + + if err != nil { + return err + } + + d.SetId(database) + + return nil +} diff --git a/mysql/provider.go b/mysql/provider.go index 256b6e61..26499230 100644 --- a/mysql/provider.go +++ b/mysql/provider.go @@ -99,6 +99,10 @@ func Provider() terraform.ResourceProvider { }, }, + DataSourcesMap: map[string]*schema.Resource{ + "mysql_tables": dataSourceTables(), + }, + ResourcesMap: map[string]*schema.Resource{ "mysql_database": resourceDatabase(), "mysql_grant": resourceGrant(), From 3735a3766b6dc7d0e769dca1ac79d0ecacc97176 Mon Sep 17 00:00:00 2001 From: winebarrel Date: Wed, 19 Feb 2020 15:46:11 +0900 Subject: [PATCH 2/3] Use resource.UniqueId() --- mysql/data_source_tables.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mysql/data_source_tables.go b/mysql/data_source_tables.go index 853aaf47..d21b1f8d 100644 --- a/mysql/data_source_tables.go +++ b/mysql/data_source_tables.go @@ -4,6 +4,7 @@ import ( "fmt" "log" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/helper/schema" ) @@ -74,7 +75,7 @@ func ShowTables(d *schema.ResourceData, meta interface{}) error { return err } - d.SetId(database) + d.SetId(resource.UniqueId()) return nil } From 1b75adb24526c56778e83e8c5feab23be01fd6db Mon Sep 17 00:00:00 2001 From: winebarrel Date: Wed, 19 Feb 2020 17:49:10 +0900 Subject: [PATCH 3/3] Add mysql_tables data source test --- mysql/data_source_tables_test.go | 79 ++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 mysql/data_source_tables_test.go diff --git a/mysql/data_source_tables_test.go b/mysql/data_source_tables_test.go new file mode 100644 index 00000000..02f1f97f --- /dev/null +++ b/mysql/data_source_tables_test.go @@ -0,0 +1,79 @@ +package mysql + +import ( + "fmt" + "strconv" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func TestAccDataSourceTables(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccTablesConfig_basic("mysql", "%"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.mysql_tables.test", "database", "mysql"), + resource.TestCheckResourceAttr("data.mysql_tables.test", "pattern", "%"), + testAccTablesCount("data.mysql_tables.test", "tables.#", func(rn string, table_count int) error { + if table_count < 1 { + return fmt.Errorf("%s: tables not found", rn) + } + + return nil + }), + ), + }, + { + Config: testAccTablesConfig_basic("mysql", "__table_does_not_exist__"), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data.mysql_tables.test", "database", "mysql"), + resource.TestCheckResourceAttr("data.mysql_tables.test", "pattern", "__table_does_not_exist__"), + testAccTablesCount("data.mysql_tables.test", "tables.#", func(rn string, table_count int) error { + if table_count > 0 { + return fmt.Errorf("%s: unexpected table found", rn) + } + + return nil + }), + ), + }, + }, + }) +} + +func testAccTablesCount(rn string, key string, check func(string, int) error) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[rn] + + if !ok { + return fmt.Errorf("resource not found: %s", rn) + } + + value, ok := rs.Primary.Attributes[key] + + if !ok { + return fmt.Errorf("%s: attribute '%s' not found", rn, key) + } + + table_count, err := strconv.Atoi(value) + + if err != nil { + return err + } + + return check(rn, table_count) + } +} + +func testAccTablesConfig_basic(database string, pattern string) string { + return fmt.Sprintf(` +data "mysql_tables" "test" { + database = "%s" + pattern = "%s" +}`, database, pattern) +}