Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

aws_db_cluster_snapshot resource and data source #4526

Merged
merged 2 commits into from
Aug 8, 2018

Conversation

4dz
Copy link
Contributor

@4dz 4dz commented May 13, 2018

Implements #4525

This is my first pull request/attempt at writing in Go! Really its plagiarised/heavily influenced by the aws_db_snapshot source code and documentation.

Changes proposed in this pull request:

  • New data source for DBClusterSnapshot
  • New resource for DBClusterSnapshot
  • Added website documentation

Output from acceptance testing:

% TESTARGS="-run TestAccAwsDbClusterSnapshotDataSource_basic" make testacc
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run TestAccAwsDbClusterSnapshotDataSource_basic -timeout 120m
?       github.com/terraform-providers/terraform-provider-aws   [no test files]
=== RUN   TestAccAwsDbClusterSnapshotDataSource_basic
--- PASS: TestAccAwsDbClusterSnapshotDataSource_basic (693.03s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       693.069s


% TESTARGS="-run TestAccAWSDBClusterSnapshot_basic" make testacc
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run TestAccAWSDBClusterSnapshot_basic -timeout 120m
?       github.com/terraform-providers/terraform-provider-aws   [no test files]
=== RUN   TestAccAWSDBClusterSnapshot_basic
--- PASS: TestAccAWSDBClusterSnapshot_basic (699.86s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       699.907s



% make website-test 
==> Testing aws provider website in Docker...
Error response from daemon: No such container: tf-website-aws-temp
make[1]: [website-provider-test] Error 1 (ignored)
d3e5c906aeef30e7c5b8da82a15089cb8f51ded6093aedebee6fba91895603a7
~/go/src/github.com/hashicorp/terraform-website/content/scripts/check-links.sh "http://127.0.0.1:4567/docs/providers/aws/"
No data received.
No data received.
No data received.
No data received.
No data received.
No data received.
2018-05-13 12:18:35 URL:http://127.0.0.1:4567/docs/providers/aws/ [135868/135868] -> "index.html.tmp.tmp" [1]
2018-05-13 12:18:35 URL:http://127.0.0.1:4567/robots.txt [44/44] -> "robots.txt.tmp" [1]
2018-05-13 12:18:35 URL:http://127.0.0.1:4567/docs/providers/aws/index.html [135868/135868] -> "index.html.tmp.tmp" [1]
...
...
...
2018-05-13 12:18:37 URL:http://127.0.0.1:4567/docs/providers/aws/d/db_cluster_snapshot.html [119499/119499] -> "db_cluster_snapshot.html.tmp.tmp" [1]
...
...
2018-05-13 12:19:00 URL:http://127.0.0.1:4567/docs/providers/aws/r/db_cluster_snapshot.html [119377/119377] -> "db_cluster_snapshot.html.tmp.tmp" [1]
...
...
Found no broken links.

FINISHED --2018-05-13 12:19:11--
Total wall clock time: 57s
Downloaded: 449 files, 50M in 0.3s (172 MB/s)
tf-website-aws-temp

4dz added 2 commits May 12, 2018 21:19
…aws_db_snapshot

```
% TESTARGS="-run TestAccAwsDbClusterSnapshotDataSource_basic" make testacc
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run TestAccAwsDbClusterSnapshotDataSource_basic -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAwsDbClusterSnapshotDataSource_basic
--- PASS: TestAccAwsDbClusterSnapshotDataSource_basic (693.03s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	693.069s
% TESTARGS="-run TestAccAWSDBClusterSnapshot_basic" make testacc
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./... -v -run TestAccAWSDBClusterSnapshot_basic -timeout 120m
?   	github.com/terraform-providers/terraform-provider-aws	[no test files]
=== RUN   TestAccAWSDBClusterSnapshot_basic
--- PASS: TestAccAWSDBClusterSnapshot_basic (699.86s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	699.907s
```
@ghost ghost added the size/XL Managed by automation to categorize the size of a PR. label May 13, 2018
@bflad bflad added new-resource Introduces a new resource. new-data-source Introduces a new data source. service/rds Issues and PRs that pertain to the rds service. labels May 14, 2018
Copy link
Contributor

@bflad bflad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @4dz 👋 Thanks so much for submitting this and sorry for the lengthy delay in getting it reviewed. 😅 Overall this pull request is in decent shape, but I left some comments below. Normally we'd allow you to address these, however we have an immediate need for this resource to perform some additional acceptance testing with the aws_rds_cluster resource snapshot_identifier argument. I'll add a follow up commit after yours which addresses these items so we can get this in.

Thanks again! 🚀

"db_cluster_identifier": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: ForceNew: true for data source attributes should be extraneous as data sources are implicitly refreshed each Terraform run.

snapshotIdentifier, snapshotIdentifierOk := d.GetOk("db_cluster_snapshot_identifier")

if !clusterIdentifierOk && !snapshotIdentifierOk {
return fmt.Errorf("One of db_cluster_snapshot_identifier or db_cluster_identifier must be assigned")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor lint nitpick: When creating a fmt.Errorf() without extra arguments, we should prefer errors.New()

}

func DBClusterSnapshotDescriptionAttributes(d *schema.ResourceData, snapshot *rds.DBClusterSnapshot) error {
d.SetId(*snapshot.DBClusterSnapshotIdentifier)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent potential panics with nil dereferences, we should use the SDK provided function to dereference this attribute instead. e.g. aws.StringValue(snapshot.DBClusterSnapshotIdentifier)


func DBClusterSnapshotDescriptionAttributes(d *schema.ResourceData, snapshot *rds.DBClusterSnapshot) error {
d.SetId(*snapshot.DBClusterSnapshotIdentifier)
d.Set("allocated_storage", snapshot.AllocatedStorage)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These attributes should be covered by an acceptance test check 😄

{
Config: testAccCheckAwsDbClusterSnapshotDataSourceConfig(rInt),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsDbClusterSnapshotDataSourceID("data.aws_db_cluster_snapshot.snapshot"),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When testing data sources that have closely matching resource counterparts, we recommend comparing the attribute values between the two, e.g.

resource.TestCheckResourceAttrPair("data.aws_db_cluster_snapshot.snapshot", "allocated_storage", "aws_db_cluster_snapshot.test", "allocated_storage"),


resp, err := conn.DescribeDBClusterSnapshots(opts)
if err != nil {
snapshoterr, ok := err.(awserr.Error)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nitpick: We have a helper function for this type of check to reduce code complexity and should also prefer to use the SDK provided constant. e.g.

if isAWSErr(err, rds.ErrCodeDBClusterSnapshotNotFoundFault, "") {


snapshot := resp.DBClusterSnapshots[0]

return resp, *snapshot.Status, nil
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent potential panics with nil dereferences, we should use the SDK provided function to dereference this attribute instead. e.g. aws.StringValue(snapshot.Status)

}
_, err := conn.DeleteDBClusterSnapshot(params)
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When expecting to delete this resource, we should allow it to pass without error when the underlying resource has already been deleted. In this case:

if err != nil {
  if isAWSErr(err, rds.ErrCodeDBClusterSnapshotNotFoundFault, "") {
    return nil
  }
  return fmt.Errorf("error deleting RDS DB Cluster Snapshot %q: %s", d.Id(), err)
}

}
resp, err := conn.DescribeDBClusterSnapshots(params)
if err != nil {
return err
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When expecting to read this resource, we should allow it to pass without error by removing the resource from the Terraform state (to force recreation) when the underlying resource has already been deleted. In this case:

if err != nil {
  if isAWSErr(err, rds.ErrCodeDBClusterSnapshotNotFoundFault, "") {
    log.Printf("[WARN] RDS DB Cluster Snapshot %q not found, removing from state", d.Id())
    d.SetId("")
    return nil
  }
  return fmt.Errorf("error reading RDS DB Cluster Snapshot %q: %s", d.Id(), err)
}

return err
}

snapshot := resp.DBClusterSnapshots[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To prevent potential panics and ensure our snapshot matches what we expect, we should perform some additional checking before performing this reference:

if resp == nil || len(resp.DBClusterSnapshots) == 0 || resp.DBClusterSnapshots[0] == nil || aws.StringValue(resp.DBClusterSnapshots[0].DBClusterSnapshotIdentifer) != d.Id() {
  log.Printf("[WARN] RDS DB Cluster Snapshot %q not found, removing from state", d.Id())
  d.SetId("")
  return nil
}

@bflad bflad added this to the v1.31.0 milestone Aug 8, 2018
@bflad bflad merged commit 70a3f81 into hashicorp:master Aug 8, 2018
bflad added a commit that referenced this pull request Aug 8, 2018
=== RUN   TestAccAWSDBClusterSnapshot_basic
--- PASS: TestAccAWSDBClusterSnapshot_basic (146.93s)
=== RUN   TestAccAWSDbClusterSnapshotDataSource_DbClusterSnapshotIdentifier
--- PASS: TestAccAWSDbClusterSnapshotDataSource_DbClusterSnapshotIdentifier (128.45s)
=== RUN   TestAccAWSDbClusterSnapshotDataSource_DbClusterIdentifier
--- PASS: TestAccAWSDbClusterSnapshotDataSource_DbClusterIdentifier (137.24s)
=== RUN   TestAccAWSDbClusterSnapshotDataSource_MostRecent
--- PASS: TestAccAWSDbClusterSnapshotDataSource_MostRecent (197.72s)
bflad added a commit that referenced this pull request Aug 8, 2018
@4dz
Copy link
Contributor Author

4dz commented Aug 8, 2018 via email

@bflad
Copy link
Contributor

bflad commented Aug 9, 2018

This has been released in version 1.31.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@ghost
Copy link

ghost commented Apr 4, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
new-data-source Introduces a new data source. new-resource Introduces a new resource. service/rds Issues and PRs that pertain to the rds service. size/XL Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants