Skip to content

Commit

Permalink
Merge pull request hashicorp#26184 from pwillie/fsx-openzfs-snapshot-…
Browse files Browse the repository at this point in the history
…datasource

Add aws_fsx_openzfs_snapshot data source
  • Loading branch information
ewbankkit authored Aug 10, 2022
2 parents 6f0dbca + e229fb5 commit 1eda869
Show file tree
Hide file tree
Showing 7 changed files with 476 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/26184.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:new-data-source
aws_fsx_openzfs_snapshot
```
2 changes: 2 additions & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,8 @@ func New(_ context.Context) (*schema.Provider, error) {

"aws_kinesis_firehose_delivery_stream": firehose.DataSourceDeliveryStream(),

"aws_fsx_openzfs_snapshot": fsx.DataSourceOpenzfsSnapshot(),

"aws_globalaccelerator_accelerator": globalaccelerator.DataSourceAccelerator(),

"aws_glue_connection": glue.DataSourceConnection(),
Expand Down
44 changes: 44 additions & 0 deletions internal/service/fsx/common_schema_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package fsx

import (
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func BuildSnapshotFiltersDataSource(set *schema.Set) []*fsx.SnapshotFilter {
var filters []*fsx.SnapshotFilter
for _, v := range set.List() {
m := v.(map[string]interface{})
var filterValues []*string
for _, e := range m["values"].([]interface{}) {
filterValues = append(filterValues, aws.String(e.(string)))
}
filters = append(filters, &fsx.SnapshotFilter{
Name: aws.String(m["name"].(string)),
Values: filterValues,
})
}
return filters
}

func DataSourceSnapshotFiltersSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},

"values": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}
31 changes: 31 additions & 0 deletions internal/service/fsx/find.go
Original file line number Diff line number Diff line change
Expand Up @@ -234,3 +234,34 @@ func FindSnapshotByID(conn *fsx.FSx, id string) (*fsx.Snapshot, error) {

return output.Snapshots[0], nil
}

func FindSnapshots(conn *fsx.FSx, input *fsx.DescribeSnapshotsInput) ([]*fsx.Snapshot, error) {
var output []*fsx.Snapshot

err := conn.DescribeSnapshotsPages(input, func(page *fsx.DescribeSnapshotsOutput, lastPage bool) bool {
if page == nil {
return !lastPage
}

for _, v := range page.Snapshots {
if v != nil {
output = append(output, v)
}
}

return !lastPage
})

if tfawserr.ErrCodeEquals(err, fsx.ErrCodeSnapshotNotFound) {
return nil, &resource.NotFoundError{
LastError: err,
LastRequest: input,
}
}

if err != nil {
return nil, err
}

return output, nil
}
121 changes: 121 additions & 0 deletions internal/service/fsx/openzfs_snapshot_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package fsx

import (
"fmt"
"sort"
"time"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/fsx"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-aws/internal/conns"
"github.com/hashicorp/terraform-provider-aws/internal/flex"
tftags "github.com/hashicorp/terraform-provider-aws/internal/tags"
)

func DataSourceOpenzfsSnapshot() *schema.Resource {
return &schema.Resource{
Read: dataSourceOpenzfsSnapshotRead,

Schema: map[string]*schema.Schema{
"arn": {
Type: schema.TypeString,
Computed: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
},
"filter": DataSourceSnapshotFiltersSchema(),
"most_recent": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"snapshot_id": {
Type: schema.TypeString,
Computed: true,
},
"snapshot_ids": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": tftags.TagsSchemaComputed(),
"volume_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceOpenzfsSnapshotRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*conns.AWSClient).FSxConn
ignoreTagsConfig := meta.(*conns.AWSClient).IgnoreTagsConfig

input := &fsx.DescribeSnapshotsInput{}

if v, ok := d.GetOk("snapshot_ids"); ok && len(v.([]interface{})) > 0 {
input.SnapshotIds = flex.ExpandStringList(v.([]interface{}))
}

input.Filters = append(input.Filters, BuildSnapshotFiltersDataSource(
d.Get("filter").(*schema.Set),
)...)

if len(input.Filters) == 0 {
input.Filters = nil
}

snapshots, err := FindSnapshots(conn, input)

if err != nil {
return fmt.Errorf("reading FSx Snapshots: %w", err)
}

if len(snapshots) < 1 {
return fmt.Errorf("Your query returned no results. Please change your search criteria and try again.")
}

if len(snapshots) > 1 {
if !d.Get("most_recent").(bool) {
return fmt.Errorf("Your query returned more than one result. Please try a more " +
"specific search criteria, or set `most_recent` attribute to true.")
}

sort.Slice(snapshots, func(i, j int) bool {
return aws.TimeValue(snapshots[i].CreationTime).Unix() > aws.TimeValue(snapshots[j].CreationTime).Unix()
})
}

snapshot := snapshots[0]

d.SetId(aws.StringValue(snapshot.SnapshotId))
d.Set("arn", snapshot.ResourceARN)
d.Set("name", snapshot.Name)
d.Set("snapshot_id", snapshot.SnapshotId)
d.Set("volume_id", snapshot.VolumeId)

if err := d.Set("creation_time", snapshot.CreationTime.Format(time.RFC3339)); err != nil {
return fmt.Errorf("error setting creation_time: %w", err)
}

//Snapshot tags do not get returned with describe call so need to make a separate list tags call
tags, tagserr := ListTags(conn, *snapshot.ResourceARN)

if tagserr != nil {
return fmt.Errorf("error reading Tags for FSx OpenZFS Snapshot (%s): %w", d.Id(), err)
}

//lintignore:AWSR002
if err := d.Set("tags", tags.IgnoreAWS().IgnoreConfig(ignoreTagsConfig).Map()); err != nil {
return fmt.Errorf("error setting tags: %w", err)
}

return nil
}
Loading

0 comments on commit 1eda869

Please sign in to comment.