-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_iec_sites.go
164 lines (148 loc) · 3.85 KB
/
data_source_huaweicloud_iec_sites.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package iec
import (
"context"
"log"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
ieccommon "github.com/chnsz/golangsdk/openstack/iec/v1/common"
"github.com/chnsz/golangsdk/openstack/iec/v1/sites"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API IEC GET /v1/sites
func DataSourceSites() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSitesV1Read,
Schema: map[string]*schema.Schema{
"area": {
Type: schema.TypeString,
Optional: true,
},
"province": {
Type: schema.TypeString,
Optional: true,
},
"city": {
Type: schema.TypeString,
Optional: true,
},
"sites": {
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,
},
"area": {
Type: schema.TypeString,
Computed: true,
},
"province": {
Type: schema.TypeString,
Computed: true,
},
"city": {
Type: schema.TypeString,
Computed: true,
},
"lines": {
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,
},
"operator": {
Type: schema.TypeString,
Computed: true,
},
"ip_version": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"status": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
},
}
}
func dataSourceSitesV1Read(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
iecClient, err := cfg.IECV1Client(cfg.GetRegion(d))
if err != nil {
return diag.Errorf("error creating IEC client: %s", err)
}
listOpts := sites.ListSiteOpts{
Area: d.Get("area").(string),
Province: d.Get("province").(string),
City: d.Get("city").(string),
}
pages, err := sites.List(iecClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("unable to retrieve IEC sites: %s", err)
}
allSites, err := sites.ExtractSites(pages)
if err != nil {
return diag.Errorf("unable to extract IEC sites: %s", err)
}
total := len(allSites.Sites)
if total < 1 {
return diag.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
log.Printf("[INFO] Retrieved [%d] IEC sites using given filter", total)
iecSites := make([]map[string]interface{}, 0, total)
for i := range allSites.Sites {
item := allSites.Sites[i]
val := map[string]interface{}{
"id": item.ID,
"name": item.Name,
"area": item.Area,
"province": item.Province,
"city": item.City,
"status": item.Status,
"lines": flattenSiteLines(&item),
}
iecSites = append(iecSites, val)
}
mErr := multierror.Append(nil,
d.Set("sites", iecSites),
)
if err := mErr.ErrorOrNil(); err != nil {
return diag.Errorf("error saving IEC sites: %s", err)
}
site := allSites.Sites[0]
d.SetId(site.ID)
return nil
}
func flattenSiteLines(site *ieccommon.Site) []map[string]interface{} {
siteLines := make([]map[string]interface{}, len(site.EipPools))
for i, item := range site.EipPools {
siteLines[i] = map[string]interface{}{
"id": item.PoolID,
"name": item.DisplayName,
"operator": item.OperatorID.Name,
"ip_version": item.IPVersion,
}
}
return siteLines
}