-
Notifications
You must be signed in to change notification settings - Fork 260
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
databases: Add data source for database user. (#989)
- Loading branch information
Showing
4 changed files
with
165 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,66 @@ | ||
package database | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/config" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func DataSourceDigitalOceanDatabaseUser() *schema.Resource { | ||
return &schema.Resource{ | ||
ReadContext: dataSourceDigitalOceanDatabaseUserRead, | ||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"cluster_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
}, | ||
"role": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"password": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Sensitive: true, | ||
}, | ||
"mysql_auth_plugin": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceDigitalOceanDatabaseUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
client := meta.(*config.CombinedConfig).GodoClient() | ||
|
||
clusterID := d.Get("cluster_id").(string) | ||
name := d.Get("name").(string) | ||
|
||
user, resp, err := client.Databases.GetUser(context.Background(), clusterID, name) | ||
if err != nil { | ||
if resp != nil && resp.StatusCode == 404 { | ||
return diag.Errorf("Database user not found: %s", err) | ||
} | ||
return diag.Errorf("Error retrieving database user: %s", err) | ||
} | ||
|
||
d.SetId(makeDatabaseUserID(clusterID, name)) | ||
d.Set("role", user.Role) | ||
d.Set("password", user.Password) | ||
|
||
if user.MySQLSettings != nil { | ||
d.Set("mysql_auth_plugin", user.MySQLSettings.AuthPlugin) | ||
} | ||
|
||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package database_test | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/digitalocean/godo" | ||
"github.com/digitalocean/terraform-provider-digitalocean/digitalocean/acceptance" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceDigitalOceanDatabaseUser_Basic(t *testing.T) { | ||
var user godo.DatabaseUser | ||
|
||
databaseName := acceptance.RandomTestName() | ||
userName := acceptance.RandomTestName() | ||
|
||
resourceConfig := fmt.Sprintf(testAccCheckDigitalOceanDatabaseUserConfigBasic, databaseName, userName) | ||
datasourceConfig := fmt.Sprintf(testAccCheckDigitalOceanDatasourceDatabaseUserConfigBasic, userName) | ||
|
||
resource.ParallelTest(t, resource.TestCase{ | ||
PreCheck: func() { acceptance.TestAccPreCheck(t) }, | ||
ProviderFactories: acceptance.TestAccProviderFactories, | ||
CheckDestroy: testAccCheckDigitalOceanDatabaseUserDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: resourceConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDigitalOceanDatabaseUserExists("digitalocean_database_user.foobar_user", &user), | ||
testAccCheckDigitalOceanDatabaseUserAttributes(&user, userName), | ||
resource.TestCheckResourceAttr( | ||
"digitalocean_database_user.foobar_user", "name", userName), | ||
resource.TestCheckResourceAttrSet( | ||
"digitalocean_database_user.foobar_user", "role"), | ||
resource.TestCheckResourceAttrSet( | ||
"digitalocean_database_user.foobar_user", "password"), | ||
), | ||
}, | ||
{ | ||
Config: resourceConfig + datasourceConfig, | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttrPair("digitalocean_database_user.foobar_user", "name", | ||
"data.digitalocean_database_user.foobar_user", "name"), | ||
resource.TestCheckResourceAttrPair("digitalocean_database_user.foobar_user", "role", | ||
"data.digitalocean_database_user.foobar_user", "role"), | ||
resource.TestCheckResourceAttrPair("digitalocean_database_user.foobar_user", "password", | ||
"data.digitalocean_database_user.foobar_user", "password"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
const testAccCheckDigitalOceanDatasourceDatabaseUserConfigBasic = ` | ||
data "digitalocean_database_user" "foobar_user" { | ||
cluster_id = digitalocean_database_cluster.foobar.id | ||
name = "%s" | ||
}` |
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
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,40 @@ | ||
--- | ||
page_title: "DigitalOcean: digitalocean_database_user" | ||
--- | ||
|
||
# digitalocean\_database\_user | ||
|
||
Provides information on a DigitalOcean database user resource. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "digitalocean_database_cluster" "main" { | ||
name = "main-cluster" | ||
} | ||
data "digitalocean_database_user" "example" { | ||
cluster_id = data.digitalocean_database_cluster.main.id | ||
name = "example-user" | ||
} | ||
output "database_user_password" { | ||
value = data.digitalocean_database_user.example.password | ||
sensitive = true | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `cluster_id` - (Required) The ID of the database cluster. | ||
* `name` - (Required) The name of the database user. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `role` - The role of the database user. The value will be either `primary` or `normal`. | ||
* `password` - The password of the database user. This will not be set for MongoDB users. | ||
* `mysql_auth_plugin` - The authentication method of the MySQL user. The value will be `mysql_native_password` or `caching_sha2_password`. |