|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/RedisLabs/rediscloud-go-api/redis" |
| 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 | +) |
| 11 | + |
| 12 | +func dataSourceRedisCloudActiveActiveSubscriptionRegions() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: "Gets a list of regions in the specified Active-Active subscription.", |
| 15 | + ReadContext: dataSourceRedisCloudActiveActiveRegionsRead, |
| 16 | + |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "subscription_name": { |
| 19 | + Description: "The name of the subscription", |
| 20 | + Type: schema.TypeString, |
| 21 | + Required: true, |
| 22 | + }, |
| 23 | + "regions": { |
| 24 | + Description: "A list of regions from an active active subscription", |
| 25 | + Type: schema.TypeList, |
| 26 | + Computed: true, |
| 27 | + Elem: &schema.Resource{ |
| 28 | + Schema: map[string]*schema.Schema{ |
| 29 | + "region": { |
| 30 | + Description: "Deployment region as defined by cloud provider", |
| 31 | + Type: schema.TypeString, |
| 32 | + Computed: true, |
| 33 | + }, |
| 34 | + "networking_deployment_cidr": { |
| 35 | + Description: "Deployment CIDR mask", |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + "vpc_id": { |
| 40 | + Description: "VPC ID for the region", |
| 41 | + Computed: true, |
| 42 | + Type: schema.TypeString, |
| 43 | + }, |
| 44 | + "databases": { |
| 45 | + Description: "A list of databases found in the region", |
| 46 | + Computed: true, |
| 47 | + Type: schema.TypeList, |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "database_id": { |
| 51 | + Description: "A numeric id for the database", |
| 52 | + Type: schema.TypeInt, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "database_name": { |
| 56 | + Description: "A meaningful name to identify the database", |
| 57 | + Type: schema.TypeString, |
| 58 | + Computed: true, |
| 59 | + }, |
| 60 | + "write_operations_per_second": { |
| 61 | + Description: "Write operations per second for the database", |
| 62 | + Type: schema.TypeInt, |
| 63 | + Computed: true, |
| 64 | + }, |
| 65 | + "read_operations_per_second": { |
| 66 | + Description: "Read operations per second for the database", |
| 67 | + Type: schema.TypeInt, |
| 68 | + Computed: true, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +func dataSourceRedisCloudActiveActiveRegionsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 81 | + var diags diag.Diagnostics |
| 82 | + api := meta.(*apiClient) |
| 83 | + |
| 84 | + subs, err := api.client.Subscription.List(ctx) |
| 85 | + |
| 86 | + if err != nil { |
| 87 | + return diag.FromErr(err) |
| 88 | + } |
| 89 | + |
| 90 | + var filters []func(method *subscriptions.Subscription) bool |
| 91 | + |
| 92 | + // Filter to active-active subscriptions only (pro subs come from the same endpoint) |
| 93 | + filters = append(filters, func(sub *subscriptions.Subscription) bool { |
| 94 | + return redis.StringValue(sub.DeploymentType) == "active-active" |
| 95 | + }) |
| 96 | + |
| 97 | + // Filter down to requested subscription by name |
| 98 | + if name, ok := d.GetOk("subscription_name"); ok { |
| 99 | + filters = append(filters, func(sub *subscriptions.Subscription) bool { |
| 100 | + return redis.StringValue(sub.Name) == name |
| 101 | + }) |
| 102 | + } |
| 103 | + |
| 104 | + subs = filterSubscriptions(subs, filters) |
| 105 | + |
| 106 | + if len(subs) == 0 { |
| 107 | + return diag.Errorf("Your query returned no results. Please change your search criteria and try again.") |
| 108 | + } |
| 109 | + |
| 110 | + if len(subs) > 1 { |
| 111 | + return diag.Errorf("Your query returned more than one result. Please change try a more specific search criteria and try again.") |
| 112 | + } |
| 113 | + |
| 114 | + sub := subs[0] |
| 115 | + |
| 116 | + regions, err := api.client.Subscription.ListActiveActiveRegions(ctx, *sub.ID) |
| 117 | + |
| 118 | + if err != nil { |
| 119 | + return diag.FromErr(err) |
| 120 | + } |
| 121 | + |
| 122 | + if len(regions) == 0 { |
| 123 | + return diag.Errorf("Your query returned no results. Please change your search criteria and try again.") |
| 124 | + } |
| 125 | + |
| 126 | + var genericRegions = flattenActiveActiveRegions(regions) |
| 127 | + |
| 128 | + id := fmt.Sprintf("%d-active-active-regions", *sub.ID) |
| 129 | + d.SetId(id) |
| 130 | + |
| 131 | + if err := d.Set("regions", genericRegions); err != nil { |
| 132 | + return diag.FromErr(err) |
| 133 | + } |
| 134 | + |
| 135 | + return diags |
| 136 | +} |
| 137 | + |
| 138 | +// generifies the region/db data so it can be put into the terraform schema |
| 139 | +func flattenActiveActiveRegions(regionList []*subscriptions.ActiveActiveRegion) []map[string]interface{} { |
| 140 | + var rl []map[string]interface{} |
| 141 | + for _, currentRegion := range regionList { |
| 142 | + |
| 143 | + var dbs []map[string]interface{} |
| 144 | + for _, db := range currentRegion.Databases { |
| 145 | + dbMap := map[string]interface{}{ |
| 146 | + "database_id": db.DatabaseId, |
| 147 | + "database_name": db.DatabaseName, |
| 148 | + "write_operations_per_second": db.WriteOperationsPerSecond, |
| 149 | + "read_operations_per_second": db.ReadOperationsPerSecond, |
| 150 | + } |
| 151 | + dbs = append(dbs, dbMap) |
| 152 | + } |
| 153 | + |
| 154 | + regionMap := map[string]interface{}{ |
| 155 | + "region": currentRegion.Region, |
| 156 | + "networking_deployment_cidr": currentRegion.DeploymentCIDR, |
| 157 | + "vpc_id": currentRegion.VpcId, |
| 158 | + "databases": dbs, |
| 159 | + } |
| 160 | + rl = append(rl, regionMap) |
| 161 | + } |
| 162 | + return rl |
| 163 | +} |
0 commit comments