Skip to content

Commit cb485f3

Browse files
committed
feat: incorporate api for regions into new datasource
1 parent a91b6aa commit cb485f3

7 files changed

+119
-16
lines changed

go.mod

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,3 +66,5 @@ require (
6666
google.golang.org/protobuf v1.35.1 // indirect
6767
gopkg.in/yaml.v3 v3.0.1 // indirect
6868
)
69+
70+
replace github.com/RedisLabs/rediscloud-go-api => ../rediscloud-go-api

go.sum

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,6 @@ github.com/Microsoft/go-winio v0.6.1 h1:9/kr64B9VUZrLm5YYwbGtUJnMgqWVOdUAXu6Migc
44
github.com/Microsoft/go-winio v0.6.1/go.mod h1:LRdKpFKfdobln8UmuiYcKPot9D2v6svN5+sAH+4kjUM=
55
github.com/ProtonMail/go-crypto v1.1.0-alpha.2 h1:bkyFVUP+ROOARdgCiJzNQo2V2kiB97LyUpzH9P6Hrlg=
66
github.com/ProtonMail/go-crypto v1.1.0-alpha.2/go.mod h1:rA3QumHc/FZ8pAHreoekgiAbzpNsfQAosU5td4SnOrE=
7-
github.com/RedisLabs/rediscloud-go-api v0.22.0 h1:Tb3vMtCq7ks5kpniF/Rkod7TvHhcXZ2oYJJT3pMgyQ8=
8-
github.com/RedisLabs/rediscloud-go-api v0.22.0/go.mod h1:3/oVb71rv2OstFRYEc65QCIbfwnJTgZeQhtPCcdHook=
97
github.com/agext/levenshtein v1.2.2 h1:0S/Yg6LYmFJ5stwQeRp6EeOcCbj7xiqQSdNelsXvaqE=
108
github.com/agext/levenshtein v1.2.2/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558=
119
github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec=

provider/datasource_rediscloud_active_active_subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ func dataSourceRedisCloudActiveActiveSubscriptionRead(ctx context.Context, d *sc
155155

156156
var filters []func(method *subscriptions.Subscription) bool
157157

158-
// Filter to AA subscriptions only (active-active subs) come from the same endpoint
158+
// Filter to AA subscriptions only (pro subs come from the same endpoint)
159159
filters = append(filters, func(sub *subscriptions.Subscription) bool {
160160
return redis.StringValue(sub.DeploymentType) == "active-active"
161161
})
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"github.com/RedisLabs/rediscloud-go-api/redis"
6+
"github.com/RedisLabs/rediscloud-go-api/service/cloud_accounts"
7+
"github.com/RedisLabs/rediscloud-go-api/service/subscriptions"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
11+
)
12+
13+
func dataSourceRedisCloudActiveActiveSubscriptionRegions() *schema.Resource {
14+
return &schema.Resource{
15+
Description: "The Active Active Subscription Regions data source allows access to a list of supported cloud provider regions. These regions can be used with the active active subscription resource.",
16+
ReadContext: dataSourceRedisCloudActiveActiveRegionsRead,
17+
18+
Schema: map[string]*schema.Schema{
19+
"subscription_id": {
20+
Description: "The name of the cloud provider to filter returned regions, (accepted values are `AWS` or `GCP`).",
21+
Optional: true,
22+
Type: schema.TypeString,
23+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(cloud_accounts.ProviderValues(), false)),
24+
},
25+
"provider_name": {
26+
Description: "The name of the cloud provider to filter returned regions, (accepted values are `AWS` or `GCP`).",
27+
Optional: true,
28+
Type: schema.TypeString,
29+
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice(cloud_accounts.ProviderValues(), false)),
30+
},
31+
"regions": {
32+
Description: "A list of regions from either a single or multiple cloud providers",
33+
Type: schema.TypeSet,
34+
Computed: true,
35+
Elem: &schema.Resource{
36+
Schema: map[string]*schema.Schema{
37+
"name": {
38+
Description: "The identifier assigned by the cloud provider, (for example `eu-west-1` for `AWS`)",
39+
Computed: true,
40+
Type: schema.TypeString,
41+
},
42+
"provider_name": {
43+
Description: "The identifier of the owning cloud provider, (either `AWS` or `GCP`)",
44+
Computed: true,
45+
Type: schema.TypeString,
46+
},
47+
},
48+
},
49+
},
50+
},
51+
}
52+
}
53+
54+
func dataSourceRedisCloudActiveActiveRegionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
55+
var diags diag.Diagnostics
56+
api := meta.(*apiClient)
57+
58+
subs, err := api.client.Subscription.List(ctx)
59+
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
var filters []func(method *subscriptions.Subscription) bool
65+
66+
// Filter to active-active subscriptions only (pro subs come from the same endpoint)
67+
filters = append(filters, func(sub *subscriptions.Subscription) bool {
68+
return redis.StringValue(sub.DeploymentType) == "active-active"
69+
})
70+
71+
if name, ok := d.GetOk("name"); ok {
72+
filters = append(filters, func(sub *subscriptions.Subscription) bool {
73+
return redis.StringValue(sub.Name) == name
74+
})
75+
}
76+
77+
subs = filterSubscriptions(subs, filters)
78+
79+
if len(subs) == 0 {
80+
return diag.Errorf("Your query returned no results. Please change your search criteria and try again.")
81+
}
82+
83+
if len(subs) > 1 {
84+
return diag.Errorf("Your query returned more than one result. Please change try a more specific search criteria and try again.")
85+
}
86+
87+
sub := subs[0]
88+
89+
regions, err := api.client.Subscription.ListActiveActiveRegions(ctx, *sub.ID)
90+
91+
if len(regions) == 0 {
92+
return diag.Errorf("Your query returned no results. Please change your search criteria and try again.")
93+
}
94+
95+
return diags
96+
97+
}

