From b07222ead16668694315447d3c5887bd9917a4f7 Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 6 Oct 2023 18:30:44 +0000 Subject: [PATCH] Update snapshots data_source filters closed #344 --- outscale/data_source_outscale_snapshot.go | 36 ++++++++++++++++--- .../data_source_outscale_snapshot_test.go | 4 +++ outscale/data_source_outscale_snapshots.go | 6 +++- ...step1.snapshot_datasource_attributes_ok.tf | 4 +++ 4 files changed, 45 insertions(+), 5 deletions(-) diff --git a/outscale/data_source_outscale_snapshot.go b/outscale/data_source_outscale_snapshot.go index 9a2336d62..fef3c2dc5 100644 --- a/outscale/data_source_outscale_snapshot.go +++ b/outscale/data_source_outscale_snapshot.go @@ -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" ) @@ -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)}) @@ -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{}) @@ -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": @@ -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 } diff --git a/outscale/data_source_outscale_snapshot_test.go b/outscale/data_source_outscale_snapshot_test.go index ed3d92342..b25f7ce1f 100644 --- a/outscale/data_source_outscale_snapshot_test.go +++ b/outscale/data_source_outscale_snapshot_test.go @@ -94,6 +94,10 @@ func testAccCheckOutscaleOAPISnapshotDataSourceConfigWithMultipleFilters(region name = "volume_sizes" values = ["10"] } + filter { + name = "from_creation_date" + values = ["2023"] + } } `, region) } diff --git a/outscale/data_source_outscale_snapshots.go b/outscale/data_source_outscale_snapshots.go index f17c8856b..0dbb1702e 100644 --- a/outscale/data_source_outscale_snapshots.go +++ b/outscale/data_source_outscale_snapshots.go @@ -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) diff --git a/tests/qa_provider_oapi/data/snapshot/TF-130_snapshot_datasource_attributes_ok/step1.snapshot_datasource_attributes_ok.tf b/tests/qa_provider_oapi/data/snapshot/TF-130_snapshot_datasource_attributes_ok/step1.snapshot_datasource_attributes_ok.tf index 597303ee1..d68e34215 100644 --- a/tests/qa_provider_oapi/data/snapshot/TF-130_snapshot_datasource_attributes_ok/step1.snapshot_datasource_attributes_ok.tf +++ b/tests/qa_provider_oapi/data/snapshot/TF-130_snapshot_datasource_attributes_ok/step1.snapshot_datasource_attributes_ok.tf @@ -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"] + } }