-
Notifications
You must be signed in to change notification settings - Fork 4
/
datasource_locations_regions.go
63 lines (58 loc) · 1.59 KB
/
datasource_locations_regions.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
package provider
import (
"context"
"github.com/PacketFabric/terraform-provider-packetfabric/internal/packetfabric"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func datasourceLocationsRegions() *schema.Resource {
return &schema.Resource{
ReadContext: datasourceLocationsRegionsRead,
Schema: map[string]*schema.Schema{
"locations_regions": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
Description: "The location region name.",
},
"code": {
Type: schema.TypeString,
Computed: true,
Description: "The location region code.",
},
},
},
},
},
}
}
func datasourceLocationsRegionsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*packetfabric.PFClient)
c.Ctx = ctx
var diags diag.Diagnostics
regions, err := c.GetLocationRegions()
if err != nil {
return diag.FromErr(err)
}
err = d.Set("locations_regions", flattenLocationsRegions(®ions))
if err != nil {
return diag.FromErr(err)
}
d.SetId(uuid.New().String())
return diags
}
func flattenLocationsRegions(regions *[]packetfabric.LocationRegion) []interface{} {
flattens := make([]interface{}, len(*regions))
for i, reg := range *regions {
flatten := make(map[string]interface{})
flatten["name"] = reg.Name
flatten["code"] = reg.Code
flattens[i] = flatten
}
return flattens
}