Skip to content

Commit

Permalink
Refactor Network Port
Browse files Browse the repository at this point in the history
  • Loading branch information
ismirlia committed Nov 20, 2024
1 parent f542550 commit 2e512fc
Show file tree
Hide file tree
Showing 5 changed files with 208 additions and 122 deletions.
6 changes: 3 additions & 3 deletions ibm/service/power/ibm_pi_constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ const (
Arg_NetworkID = "pi_network_id"
Arg_NetworkInterfaceID = "pi_network_interface_id"
Arg_NetworkName = "pi_network_name"
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 All @@ -64,9 +66,6 @@ const (
Arg_Processors = "pi_processors"
Arg_ProcType = "pi_proc_type"
Arg_Protocol = "pi_protocol"
Arg_PVMInstanceActionType = "pi_action"
Arg_PVMInstanceHealthStatus = "pi_health_status"
Arg_PVMInstanceId = "pi_instance_id"
Arg_Remote = "pi_remote"
Arg_Remove = "pi_remove"
Arg_Replicants = "pi_replicants"
Expand Down Expand Up @@ -294,6 +293,7 @@ const (
Attr_NetworkInterfaceID = "network_interface_id"
Attr_NetworkName = "network_name"
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
157 changes: 83 additions & 74 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 @@ -33,35 +32,40 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
Delete: schema.DefaultTimeout(60 * time.Minute),
},
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 @@ -72,22 +76,26 @@ 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_NetworkPortID: {
Computed: true,
Description: "The ID of the port.",
Type: schema.TypeString,
},
"status": {
Type: schema.TypeString,
Computed: true,
Attr_PublicIP: {
Computed: true,
Description: "The public IP associated with the port.",
Type: schema.TypeString,
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Attr_Status: {
Computed: true,
Description: "The status of the port.",
Type: schema.TypeString,
},
},
}
Expand All @@ -99,13 +107,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 @@ -117,7 +125,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 @@ -162,19 +170,19 @@ 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_NetworkPortID, networkdata.PortID)
d.Set(Attr_PublicIP, networkdata.ExternalIP)
d.Set(Attr_Status, networkdata.Status)

return nil
}
Expand All @@ -195,7 +203,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 @@ -207,12 +215,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 @@ -222,7 +230,7 @@ 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) {
Expand All @@ -231,20 +239,21 @@ func isIBMPINetworkportRefreshFunc(client *st.IBMPINetworkClient, id, networknam
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 @@ -254,7 +263,7 @@ 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) {
Expand All @@ -263,11 +272,11 @@ func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, netw
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

0 comments on commit 2e512fc

Please sign in to comment.