|
| 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 | +} |
0 commit comments