Skip to content

Commit

Permalink
Merge pull request petoju#22 from winebarrel/add_sql_resource
Browse files Browse the repository at this point in the history
Add mysql_sql resource
  • Loading branch information
winebarrel authored Sep 25, 2020
2 parents 72e0d77 + 8b920ab commit 18e3114
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
1 change: 1 addition & 0 deletions mysql/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func Provider() terraform.ResourceProvider {
"mysql_role": resourceRole(),
"mysql_user": resourceUser(),
"mysql_user_password": resourceUserPassword(),
"mysql_sql": resourceSql(),
},

ConfigureFunc: providerConfigure,
Expand Down
70 changes: 70 additions & 0 deletions mysql/resource_sql.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package mysql

import (
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceSql() *schema.Resource {
return &schema.Resource{
Create: CreateSql,
Read: ReadSql,
Delete: DeleteSql,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"create_sql": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"delete_sql": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func CreateSql(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
name := d.Get("name").(string)
create_sql := d.Get("create_sql").(string)

log.Println("Executing SQL", create_sql)

_, err := db.Exec(create_sql)

if err != nil {
return err
}

d.SetId(name)

return nil
}

func ReadSql(d *schema.ResourceData, meta interface{}) error {
return nil
}

func DeleteSql(d *schema.ResourceData, meta interface{}) error {
db := meta.(*MySQLConfiguration).Db
delete_sql := d.Get("delete_sql").(string)

log.Println("Executing SQL:", delete_sql)

_, err := db.Exec(delete_sql)

if err == nil {
d.SetId("")
}

return err
}

0 comments on commit 18e3114

Please sign in to comment.