Skip to content

Commit 24d8a4b

Browse files
authored
[Refactor] Network port attach resource and deprecate macaddress property (IBM-Cloud#5920)
* Refactor Network Port * Deprecate macaddress for mac_address
1 parent 00aac5d commit 24d8a4b

8 files changed

+245
-130
lines changed

ibm/service/power/data_source_ibm_pi_instance_ip.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func DataSourceIBMPIInstanceIP() *schema.Resource {
5757
Type: schema.TypeString,
5858
},
5959
Attr_Macaddress: {
60+
Computed: true,
61+
Deprecated: "Deprecated, use mac_address instead",
62+
Description: "The MAC address of the network that is attached to this instance.",
63+
Type: schema.TypeString,
64+
},
65+
Attr_MacAddress: {
6066
Computed: true,
6167
Description: "The MAC address of the network that is attached to this instance.",
6268
Type: schema.TypeString,
@@ -97,6 +103,7 @@ func dataSourceIBMPIInstancesIPRead(ctx context.Context, d *schema.ResourceData,
97103
d.Set(Attr_ExternalIP, network.ExternalIP)
98104
d.Set(Attr_IP, network.IPAddress)
99105
d.Set(Attr_Macaddress, network.MacAddress)
106+
d.Set(Attr_MacAddress, network.MacAddress)
100107
d.Set(Attr_NetworkID, network.NetworkID)
101108
d.Set(Attr_Type, network.Type)
102109

ibm/service/power/data_source_ibm_pi_network_port.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,12 @@ func DataSourceIBMPINetworkPort() *schema.Resource {
5757
Type: schema.TypeString,
5858
},
5959
Attr_Macaddress: {
60+
Computed: true,
61+
Deprecated: "Deprecated, use mac_address instead",
62+
Description: "The MAC address of the port.",
63+
Type: schema.TypeString,
64+
},
65+
Attr_MacAddress: {
6066
Computed: true,
6167
Description: "The MAC address of the port.",
6268
Type: schema.TypeString,
@@ -113,6 +119,7 @@ func flattenNetworkPorts(networkPorts []*models.NetworkPort) interface{} {
113119
Attr_Href: i.Href,
114120
Attr_IPaddress: *i.IPAddress,
115121
Attr_Macaddress: *i.MacAddress,
122+
Attr_MacAddress: *i.MacAddress,
116123
Attr_PortID: *i.PortID,
117124
Attr_PublicIP: i.ExternalIP,
118125
Attr_Status: *i.Status,

ibm/service/power/ibm_pi_constants.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,8 @@ const (
8989
Arg_NetworkMTU = "pi_network_mtu"
9090
Arg_NetworkName = "pi_network_name"
9191
Arg_NetworkPeer = "pi_network_peer"
92+
Arg_NetworkPortDescription = "pi_network_port_description"
93+
Arg_NetworkPortIPAddress = "pi_network_port_ipaddress"
9294
Arg_NetworkSecurityGroupID = "pi_network_security_group_id"
9395
Arg_NetworkSecurityGroupMemberID = "pi_network_security_group_member_id"
9496
Arg_NetworkSecurityGroupRuleID = "pi_network_security_group_rule_id"
@@ -343,6 +345,7 @@ const (
343345
Attr_NetworkName = "network_name"
344346
Attr_NetworkPeers = "network_peers"
345347
Attr_NetworkPorts = "network_ports"
348+
Attr_NetworkPortID = "network_port_id"
346349
Attr_Networks = "networks"
347350
Attr_NetworkSecurityGroupID = "network_security_group_id"
348351
Attr_NetworkSecurityGroupMemberID = "network_security_group_member_id"

ibm/service/power/resource_ibm_pi_network_port_attach.go

Lines changed: 90 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -7,22 +7,21 @@ import (
77
"context"
88
"fmt"
99
"log"
10+
"strings"
1011
"time"
1112

12-
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
13-
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14-
15-
st "github.com/IBM-Cloud/power-go-client/clients/instance"
16-
"github.com/IBM-Cloud/power-go-client/helpers"
13+
"github.com/IBM-Cloud/power-go-client/clients/instance"
1714
"github.com/IBM-Cloud/power-go-client/power/models"
1815
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
1916
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
17+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
18+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
2019
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
20+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
2121
)
2222

2323
func ResourceIBMPINetworkPortAttach() *schema.Resource {
2424
return &schema.Resource{
25-
2625
CreateContext: resourceIBMPINetworkPortAttachCreate,
2726
ReadContext: resourceIBMPINetworkPortAttachRead,
2827
DeleteContext: resourceIBMPINetworkPortAttachDelete,
@@ -34,35 +33,40 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
3433
},
3534

3635
Schema: map[string]*schema.Schema{
37-
helpers.PICloudInstanceId: {
38-
Type: schema.TypeString,
39-
Required: true,
40-
ForceNew: true,
36+
Arg_CloudInstanceID: {
37+
Description: "The GUID of the service instance associated with an account.",
38+
ForceNew: true,
39+
Required: true,
40+
Type: schema.TypeString,
41+
ValidateFunc: validation.NoZeroValues,
4142
},
42-
helpers.PIInstanceId: {
43-
Type: schema.TypeString,
44-
Required: true,
45-
ForceNew: true,
46-
Description: "Instance id to attach the network port to",
43+
Arg_InstanceID: {
44+
Description: "Instance id to attach the network port to.",
45+
ForceNew: true,
46+
Required: true,
47+
Type: schema.TypeString,
48+
ValidateFunc: validation.NoZeroValues,
4749
},
48-
helpers.PINetworkName: {
49-
Type: schema.TypeString,
50-
Required: true,
51-
ForceNew: true,
52-
Description: "Network Name - This is the subnet name in the Cloud instance",
50+
Arg_NetworkName: {
51+
Description: "The network ID or name.",
52+
ForceNew: true,
53+
Required: true,
54+
Type: schema.TypeString,
55+
ValidateFunc: validation.NoZeroValues,
5356
},
54-
helpers.PINetworkPortDescription: {
55-
Type: schema.TypeString,
56-
Optional: true,
57-
ForceNew: true,
58-
Description: "A human readable description for this network Port",
57+
Arg_NetworkPortDescription: {
5958
Default: "Port Created via Terraform",
59+
Description: "The description for the Network Port.",
60+
ForceNew: true,
61+
Optional: true,
62+
Type: schema.TypeString,
6063
},
61-
helpers.PINetworkPortIPAddress: {
62-
Type: schema.TypeString,
63-
Optional: true,
64-
ForceNew: true,
65-
Computed: true,
64+
Arg_NetworkPortIPAddress: {
65+
Computed: true,
66+
Description: "The requested ip address of this port",
67+
ForceNew: true,
68+
Optional: true,
69+
Type: schema.TypeString,
6670
},
6771
Arg_UserTags: {
6872
Description: "The user tags attached to this resource.",
@@ -73,22 +77,32 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
7377
Type: schema.TypeSet,
7478
},
7579

76-
//Computed Attributes
77-
"macaddress": {
78-
Type: schema.TypeString,
79-
Computed: true,
80+
// Attributes
81+
Attr_MacAddress: {
82+
Computed: true,
83+
Description: "The MAC address of the port.",
84+
Type: schema.TypeString,
8085
},
81-
"network_port_id": {
82-
Type: schema.TypeString,
83-
Computed: true,
86+
Attr_Macaddress: {
87+
Computed: true,
88+
Deprecated: "Deprecated, use mac_address instead",
89+
Description: "The MAC address of the instance.",
90+
Type: schema.TypeString,
8491
},
85-
"status": {
86-
Type: schema.TypeString,
87-
Computed: true,
92+
Attr_NetworkPortID: {
93+
Computed: true,
94+
Description: "The ID of the port.",
95+
Type: schema.TypeString,
8896
},
89-
"public_ip": {
90-
Type: schema.TypeString,
91-
Computed: true,
97+
Attr_PublicIP: {
98+
Computed: true,
99+
Description: "The public IP associated with the port.",
100+
Type: schema.TypeString,
101+
},
102+
Attr_Status: {
103+
Computed: true,
104+
Description: "The status of the port.",
105+
Type: schema.TypeString,
92106
},
93107
},
94108
}
@@ -100,13 +114,13 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
100114
if err != nil {
101115
return diag.FromErr(err)
102116
}
103-
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
104-
networkname := d.Get(helpers.PINetworkName).(string)
105-
instanceID := d.Get(helpers.PIInstanceId).(string)
106-
description := d.Get(helpers.PINetworkPortDescription).(string)
117+
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
118+
description := d.Get(Arg_NetworkPortDescription).(string)
119+
instanceID := d.Get(Arg_InstanceID).(string)
120+
networkname := d.Get(Arg_NetworkName).(string)
107121
nwportBody := &models.NetworkPortCreate{Description: description}
108122

109-
if v, ok := d.GetOk(helpers.PINetworkPortIPAddress); ok {
123+
if v, ok := d.GetOk(Arg_NetworkPortIPAddress); ok {
110124
ipaddress := v.(string)
111125
nwportBody.IPAddress = ipaddress
112126
}
@@ -118,7 +132,7 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
118132
PvmInstanceID: &instanceID,
119133
}
120134

121-
client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
135+
client := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
122136

123137
networkPortResponse, err := client.CreatePort(networkname, nwportBody)
124138
if err != nil {
@@ -163,25 +177,25 @@ func resourceIBMPINetworkPortAttachRead(ctx context.Context, d *schema.ResourceD
163177
networkname := parts[1]
164178
portID := parts[2]
165179

166-
networkC := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
180+
networkC := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
167181
networkdata, err := networkC.GetPort(networkname, portID)
168182
if err != nil {
169183
return diag.FromErr(err)
170184
}
171185

172-
d.Set(helpers.PINetworkPortIPAddress, networkdata.IPAddress)
173-
d.Set(helpers.PINetworkPortDescription, networkdata.Description)
174-
d.Set(helpers.PIInstanceId, networkdata.PvmInstance.PvmInstanceID)
175-
d.Set("macaddress", networkdata.MacAddress)
176-
d.Set("status", networkdata.Status)
177-
d.Set("network_port_id", networkdata.PortID)
178-
d.Set("public_ip", networkdata.ExternalIP)
186+
d.Set(Arg_InstanceID, networkdata.PvmInstance.PvmInstanceID)
187+
d.Set(Arg_NetworkPortDescription, networkdata.Description)
188+
d.Set(Arg_NetworkPortIPAddress, networkdata.IPAddress)
189+
d.Set(Attr_MacAddress, networkdata.MacAddress)
190+
d.Set(Attr_Macaddress, networkdata.MacAddress)
191+
d.Set(Attr_NetworkPortID, networkdata.PortID)
192+
d.Set(Attr_PublicIP, networkdata.ExternalIP)
193+
d.Set(Attr_Status, networkdata.Status)
179194

180195
return nil
181196
}
182197

183198
func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
184-
185199
log.Printf("Calling the network delete functions. ")
186200
sess, err := meta.(conns.ClientSession).IBMPISession()
187201
if err != nil {
@@ -196,7 +210,7 @@ func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.Resourc
196210
networkname := parts[1]
197211
portID := parts[2]
198212

199-
client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
213+
client := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
200214

201215
log.Printf("Calling the delete with the following params delete with cloud instance (%s) and networkid (%s) and portid (%s) ", cloudInstanceID, networkname, portID)
202216
err = client.DeletePort(networkname, portID)
@@ -208,12 +222,12 @@ func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.Resourc
208222
return nil
209223
}
210224

211-
func isWaitForIBMPINetworkportAvailable(ctx context.Context, client *st.IBMPINetworkClient, id string, networkname string, timeout time.Duration) (interface{}, error) {
225+
func isWaitForIBMPINetworkportAvailable(ctx context.Context, client *instance.IBMPINetworkClient, id string, networkname string, timeout time.Duration) (interface{}, error) {
212226
log.Printf("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available.", id, networkname)
213227

214-
stateConf := &resource.StateChangeConf{
215-
Pending: []string{"retry", helpers.PINetworkProvisioning},
216-
Target: []string{"DOWN"},
228+
stateConf := &retry.StateChangeConf{
229+
Pending: []string{State_Retry, State_Build},
230+
Target: []string{State_Down},
217231
Refresh: isIBMPINetworkportRefreshFunc(client, id, networkname),
218232
Timeout: timeout,
219233
Delay: 10 * time.Second,
@@ -223,29 +237,29 @@ func isWaitForIBMPINetworkportAvailable(ctx context.Context, client *st.IBMPINet
223237
return stateConf.WaitForStateContext(ctx)
224238
}
225239

226-
func isIBMPINetworkportRefreshFunc(client *st.IBMPINetworkClient, id, networkname string) resource.StateRefreshFunc {
227-
240+
func isIBMPINetworkportRefreshFunc(client *instance.IBMPINetworkClient, id, networkname string) retry.StateRefreshFunc {
228241
log.Printf("Calling the IsIBMPINetwork Refresh Function....with the following id (%s) for network port and following id (%s) for network name and waiting for network to be READY", id, networkname)
229242
return func() (interface{}, string, error) {
230243
network, err := client.GetPort(networkname, id)
231244
if err != nil {
232245
return nil, "", err
233246
}
234247

235-
if *network.Status == "DOWN" {
248+
if strings.ToLower(*network.Status) == State_Down {
236249
log.Printf(" The port has been created with the following ip address and attached to an instance ")
237-
return network, "DOWN", nil
250+
return network, State_Down, nil
238251
}
239252

240-
return network, helpers.PINetworkProvisioning, nil
253+
return network, State_Build, nil
241254
}
242255
}
243-
func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IBMPINetworkClient, id, networkname, instanceid string, timeout time.Duration) (interface{}, error) {
256+
257+
func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *instance.IBMPINetworkClient, id, networkname, instanceid string, timeout time.Duration) (interface{}, error) {
244258
log.Printf("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available.", id, networkname)
245259

246-
stateConf := &resource.StateChangeConf{
247-
Pending: []string{"retry", helpers.PINetworkProvisioning},
248-
Target: []string{"ACTIVE"},
260+
stateConf := &retry.StateChangeConf{
261+
Pending: []string{State_Retry, State_Build},
262+
Target: []string{State_Active},
249263
Refresh: isIBMPINetworkPortAttachRefreshFunc(client, id, networkname, instanceid),
250264
Timeout: timeout,
251265
Delay: 10 * time.Second,
@@ -255,20 +269,19 @@ func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IB
255269
return stateConf.WaitForStateContext(ctx)
256270
}
257271

258-
func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, networkname, instanceid string) resource.StateRefreshFunc {
259-
272+
func isIBMPINetworkPortAttachRefreshFunc(client *instance.IBMPINetworkClient, id, networkname, instanceid string) retry.StateRefreshFunc {
260273
log.Printf("Calling the IsIBMPINetwork Refresh Function....with the following id (%s) for network port and following id (%s) for network name and waiting for network to be READY", id, networkname)
261274
return func() (interface{}, string, error) {
262275
network, err := client.GetPort(networkname, id)
263276
if err != nil {
264277
return nil, "", err
265278
}
266279

267-
if *network.Status == "ACTIVE" && network.PvmInstance.PvmInstanceID == instanceid {
280+
if strings.ToLower(*network.Status) == State_Active && network.PvmInstance.PvmInstanceID == instanceid {
268281
log.Printf(" The port has been created with the following ip address and attached to an instance ")
269-
return network, "ACTIVE", nil
282+
return network, State_Active, nil
270283
}
271284

272-
return network, helpers.PINetworkProvisioning, nil
285+
return network, State_Build, nil
273286
}
274287
}

0 commit comments

Comments
 (0)