forked from ovh/terraform-provider-ovh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_cloud_project_database_opensearch_patterns.go
64 lines (55 loc) · 1.77 KB
/
data_cloud_project_database_opensearch_patterns.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package ovh
import (
"context"
"fmt"
"log"
"net/url"
"sort"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/ovh/terraform-provider-ovh/ovh/helpers"
"github.com/ovh/terraform-provider-ovh/ovh/helpers/hashcode"
)
func dataSourceCloudProjectDatabaseOpensearchPatterns() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceCloudProjectDatabaseOpensearchPatternsRead,
Schema: map[string]*schema.Schema{
"service_name": {
Type: schema.TypeString,
Required: true,
DefaultFunc: schema.EnvDefaultFunc("OVH_CLOUD_PROJECT_SERVICE", nil),
},
"cluster_id": {
Type: schema.TypeString,
Description: "Id of the database cluster",
Required: true,
},
// Computed
"pattern_ids": {
Type: schema.TypeList,
Description: "List of pattern ids",
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceCloudProjectDatabaseOpensearchPatternsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
serviceName := d.Get("service_name").(string)
clusterId := d.Get("cluster_id").(string)
endpoint := fmt.Sprintf("/cloud/project/%s/database/opensearch/%s/pattern",
url.PathEscape(serviceName),
url.PathEscape(clusterId),
)
res := make([]string, 0)
log.Printf("[DEBUG] Will read patterns from cluster %s from project %s", clusterId, serviceName)
if err := config.OVHClient.GetWithContext(ctx, endpoint, &res); err != nil {
return diag.FromErr(helpers.CheckDeleted(d, err, endpoint))
}
// sort.Strings sorts in place, returns nothing
sort.Strings(res)
d.SetId(hashcode.Strings(res))
d.Set("pattern_ids", res)
return nil
}