Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] Network port attach resource and deprecate macaddress property #5920

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions ibm/service/power/data_source_ibm_pi_instance_ip.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func DataSourceIBMPIInstanceIP() *schema.Resource {
Type: schema.TypeString,
},
Attr_Macaddress: {
Computed: true,
Deprecated: "Deprecated, use mac_address instead",
Description: "The MAC address of the network that is attached to this instance.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Computed: true,
Description: "The MAC address of the network that is attached to this instance.",
Type: schema.TypeString,
Expand Down Expand Up @@ -97,6 +103,7 @@ func dataSourceIBMPIInstancesIPRead(ctx context.Context, d *schema.ResourceData,
d.Set(Attr_ExternalIP, network.ExternalIP)
d.Set(Attr_IP, network.IPAddress)
d.Set(Attr_Macaddress, network.MacAddress)
d.Set(Attr_MacAddress, network.MacAddress)
d.Set(Attr_NetworkID, network.NetworkID)
d.Set(Attr_Type, network.Type)

Expand Down
7 changes: 7 additions & 0 deletions ibm/service/power/data_source_ibm_pi_network_port.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func DataSourceIBMPINetworkPort() *schema.Resource {
Type: schema.TypeString,
},
Attr_Macaddress: {
Computed: true,
Deprecated: "Deprecated, use mac_address instead",
Description: "The MAC address of the port.",
Type: schema.TypeString,
},
Attr_MacAddress: {
Computed: true,
Description: "The MAC address of the port.",
Type: schema.TypeString,
Expand Down Expand Up @@ -113,6 +119,7 @@ func flattenNetworkPorts(networkPorts []*models.NetworkPort) interface{} {
Attr_Href: i.Href,
Attr_IPaddress: *i.IPAddress,
Attr_Macaddress: *i.MacAddress,
Attr_MacAddress: *i.MacAddress,
Attr_PortID: *i.PortID,
Attr_PublicIP: i.ExternalIP,
Attr_Status: *i.Status,
Expand Down
3 changes: 3 additions & 0 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ const (
Arg_NetworkMTU = "pi_network_mtu"
Arg_NetworkName = "pi_network_name"
Arg_NetworkPeer = "pi_network_peer"
Arg_NetworkPortDescription = "pi_network_port_description"
Arg_NetworkPortIPAddress = "pi_network_port_ipaddress"
Arg_NetworkSecurityGroupID = "pi_network_security_group_id"
Arg_NetworkSecurityGroupMemberID = "pi_network_security_group_member_id"
Arg_NetworkSecurityGroupRuleID = "pi_network_security_group_rule_id"
Expand Down Expand Up @@ -343,6 +345,7 @@ const (
Attr_NetworkName = "network_name"
Attr_NetworkPeers = "network_peers"
Attr_NetworkPorts = "network_ports"
Attr_NetworkPortID = "network_port_id"
Attr_Networks = "networks"
Attr_NetworkSecurityGroupID = "network_security_group_id"
Attr_NetworkSecurityGroupMemberID = "network_security_group_member_id"
Expand Down
167 changes: 90 additions & 77 deletions ibm/service/power/resource_ibm_pi_network_port_attach.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,21 @@ import (
"context"
"fmt"
"log"
"strings"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"

st "github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/helpers"
"github.com/IBM-Cloud/power-go-client/clients/instance"
"github.com/IBM-Cloud/power-go-client/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func ResourceIBMPINetworkPortAttach() *schema.Resource {
return &schema.Resource{

CreateContext: resourceIBMPINetworkPortAttachCreate,
ReadContext: resourceIBMPINetworkPortAttachRead,
DeleteContext: resourceIBMPINetworkPortAttachDelete,
Expand All @@ -34,35 +33,40 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
},

Schema: map[string]*schema.Schema{
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Arg_CloudInstanceID: {
Description: "The GUID of the service instance associated with an account.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PIInstanceId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Instance id to attach the network port to",
Arg_InstanceID: {
Description: "Instance id to attach the network port to.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PINetworkName: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Network Name - This is the subnet name in the Cloud instance",
Arg_NetworkName: {
Description: "The network ID or name.",
ForceNew: true,
Required: true,
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
helpers.PINetworkPortDescription: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "A human readable description for this network Port",
Arg_NetworkPortDescription: {
Default: "Port Created via Terraform",
Description: "The description for the Network Port.",
ForceNew: true,
Optional: true,
Type: schema.TypeString,
},
helpers.PINetworkPortIPAddress: {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Arg_NetworkPortIPAddress: {
Computed: true,
Description: "The requested ip address of this port",
ForceNew: true,
Optional: true,
Type: schema.TypeString,
},
Arg_UserTags: {
Description: "The user tags attached to this resource.",
Expand All @@ -73,22 +77,32 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
Type: schema.TypeSet,
},

//Computed Attributes
"macaddress": {
Type: schema.TypeString,
Computed: true,
// Attributes
Attr_MacAddress: {
Computed: true,
Description: "The MAC address of the port.",
Type: schema.TypeString,
},
"network_port_id": {
Type: schema.TypeString,
Computed: true,
Attr_Macaddress: {
Computed: true,
Deprecated: "Deprecated, use mac_address instead",
Description: "The MAC address of the instance.",
Type: schema.TypeString,
},
"status": {
Type: schema.TypeString,
Computed: true,
Attr_NetworkPortID: {
Computed: true,
Description: "The ID of the port.",
Type: schema.TypeString,
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Attr_PublicIP: {
Computed: true,
Description: "The public IP associated with the port.",
Type: schema.TypeString,
},
Attr_Status: {
Computed: true,
Description: "The status of the port.",
Type: schema.TypeString,
},
},
}
Expand All @@ -100,13 +114,13 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
networkname := d.Get(helpers.PINetworkName).(string)
instanceID := d.Get(helpers.PIInstanceId).(string)
description := d.Get(helpers.PINetworkPortDescription).(string)
cloudInstanceID := d.Get(Arg_CloudInstanceID).(string)
description := d.Get(Arg_NetworkPortDescription).(string)
instanceID := d.Get(Arg_InstanceID).(string)
networkname := d.Get(Arg_NetworkName).(string)
nwportBody := &models.NetworkPortCreate{Description: description}

if v, ok := d.GetOk(helpers.PINetworkPortIPAddress); ok {
if v, ok := d.GetOk(Arg_NetworkPortIPAddress); ok {
ipaddress := v.(string)
nwportBody.IPAddress = ipaddress
}
Expand All @@ -118,7 +132,7 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
PvmInstanceID: &instanceID,
}

client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)

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

networkC := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
networkC := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
networkdata, err := networkC.GetPort(networkname, portID)
if err != nil {
return diag.FromErr(err)
}

d.Set(helpers.PINetworkPortIPAddress, networkdata.IPAddress)
d.Set(helpers.PINetworkPortDescription, networkdata.Description)
d.Set(helpers.PIInstanceId, networkdata.PvmInstance.PvmInstanceID)
d.Set("macaddress", networkdata.MacAddress)
d.Set("status", networkdata.Status)
d.Set("network_port_id", networkdata.PortID)
d.Set("public_ip", networkdata.ExternalIP)
d.Set(Arg_InstanceID, networkdata.PvmInstance.PvmInstanceID)
d.Set(Arg_NetworkPortDescription, networkdata.Description)
d.Set(Arg_NetworkPortIPAddress, networkdata.IPAddress)
d.Set(Attr_MacAddress, networkdata.MacAddress)
d.Set(Attr_Macaddress, networkdata.MacAddress)
d.Set(Attr_NetworkPortID, networkdata.PortID)
d.Set(Attr_PublicIP, networkdata.ExternalIP)
d.Set(Attr_Status, networkdata.Status)

return nil
}

func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {

log.Printf("Calling the network delete functions. ")
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
Expand All @@ -196,7 +210,7 @@ func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.Resourc
networkname := parts[1]
portID := parts[2]

client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)
client := instance.NewIBMPINetworkClient(ctx, sess, cloudInstanceID)

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

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

stateConf := &resource.StateChangeConf{
Pending: []string{"retry", helpers.PINetworkProvisioning},
Target: []string{"DOWN"},
stateConf := &retry.StateChangeConf{
Pending: []string{State_Retry, State_Build},
Target: []string{State_Down},
Refresh: isIBMPINetworkportRefreshFunc(client, id, networkname),
Timeout: timeout,
Delay: 10 * time.Second,
Expand All @@ -223,29 +237,29 @@ func isWaitForIBMPINetworkportAvailable(ctx context.Context, client *st.IBMPINet
return stateConf.WaitForStateContext(ctx)
}

func isIBMPINetworkportRefreshFunc(client *st.IBMPINetworkClient, id, networkname string) resource.StateRefreshFunc {

func isIBMPINetworkportRefreshFunc(client *instance.IBMPINetworkClient, id, networkname string) retry.StateRefreshFunc {
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)
return func() (interface{}, string, error) {
network, err := client.GetPort(networkname, id)
if err != nil {
return nil, "", err
}

if *network.Status == "DOWN" {
if strings.ToLower(*network.Status) == State_Down {
log.Printf(" The port has been created with the following ip address and attached to an instance ")
return network, "DOWN", nil
return network, State_Down, nil
}

return network, helpers.PINetworkProvisioning, nil
return network, State_Build, nil
}
}
func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IBMPINetworkClient, id, networkname, instanceid string, timeout time.Duration) (interface{}, error) {

func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *instance.IBMPINetworkClient, id, networkname, instanceid string, timeout time.Duration) (interface{}, error) {
log.Printf("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available.", id, networkname)

stateConf := &resource.StateChangeConf{
Pending: []string{"retry", helpers.PINetworkProvisioning},
Target: []string{"ACTIVE"},
stateConf := &retry.StateChangeConf{
Pending: []string{State_Retry, State_Build},
Target: []string{State_Active},
Refresh: isIBMPINetworkPortAttachRefreshFunc(client, id, networkname, instanceid),
Timeout: timeout,
Delay: 10 * time.Second,
Expand All @@ -255,20 +269,19 @@ func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IB
return stateConf.WaitForStateContext(ctx)
}

func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, networkname, instanceid string) resource.StateRefreshFunc {

func isIBMPINetworkPortAttachRefreshFunc(client *instance.IBMPINetworkClient, id, networkname, instanceid string) retry.StateRefreshFunc {
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)
return func() (interface{}, string, error) {
network, err := client.GetPort(networkname, id)
if err != nil {
return nil, "", err
}

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

return network, helpers.PINetworkProvisioning, nil
return network, State_Build, nil
}
}
Loading
Loading