From 84009e88b985802ab692aa1974b872a67198a7a4 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Mon, 6 Apr 2020 22:32:22 +0100 Subject: [PATCH 01/16] resource/aws_db_proxy: Initial resource --- aws/provider.go | 1 + aws/resource_aws_db_proxy.go | 314 ++++++++++++++++++++++++++ website/aws.erb | 3 + website/docs/r/db_proxy.html.markdown | 86 +++++++ 4 files changed, 404 insertions(+) create mode 100644 aws/resource_aws_db_proxy.go create mode 100644 website/docs/r/db_proxy.html.markdown diff --git a/aws/provider.go b/aws/provider.go index be386f16d6b..78bbb4f7c56 100644 --- a/aws/provider.go +++ b/aws/provider.go @@ -449,6 +449,7 @@ func Provider() terraform.ResourceProvider { "aws_db_instance_role_association": resourceAwsDbInstanceRoleAssociation(), "aws_db_option_group": resourceAwsDbOptionGroup(), "aws_db_parameter_group": resourceAwsDbParameterGroup(), + "aws_db_proxy": resourceAwsDbProxy(), "aws_db_security_group": resourceAwsDbSecurityGroup(), "aws_db_snapshot": resourceAwsDbSnapshot(), "aws_db_subnet_group": resourceAwsDbSubnetGroup(), diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go new file mode 100644 index 00000000000..502512a58a3 --- /dev/null +++ b/aws/resource_aws_db_proxy.go @@ -0,0 +1,314 @@ +package aws + +import ( + "bytes" + "fmt" + "log" + "time" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/service/rds" + "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" +) + +func resourceAwsDbProxy() *schema.Resource { + return &schema.Resource{ + Create: resourceAwsDbProxyCreate, + Read: resourceAwsDbProxyRead, + Update: resourceAwsDbProxyUpdate, + Delete: resourceAwsDbProxyDelete, + Importer: &schema.ResourceImporter{ + State: schema.ImportStatePassthrough, + }, + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Update: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "arn": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateRdsIdentifier, + }, + "debug_logging": { + Type: schema.TypeBool, + Optional: true, + }, + "engine_family": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + rds.EngineFamilyMysql, + }, false), + }, + "idle_client_timeout": { + Type: schema.TypeInt, + Optional: true, + }, + "require_tls": { + Type: schema.TypeBool, + Optional: true, + }, + "role_arn": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validateArn, + }, + "vpc_security_group_ids": { + Type: schema.TypeSet, + Optional: true, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "vpc_subnet_ids": { + Type: schema.TypeSet, + Required: true, + Elem: &schema.Schema{Type: schema.TypeString}, + Set: schema.HashString, + }, + "auth": { + Type: schema.TypeSet, + Required: true, + ForceNew: false, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "auth_scheme": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + rds.AuthSchemeSecrets, + }, false), + }, + "description": { + Type: schema.TypeString, + Optional: true, + }, + "iam_auth": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validation.StringInSlice([]string{ + rds.IAMAuthModeDisabled, + rds.IAMAuthModeRequired, + }, false), + }, + "secret_arn": { + Type: schema.TypeString, + Optional: true, + ValidateFunc: validateArn, + }, + "username": { + Type: schema.TypeString, + Optional: true, + }, + }, + }, + Set: resourceAwsDbProxyAuthHash, + }, + + "tags": tagsSchema(), + }, + } +} + +func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { + rdsconn := meta.(*AWSClient).rdsconn + tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().RdsTags() + + params := rds.CreateDBProxyInput{ + Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), + DBProxyName: aws.String(d.Get("name").(string)), + EngineFamily: aws.String(d.Get("engine_family").(string)), + RoleArn: aws.String(d.Get("role_arn").(string)), + Tags: tags, + VpcSubnetIds: expandStringSet(d.Get("vpc_subnet_ids").(*schema.Set)), + } + + if v, ok := d.GetOk("debug_logging"); ok { + params.DebugLogging = aws.Bool(v.(bool)) + } + + if v, ok := d.GetOk("idle_client_timeout"); ok { + params.IdleClientTimeout = aws.Int64(int64(v.(int))) + } + + if v, ok := d.GetOk("require_tls"); ok { + params.RequireTLS = aws.Bool(v.(bool)) + } + + if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 { + params.VpcSecurityGroupIds = expandStringSet(v) + } + + log.Printf("[DEBUG] Create DB Proxy: %#v", params) + resp, err := rdsconn.CreateDBProxy(¶ms) + if err != nil { + return fmt.Errorf("Error creating DB Proxy: %s", err) + } + + d.SetId(aws.StringValue(resp.DBProxy.DBProxyName)) + d.Set("arn", resp.DBProxy.DBProxyArn) + log.Printf("[INFO] DB Proxy ID: %s", d.Id()) + + return resourceAwsDbProxyRead(d, meta) +} + +func expandDbProxyAuth(l []interface{}) []*rds.UserAuthConfig { + if len(l) == 0 { + return nil + } + + userAuthConfigs := make([]*rds.UserAuthConfig, 0, len(l)) + + for _, mRaw := range l { + m, ok := mRaw.(map[string]interface{}) + + if !ok { + continue + } + + userAuthConfig := &rds.UserAuthConfig{} + + if v, ok := m["auth_scheme"].(string); ok && v != "" { + userAuthConfig.AuthScheme = aws.String(v) + } + + if v, ok := m["description"].(string); ok && v != "" { + userAuthConfig.Description = aws.String(v) + } + + if v, ok := m["iam_auth"].(string); ok && v != "" { + userAuthConfig.IAMAuth = aws.String(v) + } + + if v, ok := m["secret_arn"].(string); ok && v != "" { + userAuthConfig.SecretArn = aws.String(v) + } + + if v, ok := m["username"].(string); ok && v != "" { + userAuthConfig.UserName = aws.String(v) + } + + userAuthConfigs = append(userAuthConfigs, userAuthConfig) + } + + return userAuthConfigs +} + +func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { + rdsconn := meta.(*AWSClient).rdsconn + + params := rds.DescribeDBProxiesInput{ + DBProxyName: aws.String(d.Id()), + } + + resp, err := rdsconn.DescribeDBProxies(¶ms) + if err != nil { + if isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") { + log.Printf("[WARN] DB Proxy (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil + } + return err + } + + if len(resp.DBProxies) != 1 || + *resp.DBProxies[0].DBProxyName != d.Id() { + return fmt.Errorf("Unable to find DB Proxy: %#v", resp.DBProxies) + } + + v := resp.DBProxies[0] + + d.Set("arn", aws.StringValue(v.DBProxyArn)) + d.Set("name", v.DBProxyName) + d.Set("debug_logging", v.DebugLogging) + d.Set("engine_family", v.EngineFamily) + d.Set("idle_client_timeout", v.IdleClientTimeout) + d.Set("require_tls", v.RequireTLS) + d.Set("role_arn", v.RoleArn) + d.Set("vpc_subnet_ids", flattenStringSet(v.VpcSubnetIds)) + d.Set("security_group_ids", flattenStringSet(v.VpcSecurityGroupIds)) + + tags, err := keyvaluetags.RdsListTags(rdsconn, d.Get("arn").(string)) + + if err != nil { + return fmt.Errorf("Error listing tags for RDS DB Proxy (%s): %s", d.Get("arn").(string), err) + } + + if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil { + return fmt.Errorf("Error setting tags: %s", err) + } + + return nil +} + +func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { + rdsconn := meta.(*AWSClient).rdsconn + + if d.HasChange("tags") { + o, n := d.GetChange("tags") + + if err := keyvaluetags.RdsUpdateTags(rdsconn, d.Get("arn").(string), o, n); err != nil { + return fmt.Errorf("Error updating RDS DB Proxy (%s) tags: %s", d.Get("arn").(string), err) + } + } + + return resourceAwsDbProxyRead(d, meta) +} + +func resourceAwsDbProxyDelete(d *schema.ResourceData, meta interface{}) error { + conn := meta.(*AWSClient).rdsconn + params := rds.DeleteDBProxyInput{ + DBProxyName: aws.String(d.Id()), + } + err := resource.Retry(3*time.Minute, func() *resource.RetryError { + _, err := conn.DeleteDBProxy(¶ms) + if err != nil { + if isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") || isAWSErr(err, rds.ErrCodeInvalidDBProxyStateFault, "") { + return resource.RetryableError(err) + } + return resource.NonRetryableError(err) + } + return nil + }) + if isResourceTimeoutError(err) { + _, err = conn.DeleteDBProxy(¶ms) + } + if err != nil { + return fmt.Errorf("Error deleting DB Proxy: %s", err) + } + return nil +} + +func resourceAwsDbProxyAuthHash(v interface{}) int { + var buf bytes.Buffer + m := v.(map[string]interface{}) + if v, ok := m["auth_scheme"].(string); ok { + buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := m["description"].(string); ok { + buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := m["iam_auth"].(string); ok { + buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := m["secret_arn"].(string); ok { + buf.WriteString(fmt.Sprintf("%s-", v)) + } + if v, ok := m["username"].(string); ok { + buf.WriteString(fmt.Sprintf("%s-", v)) + } + return hashcode.String(buf.String()) +} diff --git a/website/aws.erb b/website/aws.erb index b5b8540d4c4..5035745729c 100644 --- a/website/aws.erb +++ b/website/aws.erb @@ -2484,6 +2484,9 @@
  • aws_db_parameter_group
  • +
  • + aws_db_proxy +
  • aws_db_security_group
  • diff --git a/website/docs/r/db_proxy.html.markdown b/website/docs/r/db_proxy.html.markdown new file mode 100644 index 00000000000..5e0c57c03b1 --- /dev/null +++ b/website/docs/r/db_proxy.html.markdown @@ -0,0 +1,86 @@ +--- +subcategory: "RDS" +layout: "aws" +page_title: "AWS: aws_db_proxy" +description: |- + Provides an RDS DB proxy resource. +--- + +# Resource: aws_db_proxy + +Provides an RDS DB proxy resource. + +## Example Usage + +```hcl +resource "aws_db_proxy" "example" { + name = "example" + debug_logging = false + engine_family = "MYSQL" + idle_client_timeout = 1800 + require_tls = true + role_arn = "arn:aws:iam:us-east-1:123456789012:role/example" + vpc_security_group_ids = ["sg-12345678901234567"] + vpc_subnet_ids = ["subnet-12345678901234567"] + + auth { + auth_scheme = "SECRETS" + description = "example" + iam_auth = "DISABLED" + secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:example" + } + + tags = { + Name = "example" + Key = "value" + } +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - (Required) The identifier for the proxy. This name must be unique for all proxies owned by your AWS account in the specified AWS Region. An identifier must begin with a letter and must contain only ASCII letters, digits, and hyphens; it can't end with a hyphen or contain two consecutive hyphens. +* `auth` - (Required) The authorization mechanism that the proxy uses. +* `debug_logging` - (Optional) Whether the proxy includes detailed information about SQL statements in its logs. This information helps you to debug issues involving SQL behavior or the performance and scalability of the proxy connections. The debug information includes the text of SQL statements that you submit through the proxy. Thus, only enable this setting when needed for debugging, and only when you have security measures in place to safeguard any sensitive information that appears in the logs. +* `engine_family` - (Required, Forces new resource) The kinds of databases that the proxy can connect to. This value determines which database network protocol the proxy recognizes when it interprets network traffic to and from the database. Currently, this value is always `MYSQL`. The engine family applies to both RDS MySQL and Aurora MySQL. +* `idle_client_timeout` - (Optional) The number of seconds that a connection to the proxy can be inactive before the proxy disconnects it. You can set this value higher or lower than the connection timeout limit for the associated database. +* `require_tls` - (Optional) A Boolean parameter that specifies whether Transport Layer Security (TLS) encryption is required for connections to the proxy. By enabling this setting, you can enforce encrypted TLS connections to the proxy. +* `role_arn` - (Required) The Amazon Resource Name (ARN) of the IAM role that the proxy uses to access secrets in AWS Secrets Manager. +* `vpc_security_group_ids` - (Optional) One or more VPC security group IDs to associate with the new proxy. +* `vpc_subnet_ids` - (Required) One or more VPC subnet IDs to associate with the new proxy. +describe-db-parameters.html) after initial creation of the group. +* `tags` - (Optional) A mapping of tags to assign to the resource. + +`auth` blocks support the following: + +* `auth_scheme` - (Optional) The type of authentication that the proxy uses for connections from the proxy to the underlying database. One of `SECRETS`. +* `description` - (Optional) A user-specified description about the authentication used by a proxy to log in as a specific database user. +* `iam_auth` - (Optional) Whether to require or disallow AWS Identity and Access Management (IAM) authentication for connections to the proxy. One of `DISABLED`, `REQUIRED`. +* `secret_arn` - (Optional) The Amazon Resource Name (ARN) representing the secret that the proxy uses to authenticate to the RDS DB instance or Aurora DB cluster. These secrets are stored within Amazon Secrets Manager. +* `username` - (Optional) The name of the database user to which the proxy connects. + +## Attributes Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The Amazon Resource Name (ARN) for the proxy. +* `arn` - The Amazon Resource Name (ARN) for the proxy. +* `endpoint` - The endpoint that you can use to connect to the proxy. You include the endpoint value in the connection string for a database client application. + +### Timeouts + +`aws_db_proxy` provides the following [Timeouts](/docs/configuration/resources.html#timeouts) configuration options: + +- `create` - (Default `30 minutes`) Used for creating DB proxies. +- `update` - (Default `30 minutes`) Used for modifying DB proxies. +- `delete` - (Default `30 minutes`) Used for destroying DB proxies. + +## Import + +DB proxies can be imported using the `name`, e.g. + +``` +$ terraform import aws_db_proxy.example example +``` From f4f95fb5a4df9f1a5ad0ef8f9719ec0adc24c267 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Mon, 6 Apr 2020 23:24:32 +0100 Subject: [PATCH 02/16] resource/aws_db_proxy: Set up WaitForState --- aws/resource_aws_db_proxy.go | 91 ++++++++++++++++++++++++------------ 1 file changed, 61 insertions(+), 30 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index 502512a58a3..959f62aba2d 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -124,7 +124,7 @@ func resourceAwsDbProxy() *schema.Resource { } func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { - rdsconn := meta.(*AWSClient).rdsconn + conn := meta.(*AWSClient).rdsconn tags := keyvaluetags.New(d.Get("tags").(map[string]interface{})).IgnoreAws().RdsTags() params := rds.CreateDBProxyInput{ @@ -153,7 +153,7 @@ func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { } log.Printf("[DEBUG] Create DB Proxy: %#v", params) - resp, err := rdsconn.CreateDBProxy(¶ms) + resp, err := conn.CreateDBProxy(¶ms) if err != nil { return fmt.Errorf("Error creating DB Proxy: %s", err) } @@ -162,9 +162,39 @@ func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { d.Set("arn", resp.DBProxy.DBProxyArn) log.Printf("[INFO] DB Proxy ID: %s", d.Id()) + stateChangeConf := &resource.StateChangeConf{ + Pending: []string{rds.DBProxyStatusCreating}, + Target: []string{rds.DBProxyStatusAvailable}, + Refresh: resourceAwsDbProxyRefreshFunc(conn, d.Id()), + Timeout: d.Timeout(schema.TimeoutCreate), + } + + _, err = stateChangeConf.WaitForState() + if err != nil { + return fmt.Errorf("Error waiting for DB Proxy creation: %s", err) + } + return resourceAwsDbProxyRead(d, meta) } +func resourceAwsDbProxyRefreshFunc(conn *rds.RDS, proxyName string) resource.StateRefreshFunc { + return func() (interface{}, string, error) { + resp, err := conn.DescribeDBProxies(&rds.DescribeDBProxiesInput{ + DBProxyName: aws.String(proxyName), + }) + + if err != nil { + if isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") { + return 42, "", nil + } + return 42, "", err + } + + dbProxy := resp.DBProxies[0] + return dbProxy, *dbProxy.Status, nil + } +} + func expandDbProxyAuth(l []interface{}) []*rds.UserAuthConfig { if len(l) == 0 { return nil @@ -208,13 +238,13 @@ func expandDbProxyAuth(l []interface{}) []*rds.UserAuthConfig { } func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { - rdsconn := meta.(*AWSClient).rdsconn + conn := meta.(*AWSClient).rdsconn params := rds.DescribeDBProxiesInput{ DBProxyName: aws.String(d.Id()), } - resp, err := rdsconn.DescribeDBProxies(¶ms) + resp, err := conn.DescribeDBProxies(¶ms) if err != nil { if isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") { log.Printf("[WARN] DB Proxy (%s) not found, removing from state", d.Id()) @@ -229,19 +259,19 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Unable to find DB Proxy: %#v", resp.DBProxies) } - v := resp.DBProxies[0] + dbProxy := resp.DBProxies[0] - d.Set("arn", aws.StringValue(v.DBProxyArn)) - d.Set("name", v.DBProxyName) - d.Set("debug_logging", v.DebugLogging) - d.Set("engine_family", v.EngineFamily) - d.Set("idle_client_timeout", v.IdleClientTimeout) - d.Set("require_tls", v.RequireTLS) - d.Set("role_arn", v.RoleArn) - d.Set("vpc_subnet_ids", flattenStringSet(v.VpcSubnetIds)) - d.Set("security_group_ids", flattenStringSet(v.VpcSecurityGroupIds)) + d.Set("arn", aws.StringValue(dbProxy.DBProxyArn)) + d.Set("name", dbProxy.DBProxyName) + d.Set("debug_logging", dbProxy.DebugLogging) + d.Set("engine_family", dbProxy.EngineFamily) + d.Set("idle_client_timeout", dbProxy.IdleClientTimeout) + d.Set("require_tls", dbProxy.RequireTLS) + d.Set("role_arn", dbProxy.RoleArn) + d.Set("vpc_subnet_ids", flattenStringSet(dbProxy.VpcSubnetIds)) + d.Set("security_group_ids", flattenStringSet(dbProxy.VpcSecurityGroupIds)) - tags, err := keyvaluetags.RdsListTags(rdsconn, d.Get("arn").(string)) + tags, err := keyvaluetags.RdsListTags(conn, d.Get("arn").(string)) if err != nil { return fmt.Errorf("Error listing tags for RDS DB Proxy (%s): %s", d.Get("arn").(string), err) @@ -255,12 +285,12 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { } func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { - rdsconn := meta.(*AWSClient).rdsconn + conn := meta.(*AWSClient).rdsconn if d.HasChange("tags") { o, n := d.GetChange("tags") - if err := keyvaluetags.RdsUpdateTags(rdsconn, d.Get("arn").(string), o, n); err != nil { + if err := keyvaluetags.RdsUpdateTags(conn, d.Get("arn").(string), o, n); err != nil { return fmt.Errorf("Error updating RDS DB Proxy (%s) tags: %s", d.Get("arn").(string), err) } } @@ -270,25 +300,26 @@ func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbProxyDelete(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn + params := rds.DeleteDBProxyInput{ DBProxyName: aws.String(d.Id()), } - err := resource.Retry(3*time.Minute, func() *resource.RetryError { - _, err := conn.DeleteDBProxy(¶ms) - if err != nil { - if isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") || isAWSErr(err, rds.ErrCodeInvalidDBProxyStateFault, "") { - return resource.RetryableError(err) - } - return resource.NonRetryableError(err) - } - return nil - }) - if isResourceTimeoutError(err) { - _, err = conn.DeleteDBProxy(¶ms) - } + _, err := conn.DeleteDBProxy(¶ms) if err != nil { return fmt.Errorf("Error deleting DB Proxy: %s", err) } + + stateChangeConf := &resource.StateChangeConf{ + Pending: []string{rds.DBProxyStatusDeleting}, + Refresh: resourceAwsDbProxyRefreshFunc(conn, d.Id()), + Timeout: d.Timeout(schema.TimeoutDelete), + } + + _, err = stateChangeConf.WaitForState() + if err != nil { + return fmt.Errorf("Error waiting for DB Proxy deletion: %s", err) + } + return nil } From cc02e3a751a30f8807952100bc0e1b36b84b6da3 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 7 Apr 2020 19:27:46 +0100 Subject: [PATCH 03/16] resource/aws_db_proxy: Initial tests --- aws/resource_aws_db_proxy_test.go | 274 ++++++++++++++++++++++++++++++ 1 file changed, 274 insertions(+) create mode 100644 aws/resource_aws_db_proxy_test.go diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go new file mode 100644 index 00000000000..a5396be5213 --- /dev/null +++ b/aws/resource_aws_db_proxy_test.go @@ -0,0 +1,274 @@ +package aws + +import ( + "fmt" + "log" + "regexp" + "testing" + + "github.com/aws/aws-sdk-go/aws" + "github.com/aws/aws-sdk-go/aws/awserr" + "github.com/aws/aws-sdk-go/service/rds" + "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" +) + +func init() { + resource.AddTestSweepers("aws_db_proxy", &resource.Sweeper{ + Name: "aws_db_proxy", + F: testSweepRdsDbProxies, + }) +} + +func testSweepRdsDbProxies(region string) error { + client, err := sharedClientForRegion(region) + if err != nil { + return fmt.Errorf("Error getting client: %s", err) + } + conn := client.(*AWSClient).rdsconn + + err = conn.DescribeDBProxiesPages(&rds.DescribeDBProxiesInput{}, func(out *rds.DescribeDBProxiesOutput, lastPage bool) bool { + for _, dbpg := range out.DBProxies { + if dbpg == nil { + continue + } + + input := &rds.DeleteDBProxyInput{ + DBProxyName: dbpg.DBProxyName, + } + name := aws.StringValue(dbpg.DBProxyName) + + log.Printf("[INFO] Deleting DB Proxy: %s", name) + + _, err := conn.DeleteDBProxy(input) + + if err != nil { + log.Printf("[ERROR] Failed to delete DB Proxy %s: %s", name, err) + continue + } + } + + return !lastPage + }) + + if testSweepSkipSweepError(err) { + log.Printf("[WARN] Skipping RDS DB Proxy sweep for %s: %s", region, err) + return nil + } + + if err != nil { + return fmt.Errorf("Error retrieving DB Proxies: %s", err) + } + + return nil +} + +func TestAccAWSDBProxy_basic(t *testing.T) { + var v rds.DBProxy + resourceName := "aws_db_proxy.test" + name := fmt.Sprintf("tf-acc-db-proxy-%d", acctest.RandInt()) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfig(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &v), + resource.TestCheckResourceAttr( + resourceName, "name", name), + resource.TestCheckResourceAttr( + resourceName, "engine_family", "MYSQL"), + resource.TestMatchResourceAttr( + resourceName, "arn", regexp.MustCompile(`^arn:[^:]+:rds:[^:]+:\d{12}:db-proxy:.+`)), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func testAccCheckAWSDBProxyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).rdsconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_db_proxy" { + continue + } + + // Try to find the Group + resp, err := conn.DescribeDBProxies( + &rds.DescribeDBProxiesInput{ + DBProxyName: aws.String(rs.Primary.ID), + }) + + if err == nil { + if len(resp.DBProxies) != 0 && + *resp.DBProxies[0].DBProxyName == rs.Primary.ID { + return fmt.Errorf("DB Proxy still exists") + } + } + + // Verify the error + newerr, ok := err.(awserr.Error) + if !ok { + return err + } + if newerr.Code() != rds.ErrCodeDBProxyNotFoundFault { + return err + } + } + + return nil +} + +func testAccCheckAWSDBProxyExists(n string, v *rds.DBProxy) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No DB Proxy ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).rdsconn + + opts := rds.DescribeDBProxiesInput{ + DBProxyName: aws.String(rs.Primary.ID), + } + + resp, err := conn.DescribeDBProxies(&opts) + + if err != nil { + return err + } + + if len(resp.DBProxies) != 1 || + *resp.DBProxies[0].DBProxyName != rs.Primary.ID { + return fmt.Errorf("DB Proxy not found") + } + + *v = *resp.DBProxies[0] + + return nil + } +} + +func testAccAWSDBProxyConfig(n string) string { + return fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] + + name = "%s" + debug_logging = false + engine_family = "MYSQL" + idle_client_timeout = 1800 + require_tls = true + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [] + vpc_subnet_ids = aws_subnet.test.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } + + tags = { + Name = "%s" + } +} + +# Secrets Manager setup + +resource "aws_secretsmanager_secret" "test" { + name = "%s" +} + +resource "aws_secretsmanager_secret_version" "test" { + secret_id = aws_secretsmanager_secret.test.id + secret_string = "{\"username\":\"db_user\",\"password\":\"db_user_password\"}" +} + +# IAM setup + +resource "aws_iam_role" "test" { + name = "%s" + assume_role_policy = data.aws_iam_policy_document.assume.json +} + +data "aws_iam_policy_document" "assume" { + statement { + actions = ["sts:AssumeRole"] + principals { + type = "Service" + identifiers = ["rds.amazonaws.com"] + } + } +} + +resource "aws_iam_role_policy" "test" { + role = aws_iam_role.test.id + policy = data.aws_iam_policy_document.test.json +} + +data "aws_iam_policy_document" "test" { + statement { + actions = [ + "secretsmanager:GetRandomPassword", + "secretsmanager:CreateSecret", + "secretsmanager:ListSecrets", + ] + resources = ["*"] + } + + statement { + actions = ["secretsmanager:*"] + resources = [aws_secretsmanager_secret.test.arn] + } +} + +# VPC setup + +data "aws_availability_zones" "available" { + state = "available" + + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags = { + Name = "%s" + } +} + +resource "aws_subnet" "test" { + count = 2 + cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index) + availability_zone = data.aws_availability_zones.available.names[count.index] + vpc_id = aws_vpc.test.id + + tags = { + Name = "%s-${count.index}" + } +} +`, n, n, n, n, n, n) +} From b118809f294470ca31b4bf3e1d9722237e4c5ed3 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 7 Apr 2020 22:05:03 +0100 Subject: [PATCH 04/16] resource/aws_db_proxy: Read UserAuthInfo back, handle updates --- aws/resource_aws_db_proxy.go | 65 ++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index 959f62aba2d..e65234760dc 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -76,6 +76,7 @@ func resourceAwsDbProxy() *schema.Resource { "vpc_subnet_ids": { Type: schema.TypeSet, Required: true, + ForceNew: true, Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, @@ -237,6 +238,26 @@ func expandDbProxyAuth(l []interface{}) []*rds.UserAuthConfig { return userAuthConfigs } +func flattenDbProxyAuth(userAuthConfig *rds.UserAuthConfigInfo) map[string]interface{} { + m := make(map[string]interface{}) + + m["auth_scheme"] = aws.StringValue(userAuthConfig.AuthScheme) + m["description"] = aws.StringValue(userAuthConfig.Description) + m["iam_auth"] = aws.StringValue(userAuthConfig.IAMAuth) + m["secret_arn"] = aws.StringValue(userAuthConfig.SecretArn) + m["username"] = aws.StringValue(userAuthConfig.UserName) + + return m +} + +func flattenDbProxyAuths(userAuthConfigs []*rds.UserAuthConfigInfo) *schema.Set { + s := []interface{}{} + for _, v := range userAuthConfigs { + s = append(s, flattenDbProxyAuth(v)) + } + return schema.NewSet(resourceAwsDbProxyAuthHash, s) +} + func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn @@ -262,6 +283,7 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { dbProxy := resp.DBProxies[0] d.Set("arn", aws.StringValue(dbProxy.DBProxyArn)) + d.Set("auth", flattenDbProxyAuths(dbProxy.Auth)) d.Set("name", dbProxy.DBProxyName) d.Set("debug_logging", dbProxy.DebugLogging) d.Set("engine_family", dbProxy.EngineFamily) @@ -287,6 +309,49 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn + oName, nName := d.GetChange("name") + + params := rds.ModifyDBProxyInput{ + Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), + DBProxyName: aws.String(oName.(string)), + NewDBProxyName: aws.String(nName.(string)), + RoleArn: aws.String(d.Get("role_arn").(string)), + } + + if v, ok := d.GetOk("debug_logging"); ok { + params.DebugLogging = aws.Bool(v.(bool)) + } + + if v, ok := d.GetOk("idle_client_timeout"); ok { + params.IdleClientTimeout = aws.Int64(int64(v.(int))) + } + + if v, ok := d.GetOk("require_tls"); ok { + params.RequireTLS = aws.Bool(v.(bool)) + } + + if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 { + params.SecurityGroups = expandStringSet(v) + } + + log.Printf("[DEBUG] Update DB Proxy: %#v", params) + _, err := conn.ModifyDBProxy(¶ms) + if err != nil { + return fmt.Errorf("Error updating DB Proxy: %s", err) + } + + stateChangeConf := &resource.StateChangeConf{ + Pending: []string{rds.DBProxyStatusModifying}, + Target: []string{rds.DBProxyStatusAvailable}, + Refresh: resourceAwsDbProxyRefreshFunc(conn, d.Id()), + Timeout: d.Timeout(schema.TimeoutCreate), + } + + _, err = stateChangeConf.WaitForState() + if err != nil { + return fmt.Errorf("Error waiting for DB Proxy update: %s", err) + } + if d.HasChange("tags") { o, n := d.GetChange("tags") From b656527cac3502105b149ae0265d65ebd123375d Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 7 Apr 2020 23:33:23 +0100 Subject: [PATCH 05/16] resource/aws_db_proxy: Fix attributes, fix deletion WaitForState --- aws/resource_aws_db_proxy.go | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index e65234760dc..70b386970d3 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -131,7 +131,9 @@ func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { params := rds.CreateDBProxyInput{ Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), DBProxyName: aws.String(d.Get("name").(string)), + DebugLogging: aws.Bool(d.Get("debug_logging").(bool)), EngineFamily: aws.String(d.Get("engine_family").(string)), + RequireTLS: aws.Bool(d.Get("require_tls").(bool)), RoleArn: aws.String(d.Get("role_arn").(string)), Tags: tags, VpcSubnetIds: expandStringSet(d.Get("vpc_subnet_ids").(*schema.Set)), @@ -315,21 +317,15 @@ func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), DBProxyName: aws.String(oName.(string)), NewDBProxyName: aws.String(nName.(string)), + DebugLogging: aws.Bool(d.Get("debug_logging").(bool)), + RequireTLS: aws.Bool(d.Get("require_tls").(bool)), RoleArn: aws.String(d.Get("role_arn").(string)), } - if v, ok := d.GetOk("debug_logging"); ok { - params.DebugLogging = aws.Bool(v.(bool)) - } - if v, ok := d.GetOk("idle_client_timeout"); ok { params.IdleClientTimeout = aws.Int64(int64(v.(int))) } - if v, ok := d.GetOk("require_tls"); ok { - params.RequireTLS = aws.Bool(v.(bool)) - } - if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 { params.SecurityGroups = expandStringSet(v) } @@ -376,6 +372,7 @@ func resourceAwsDbProxyDelete(d *schema.ResourceData, meta interface{}) error { stateChangeConf := &resource.StateChangeConf{ Pending: []string{rds.DBProxyStatusDeleting}, + Target: []string{""}, Refresh: resourceAwsDbProxyRefreshFunc(conn, d.Id()), Timeout: d.Timeout(schema.TimeoutDelete), } From 1c92e9bde4df84e4c81f7b024ca8fb1d1b4dd967 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Fri, 10 Apr 2020 18:12:12 +0100 Subject: [PATCH 06/16] resource/aws_db_proxy: Add support for Postgres, immediately remove secrets created by tests --- aws/resource_aws_db_proxy.go | 1 + aws/resource_aws_db_proxy_test.go | 3 ++- 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index 70b386970d3..4884a9a6ce0 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -51,6 +51,7 @@ func resourceAwsDbProxy() *schema.Resource { ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ rds.EngineFamilyMysql, + "POSTGRESQL", }, false), }, "idle_client_timeout": { diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index a5396be5213..862a8275575 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -195,7 +195,8 @@ resource "aws_db_proxy" "test" { # Secrets Manager setup resource "aws_secretsmanager_secret" "test" { - name = "%s" + name = "%s" + recovery_window_in_days = 0 } resource "aws_secretsmanager_secret_version" "test" { From 33c9d396731bbe636975bdeb33123db77ac0094b Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Mon, 3 Aug 2020 14:10:34 +0100 Subject: [PATCH 07/16] resource/aws_db_proxy: Use constant for Postgreqsl engine family --- aws/resource_aws_db_proxy.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index 4884a9a6ce0..d91b31fe381 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -51,7 +51,7 @@ func resourceAwsDbProxy() *schema.Resource { ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ rds.EngineFamilyMysql, - "POSTGRESQL", + rds.EngineFamilyPostgresql, }, false), }, "idle_client_timeout": { From 00ab3d2893228257497cdeeec26414d7d7cdc26d Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Mon, 3 Aug 2020 16:41:05 +0100 Subject: [PATCH 08/16] resource/aws_db_proxy: Address linter issues --- aws/resource_aws_db_proxy.go | 7 ++++++- aws/resource_aws_db_proxy_test.go | 3 +-- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index d91b31fe381..919449049ac 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -263,6 +263,7 @@ func flattenDbProxyAuths(userAuthConfigs []*rds.UserAuthConfigInfo) *schema.Set func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn + ignoreTagsConfig := meta.(*AWSClient).IgnoreTagsConfig params := rds.DescribeDBProxiesInput{ DBProxyName: aws.String(d.Id()), @@ -302,7 +303,11 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error listing tags for RDS DB Proxy (%s): %s", d.Get("arn").(string), err) } - if err := d.Set("tags", tags.IgnoreAws().Map()); err != nil { + // if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { + // return fmt.Errorf("error setting tags: %s", err) + // } + + if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return fmt.Errorf("Error setting tags: %s", err) } diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index 862a8275575..b015f1994ba 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -82,8 +82,7 @@ func TestAccAWSDBProxy_basic(t *testing.T) { resourceName, "name", name), resource.TestCheckResourceAttr( resourceName, "engine_family", "MYSQL"), - resource.TestMatchResourceAttr( - resourceName, "arn", regexp.MustCompile(`^arn:[^:]+:rds:[^:]+:\d{12}:db-proxy:.+`)), + testAccMatchResourceAttrRegionalARN(resourceName, "arn", "rds", regexp.MustCompile(`db-proxy:.+`)), ), }, { From 8432ff0e342bf33efb8310c9e14b645d5723d5b2 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 25 Aug 2020 21:21:18 +0100 Subject: [PATCH 09/16] resource/aws_db_proxy: Update documentation --- website/aws.erb | 3744 ------------------------- website/docs/r/db_proxy.html.markdown | 4 +- 2 files changed, 2 insertions(+), 3746 deletions(-) delete mode 100644 website/aws.erb diff --git a/website/aws.erb b/website/aws.erb deleted file mode 100644 index 74cb45eb5dc..00000000000 --- a/website/aws.erb +++ /dev/null @@ -1,3744 +0,0 @@ -<% wrap_layout :inner do %> - <% content_for :sidebar do %> - - <% end %> - <%= yield %> -<% end %> diff --git a/website/docs/r/db_proxy.html.markdown b/website/docs/r/db_proxy.html.markdown index 5e0c57c03b1..30cb71cef0a 100644 --- a/website/docs/r/db_proxy.html.markdown +++ b/website/docs/r/db_proxy.html.markdown @@ -8,7 +8,7 @@ description: |- # Resource: aws_db_proxy -Provides an RDS DB proxy resource. +Provides an RDS DB proxy resource. For additional information, see the [RDS User Guide](https://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/rds-proxy.html). ## Example Usage @@ -42,7 +42,7 @@ resource "aws_db_proxy" "example" { The following arguments are supported: * `name` - (Required) The identifier for the proxy. This name must be unique for all proxies owned by your AWS account in the specified AWS Region. An identifier must begin with a letter and must contain only ASCII letters, digits, and hyphens; it can't end with a hyphen or contain two consecutive hyphens. -* `auth` - (Required) The authorization mechanism that the proxy uses. +* `auth` - (Required) Configuration block(s) with authorization mechanisms to connect to the associated instances or clusters. Described below. * `debug_logging` - (Optional) Whether the proxy includes detailed information about SQL statements in its logs. This information helps you to debug issues involving SQL behavior or the performance and scalability of the proxy connections. The debug information includes the text of SQL statements that you submit through the proxy. Thus, only enable this setting when needed for debugging, and only when you have security measures in place to safeguard any sensitive information that appears in the logs. * `engine_family` - (Required, Forces new resource) The kinds of databases that the proxy can connect to. This value determines which database network protocol the proxy recognizes when it interprets network traffic to and from the database. Currently, this value is always `MYSQL`. The engine family applies to both RDS MySQL and Aurora MySQL. * `idle_client_timeout` - (Optional) The number of seconds that a connection to the proxy can be inactive before the proxy disconnects it. You can set this value higher or lower than the connection timeout limit for the associated database. From ffbcf7ddcc49baef6cb547bdd680511f9cbd8e20 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 25 Aug 2020 21:24:41 +0100 Subject: [PATCH 10/16] resource/aws_db_proxy: Address review comments I --- aws/resource_aws_db_proxy.go | 130 ++++++++++++++++-------------- aws/resource_aws_db_proxy_test.go | 61 ++++++++++---- 2 files changed, 114 insertions(+), 77 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index 919449049ac..ce7cfd3fe70 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -8,10 +8,10 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/rds" - "github.com/hashicorp/terraform-plugin-sdk/helper/hashcode" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode" "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) @@ -49,10 +49,7 @@ func resourceAwsDbProxy() *schema.Resource { Type: schema.TypeString, Required: true, ForceNew: true, - ValidateFunc: validation.StringInSlice([]string{ - rds.EngineFamilyMysql, - rds.EngineFamilyPostgresql, - }, false), + ValidateFunc: validation.StringInSlice(rds.EngineFamily_Values(), false), }, "idle_client_timeout": { Type: schema.TypeInt, @@ -84,15 +81,12 @@ func resourceAwsDbProxy() *schema.Resource { "auth": { Type: schema.TypeSet, Required: true, - ForceNew: false, Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "auth_scheme": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - rds.AuthSchemeSecrets, - }, false), + ValidateFunc: validation.StringInSlice(rds.AuthScheme_Values(), false), }, "description": { Type: schema.TypeString, @@ -101,10 +95,7 @@ func resourceAwsDbProxy() *schema.Resource { "iam_auth": { Type: schema.TypeString, Optional: true, - ValidateFunc: validation.StringInSlice([]string{ - rds.IAMAuthModeDisabled, - rds.IAMAuthModeRequired, - }, false), + ValidateFunc: validation.StringInSlice(rds.IAMAuthMode_Values(), false), }, "secret_arn": { Type: schema.TypeString, @@ -119,7 +110,10 @@ func resourceAwsDbProxy() *schema.Resource { }, Set: resourceAwsDbProxyAuthHash, }, - + "endpoint": { + Type: schema.TypeString, + Computed: true, + }, "tags": tagsSchema(), }, } @@ -132,9 +126,7 @@ func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { params := rds.CreateDBProxyInput{ Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), DBProxyName: aws.String(d.Get("name").(string)), - DebugLogging: aws.Bool(d.Get("debug_logging").(bool)), EngineFamily: aws.String(d.Get("engine_family").(string)), - RequireTLS: aws.Bool(d.Get("require_tls").(bool)), RoleArn: aws.String(d.Get("role_arn").(string)), Tags: tags, VpcSubnetIds: expandStringSet(d.Get("vpc_subnet_ids").(*schema.Set)), @@ -163,7 +155,6 @@ func resourceAwsDbProxyCreate(d *schema.ResourceData, meta interface{}) error { } d.SetId(aws.StringValue(resp.DBProxy.DBProxyName)) - d.Set("arn", resp.DBProxy.DBProxyArn) log.Printf("[INFO] DB Proxy ID: %s", d.Id()) stateChangeConf := &resource.StateChangeConf{ @@ -276,17 +267,27 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { d.SetId("") return nil } - return err + return fmt.Errorf("Error reading RDS DB Proxy (%s): %w", d.Id(), err) } - if len(resp.DBProxies) != 1 || - *resp.DBProxies[0].DBProxyName != d.Id() { - return fmt.Errorf("Unable to find DB Proxy: %#v", resp.DBProxies) + var dbProxy *rds.DBProxy + for _, proxy := range resp.DBProxies { + if proxy == nil { + continue + } + + if aws.StringValue(proxy.DBProxyName) == d.Id() { + dbProxy = proxy + break + } + } + if dbProxy == nil { + log.Printf("[WARN] DB Proxy (%s) not found, removing from state", d.Id()) + d.SetId("") + return nil } - dbProxy := resp.DBProxies[0] - - d.Set("arn", aws.StringValue(dbProxy.DBProxyArn)) + d.Set("arn", dbProxy.DBProxyArn) d.Set("auth", flattenDbProxyAuths(dbProxy.Auth)) d.Set("name", dbProxy.DBProxyName) d.Set("debug_logging", dbProxy.DebugLogging) @@ -296,6 +297,7 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { d.Set("role_arn", dbProxy.RoleArn) d.Set("vpc_subnet_ids", flattenStringSet(dbProxy.VpcSubnetIds)) d.Set("security_group_ids", flattenStringSet(dbProxy.VpcSecurityGroupIds)) + d.Set("endpoint", dbProxy.Endpoint) tags, err := keyvaluetags.RdsListTags(conn, d.Get("arn").(string)) @@ -303,10 +305,6 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error listing tags for RDS DB Proxy (%s): %s", d.Get("arn").(string), err) } - // if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { - // return fmt.Errorf("error setting tags: %s", err) - // } - if err := d.Set("tags", tags.IgnoreAws().IgnoreConfig(ignoreTagsConfig).Map()); err != nil { return fmt.Errorf("Error setting tags: %s", err) } @@ -317,41 +315,51 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { conn := meta.(*AWSClient).rdsconn - oName, nName := d.GetChange("name") - - params := rds.ModifyDBProxyInput{ - Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), - DBProxyName: aws.String(oName.(string)), - NewDBProxyName: aws.String(nName.(string)), - DebugLogging: aws.Bool(d.Get("debug_logging").(bool)), - RequireTLS: aws.Bool(d.Get("require_tls").(bool)), - RoleArn: aws.String(d.Get("role_arn").(string)), - } + if d.HasChanges( + "auth", + "debug_logging", + "idle_client_timeout", + "name", + "require_tls", + "role_arn", + "vpc_security_group_ids") { + + oName, nName := d.GetChange("name") + + params := rds.ModifyDBProxyInput{ + Auth: expandDbProxyAuth(d.Get("auth").(*schema.Set).List()), + DBProxyName: aws.String(oName.(string)), + NewDBProxyName: aws.String(nName.(string)), + DebugLogging: aws.Bool(d.Get("debug_logging").(bool)), + RequireTLS: aws.Bool(d.Get("require_tls").(bool)), + RoleArn: aws.String(d.Get("role_arn").(string)), + } - if v, ok := d.GetOk("idle_client_timeout"); ok { - params.IdleClientTimeout = aws.Int64(int64(v.(int))) - } + if v, ok := d.GetOk("idle_client_timeout"); ok { + params.IdleClientTimeout = aws.Int64(int64(v.(int))) + } - if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 { - params.SecurityGroups = expandStringSet(v) - } + if v := d.Get("vpc_security_group_ids").(*schema.Set); v.Len() > 0 { + params.SecurityGroups = expandStringSet(v) + } - log.Printf("[DEBUG] Update DB Proxy: %#v", params) - _, err := conn.ModifyDBProxy(¶ms) - if err != nil { - return fmt.Errorf("Error updating DB Proxy: %s", err) - } + log.Printf("[DEBUG] Update DB Proxy: %#v", params) + _, err := conn.ModifyDBProxy(¶ms) + if err != nil { + return fmt.Errorf("Error updating DB Proxy: %s", err) + } - stateChangeConf := &resource.StateChangeConf{ - Pending: []string{rds.DBProxyStatusModifying}, - Target: []string{rds.DBProxyStatusAvailable}, - Refresh: resourceAwsDbProxyRefreshFunc(conn, d.Id()), - Timeout: d.Timeout(schema.TimeoutCreate), - } + stateChangeConf := &resource.StateChangeConf{ + Pending: []string{rds.DBProxyStatusModifying}, + Target: []string{rds.DBProxyStatusAvailable}, + Refresh: resourceAwsDbProxyRefreshFunc(conn, d.Id()), + Timeout: d.Timeout(schema.TimeoutUpdate), + } - _, err = stateChangeConf.WaitForState() - if err != nil { - return fmt.Errorf("Error waiting for DB Proxy update: %s", err) + _, err = stateChangeConf.WaitForState() + if err != nil { + return fmt.Errorf("Error waiting for DB Proxy update: %s", err) + } } if d.HasChange("tags") { diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index b015f1994ba..8adb1bbb0c9 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -9,9 +9,9 @@ import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/rds" - "github.com/hashicorp/terraform-plugin-sdk/helper/acctest" - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" ) func init() { @@ -83,7 +83,20 @@ func TestAccAWSDBProxy_basic(t *testing.T) { resource.TestCheckResourceAttr( resourceName, "engine_family", "MYSQL"), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "rds", regexp.MustCompile(`db-proxy:.+`)), - ), + resource.TestCheckResourceAttr(resourceName, "auth.#", "1"), + tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "auth.*", map[string]string{ + "auth_scheme": "SECRETS", + "description": "test", + "iam_auth": "DISABLED", + }), + resource.TestCheckResourceAttr(resourceName, "debug_logging", "false"), + resource.TestMatchResourceAttr(resourceName, "endpoint", regexp.MustCompile(`fill me in`)), + resource.TestCheckResourceAttr(resourceName, "idle_client_timeout", "1800"), + resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test", "arn"), + resource.TestCheckResourceAttr(resourceName, "require_tls", "false"), + resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), ), }, { ResourceName: resourceName, @@ -94,6 +107,27 @@ func TestAccAWSDBProxy_basic(t *testing.T) { }) } +func TestAccAWSDBProxy_disappears(t *testing.T) { + var v rds.DBProxy + resourceName := "aws_db_proxy.test" + name := fmt.Sprintf("tf-acc-db-proxy-%d", acctest.RandInt()) + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfig(name), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &v), + testAccCheckResourceDisappears(testAccProvider, resourceAwsDbProxy(), resourceName), + ), + ExpectNonEmptyPlan: true, + }, + }, + }) +} + func testAccCheckAWSDBProxyDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).rdsconn @@ -115,12 +149,7 @@ func testAccCheckAWSDBProxyDestroy(s *terraform.State) error { } } - // Verify the error - newerr, ok := err.(awserr.Error) - if !ok { - return err - } - if newerr.Code() != rds.ErrCodeDBProxyNotFoundFault { + if !isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") { return err } } @@ -187,14 +216,14 @@ resource "aws_db_proxy" "test" { } tags = { - Name = "%s" + Name = "%[1]s" } } # Secrets Manager setup resource "aws_secretsmanager_secret" "test" { - name = "%s" + name = "%[1]s" recovery_window_in_days = 0 } @@ -206,7 +235,7 @@ resource "aws_secretsmanager_secret_version" "test" { # IAM setup resource "aws_iam_role" "test" { - name = "%s" + name = "%[1]s" assume_role_policy = data.aws_iam_policy_document.assume.json } @@ -256,7 +285,7 @@ resource "aws_vpc" "test" { cidr_block = "10.0.0.0/16" tags = { - Name = "%s" + Name = "%[1]s" } } @@ -267,8 +296,8 @@ resource "aws_subnet" "test" { vpc_id = aws_vpc.test.id tags = { - Name = "%s-${count.index}" + Name = "%[1]s-${count.index}" } } -`, n, n, n, n, n, n) +`, n) } From 67dd546c48314e8ee1119474afe781fdfc79bab1 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 25 Aug 2020 21:27:39 +0100 Subject: [PATCH 11/16] resource/aws_db_proxy: Address review comments II --- aws/resource_aws_db_proxy.go | 18 +++++++++--------- aws/resource_aws_db_proxy_test.go | 2 +- 2 files changed, 10 insertions(+), 10 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index ce7cfd3fe70..42450c0f99f 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -46,9 +46,9 @@ func resourceAwsDbProxy() *schema.Resource { Optional: true, }, "engine_family": { - Type: schema.TypeString, - Required: true, - ForceNew: true, + Type: schema.TypeString, + Required: true, + ForceNew: true, ValidateFunc: validation.StringInSlice(rds.EngineFamily_Values(), false), }, "idle_client_timeout": { @@ -84,8 +84,8 @@ func resourceAwsDbProxy() *schema.Resource { Elem: &schema.Resource{ Schema: map[string]*schema.Schema{ "auth_scheme": { - Type: schema.TypeString, - Optional: true, + Type: schema.TypeString, + Optional: true, ValidateFunc: validation.StringInSlice(rds.AuthScheme_Values(), false), }, "description": { @@ -93,8 +93,8 @@ func resourceAwsDbProxy() *schema.Resource { Optional: true, }, "iam_auth": { - Type: schema.TypeString, - Optional: true, + Type: schema.TypeString, + Optional: true, ValidateFunc: validation.StringInSlice(rds.IAMAuthMode_Values(), false), }, "secret_arn": { @@ -275,7 +275,7 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { if proxy == nil { continue } - + if aws.StringValue(proxy.DBProxyName) == d.Id() { dbProxy = proxy break @@ -323,7 +323,7 @@ func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { "require_tls", "role_arn", "vpc_security_group_ids") { - + oName, nName := d.GetChange("name") params := rds.ModifyDBProxyInput{ diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index 8adb1bbb0c9..db4b80f41c4 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -96,7 +96,7 @@ func TestAccAWSDBProxy_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "require_tls", "false"), resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), - tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), ), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id")), }, { ResourceName: resourceName, From a75f68a6d179061b00210fb146235668baa34cf6 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Tue, 25 Aug 2020 22:10:57 +0100 Subject: [PATCH 12/16] resource/aws_db_proxy: Address review comments III --- aws/resource_aws_db_proxy_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index db4b80f41c4..c3e885e3235 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -7,11 +7,11 @@ import ( "testing" "github.com/aws/aws-sdk-go/aws" - "github.com/aws/aws-sdk-go/aws/awserr" "github.com/aws/aws-sdk-go/service/rds" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/terraform" + "github.com/terraform-providers/terraform-provider-aws/aws/internal/tfawsresource" ) func init() { @@ -90,13 +90,13 @@ func TestAccAWSDBProxy_basic(t *testing.T) { "iam_auth": "DISABLED", }), resource.TestCheckResourceAttr(resourceName, "debug_logging", "false"), - resource.TestMatchResourceAttr(resourceName, "endpoint", regexp.MustCompile(`fill me in`)), resource.TestCheckResourceAttr(resourceName, "idle_client_timeout", "1800"), resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test", "arn"), resource.TestCheckResourceAttr(resourceName, "require_tls", "false"), resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), - tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id")), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), + resource.TestMatchResourceAttr(resourceName, "endpoint", regexp.MustCompile(`fill me in`))), }, { ResourceName: resourceName, From e7baee062feeee230635ee730cd345138b8e1fd3 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Sat, 29 Aug 2020 13:37:44 +0100 Subject: [PATCH 13/16] Update website/docs/r/db_proxy.html.markdown Co-authored-by: Brian Flad --- website/docs/r/db_proxy.html.markdown | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/website/docs/r/db_proxy.html.markdown b/website/docs/r/db_proxy.html.markdown index 30cb71cef0a..0253f19ceff 100644 --- a/website/docs/r/db_proxy.html.markdown +++ b/website/docs/r/db_proxy.html.markdown @@ -19,15 +19,15 @@ resource "aws_db_proxy" "example" { engine_family = "MYSQL" idle_client_timeout = 1800 require_tls = true - role_arn = "arn:aws:iam:us-east-1:123456789012:role/example" - vpc_security_group_ids = ["sg-12345678901234567"] - vpc_subnet_ids = ["subnet-12345678901234567"] + role_arn = aws_iam_role.example.arn + vpc_security_group_ids = [aws_security_group.example.id] + vpc_subnet_ids = [aws_subnet.example.id] auth { auth_scheme = "SECRETS" description = "example" iam_auth = "DISABLED" - secret_arn = "arn:aws:secretsmanager:us-east-1:123456789012:secret:example" + secret_arn = aws_secretsmanager_secret.example.arn } tags = { From 164cc25249176df849d242079bcbbbe5af6d427e Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Sat, 29 Aug 2020 14:08:52 +0100 Subject: [PATCH 14/16] resource/aws_db_proxy: Address review comments IV --- aws/resource_aws_db_proxy.go | 28 ++-------------------------- aws/resource_aws_db_proxy_test.go | 8 +++----- 2 files changed, 5 insertions(+), 31 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index 42450c0f99f..d49db3aee20 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -1,7 +1,6 @@ package aws import ( - "bytes" "fmt" "log" "time" @@ -11,7 +10,6 @@ import ( "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" - "github.com/terraform-providers/terraform-provider-aws/aws/internal/hashcode" "github.com/terraform-providers/terraform-provider-aws/aws/internal/keyvaluetags" ) @@ -108,7 +106,6 @@ func resourceAwsDbProxy() *schema.Resource { }, }, }, - Set: resourceAwsDbProxyAuthHash, }, "endpoint": { Type: schema.TypeString, @@ -244,12 +241,12 @@ func flattenDbProxyAuth(userAuthConfig *rds.UserAuthConfigInfo) map[string]inter return m } -func flattenDbProxyAuths(userAuthConfigs []*rds.UserAuthConfigInfo) *schema.Set { +func flattenDbProxyAuths(userAuthConfigs []*rds.UserAuthConfigInfo) []interface{} { s := []interface{}{} for _, v := range userAuthConfigs { s = append(s, flattenDbProxyAuth(v)) } - return schema.NewSet(resourceAwsDbProxyAuthHash, s) + return s } func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { @@ -398,24 +395,3 @@ func resourceAwsDbProxyDelete(d *schema.ResourceData, meta interface{}) error { return nil } - -func resourceAwsDbProxyAuthHash(v interface{}) int { - var buf bytes.Buffer - m := v.(map[string]interface{}) - if v, ok := m["auth_scheme"].(string); ok { - buf.WriteString(fmt.Sprintf("%s-", v)) - } - if v, ok := m["description"].(string); ok { - buf.WriteString(fmt.Sprintf("%s-", v)) - } - if v, ok := m["iam_auth"].(string); ok { - buf.WriteString(fmt.Sprintf("%s-", v)) - } - if v, ok := m["secret_arn"].(string); ok { - buf.WriteString(fmt.Sprintf("%s-", v)) - } - if v, ok := m["username"].(string); ok { - buf.WriteString(fmt.Sprintf("%s-", v)) - } - return hashcode.String(buf.String()) -} diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index c3e885e3235..246c246556d 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -78,10 +78,8 @@ func TestAccAWSDBProxy_basic(t *testing.T) { Config: testAccAWSDBProxyConfig(name), Check: resource.ComposeTestCheckFunc( testAccCheckAWSDBProxyExists(resourceName, &v), - resource.TestCheckResourceAttr( - resourceName, "name", name), - resource.TestCheckResourceAttr( - resourceName, "engine_family", "MYSQL"), + resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "engine_family", "MYSQL"), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "rds", regexp.MustCompile(`db-proxy:.+`)), resource.TestCheckResourceAttr(resourceName, "auth.#", "1"), tfawsresource.TestCheckTypeSetElemNestedAttrs(resourceName, "auth.*", map[string]string{ @@ -96,7 +94,7 @@ func TestAccAWSDBProxy_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), - resource.TestMatchResourceAttr(resourceName, "endpoint", regexp.MustCompile(`fill me in`))), + resource.TestMatchResourceAttr(resourceName, "endpoint", regexp.MustCompile(`^[\w\-\.]+\.rds\.amazonaws\.com$`))), }, { ResourceName: resourceName, From 6640855bea9739c0c1b8fa0b4bac2c431231b555 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Mon, 31 Aug 2020 10:52:32 +0100 Subject: [PATCH 15/16] resource/aws_db_proxy: Adding new tests --- aws/resource_aws_db_proxy.go | 8 +- aws/resource_aws_db_proxy_test.go | 980 ++++++++++++++++++++++++++---- 2 files changed, 873 insertions(+), 115 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index d49db3aee20..cd77a409abc 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -52,6 +52,7 @@ func resourceAwsDbProxy() *schema.Resource { "idle_client_timeout": { Type: schema.TypeInt, Optional: true, + Computed: true, }, "require_tls": { Type: schema.TypeBool, @@ -293,7 +294,7 @@ func resourceAwsDbProxyRead(d *schema.ResourceData, meta interface{}) error { d.Set("require_tls", dbProxy.RequireTLS) d.Set("role_arn", dbProxy.RoleArn) d.Set("vpc_subnet_ids", flattenStringSet(dbProxy.VpcSubnetIds)) - d.Set("security_group_ids", flattenStringSet(dbProxy.VpcSecurityGroupIds)) + d.Set("vpc_security_group_ids", flattenStringSet(dbProxy.VpcSecurityGroupIds)) d.Set("endpoint", dbProxy.Endpoint) tags, err := keyvaluetags.RdsListTags(conn, d.Get("arn").(string)) @@ -346,6 +347,11 @@ func resourceAwsDbProxyUpdate(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error updating DB Proxy: %s", err) } + // DB Proxy Name is used as an ID as the API doesn't provide a way to read/ + // update/delete DB proxies using the ARN + d.SetId(nName.(string)) + log.Printf("[INFO] Updated DB Proxy ID: %s", d.Id()) + stateChangeConf := &resource.StateChangeConf{ Pending: []string{rds.DBProxyStatusModifying}, Target: []string{rds.DBProxyStatusAvailable}, diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index 246c246556d..75b260de3aa 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -67,7 +67,7 @@ func testSweepRdsDbProxies(region string) error { func TestAccAWSDBProxy_basic(t *testing.T) { var v rds.DBProxy resourceName := "aws_db_proxy.test" - name := fmt.Sprintf("tf-acc-db-proxy-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, @@ -75,10 +75,10 @@ func TestAccAWSDBProxy_basic(t *testing.T) { CheckDestroy: testAccCheckAWSDBProxyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSDBProxyConfig(name), + Config: testAccAWSDBProxyConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSDBProxyExists(resourceName, &v), - resource.TestCheckResourceAttr(resourceName, "name", name), + resource.TestCheckResourceAttr(resourceName, "name", rName), resource.TestCheckResourceAttr(resourceName, "engine_family", "MYSQL"), testAccMatchResourceAttrRegionalARN(resourceName, "arn", "rds", regexp.MustCompile(`db-proxy:.+`)), resource.TestCheckResourceAttr(resourceName, "auth.#", "1"), @@ -90,7 +90,7 @@ func TestAccAWSDBProxy_basic(t *testing.T) { resource.TestCheckResourceAttr(resourceName, "debug_logging", "false"), resource.TestCheckResourceAttr(resourceName, "idle_client_timeout", "1800"), resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test", "arn"), - resource.TestCheckResourceAttr(resourceName, "require_tls", "false"), + resource.TestCheckResourceAttr(resourceName, "require_tls", "true"), resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), @@ -105,17 +105,429 @@ func TestAccAWSDBProxy_basic(t *testing.T) { }) } +func TestAccAWSDBProxy_Name(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + nName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "name", rName), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigName(rName, nName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "name", nName), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_DebugLogging(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigDebugLogging(rName, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "debug_logging", "true"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigDebugLogging(rName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "debug_logging", "false"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_IdleClientTimeout(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigIdleClientTimeout(rName, 900), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "idle_client_timeout", "900"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigIdleClientTimeout(rName, 3600), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "idle_client_timeout", "3600"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_RequireTls(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigRequireTls(rName, true), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "require_tls", "true"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigRequireTls(rName, false), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "require_tls", "false"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_RoleArn(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + nName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test", "arn"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigRoleArn(rName, nName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttrPair(resourceName, "role_arn", "aws_iam_role.test2", "arn"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_VpcSecurityGroupIds(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + nName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "vpc_security_group_ids.#", "1"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_security_group_ids.*", "aws_security_group.test", "id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigVpcSecurityGroupIds(rName, nName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "vpc_security_group_ids.#", "1"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_security_group_ids.*", "aws_security_group.test2", "id"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_VpcSubnetIds(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + nName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigVpcSubnetIds(rName, nName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test2.0", "id"), + tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test2.0", "id"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_AuthDescription(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + description := "foo" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "auth.0.description", "test"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigAuthDescription(rName, description), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "auth.0.description", description), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_AuthIamAuth(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + iamAuth := "REQUIRED" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "auth.0.iam_auth", "DISABLED"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigAuthIamAuth(rName, iamAuth), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "auth.0.iam_auth", iamAuth), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_AuthSecretArn(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + nName := acctest.RandomWithPrefix("tf-acc-test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttrPair(resourceName, "auth.0.secret_arn", "aws_secretsmanager_secret.test", "arn"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigAuthSecretArn(rName, nName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttrPair(resourceName, "auth.0.secret_arn", "aws_secretsmanager_secret.test2", "arn"), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_AuthUsername(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + username := "foo" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "auth.0.username", ""), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigAuthUsername(rName, username), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "auth.0.username", username), + ), + }, + }, + }) +} + +func TestAccAWSDBProxy_Tags(t *testing.T) { + var dbProxy rds.DBProxy + resourceName := "aws_db_proxy.test" + rName := acctest.RandomWithPrefix("tf-acc-test") + key := "foo" + value := "bar" + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testAccCheckAWSDBProxyDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAWSDBProxyConfigName(rName, rName), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "tags.#", "0"), + ), + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + { + Config: testAccAWSDBProxyConfigTags(rName, key, value), + Check: resource.ComposeTestCheckFunc( + testAccCheckAWSDBProxyExists(resourceName, &dbProxy), + resource.TestCheckResourceAttr(resourceName, "tags.foo", value), + ), + }, + }, + }) +} + func TestAccAWSDBProxy_disappears(t *testing.T) { var v rds.DBProxy resourceName := "aws_db_proxy.test" - name := fmt.Sprintf("tf-acc-db-proxy-%d", acctest.RandInt()) + rName := acctest.RandomWithPrefix("tf-acc-test") resource.ParallelTest(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAWSDBProxyDestroy, Steps: []resource.TestStep{ { - Config: testAccAWSDBProxyConfig(name), + Config: testAccAWSDBProxyConfig(rName), Check: resource.ComposeTestCheckFunc( testAccCheckAWSDBProxyExists(resourceName, &v), testAccCheckResourceDisappears(testAccProvider, resourceAwsDbProxy(), resourceName), @@ -126,84 +538,332 @@ func TestAccAWSDBProxy_disappears(t *testing.T) { }) } -func testAccCheckAWSDBProxyDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*AWSClient).rdsconn +func testAccCheckAWSDBProxyDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*AWSClient).rdsconn + + for _, rs := range s.RootModule().Resources { + if rs.Type != "aws_db_proxy" { + continue + } + + // Try to find the Group + resp, err := conn.DescribeDBProxies( + &rds.DescribeDBProxiesInput{ + DBProxyName: aws.String(rs.Primary.ID), + }) + + if err == nil { + if len(resp.DBProxies) != 0 && + *resp.DBProxies[0].DBProxyName == rs.Primary.ID { + return fmt.Errorf("DB Proxy still exists") + } + } + + if !isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") { + return err + } + } + + return nil +} + +func testAccCheckAWSDBProxyExists(n string, v *rds.DBProxy) resource.TestCheckFunc { + return func(s *terraform.State) error { + rs, ok := s.RootModule().Resources[n] + if !ok { + return fmt.Errorf("Not found: %s", n) + } + + if rs.Primary.ID == "" { + return fmt.Errorf("No DB Proxy ID is set") + } + + conn := testAccProvider.Meta().(*AWSClient).rdsconn + + opts := rds.DescribeDBProxiesInput{ + DBProxyName: aws.String(rs.Primary.ID), + } + + resp, err := conn.DescribeDBProxies(&opts) + + if err != nil { + return err + } + + if len(resp.DBProxies) != 1 || + *resp.DBProxies[0].DBProxyName != rs.Primary.ID { + return fmt.Errorf("DB Proxy not found") + } + + *v = *resp.DBProxies[0] + + return nil + } +} + +func testAccAWSDBProxyConfigBase(rName string) string { + return fmt.Sprintf(` +# Secrets Manager setup + +resource "aws_secretsmanager_secret" "test" { + name = "%[1]s" + recovery_window_in_days = 0 +} + +resource "aws_secretsmanager_secret_version" "test" { + secret_id = aws_secretsmanager_secret.test.id + secret_string = "{\"username\":\"db_user\",\"password\":\"db_user_password\"}" +} + +# IAM setup + +resource "aws_iam_role" "test" { + name = "%[1]s" + assume_role_policy = data.aws_iam_policy_document.assume.json +} + +data "aws_iam_policy_document" "assume" { + statement { + actions = ["sts:AssumeRole"] + principals { + type = "Service" + identifiers = ["rds.amazonaws.com"] + } + } +} + +resource "aws_iam_role_policy" "test" { + role = aws_iam_role.test.id + policy = data.aws_iam_policy_document.test.json +} + +data "aws_iam_policy_document" "test" { + statement { + actions = [ + "secretsmanager:GetRandomPassword", + "secretsmanager:CreateSecret", + "secretsmanager:ListSecrets", + ] + resources = ["*"] + } + + statement { + actions = ["secretsmanager:*"] + resources = [aws_secretsmanager_secret.test.arn] + } +} + +# VPC setup + +data "aws_availability_zones" "available" { + state = "available" + + filter { + name = "opt-in-status" + values = ["opt-in-not-required"] + } +} + +resource "aws_vpc" "test" { + cidr_block = "10.0.0.0/16" + + tags = { + Name = "%[1]s" + } +} + +resource "aws_security_group" "test" { + name = "%[1]s" + vpc_id = aws_vpc.test.id +} + +resource "aws_subnet" "test" { + count = 2 + cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index) + availability_zone = data.aws_availability_zones.available.names[count.index] + vpc_id = aws_vpc.test.id + + tags = { + Name = "%[1]s-${count.index}" + } +} +`, rName) +} + +func testAccAWSDBProxyConfig(rName string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] + + name = "%[1]s" + debug_logging = false + engine_family = "MYSQL" + idle_client_timeout = 1800 + require_tls = true + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } + + tags = { + Name = "%[1]s" + } +} +`, rName) +} + +func testAccAWSDBProxyConfigName(rName, nName string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] + + name = "%[2]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } +} +`, rName, nName) +} + +func testAccAWSDBProxyConfigDebugLogging(rName string, debugLogging bool) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] - for _, rs := range s.RootModule().Resources { - if rs.Type != "aws_db_proxy" { - continue - } + name = "%[1]s" + debug_logging = %[2]t + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_subnet_ids = aws_subnet.test.*.id - // Try to find the Group - resp, err := conn.DescribeDBProxies( - &rds.DescribeDBProxiesInput{ - DBProxyName: aws.String(rs.Primary.ID), - }) + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } +} +`, rName, debugLogging) +} - if err == nil { - if len(resp.DBProxies) != 0 && - *resp.DBProxies[0].DBProxyName == rs.Primary.ID { - return fmt.Errorf("DB Proxy still exists") - } - } +func testAccAWSDBProxyConfigIdleClientTimeout(rName string, idleClientTimeout int) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] - if !isAWSErr(err, rds.ErrCodeDBProxyNotFoundFault, "") { - return err - } - } + name = "%[1]s" + idle_client_timeout = %[2]d + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_subnet_ids = aws_subnet.test.*.id - return nil + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } +} +`, rName, idleClientTimeout) } -func testAccCheckAWSDBProxyExists(n string, v *rds.DBProxy) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - if !ok { - return fmt.Errorf("Not found: %s", n) - } +func testAccAWSDBProxyConfigRequireTls(rName string, requireTls bool) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] - if rs.Primary.ID == "" { - return fmt.Errorf("No DB Proxy ID is set") - } + name = "%[1]s" + require_tls = %[2]t + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_subnet_ids = aws_subnet.test.*.id - conn := testAccProvider.Meta().(*AWSClient).rdsconn + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } +} +`, rName, requireTls) +} - opts := rds.DescribeDBProxiesInput{ - DBProxyName: aws.String(rs.Primary.ID), - } +func testAccAWSDBProxyConfigRoleArn(rName, nName string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test2 + ] - resp, err := conn.DescribeDBProxies(&opts) + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test2.arn + vpc_subnet_ids = aws_subnet.test.*.id - if err != nil { - return err - } + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } +} - if len(resp.DBProxies) != 1 || - *resp.DBProxies[0].DBProxyName != rs.Primary.ID { - return fmt.Errorf("DB Proxy not found") - } +# IAM setup - *v = *resp.DBProxies[0] +resource "aws_iam_role" "test2" { + name = "%[2]s" + assume_role_policy = data.aws_iam_policy_document.assume.json +} - return nil - } +resource "aws_iam_role_policy" "test2" { + role = aws_iam_role.test.id + policy = data.aws_iam_policy_document.test.json +} +`, rName, nName) } -func testAccAWSDBProxyConfig(n string) string { - return fmt.Sprintf(` +func testAccAWSDBProxyConfigVpcSecurityGroupIds(rName, nName string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` resource "aws_db_proxy" "test" { depends_on = [ aws_secretsmanager_secret_version.test, aws_iam_role_policy.test ] - name = "%s" - debug_logging = false + name = "%[1]s" engine_family = "MYSQL" - idle_client_timeout = 1800 - require_tls = true role_arn = aws_iam_role.test.arn - vpc_security_group_ids = [] + vpc_security_group_ids = [aws_security_group.test2.id] vpc_subnet_ids = aws_subnet.test.*.id auth { @@ -212,90 +872,182 @@ resource "aws_db_proxy" "test" { iam_auth = "DISABLED" secret_arn = aws_secretsmanager_secret.test.arn } +} - tags = { - Name = "%[1]s" - } +resource "aws_security_group" "test2" { + name = "%[2]s" + vpc_id = aws_vpc.test.id +} +`, rName, nName) } -# Secrets Manager setup +func testAccAWSDBProxyConfigVpcSubnetIds(rName, nName string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] -resource "aws_secretsmanager_secret" "test" { - name = "%[1]s" - recovery_window_in_days = 0 + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test2.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } } -resource "aws_secretsmanager_secret_version" "test" { - secret_id = aws_secretsmanager_secret.test.id - secret_string = "{\"username\":\"db_user\",\"password\":\"db_user_password\"}" +resource "aws_subnet" "test2" { + count = 2 + cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index + 2) + availability_zone = data.aws_availability_zones.available.names[count.index] + vpc_id = aws_vpc.test.id + + tags = { + Name = "%[2]s-${count.index}" + } + } +`, rName, nName) } -# IAM setup +func testAccAWSDBProxyConfigAuthDescription(rName, description string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] -resource "aws_iam_role" "test" { - name = "%[1]s" - assume_role_policy = data.aws_iam_policy_document.assume.json -} + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id -data "aws_iam_policy_document" "assume" { - statement { - actions = ["sts:AssumeRole"] - principals { - type = "Service" - identifiers = ["rds.amazonaws.com"] - } + auth { + auth_scheme = "SECRETS" + description = "%[2]s" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn } } - -resource "aws_iam_role_policy" "test" { - role = aws_iam_role.test.id - policy = data.aws_iam_policy_document.test.json +`, rName, description) } -data "aws_iam_policy_document" "test" { - statement { - actions = [ - "secretsmanager:GetRandomPassword", - "secretsmanager:CreateSecret", - "secretsmanager:ListSecrets", - ] - resources = ["*"] - } +func testAccAWSDBProxyConfigAuthIamAuth(rName, iamAuth string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] - statement { - actions = ["secretsmanager:*"] - resources = [aws_secretsmanager_secret.test.arn] + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + require_tls = true + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "%[2]s" + secret_arn = aws_secretsmanager_secret.test.arn } } +`, rName, iamAuth) +} -# VPC setup +func testAccAWSDBProxyConfigAuthSecretArn(rName, nName string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] -data "aws_availability_zones" "available" { - state = "available" + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id - filter { - name = "opt-in-status" - values = ["opt-in-not-required"] + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test2.arn } } -resource "aws_vpc" "test" { - cidr_block = "10.0.0.0/16" +resource "aws_secretsmanager_secret" "test2" { + name = "%[2]s" + recovery_window_in_days = 0 +} - tags = { - Name = "%[1]s" +resource "aws_secretsmanager_secret_version" "test2" { + secret_id = aws_secretsmanager_secret.test2.id + secret_string = "{\"username\":\"db_user\",\"password\":\"db_user_password\"}" +} +`, rName, nName) +} + +func testAccAWSDBProxyConfigAuthUsername(rName, username string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] + + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + username = "%[2]s" } } +`, rName, username) +} -resource "aws_subnet" "test" { - count = 2 - cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index) - availability_zone = data.aws_availability_zones.available.names[count.index] - vpc_id = aws_vpc.test.id +func testAccAWSDBProxyConfigTags(rName, key, value string) string { + return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` +resource "aws_db_proxy" "test" { + depends_on = [ + aws_secretsmanager_secret_version.test, + aws_iam_role_policy.test + ] + + name = "%[1]s" + engine_family = "MYSQL" + role_arn = aws_iam_role.test.arn + vpc_security_group_ids = [aws_security_group.test.id] + vpc_subnet_ids = aws_subnet.test.*.id + + auth { + auth_scheme = "SECRETS" + description = "test" + iam_auth = "DISABLED" + secret_arn = aws_secretsmanager_secret.test.arn + } tags = { - Name = "%[1]s-${count.index}" + %[2]s = "%[3]s" } } -`, n) +`, rName, key, value) } From c7c1ea27332e758424826e649ba7e531bf4966d7 Mon Sep 17 00:00:00 2001 From: Gareth Oakley Date: Mon, 31 Aug 2020 11:12:09 +0100 Subject: [PATCH 16/16] resource/aws_db_proxy: Address review comments V --- aws/resource_aws_db_proxy.go | 9 -- aws/resource_aws_db_proxy_test.go | 132 ------------------------------ 2 files changed, 141 deletions(-) diff --git a/aws/resource_aws_db_proxy.go b/aws/resource_aws_db_proxy.go index cd77a409abc..8905e8f3627 100644 --- a/aws/resource_aws_db_proxy.go +++ b/aws/resource_aws_db_proxy.go @@ -101,10 +101,6 @@ func resourceAwsDbProxy() *schema.Resource { Optional: true, ValidateFunc: validateArn, }, - "username": { - Type: schema.TypeString, - Optional: true, - }, }, }, }, @@ -220,10 +216,6 @@ func expandDbProxyAuth(l []interface{}) []*rds.UserAuthConfig { userAuthConfig.SecretArn = aws.String(v) } - if v, ok := m["username"].(string); ok && v != "" { - userAuthConfig.UserName = aws.String(v) - } - userAuthConfigs = append(userAuthConfigs, userAuthConfig) } @@ -237,7 +229,6 @@ func flattenDbProxyAuth(userAuthConfig *rds.UserAuthConfigInfo) map[string]inter m["description"] = aws.StringValue(userAuthConfig.Description) m["iam_auth"] = aws.StringValue(userAuthConfig.IAMAuth) m["secret_arn"] = aws.StringValue(userAuthConfig.SecretArn) - m["username"] = aws.StringValue(userAuthConfig.UserName) return m } diff --git a/aws/resource_aws_db_proxy_test.go b/aws/resource_aws_db_proxy_test.go index 75b260de3aa..8cbab8c62bc 100644 --- a/aws/resource_aws_db_proxy_test.go +++ b/aws/resource_aws_db_proxy_test.go @@ -308,44 +308,6 @@ func TestAccAWSDBProxy_VpcSecurityGroupIds(t *testing.T) { }) } -func TestAccAWSDBProxy_VpcSubnetIds(t *testing.T) { - var dbProxy rds.DBProxy - resourceName := "aws_db_proxy.test" - rName := acctest.RandomWithPrefix("tf-acc-test") - nName := acctest.RandomWithPrefix("tf-acc-test") - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSDBProxyDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSDBProxyConfigName(rName, rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSDBProxyExists(resourceName, &dbProxy), - resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), - tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.0", "id"), - tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test.1", "id"), - ), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, - { - Config: testAccAWSDBProxyConfigVpcSubnetIds(rName, nName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSDBProxyExists(resourceName, &dbProxy), - resource.TestCheckResourceAttr(resourceName, "vpc_subnet_ids.#", "2"), - tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test2.0", "id"), - tfawsresource.TestCheckTypeSetElemAttrPair(resourceName, "vpc_subnet_ids.*", "aws_subnet.test2.0", "id"), - ), - }, - }, - }) -} - func TestAccAWSDBProxy_AuthDescription(t *testing.T) { var dbProxy rds.DBProxy resourceName := "aws_db_proxy.test" @@ -448,40 +410,6 @@ func TestAccAWSDBProxy_AuthSecretArn(t *testing.T) { }) } -func TestAccAWSDBProxy_AuthUsername(t *testing.T) { - var dbProxy rds.DBProxy - resourceName := "aws_db_proxy.test" - rName := acctest.RandomWithPrefix("tf-acc-test") - username := "foo" - - resource.ParallelTest(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckAWSDBProxyDestroy, - Steps: []resource.TestStep{ - { - Config: testAccAWSDBProxyConfigName(rName, rName), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSDBProxyExists(resourceName, &dbProxy), - resource.TestCheckResourceAttr(resourceName, "auth.0.username", ""), - ), - }, - { - ResourceName: resourceName, - ImportState: true, - ImportStateVerify: true, - }, - { - Config: testAccAWSDBProxyConfigAuthUsername(rName, username), - Check: resource.ComposeTestCheckFunc( - testAccCheckAWSDBProxyExists(resourceName, &dbProxy), - resource.TestCheckResourceAttr(resourceName, "auth.0.username", username), - ), - }, - }, - }) -} - func TestAccAWSDBProxy_Tags(t *testing.T) { var dbProxy rds.DBProxy resourceName := "aws_db_proxy.test" @@ -881,41 +809,6 @@ resource "aws_security_group" "test2" { `, rName, nName) } -func testAccAWSDBProxyConfigVpcSubnetIds(rName, nName string) string { - return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` -resource "aws_db_proxy" "test" { - depends_on = [ - aws_secretsmanager_secret_version.test, - aws_iam_role_policy.test - ] - - name = "%[1]s" - engine_family = "MYSQL" - role_arn = aws_iam_role.test.arn - vpc_security_group_ids = [aws_security_group.test.id] - vpc_subnet_ids = aws_subnet.test2.*.id - - auth { - auth_scheme = "SECRETS" - description = "test" - iam_auth = "DISABLED" - secret_arn = aws_secretsmanager_secret.test.arn - } -} - -resource "aws_subnet" "test2" { - count = 2 - cidr_block = cidrsubnet(aws_vpc.test.cidr_block, 8, count.index + 2) - availability_zone = data.aws_availability_zones.available.names[count.index] - vpc_id = aws_vpc.test.id - - tags = { - Name = "%[2]s-${count.index}" - } - } -`, rName, nName) -} - func testAccAWSDBProxyConfigAuthDescription(rName, description string) string { return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` resource "aws_db_proxy" "test" { @@ -999,31 +892,6 @@ resource "aws_secretsmanager_secret_version" "test2" { `, rName, nName) } -func testAccAWSDBProxyConfigAuthUsername(rName, username string) string { - return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` -resource "aws_db_proxy" "test" { - depends_on = [ - aws_secretsmanager_secret_version.test, - aws_iam_role_policy.test - ] - - name = "%[1]s" - engine_family = "MYSQL" - role_arn = aws_iam_role.test.arn - vpc_security_group_ids = [aws_security_group.test.id] - vpc_subnet_ids = aws_subnet.test.*.id - - auth { - auth_scheme = "SECRETS" - description = "test" - iam_auth = "DISABLED" - secret_arn = aws_secretsmanager_secret.test.arn - username = "%[2]s" - } -} -`, rName, username) -} - func testAccAWSDBProxyConfigTags(rName, key, value string) string { return testAccAWSDBProxyConfigBase(rName) + fmt.Sprintf(` resource "aws_db_proxy" "test" {