Skip to content
This repository was archived by the owner on Jan 17, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions docs/data-sources/database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "redshift_database Data Source - terraform-provider-redshift"
subcategory: ""
description: |-
Fetches information about a Redshift database.
---

# redshift_database (Data Source)

Fetches information about a Redshift database.

## Example Usage

```terraform
data "redshift_database" "database" {
name = "my_database"
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) Name of the database

### Optional

- **datashare_source** (Block List, Max: 1) Configuration for a database created from a redshift datashare. (see [below for nested schema](#nestedblock--datashare_source))
- **id** (String) The ID of this resource.

### Read-Only

- **connection_limit** (Number) The maximum number of concurrent connections that can be made to this database. A value of -1 means no limit.
- **owner** (String) Owner of the database, usually the user who created it

<a id="nestedblock--datashare_source"></a>
### Nested Schema for `datashare_source`

Optional:

- **account_id** (String) The AWS account ID of the producer cluster.
- **namespace** (String) The namespace (guid) of the producer cluster
- **share_name** (String) The name of the datashare on the producer cluster


69 changes: 69 additions & 0 deletions docs/resources/database.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "redshift_database Resource - terraform-provider-redshift"
subcategory: ""
description: |-
Defines a local database.
---

# redshift_database (Resource)

Defines a local database.

## Example Usage

```terraform
# Example resource declaration of a local database
resource "redshift_database" "db" {
name = "my_database"
owner = "my_user"
connection_limit = 123456 # use -1 for unlimited

lifecycle {
prevent_destroy = true
}
}


