2016-10-04 12:05:51 +02:00
|
|
|
package postgresql
|
|
|
|
|
|
|
|
import (
|
2016-11-02 15:36:39 +01:00
|
|
|
"database/sql"
|
2016-10-04 12:05:51 +02:00
|
|
|
"fmt"
|
2016-12-12 10:33:02 +01:00
|
|
|
"log"
|
2016-10-04 12:05:51 +02:00
|
|
|
|
|
|
|
"github.com/hashicorp/errwrap"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/lib/pq"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourcePostgreSQLExtension() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourcePostgreSQLExtensionCreate,
|
2016-11-02 15:36:39 +01:00
|
|
|
Read: resourcePostgreSQLExtensionRead,
|
2016-10-04 12:05:51 +02:00
|
|
|
Delete: resourcePostgreSQLExtensionDelete,
|
2016-12-12 10:33:02 +01:00
|
|
|
Importer: &schema.ResourceImporter{
|
|
|
|
State: schema.ImportStatePassthrough,
|
|
|
|
},
|
2016-10-04 12:05:51 +02:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"name": {
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePostgreSQLExtensionCreate(d *schema.ResourceData, meta interface{}) error {
|
2016-12-12 10:33:02 +01:00
|
|
|
c := meta.(*Client)
|
|
|
|
conn, err := c.Connect()
|
2016-10-04 12:05:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
extensionName := d.Get("name").(string)
|
|
|
|
|
|
|
|
query := fmt.Sprintf("CREATE EXTENSION %s", pq.QuoteIdentifier(extensionName))
|
|
|
|
_, err = conn.Query(query)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error creating extension: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(extensionName)
|
|
|
|
|
2016-11-02 15:36:39 +01:00
|
|
|
return resourcePostgreSQLExtensionRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePostgreSQLExtensionRead(d *schema.ResourceData, meta interface{}) error {
|
2016-12-12 10:33:02 +01:00
|
|
|
c := meta.(*Client)
|
|
|
|
conn, err := c.Connect()
|
2016-11-02 15:36:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
2016-12-12 10:33:02 +01:00
|
|
|
dbId := d.Id()
|
2016-11-02 15:36:39 +01:00
|
|
|
extensionName := d.Get("name").(string)
|
|
|
|
|
|
|
|
var hasExtension bool
|
2016-12-12 10:33:02 +01:00
|
|
|
err = conn.QueryRow("SELECT TRUE from pg_catalog.pg_extension d WHERE extname=$1", dbId).Scan(&hasExtension)
|
2016-11-02 15:36:39 +01:00
|
|
|
switch {
|
|
|
|
case err == sql.ErrNoRows:
|
2016-12-12 10:33:02 +01:00
|
|
|
log.Printf("[WARN] PostgreSQL extension (%s) not found", d.Id())
|
2016-11-02 15:36:39 +01:00
|
|
|
d.SetId("")
|
|
|
|
return nil
|
|
|
|
case err != nil:
|
|
|
|
return errwrap.Wrapf("Error reading extension: {{err}}", err)
|
|
|
|
default:
|
|
|
|
d.Set("extension", hasExtension)
|
2016-12-12 10:33:02 +01:00
|
|
|
d.SetId(extensionName)
|
2016-11-02 15:36:39 +01:00
|
|
|
return nil
|
|
|
|
}
|
2016-10-04 12:05:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourcePostgreSQLExtensionDelete(d *schema.ResourceData, meta interface{}) error {
|
2016-12-12 10:33:02 +01:00
|
|
|
c := meta.(*Client)
|
|
|
|
conn, err := c.Connect()
|
2016-10-04 12:05:51 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer conn.Close()
|
|
|
|
|
|
|
|
extensionName := d.Get("name").(string)
|
|
|
|
|
|
|
|
query := fmt.Sprintf("DROP EXTENSION %s", pq.QuoteIdentifier(extensionName))
|
|
|
|
_, err = conn.Query(query)
|
|
|
|
if err != nil {
|
|
|
|
return errwrap.Wrapf("Error deleting extension: {{err}}", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId("")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|