Skip to content

Commit

Permalink
Update snapshots data_source filters
Browse files Browse the repository at this point in the history
closed #344
  • Loading branch information
outscale-toa committed Oct 9, 2023
1 parent 6fea749 commit 950fabd
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 5 deletions.
36 changes: 32 additions & 4 deletions outscale/data_source_outscale_snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/nav-inc/datetime"
oscgo "github.com/outscale/osc-sdk-go/v2"
"github.com/terraform-providers/terraform-provider-outscale/utils"
)
Expand Down Expand Up @@ -108,7 +109,9 @@ func dataSourceOutscaleOAPISnapshotRead(d *schema.ResourceData, meta interface{}
params.SetFilters(filter)
}
if filtersOk {
buildOutscaleOapiSnapshootDataSourceFilters(filters.(*schema.Set), params.Filters)
if err := buildOutscaleOapiSnapshootDataSourceFilters(filters.(*schema.Set), params.Filters); err != nil {
return err
}
}
if ownersOk {
filter.SetAccountIds([]string{owners.(string)})
Expand Down Expand Up @@ -188,7 +191,7 @@ func snapshotOAPIDescriptionAttributes(d *schema.ResourceData, snapshot *oscgo.S
return d.Set("tags", tagsOSCAPIToMap(snapshot.GetTags()))
}

func buildOutscaleOapiSnapshootDataSourceFilters(set *schema.Set, filter *oscgo.FiltersSnapshot) *oscgo.FiltersSnapshot {
func buildOutscaleOapiSnapshootDataSourceFilters(set *schema.Set, filter *oscgo.FiltersSnapshot) error {

for _, v := range set.List() {
m := v.(map[string]interface{})
Expand All @@ -207,12 +210,28 @@ func buildOutscaleOapiSnapshootDataSourceFilters(set *schema.Set, filter *oscgo.

case "descriptions":
filter.SetDescriptions(values)
case "to_creation_date":
valDate, err := parsingfilterDateFormat("to_creation_date", values[0])
if err != nil {
return err
}
filter.SetToCreationDate(valDate)

case "from_creation_date":
valDate, err := parsingfilterDateFormat("from_creation_date", values[0])
if err != nil {
return err
}
filter.SetFromCreationDate(valDate)

case "permissions_to_create_volume_account_ids":
filter.SetPermissionsToCreateVolumeAccountIds(values)

case "permissions_to_create_volume_global_permission":
boolean, _ := strconv.ParseBool(values[0])
boolean, err := strconv.ParseBool(values[0])
if err != nil {
return err
}
filter.SetPermissionsToCreateVolumeGlobalPermission(boolean)

case "progresses":
Expand Down Expand Up @@ -243,5 +262,14 @@ func buildOutscaleOapiSnapshootDataSourceFilters(set *schema.Set, filter *oscgo.
log.Printf("[Debug] Unknown Filter Name: %s.", name)
}
}
return filter
return nil
}
func parsingfilterDateFormat(filterName, value string) (time.Time, error) {
var err error
var filterDate time.Time

if filterDate, err = datetime.Parse(value, time.UTC); err != nil {
return filterDate, fmt.Errorf("%s value should be 'ISO 8601' format ('2017-06-14' or '2017-06-14T00:00:00Z, ...) %s", filterName, err)
}
return filterDate, nil
}
4 changes: 4 additions & 0 deletions outscale/data_source_outscale_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@ func testAccCheckOutscaleOAPISnapshotDataSourceConfigWithMultipleFilters(region
name = "volume_sizes"
values = ["10"]
}
filter {
name = "from_creation_date"
values = ["2023"]
}
}
`, region)
}
6 changes: 5 additions & 1 deletion outscale/data_source_outscale_snapshots.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,13 @@ func dataSourceOutscaleOAPISnapshotsRead(d *schema.ResourceData, meta interface{
filter.SetPermissionsToCreateVolumeAccountIds(utils.InterfaceSliceToStringSlice(restorableUsers.([]interface{})))
params.SetFilters(filter)
}

if filtersOk {
buildOutscaleOapiSnapshootDataSourceFilters(filters.(*schema.Set), params.Filters)
if err := buildOutscaleOapiSnapshootDataSourceFilters(filters.(*schema.Set), params.Filters); err != nil {
return err
}
}

if ownersOk {
filter.SetAccountIds(utils.InterfaceSliceToStringSlice(owners.([]interface{})))
params.SetFilters(filter)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@ data "outscale_snapshot" "outscale_snapshot" {
name = "snapshot_ids"
values = [outscale_snapshot.outscale_snapshot.snapshot_id]
}
filter {
name = "from_creation_date"
values = ["2023"]
}
}

0 comments on commit 950fabd

Please sign in to comment.