-
Notifications
You must be signed in to change notification settings - Fork 154
/
data_source_huaweicloud_iec_port.go
117 lines (102 loc) · 2.88 KB
/
data_source_huaweicloud_iec_port.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
package iec
import (
"context"
"fmt"
"log"
"github.com/hashicorp/go-multierror"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/chnsz/golangsdk/openstack/iec/v1/ports"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)
// @API IEC GET /v1/ports
func DataSourcePort() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourcePortRead,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"fixed_ip": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.IsIPAddress,
},
"mac_address": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"subnet_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"site_id": {
Type: schema.TypeString,
Computed: true,
},
"security_groups": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func dataSourcePortRead(_ 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 := ports.ListOpts{
ID: d.Get("id").(string),
NetworkID: d.Get("subnet_id").(string),
MacAddress: d.Get("mac_address").(string),
}
var ipFilter bool
if v, ipFilter := d.GetOk("fixed_ip"); ipFilter {
listOpts.FixedIPs = []string{fmt.Sprintf("ip_address=%s", v)}
}
allPorts, err := ports.List(iecClient, listOpts).Extract()
if err != nil {
return diag.Errorf("unable to retrieve IEC port: %s", err)
}
total := len(allPorts.Ports)
if total < 1 {
return diag.Errorf("your query returned no results. Please change your search criteria and try again")
}
if total > 1 {
return diag.Errorf("your query returned more than one result. Please try a more specific search criteria")
}
port := allPorts.Ports[0]
log.Printf("[DEBUG] Retrieved IEC port %s: %+v", port.ID, port)
d.SetId(port.ID)
mErr := multierror.Append(nil,
d.Set("region", cfg.GetRegion(d)),
d.Set("mac_address", port.MacAddress),
d.Set("subnet_id", port.NetworkID),
d.Set("status", port.Status),
d.Set("site_id", port.SiteID),
d.Set("security_groups", port.SecurityGroups),
)
if !ipFilter && len(port.FixedIPs) > 0 {
fixedIP := port.FixedIPs[0].IpAddress
mErr = multierror.Append(mErr, d.Set("fixed_ip", fixedIP))
}
return diag.FromErr(mErr.ErrorOrNil())
}