@@ -2,20 +2,44 @@ package provider
22
33import (
44 "context"
5+ "strings"
56
67 "github.com/datastax/astra-client-go/v2/astra"
78 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
89 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/id"
910 "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1012)
1113
14+ var regionTypes = []string {"serverless" , "vector" , "all" }
15+
1216func dataSourceAvailableRegions () * schema.Resource {
1317 return & schema.Resource {
1418 Description : "Retrieve a list of available cloud regions in Astra" ,
1519
1620 ReadContext : dataSourceRegionsRead ,
1721
1822 Schema : map [string ]* schema.Schema {
23+ "cloud_provider" : {
24+ Type : schema .TypeString ,
25+ Description : "The cloud provider to filter by" ,
26+ Optional : true ,
27+ ValidateFunc : validation .StringInSlice (availableCloudProviders , true ),
28+ DiffSuppressFunc : ignoreCase ,
29+ },
30+ "region_type" : {
31+ Type : schema .TypeString ,
32+ Description : "The region type to filter by (currently either 'serverless', 'vector' or 'all'). If omitted, the default is 'serverless'" ,
33+ Optional : true ,
34+ ValidateFunc : validation .StringInSlice (regionTypes , true ),
35+ DiffSuppressFunc : ignoreCase ,
36+ },
37+ "only_enabled" : {
38+ Type : schema .TypeBool ,
39+ Description : "Whether to filter by enabled regions. If 'false' or omitted, all regions are returned, enabled or not" ,
40+ Optional : true ,
41+ Default : false ,
42+ },
1943 "results" : {
2044 Type : schema .TypeList ,
2145 Description : "The list of supported Astra regions by cloud provider and tier." ,
@@ -42,6 +66,21 @@ func dataSourceAvailableRegions() *schema.Resource {
4266 Type : schema .TypeString ,
4367 Computed : true ,
4468 },
69+ "region_type" : {
70+ Description : "The region type, either serverless or vector" ,
71+ Type : schema .TypeString ,
72+ Computed : true ,
73+ },
74+ "enabled" : {
75+ Description : "Whether the region is enabled" ,
76+ Type : schema .TypeBool ,
77+ Computed : true ,
78+ },
79+ "reserved_for_qualified_users" : {
80+ Description : "Whether the region is reserved for qualified users" ,
81+ Type : schema .TypeBool ,
82+ Computed : true ,
83+ },
4584 },
4685 },
4786 },
@@ -52,7 +91,14 @@ func dataSourceAvailableRegions() *schema.Resource {
5291func dataSourceRegionsRead (ctx context.Context , d * schema.ResourceData , meta interface {}) diag.Diagnostics {
5392 client := meta .(astraClients ).astraClient .(* astra.ClientWithResponses )
5493
55- regionsResp , err := client .ListServerlessRegionsWithResponse (ctx )
94+ params := & astra.ListServerlessRegionsParams {}
95+ if d , ok := d .GetOk ("region_type" ); ok {
96+ regionType := d .(string )
97+ params .RegionType = & regionType
98+ }
99+ cloud_provider := d .Get ("cloud_provider" ).(string )
100+ enabled := d .Get ("only_enabled" ).(bool )
101+ regionsResp , err := client .ListServerlessRegionsWithResponse (ctx , params )
56102 if err != nil {
57103 return diag .FromErr (err )
58104 } else if regionsResp .StatusCode () != 200 {
@@ -66,6 +112,14 @@ func dataSourceRegionsRead(ctx context.Context, d *schema.ResourceData, meta int
66112 regions := * regionsResp .JSON200
67113 flatRegions := make ([]map [string ]interface {}, 0 , len (regions ))
68114 for _ , region := range regions {
115+ if cloud_provider != "" && ! strings .EqualFold (string (region .CloudProvider ), cloud_provider ) {
116+ // skip
117+ continue
118+ }
119+ if enabled && ! * region .Enabled {
120+ // skip
121+ continue
122+ }
69123 flatRegions = append (flatRegions , flattenRegion (& region ))
70124 }
71125
@@ -79,9 +133,12 @@ func dataSourceRegionsRead(ctx context.Context, d *schema.ResourceData, meta int
79133
80134func flattenRegion (region * astra.ServerlessRegion ) map [string ]interface {} {
81135 return map [string ]interface {}{
82- "cloud_provider" : region .CloudProvider ,
83- "region" : region .Name ,
84- "zone" : region .Zone ,
85- "display_name" : region .DisplayName ,
136+ "cloud_provider" : region .CloudProvider ,
137+ "region" : region .Name ,
138+ "zone" : region .Zone ,
139+ "display_name" : region .DisplayName ,
140+ "region_type" : region .RegionType ,
141+ "enabled" : region .Enabled ,
142+ "reserved_for_qualified_users" : region .ReservedForQualifiedUsers ,
86143 }
87144}
0 commit comments