forked from petoju/terraform-provider-mysql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
74708bb
commit 48ed39d
Showing
2 changed files
with
84 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters