Skip to content

Commit f0d041e

Browse files
authored
Implement mysql_databases data source (petoju#160)
1 parent fba6929 commit f0d041e

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

mysql/data_source_databases.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package mysql
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"log"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
)
12+
13+
func dataSourceDatabases() *schema.Resource {
14+
return &schema.Resource{
15+
ReadContext: ShowDatabases,
16+
Schema: map[string]*schema.Schema{
17+
"pattern": {
18+
Type: schema.TypeString,
19+
Optional: true,
20+
},
21+
"databases": {
22+
Type: schema.TypeList,
23+
Computed: true,
24+
Elem: &schema.Schema{Type: schema.TypeString},
25+
},
26+
},
27+
}
28+
}
29+
30+
func ShowDatabases(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
31+
db, err := getDatabaseFromMeta(ctx, meta)
32+
if err != nil {
33+
return diag.FromErr(err)
34+
}
35+
36+
pattern := d.Get("pattern").(string)
37+
38+
sql := fmt.Sprint("SHOW DATABASES")
39+
40+
if pattern != "" {
41+
sql += fmt.Sprintf(" LIKE '%s'", pattern)
42+
}
43+
44+
log.Printf("[DEBUG] SQL: %s", sql)
45+
46+
rows, err := db.QueryContext(ctx, sql)
47+
if err != nil {
48+
return diag.Errorf("failed querying for databases: %v", err)
49+
}
50+
defer rows.Close()
51+
52+
var databases []string
53+
for rows.Next() {
54+
var database string
55+
56+
if err := rows.Scan(&database); err != nil {
57+
return diag.Errorf("failed scanning MySQL rows: %v", err)
58+
}
59+
60+
databases = append(databases, database)
61+
}
62+
63+
if err := d.Set("databases", databases); err != nil {
64+
return diag.Errorf("failed setting databases field: %v", err)
65+
}
66+
67+
d.SetId(id.UniqueId())
68+
69+
return nil
70+
}

mysql/data_source_databases_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package mysql
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
)
11+
12+
func TestAccDataSourceDatabases(t *testing.T) {
13+
resource.Test(t, resource.TestCase{
14+
PreCheck: func() { testAccPreCheck(t) },
15+
ProviderFactories: testAccProviderFactories,
16+
Steps: []resource.TestStep{
17+
{
18+
Config: testAccDatabasesConfigBasic("%"),
19+
Check: resource.ComposeTestCheckFunc(
20+
resource.TestCheckResourceAttr("data.mysql_databases.test", "pattern", "%"),
21+
testAccDatabasesCount("data.mysql_databases.test", "databases.#", func(rn string, databaseCount int) error {
22+
if databaseCount < 1 {
23+
return fmt.Errorf("%s: databases not found", rn)
24+
}
25+
26+
return nil
27+
}),
28+
),
29+
},
30+
{
31+
Config: testAccDatabasesConfigBasic("__database_does_not_exist__"),
32+
Check: resource.ComposeTestCheckFunc(
33+
resource.TestCheckResourceAttr("data.mysql_databases.test", "pattern", "__database_does_not_exist__"),
34+
testAccDatabasesCount("data.mysql_databases.test", "databases.#", func(rn string, databaseCount int) error {
35+
if databaseCount > 0 {
36+
return fmt.Errorf("%s: unexpected database found", rn)
37+
}
38+
39+
return nil
40+
}),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func testAccDatabasesCount(rn string, key string, check func(string, int) error) resource.TestCheckFunc {
48+
return func(s *terraform.State) error {
49+
rs, ok := s.RootModule().Resources[rn]
50+
51+
if !ok {
52+
return fmt.Errorf("resource not found: %s", rn)
53+
}
54+
55+
value, ok := rs.Primary.Attributes[key]
56+
57+
if !ok {
58+
return fmt.Errorf("%s: attribute '%s' not found", rn, key)
59+
}
60+
61+
databaseCount, err := strconv.Atoi(value)
62+
63+
if err != nil {
64+
return err
65+
}
66+
67+
return check(rn, databaseCount)
68+
}
69+
}
70+
71+
func testAccDatabasesConfigBasic(pattern string) string {
72+
return fmt.Sprintf(`
73+
data "mysql_databases" "test" {
74+
pattern = "%s"
75+
}`, pattern)
76+
}

mysql/provider.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,8 @@ func Provider() *schema.Provider {
241241
},
242242

243243
DataSourcesMap: map[string]*schema.Resource{
244-
"mysql_tables": dataSourceTables(),
244+
"mysql_databases": dataSourceDatabases(),
245+
"mysql_tables": dataSourceTables(),
245246
},
246247

247248
ResourcesMap: map[string]*schema.Resource{
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
layout: "mysql"
3+
page_title: "MySQL: mysql_databases"
4+
sidebar_current: "docs-mysql-datasource-databases"
5+
description: |-
6+
Gets databases on a MySQL server.
7+
---
8+
9+
# Data Source: mysql\_databases
10+
11+
The ``mysql_databases`` gets databases on a MySQL
12+
server.
13+
14+
## Example Usage
15+
16+
```hcl
17+
data "mysql_databases" "app" {
18+
pattern = "test_%"
19+
}
20+
```
21+
22+
## Argument Reference
23+
24+
The following arguments are supported:
25+
26+
* `pattern` - (Optional) Patterns for searching databases.
27+
28+
## Attributes Reference
29+
30+
The following attributes are exported:
31+
32+
* `databases` - The list of the database names.

website/mysql.erb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@
4040
<a href="#">Data Sources</a>
4141
<ul class="nav nav-visible">
4242

43+
<li<%= sidebar_current("docs-mysql-datasource-databases") %>>
44+
<a href="/docs/providers/mysql/d/databases.html">mysql_databases</a>
45+
</li>
46+
4347
<li<%= sidebar_current("docs-mysql-datasource-tables") %>>
4448
<a href="/docs/providers/mysql/d/tables.html">mysql_tables</a>
4549
</li>

0 commit comments

Comments
 (0)