Skip to content

Commit

Permalink
databases: Support project assignment. (#955)
Browse files Browse the repository at this point in the history
* databases: Support project assignment.

* Run make terrafmt
  • Loading branch information
andrewsomething committed Feb 17, 2023
1 parent d1c1d2d commit e847e92
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 0 deletions.
6 changes: 6 additions & 0 deletions digitalocean/database/datasource_database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,11 @@ func DataSourceDigitalOceanDatabaseCluster() *schema.Resource {
Computed: true,
},

"project_id": {
Type: schema.TypeString,
Computed: true,
},

"tags": tag.TagsSchema(),
},
}
Expand Down Expand Up @@ -182,6 +187,7 @@ func dataSourceDigitalOceanDatabaseClusterRead(ctx context.Context, d *schema.Re
}
d.Set("urn", db.URN())
d.Set("private_network_uuid", db.PrivateNetworkUUID)
d.Set("project_id", db.ProjectID)

break
}
Expand Down
2 changes: 2 additions & 0 deletions digitalocean/database/datasource_database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ func TestAccDataSourceDigitalOceanDatabaseCluster_Basic(t *testing.T) {
"data.digitalocean_database_cluster.foobar", "password"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_database_cluster.foobar", "private_network_uuid"),
resource.TestCheckResourceAttrSet(
"data.digitalocean_database_cluster.foobar", "project_id"),
testAccCheckDigitalOceanDatabaseClusterURIPassword(
"digitalocean_database_cluster.foobar", "uri"),
testAccCheckDigitalOceanDatabaseClusterURIPassword(
Expand Down
13 changes: 13 additions & 0 deletions digitalocean/database/resource_database_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,14 @@ func ResourceDigitalOceanDatabaseCluster() *schema.Resource {
ValidateFunc: validation.NoZeroValues,
},

"project_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.NoZeroValues,
},

"host": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -249,6 +257,10 @@ func resourceDigitalOceanDatabaseClusterCreate(ctx context.Context, d *schema.Re
opts.PrivateNetworkUUID = v.(string)
}

if v, ok := d.GetOk("project_id"); ok {
opts.ProjectID = v.(string)
}

log.Printf("[DEBUG] database cluster create configuration: %#v", opts)
database, _, err := client.Databases.Create(context.Background(), opts)
if err != nil {
Expand Down Expand Up @@ -466,6 +478,7 @@ func resourceDigitalOceanDatabaseClusterRead(ctx context.Context, d *schema.Reso
}
d.Set("urn", database.URN())
d.Set("private_network_uuid", database.PrivateNetworkUUID)
d.Set("project_id", database.ProjectID)

return nil
}
Expand Down
42 changes: 42 additions & 0 deletions digitalocean/database/resource_database_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ func TestAccDigitalOceanDatabaseCluster_Basic(t *testing.T) {
"digitalocean_database_cluster.foobar", "tags.#", "1"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_cluster.foobar", "private_network_uuid"),
resource.TestCheckResourceAttrSet(
"digitalocean_database_cluster.foobar", "project_id"),
testAccCheckDigitalOceanDatabaseClusterURIPassword(
"digitalocean_database_cluster.foobar", "uri"),
testAccCheckDigitalOceanDatabaseClusterURIPassword(
Expand Down Expand Up @@ -455,6 +457,31 @@ func TestAccDigitalOceanDatabaseCluster_Upgrade(t *testing.T) {
})
}

func TestAccDigitalOceanDatabaseCluster_nonDefaultProject(t *testing.T) {
var database godo.Database
databaseName := acceptance.RandomTestName()
projectName := acceptance.RandomTestName()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { acceptance.TestAccPreCheck(t) },
ProviderFactories: acceptance.TestAccProviderFactories,
CheckDestroy: testAccCheckDigitalOceanDatabaseClusterDestroy,
Steps: []resource.TestStep{
{
Config: fmt.Sprintf(testAccCheckDigitalOceanDatabaseClusterConfigNonDefaultProject, projectName, databaseName),
Check: resource.ComposeTestCheckFunc(
testAccCheckDigitalOceanDatabaseClusterExists("digitalocean_database_cluster.foobar", &database),
testAccCheckDigitalOceanDatabaseClusterAttributes(&database, databaseName),
resource.TestCheckResourceAttr(
"digitalocean_database_cluster.foobar", "name", databaseName),
resource.TestCheckResourceAttrPair(
"digitalocean_project.foobar", "id", "digitalocean_database_cluster.foobar", "project_id"),
),
},
},
})
}

func testAccCheckDigitalOceanDatabaseClusterDestroy(s *terraform.State) error {
client := acceptance.TestAccProvider.Meta().(*config.CombinedConfig).GodoClient()

Expand Down Expand Up @@ -731,3 +758,18 @@ resource "digitalocean_database_cluster" "foobar" {
region = "nyc3"
node_count = 1
}`

const testAccCheckDigitalOceanDatabaseClusterConfigNonDefaultProject = `
resource "digitalocean_project" "foobar" {
name = "%s"
}
resource "digitalocean_database_cluster" "foobar" {
name = "%s"
engine = "pg"
version = "11"
size = "db-s-1vcpu-2gb"
region = "nyc1"
node_count = 1
project_id = digitalocean_project.foobar.id
}`
1 change: 1 addition & 0 deletions docs/data-sources/database_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The following attributes are exported:
* `database` - Name of the cluster's default database.
* `user` - Username for the cluster's default user.
* `password` - Password for the cluster's default user.
* `project_id` - The ID of the project that the database cluster is assigned to.

`maintenance_window` supports the following:

Expand Down
1 change: 1 addition & 0 deletions docs/resources/database_cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ The following arguments are supported:
When this value is changed, a call to the [Upgrade major Version for a Database](https://docs.digitalocean.com/reference/api/api-reference/#operation/databases_update_major_version) API operation is made with the new version.
* `tags` - (Optional) A list of tag names to be applied to the database cluster.
* `private_network_uuid` - (Optional) The ID of the VPC where the database cluster will be located.
* `project_id` - (Optional) The ID of the project that the database cluster is assigned to. If excluded when creating a new database cluster, it will be assigned to your default project.
* `eviction_policy` - (Optional) A string specifying the eviction policy for a Redis cluster. Valid values are: `noeviction`, `allkeys_lru`, `allkeys_random`, `volatile_lru`, `volatile_random`, or `volatile_ttl`.
* `sql_mode` - (Optional) A comma separated string specifying the SQL modes for a MySQL cluster.
* `maintenance_window` - (Optional) Defines when the automatic maintenance should be performed for the database cluster.
Expand Down

0 comments on commit e847e92

Please sign in to comment.