-
Notifications
You must be signed in to change notification settings - Fork 666
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
added docs & enhanced network-port-attach resource #3756
Merged
hkantare
merged 3 commits into
IBM-Cloud:master
from
dhirendersingh19:network-port-attach
May 24, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -25,40 +25,63 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource { | |
|
||
CreateContext: resourceIBMPINetworkPortAttachCreate, | ||
ReadContext: resourceIBMPINetworkPortAttachRead, | ||
UpdateContext: resourceIBMPINetworkPortAttachUpdate, | ||
DeleteContext: resourceIBMPINetworkPortAttachDelete, | ||
Importer: &schema.ResourceImporter{}, | ||
|
||
Timeouts: &schema.ResourceTimeout{ | ||
Create: schema.DefaultTimeout(60 * time.Minute), | ||
Delete: schema.DefaultTimeout(60 * time.Minute), | ||
Update: schema.DefaultTimeout(60 * time.Minute), | ||
}, | ||
Schema: map[string]*schema.Schema{ | ||
"port_id": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
helpers.PICloudInstanceId: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
helpers.PIInstanceName: { | ||
helpers.PIInstanceId: { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above can't change a argument .. |
||
Type: schema.TypeString, | ||
Required: true, | ||
Description: "Instance name to attach the network port to", | ||
ForceNew: true, | ||
Description: "Instance id to attach the network port to", | ||
}, | ||
helpers.PINetworkName: { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
Description: "Network Name - This is the subnet name in the Cloud instance", | ||
}, | ||
helpers.PINetworkPortDescription: { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Description: "A human readable description for this network Port", | ||
Default: "Port Created via Terraform", | ||
}, | ||
helpers.PINetworkPortIPAddress: { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
Computed: true, | ||
}, | ||
|
||
//Computed Attributes | ||
"macaddress": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"port_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Deprecated: "port_id attribute is deprecated, use network_port_id instead.", | ||
}, | ||
"network_port_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"status": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"public_ip": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
|
@@ -73,118 +96,152 @@ 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) | ||
portid := d.Get("port_id").(string) | ||
instancename := d.Get(helpers.PIInstanceName).(string) | ||
instanceID := d.Get(helpers.PIInstanceId).(string) | ||
description := d.Get(helpers.PINetworkPortDescription).(string) | ||
client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID) | ||
nwportBody := &models.NetworkPortCreate{Description: description} | ||
|
||
log.Printf("Printing the input to the resource: cloud instance [%s] and network name [%s] and the portid [%s]", cloudInstanceID, networkname, portid) | ||
body := &models.NetworkPortUpdate{ | ||
if v, ok := d.GetOk(helpers.PINetworkPortIPAddress); ok { | ||
ipaddress := v.(string) | ||
nwportBody.IPAddress = ipaddress | ||
} | ||
|
||
nwportattachBody := &models.NetworkPortUpdate{ | ||
Description: &description, | ||
PvmInstanceID: &instancename, | ||
PvmInstanceID: &instanceID, | ||
} | ||
networkPortResponse, err := client.UpdatePort(networkname, portid, body) | ||
|
||
client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID) | ||
|
||
networkPortResponse, err := client.CreatePort(networkname, nwportBody) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
log.Printf("Printing the networkresponse %+v", &networkPortResponse) | ||
|
||
IBMPINetworkPortID := *networkPortResponse.PortID | ||
networkPortID := *networkPortResponse.PortID | ||
|
||
d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, networkname, IBMPINetworkPortID)) | ||
_, err = isWaitForIBMPINetworkportAvailable(ctx, client, networkPortID, networkname, d.Timeout(schema.TimeoutCreate)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
networkPortResponse, err = client.UpdatePort(networkname, networkPortID, nwportattachBody) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
_, err = isWaitForIBMPINetworkPortAttachAvailable(ctx, client, IBMPINetworkPortID, networkname, d.Timeout(schema.TimeoutCreate)) | ||
_, err = isWaitForIBMPINetworkPortAttachAvailable(ctx, client, networkPortID, networkname, instanceID, d.Timeout(schema.TimeoutUpdate)) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, networkname, networkPortID)) | ||
|
||
return resourceIBMPINetworkPortAttachRead(ctx, d, meta) | ||
} | ||
|
||
func resourceIBMPINetworkPortAttachRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
log.Printf("Calling ther Network Port Attach Read code") | ||
sess, err := meta.(conns.ClientSession).IBMPISession() | ||
|
||
if err != nil { | ||
fmt.Printf("failed to get a session from the IBM Cloud Service %v", err) | ||
return diag.FromErr(err) | ||
} | ||
|
||
parts, err := flex.IdParts(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
cloudInstanceID := parts[0] | ||
networkID := parts[1] | ||
networkname := parts[1] | ||
portID := parts[2] | ||
|
||
networkC := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID) | ||
networkdata, err := networkC.GetPort(networkID, portID) | ||
networkdata, err := networkC.GetPort(networkname, portID) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
d.Set("ipaddress", networkdata.IPAddress) | ||
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("portid", networkdata.PortID) | ||
d.Set("pvminstance", networkdata.PvmInstance.Href) | ||
d.Set("network_port_id", networkdata.PortID) | ||
d.Set("port_id", networkdata.PortID) | ||
d.Set("public_ip", networkdata.ExternalIP) | ||
|
||
return nil | ||
} | ||
|
||
func resourceIBMPINetworkPortAttachUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
log.Printf("Calling the attach update ") | ||
return nil | ||
} | ||
|
||
func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
log.Printf("Detaching the network port from the Instance ") | ||
|
||
log.Printf("Calling the network delete functions. ") | ||
sess, err := meta.(conns.ClientSession).IBMPISession() | ||
if err != nil { | ||
fmt.Printf("failed to get a session from the IBM Cloud Service %v", err) | ||
|
||
return diag.FromErr(err) | ||
} | ||
|
||
parts, err := flex.IdParts(d.Id()) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
cloudInstanceID := parts[0] | ||
networkID := parts[1] | ||
networkname := parts[1] | ||
portID := parts[2] | ||
|
||
client := st.NewIBMPINetworkClient(ctx, sess, cloudInstanceID) | ||
log.Printf("Executing network port detach") | ||
emptyPVM := "" | ||
body := &models.NetworkPortUpdate{ | ||
PvmInstanceID: &emptyPVM, | ||
} | ||
networkPort, err := client.UpdatePort(networkID, portID, body) | ||
|
||
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) | ||
if err != nil { | ||
return diag.FromErr(err) | ||
} | ||
|
||
log.Printf("Printing the networkresponse %+v", &networkPort) | ||
|
||
d.SetId("") | ||
return nil | ||
} | ||
|
||
func isWaitForIBMPINetworkportAvailable(ctx context.Context, client *st.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"}, | ||
Refresh: isIBMPINetworkportRefreshFunc(client, id, networkname), | ||
Timeout: timeout, | ||
Delay: 10 * time.Second, | ||
MinTimeout: 10 * time.Minute, | ||
} | ||
|
||
return stateConf.WaitForStateContext(ctx) | ||
} | ||
|
||
func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IBMPINetworkClient, id string, networkname string, timeout time.Duration) (interface{}, error) { | ||
func isIBMPINetworkportRefreshFunc(client *st.IBMPINetworkClient, id, networkname string) resource.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" { | ||
log.Printf(" The port has been created with the following ip address and attached to an instance ") | ||
return network, "DOWN", nil | ||
} | ||
|
||
return network, helpers.PINetworkProvisioning, nil | ||
} | ||
} | ||
func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.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"}, | ||
Refresh: isIBMPINetworkPortAttachRefreshFunc(client, id, networkname), | ||
Refresh: isIBMPINetworkPortAttachRefreshFunc(client, id, networkname, instanceid), | ||
Timeout: timeout, | ||
Delay: 10 * time.Second, | ||
MinTimeout: 10 * time.Minute, | ||
|
@@ -193,7 +250,7 @@ func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IB | |
return stateConf.WaitForStateContext(ctx) | ||
} | ||
|
||
func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, networkname string) resource.StateRefreshFunc { | ||
func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, networkname, instanceid string) resource.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) { | ||
|
@@ -202,8 +259,7 @@ func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, netw | |
return nil, "", err | ||
} | ||
|
||
if &network.PortID != nil && &network.PvmInstance.PvmInstanceID != nil { | ||
//if network.State == "available" { | ||
if *network.Status == "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 | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The required argument can't be removed directly needs to follow Terraform guidelines
https://www.terraform.io/plugin/sdkv2/best-practices/deprecations
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this resources we are exposin to the users for the first time ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this resource was present in an earlier version but without documentation, the network-port resource will be deprecated and the features of network-port will combine in the network-port-attach resource