|
| 1 | +package scaleway |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 9 | + redis "github.com/scaleway/scaleway-sdk-go/api/redis/v1alpha1" |
| 10 | + "github.com/scaleway/scaleway-sdk-go/scw" |
| 11 | +) |
| 12 | + |
| 13 | +func dataSourceScalewayRedisCluster() *schema.Resource { |
| 14 | + // Generate datasource schema from resource |
| 15 | + dsSchema := datasourceSchemaFromResourceSchema(resourceScalewayRedisCluster().Schema) |
| 16 | + // Set 'Optional' schema elements |
| 17 | + addOptionalFieldsToSchema(dsSchema, "name", "zone") |
| 18 | + |
| 19 | + dsSchema["name"].ConflictsWith = []string{"cluster_id"} |
| 20 | + dsSchema["cluster_id"] = &schema.Schema{ |
| 21 | + Type: schema.TypeString, |
| 22 | + Optional: true, |
| 23 | + Description: "The ID of the Redis cluster", |
| 24 | + ConflictsWith: []string{"name"}, |
| 25 | + ValidateFunc: validationUUIDorUUIDWithLocality(), |
| 26 | + } |
| 27 | + |
| 28 | + return &schema.Resource{ |
| 29 | + ReadContext: dataSourceScalewayRedisClusterRead, |
| 30 | + Schema: dsSchema, |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +func dataSourceScalewayRedisClusterRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 35 | + api, zone, err := redisAPIWithZone(d, meta) |
| 36 | + if err != nil { |
| 37 | + return diag.FromErr(err) |
| 38 | + } |
| 39 | + |
| 40 | + clusterID, ok := d.GetOk("cluster_id") |
| 41 | + if !ok { |
| 42 | + res, err := api.ListClusters(&redis.ListClustersRequest{ |
| 43 | + Zone: zone, |
| 44 | + Name: expandStringPtr(d.Get("name")), |
| 45 | + ProjectID: expandStringPtr(d.Get("project_id")), |
| 46 | + }, scw.WithContext(ctx)) |
| 47 | + if err != nil { |
| 48 | + return diag.FromErr(err) |
| 49 | + } |
| 50 | + for _, cluster := range res.Clusters { |
| 51 | + if cluster.Name == d.Get("name").(string) { |
| 52 | + if clusterID != "" { |
| 53 | + return diag.FromErr(fmt.Errorf("more than 1 cluster found with the same name %s", d.Get("name"))) |
| 54 | + } |
| 55 | + clusterID = cluster.ID |
| 56 | + } |
| 57 | + } |
| 58 | + if clusterID == "" { |
| 59 | + return diag.FromErr(fmt.Errorf("no clusters found with the name %s", d.Get("name"))) |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + zonedID := datasourceNewZonedID(clusterID, zone) |
| 64 | + d.SetId(zonedID) |
| 65 | + err = d.Set("cluster_id", zonedID) |
| 66 | + if err != nil { |
| 67 | + return diag.FromErr(err) |
| 68 | + } |
| 69 | + |
| 70 | + // Check if cluster exist as Read will return nil if resource does not exist |
| 71 | + // clusterID may be zoned if using name in data source |
| 72 | + getReq := &redis.GetClusterRequest{ |
| 73 | + Zone: zone, |
| 74 | + ClusterID: expandID(clusterID.(string)), |
| 75 | + } |
| 76 | + _, err = api.GetCluster(getReq, scw.WithContext(ctx)) |
| 77 | + if err != nil { |
| 78 | + return diag.FromErr(fmt.Errorf("no clusters found with the id %s", clusterID)) |
| 79 | + } |
| 80 | + |
| 81 | + return resourceScalewayRedisClusterRead(ctx, d, meta) |
| 82 | +} |
0 commit comments