provider/datasource_rediscloud_pro_subscription.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ func dataSourceRedisCloudProSubscriptionRead(ctx context.Context, d *schema.Reso
231231

232232
var filters []func(method *subscriptions.Subscription) bool
233233

234-
// Filter to pro subscriptions only (active-active subs) come from the same endpoint
234+
// Filter to pro subscriptions only (active-active subs come from the same endpoint)
235235
filters = append(filters, func(sub *subscriptions.Subscription) bool {
236236
return redis.StringValue(sub.DeploymentType) != "active-active"
237237
})

provider/provider.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -46,18 +46,20 @@ func New(version string) func() *schema.Provider {
4646
// Note the difference in public data-source name and the file/method name.
4747
// This is to help the developer relate their changes to what they would see happening in the Redis Console.
4848
// <default> == flexible == pro
49-
"rediscloud_subscription": dataSourceRedisCloudProSubscription(),
50-
"rediscloud_database": dataSourceRedisCloudProDatabase(),
51-
"rediscloud_database_modules": dataSourceRedisCloudDatabaseModules(),
52-
"rediscloud_payment_method": dataSourceRedisCloudPaymentMethod(),
53-
"rediscloud_regions": dataSourceRedisCloudRegions(),
54-
"rediscloud_essentials_plan": dataSourceRedisCloudEssentialsPlan(),
55-
"rediscloud_essentials_subscription": dataSourceRedisCloudEssentialsSubscription(),
56-
"rediscloud_essentials_database": dataSourceRedisCloudEssentialsDatabase(),
57-
"rediscloud_subscription_peerings": dataSourceRedisCloudSubscriptionPeerings(),
58-
"rediscloud_private_service_connect": dataSourcePrivateServiceConnect(),
59-
"rediscloud_private_service_connect_endpoints": dataSourcePrivateServiceConnectEndpoints(),
60-
"rediscloud_active_active_subscription": dataSourceRedisCloudActiveActiveSubscription(),
49+
"rediscloud_subscription": dataSourceRedisCloudProSubscription(),
50+
"rediscloud_database": dataSourceRedisCloudProDatabase(),
51+
"rediscloud_database_modules": dataSourceRedisCloudDatabaseModules(),
52+
"rediscloud_payment_method": dataSourceRedisCloudPaymentMethod(),
53+
"rediscloud_regions": dataSourceRedisCloudRegions(),
54+
"rediscloud_essentials_plan": dataSourceRedisCloudEssentialsPlan(),
55+
"rediscloud_essentials_subscription": dataSourceRedisCloudEssentialsSubscription(),
56+
"rediscloud_essentials_database": dataSourceRedisCloudEssentialsDatabase(),
57+
"rediscloud_subscription_peerings": dataSourceRedisCloudSubscriptionPeerings(),
58+
"rediscloud_private_service_connect": dataSourcePrivateServiceConnect(),
59+
"rediscloud_private_service_connect_endpoints": dataSourcePrivateServiceConnectEndpoints(),
60+
"rediscloud_active_active_subscription": dataSourceRedisCloudActiveActiveSubscription(),
61+
"rediscloud_active_active_subscription_regions": dataSourceRedisCloudActiveActiveSubscriptionRegions(),
62+
6163
// Note the difference in public data-source name and the file/method name.
6264
// active_active_subscription_database == active_active_database
6365
"rediscloud_active_active_subscription_database": dataSourceRedisCloudActiveActiveDatabase(),

provider/rediscloud_active_active_subscription_test.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -369,6 +369,10 @@ resource "rediscloud_active_active_subscription" "example" {
369369
data "rediscloud_active_active_subscription" "example" {
370370
name = rediscloud_active_active_subscription.example.name
371371
}
372+
373+
data "redis_active_active_subscription_regions" "regions" {
374+
subscription_id = rediscloud_active_active_subscription.example.id
375+
}
372376
`
373377

374378
const testAccResourceRedisCloudActiveActiveSubscriptionNoCreationPlan = `

0 commit comments

Comments
 (0)