-
Notifications
You must be signed in to change notification settings - Fork 1
/
data_source_viettelidc_networking_trunk_v2.go
186 lines (152 loc) · 4.11 KB
/
data_source_viettelidc_networking_trunk_v2.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
package viettelidc
import (
"context"
"log"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/gophercloud/gophercloud/openstack/networking/v2/extensions/trunks"
)
func dataSourceNetworkingTrunkV2() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceNetworkingTrunkV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"trunk_id": {
Type: schema.TypeString,
Optional: true,
},
"port_id": {
Type: schema.TypeString,
Optional: true,
},
"admin_state_up": {
Type: schema.TypeBool,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"project_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"sub_port": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"port_id": {
Type: schema.TypeString,
Computed: true,
},
"segmentation_type": {
Type: schema.TypeString,
Computed: true,
},
"segmentation_id": {
Type: schema.TypeInt,
Computed: true,
},
},
},
},
"tags": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"all_tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourceNetworkingTrunkV2Read(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
config := meta.(*Config)
networkingClient, err := config.NetworkingV2Client(GetRegion(d, config))
if err != nil {
return diag.Errorf("Error creating OpenStack networking client: %s", err)
}
listOpts := trunks.ListOpts{}
if v, ok := d.GetOk("name"); ok {
listOpts.Name = v.(string)
}
if v, ok := d.GetOk("description"); ok {
listOpts.Description = v.(string)
}
if v, ok := d.GetOk("trunk_id"); ok {
listOpts.ID = v.(string)
}
if v, ok := d.GetOk("port_id"); ok {
listOpts.PortID = v.(string)
}
if v, ok := d.GetOk("admin_state_up"); ok {
asu := v.(bool)
listOpts.AdminStateUp = &asu
}
if v, ok := d.GetOk("project_id"); ok {
listOpts.ProjectID = v.(string)
}
if v, ok := d.GetOk("status"); ok {
listOpts.Status = v.(string)
}
tags := networkingV2AttributesTags(d)
if len(tags) > 0 {
listOpts.Tags = strings.Join(tags, ",")
}
pages, err := trunks.List(networkingClient, listOpts).AllPages()
if err != nil {
return diag.Errorf("Unable to retrieve trunks: %s", err)
}
allTrunks, err := trunks.ExtractTrunks(pages)
if err != nil {
return diag.Errorf("Unable to extract trunks: %s", err)
}
if len(allTrunks) < 1 {
return diag.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(allTrunks) > 1 {
return diag.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
trunk := allTrunks[0]
log.Printf("[DEBUG] Retrieved Trunk %s: %+v", trunk.ID, trunk)
d.SetId(trunk.ID)
d.Set("region", GetRegion(d, config))
d.Set("name", trunk.Name)
d.Set("description", trunk.Description)
d.Set("port_id", trunk.PortID)
d.Set("admin_state_up", trunk.AdminStateUp)
d.Set("status", trunk.Status)
d.Set("project_id", trunk.ProjectID)
d.Set("all_tags", trunk.Tags)
subports := make([]map[string]interface{}, len(trunk.Subports))
for i, trunkSubport := range trunk.Subports {
subports[i] = make(map[string]interface{})
subports[i]["port_id"] = trunkSubport.PortID
subports[i]["segmentation_type"] = trunkSubport.SegmentationType
subports[i]["segmentation_id"] = trunkSubport.SegmentationID
}
if err = d.Set("sub_port", subports); err != nil {
return diag.Errorf("Unable to set sub_port for trunk %s: %s", d.Id(), err)
}
return nil
}