/
data_source_huaweicloud_er_propagations.go
171 lines (153 loc) · 4.11 KB
/
data_source_huaweicloud_er_propagations.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
165
166
167
168
169
170
171
package er
import (
"context"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/chnsz/golangsdk/openstack/er/v3/propagations"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API ER GET /v3/{project_id}/enterprise-router/{er_id}/route-tables/{route_table_id}/propagations
func DataSourcePropagations() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePropagationsRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
},
"instance_id": {
Type: schema.TypeString,
Required: true,
},
"route_table_id": {
Type: schema.TypeString,
Required: true,
},
"attachment_id": {
Type: schema.TypeString,
Optional: true,
},
"attachment_type": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"propagations": {
Type: schema.TypeList,
Computed: true,
Elem: propagationSchema(),
},
},
}
}
func propagationSchema() *schema.Resource {
return &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Computed: true,
},
"route_table_id": {
Type: schema.TypeString,
Computed: true,
},
"attachment_id": {
Type: schema.TypeString,
Computed: true,
},
"attachment_type": {
Type: schema.TypeString,
Computed: true,
},
"resource_id": {
Type: schema.TypeString,
Computed: true,
},
"route_policy_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func buildPropagationListOpts(d *schema.ResourceData) propagations.ListOpts {
opts := propagations.ListOpts{}
if attachmentId, ok := d.GetOk("attachment_id"); ok {
opts.AttachmentIds = []string{attachmentId.(string)}
}
if attachmentType, ok := d.GetOk("attachment_type"); ok {
opts.ResourceTypes = []string{attachmentType.(string)}
}
if status, ok := d.GetOk("status"); ok {
opts.Statuses = []string{status.(string)}
}
return opts
}
func flattenPropagations(all []propagations.Propagation) []map[string]interface{} {
if len(all) < 1 {
return nil
}
result := make([]map[string]interface{}, len(all))
for i, propagation := range all {
result[i] = map[string]interface{}{
"id": propagation.ID,
"instance_id": propagation.InstanceId,
"route_table_id": propagation.RouteTableId,
"attachment_id": propagation.AttachmentId,
"attachment_type": propagation.ResourceType,
"resource_id": propagation.ResourceId,
"route_policy_id": propagation.RoutePolicy.ImportPoilicyId,
"status": propagation.Status,
"created_at": propagation.CreatedAt,
"updated_at": propagation.UpdatedAt,
}
}
return result
}
func dataSourcePropagationsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
cfg := meta.(*config.Config)
region := cfg.GetRegion(d)
client, err := cfg.ErV3Client(region)
if err != nil {
return diag.Errorf("error creating ER v3 client: %s", err)
}
opts := buildPropagationListOpts(d)
resp, err := propagations.List(client, d.Get("instance_id").(string), d.Get("route_table_id").(string), opts)
if err != nil {
return diag.Errorf("error retrieving propagations: %s", err)
}
randUUID, err := uuid.GenerateUUID()
if err != nil {
return diag.Errorf("unable to generate ID: %s", err)
}
d.SetId(randUUID)
mErr := multierror.Append(nil,
d.Set("region", region),
d.Set("propagations", flattenPropagations(resp)),
)
if mErr.ErrorOrNil() != nil {
return diag.Errorf("error saving data source fields of ER propagations: %s", mErr)
}
return nil
}