-
Notifications
You must be signed in to change notification settings - Fork 7
/
datasource_region.go
99 lines (89 loc) · 2.61 KB
/
datasource_region.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package datasources
import (
"context"
"fmt"
"log"
"strings"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/clients"
"github.com/MaterializeInc/terraform-provider-materialize/pkg/utils"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func Region() *schema.Resource {
return &schema.Resource{
ReadContext: RegionRead,
Schema: map[string]*schema.Schema{
"regions": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The name of the region.",
},
"url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL at which the Region API can be reached.",
},
"cloud_provider": {
Type: schema.TypeString,
Computed: true,
Description: "The cloud provider of the region. Currently, only AWS is supported.",
},
"host": {
Type: schema.TypeString,
Computed: true,
Description: "The SQL host of the region. This is the hostname of the Materialize cluster in the region.",
},
},
},
},
},
}
}
func RegionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
providerMeta, err := utils.GetProviderMeta(meta)
if err != nil {
return diag.FromErr(err)
}
client := providerMeta.CloudAPI
providers, err := client.ListCloudProviders(ctx)
if err != nil {
return diag.FromErr(err)
}
var regions []map[string]interface{}
for _, provider := range providers {
host, err := client.GetHost(ctx, provider.ID)
if err != nil {
if strings.Contains(err.Error(), "non-200 status code: 204") {
log.Printf("[WARN] No host available for region %s, skipping", provider.ID)
continue
}
return diag.FromErr(fmt.Errorf("error fetching host for region %s: %s", provider.ID, err))
}
region := createRegionMap(provider, host)
regions = append(regions, region)
}
if err := d.Set("regions", regions); err != nil {
return diag.FromErr(err)
}
d.SetId("regions")
return nil
}
// createRegionMap creates a map of region details
func createRegionMap(provider clients.CloudProvider, host string) map[string]interface{} {
return map[string]interface{}{
"id": provider.ID,
"name": provider.Name,
"url": provider.Url,
"cloud_provider": provider.CloudProvider,
"host": host,
}
}