# Example resource declaration of a database
# created from a datashare of another redshift cluster
resource "redshift_database" "datashare_db" {
name = "my_datashare_consumer_db"
owner = "my_user"
connection_limit = 123456 # use -1 for unlimited

datashare_source {
share_name = "my_datashare"
account_id = "123456789012" # 12 digit AWS account number of the producer cluster (optional, default is current account)
namespace = "00000000-0000-0000-0000-000000000000" # producer cluster namespace (uuid)
}
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- **name** (String) Name of the database

### Optional

- **connection_limit** (Number) The maximum number of concurrent connections that can be made to this database. A value of -1 means no limit.
- **datashare_source** (Block List, Max: 1) Configuration for creating a database from a redshift datashare. (see [below for nested schema](#nestedblock--datashare_source))
- **id** (String) The ID of this resource.
- **owner** (String) Owner of the database, usually the user who created it

<a id="nestedblock--datashare_source"></a>
### Nested Schema for `datashare_source`

Required:

- **namespace** (String) The namespace (guid) of the producer cluster
- **share_name** (String) The name of the datashare on the producer cluster

Optional:

- **account_id** (String) The AWS account ID of the producer cluster.


3 changes: 3 additions & 0 deletions examples/data-sources/redshift_database/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "redshift_database" "database" {
name = "my_database"
}
25 changes: 25 additions & 0 deletions examples/resources/redshift_database/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Example resource declaration of a local database
resource "redshift_database" "db" {
name = "my_database"
owner = "my_user"
connection_limit = 123456 # use -1 for unlimited

lifecycle {
prevent_destroy = true
}
}


# Example resource declaration of a database
# created from a datashare of another redshift cluster
resource "redshift_database" "datashare_db" {
name = "my_datashare_consumer_db"
owner = "my_user"
connection_limit = 123456 # use -1 for unlimited

datashare_source {
share_name = "my_datashare"
account_id = "123456789012" # 12 digit AWS account number of the producer cluster (optional, default is current account)
namespace = "00000000-0000-0000-0000-000000000000" # producer cluster namespace (uuid)
}
}
16 changes: 16 additions & 0 deletions redshift/custom_diff.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package redshift

import (
"context"

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

func forceNewIfListSizeChanged(key string) schema.CustomizeDiffFunc {
return customdiff.ForceNewIfChange(key, listSizeChanged)
}

func listSizeChanged(ctx context.Context, old, new, meta interface{}) bool {
return len(old.([]interface{})) != len(new.([]interface{}))
}
119 changes: 119 additions & 0 deletions redshift/data_source_redshift_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package redshift

import (
"strconv"
"strings"

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

func dataSourceRedshiftDatabase() *schema.Resource {
return &schema.Resource{
Description: `Fetches information about a Redshift database.`,
Read: RedshiftResourceFunc(dataSourceRedshiftDatabaseRead),
Schema: map[string]*schema.Schema{
databaseNameAttr: {
Type: schema.TypeString,
Required: true,
Description: "Name of the database",
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
},
databaseOwnerAttr: {
Type: schema.TypeString,
Computed: true,
Description: "Owner of the database, usually the user who created it",
},
databaseConnLimitAttr: {
Type: schema.TypeInt,
Computed: true,
Description: "The maximum number of concurrent connections that can be made to this database. A value of -1 means no limit.",
},
databaseDatashareSourceAttr: {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "Configuration for a database created from a redshift datashare.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
databaseDatashareSourceShareNameAttr: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The name of the datashare on the producer cluster",
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
},
databaseDatashareSourceNamespaceAttr: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The namespace (guid) of the producer cluster",
StateFunc: func(val interface{}) string {
return strings.ToLower(val.(string))
},
},
databaseDatashareSourceAccountAttr: {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The AWS account ID of the producer cluster.",
},
},
},
},
},
}
}

func dataSourceRedshiftDatabaseRead(db *DBConnection, d *schema.ResourceData) error {
var id, owner, connLimit, databaseType, shareName, producerAccount, producerNamespace string

err := db.QueryRow(`SELECT
pg_database_info.datid,
trim(pg_user_info.usename),
COALESCE(pg_database_info.datconnlimit::text, 'UNLIMITED'),
svv_redshift_databases.database_type,
trim(COALESCE(svv_datashares.share_name, '')),
trim(COALESCE(svv_datashares.producer_account, '')),
trim(COALESCE(svv_datashares.producer_namespace, ''))
FROM
svv_redshift_databases
LEFT JOIN pg_database_info
ON svv_redshift_databases.database_name=pg_database_info.datname
LEFT JOIN pg_user_info
ON pg_user_info.usesysid = svv_redshift_databases.database_owner
LEFT JOIN svv_datashares
ON (svv_redshift_databases.database_name = svv_datashares.consumer_database AND svv_redshift_databases.database_type = 'shared' AND svv_datashares.share_type = 'INBOUND')
WHERE svv_redshift_databases.database_name = $1
`, d.Get(databaseNameAttr).(string)).Scan(&id, &owner, &connLimit, &databaseType, &shareName, &producerAccount, &producerNamespace)

if err != nil {
return err
}

connLimitNumber := -1
if connLimit != "UNLIMITED" {
if connLimitNumber, err = strconv.Atoi(connLimit); err != nil {
return err
}
}

d.SetId(id)
d.Set(databaseOwnerAttr, owner)
d.Set(databaseConnLimitAttr, connLimitNumber)

dataShareConfiguration := make([]map[string]interface{}, 0, 1)
if databaseType == "shared" {
config := make(map[string]interface{})
config[databaseDatashareSourceShareNameAttr] = &shareName
config[databaseDatashareSourceAccountAttr] = &producerAccount
config[databaseDatashareSourceNamespaceAttr] = &producerNamespace
dataShareConfiguration = append(dataShareConfiguration, config)
}
d.Set(databaseDatashareSourceAttr, dataShareConfiguration)

return nil
}
42 changes: 42 additions & 0 deletions redshift/data_source_redshift_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package redshift

import (
"fmt"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceRedshiftDatabase_basic(t *testing.T) {
dbName := strings.ReplaceAll(acctest.RandomWithPrefix("tf_acc_data_basic"), "-", "_")
resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceRedshiftDatabaseConfig_basic(dbName),
Check: resource.ComposeAggregateTestCheckFunc(
testAccCheckDatabaseExists(dbName),
resource.TestCheckResourceAttr("data.redshift_database.db", databaseNameAttr, dbName),
resource.TestCheckResourceAttrSet("data.redshift_database.db", databaseOwnerAttr),
resource.TestCheckResourceAttrSet("data.redshift_database.db", databaseConnLimitAttr),
resource.TestCheckResourceAttr("data.redshift_database.db", fmt.Sprintf("%s.#", databaseDatashareSourceAttr), "0"),
),
},
},
})
}

func testAccDataSourceRedshiftDatabaseConfig_basic(dbName string) string {
return fmt.Sprintf(`
resource "redshift_database" "db" {
%[1]s = %[2]q
}

data "redshift_database" "db" {
%[1]s = redshift_database.db.%[1]s
}
`, databaseNameAttr, dbName)
}
8 changes: 5 additions & 3 deletions redshift/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,11 +120,13 @@ func Provider() *schema.Provider {
"redshift_group": redshiftGroup(),
"redshift_schema": redshiftSchema(),
"redshift_privilege": redshiftPrivilege(),
"redshift_database": redshiftDatabase(),
},
DataSourcesMap: map[string]*schema.Resource{
"redshift_user": dataSourceRedshiftUser(),
"redshift_group": dataSourceRedshiftGroup(),
"redshift_schema": dataSourceRedshiftSchema(),
"redshift_user": dataSourceRedshiftUser(),
"redshift_group": dataSourceRedshiftGroup(),
"redshift_schema": dataSourceRedshiftSchema(),
"redshift_database": dataSourceRedshiftDatabase(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
Loading