@@ -7,13 +7,14 @@ import (
77 "context"
88 "fmt"
99 "log"
10+ "strings"
1011 "time"
1112
1213 "github.com/hashicorp/terraform-plugin-sdk/v2/diag"
13- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
14+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/retry"
15+ "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1416
15- st "github.com/IBM-Cloud/power-go-client/clients/instance"
16- "github.com/IBM-Cloud/power-go-client/helpers"
17+ "github.com/IBM-Cloud/power-go-client/clients/instance"
1718 "github.com/IBM-Cloud/power-go-client/power/models"
1819 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
1920 "github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
@@ -33,53 +34,62 @@ func ResourceIBMPINetworkPortAttach() *schema.Resource {
3334 Delete : schema .DefaultTimeout (60 * time .Minute ),
3435 },
3536 Schema : map [string ]* schema.Schema {
36- helpers .PICloudInstanceId : {
37- Type : schema .TypeString ,
38- Required : true ,
39- ForceNew : true ,
37+ Arg_CloudInstanceID : {
38+ Description : "The GUID of the service instance associated with an account." ,
39+ ForceNew : true ,
40+ Required : true ,
41+ Type : schema .TypeString ,
42+ ValidateFunc : validation .NoZeroValues ,
4043 },
41- helpers .PIInstanceId : {
42- Type : schema .TypeString ,
43- Required : true ,
44- ForceNew : true ,
45- Description : "Instance id to attach the network port to" ,
44+ Arg_NetworkName : {
45+ Description : "Network Name - This is the subnet name in the Cloud instance." ,
46+ ForceNew : true ,
47+ Required : true ,
48+ Type : schema .TypeString ,
49+ ValidateFunc : validation .NoZeroValues ,
4650 },
47- helpers . PINetworkName : {
48- Type : schema . TypeString ,
49- Required : true ,
51+ Arg_NetworkPortDescription : {
52+ Default : "Port Created via Terraform" ,
53+ Description : "A human readable description for this network Port." ,
5054 ForceNew : true ,
51- Description : "Network Name - This is the subnet name in the Cloud instance" ,
52- },
53- helpers .PINetworkPortDescription : {
54- Type : schema .TypeString ,
5555 Optional : true ,
56- ForceNew : true ,
57- Description : "A human readable description for this network Port" ,
58- Default : "Port Created via Terraform" ,
56+ Type : schema .TypeString ,
5957 },
60- helpers . PINetworkPortIPAddress : {
61- Type : schema . TypeString ,
62- Optional : true ,
58+ Arg_NetworkPortIPAddress : {
59+ Description : "The requested ip address of this port." ,
60+ // Computed: true,
6361 ForceNew : true ,
64- Computed : true ,
62+ Optional : true ,
63+ Type : schema .TypeString ,
64+ },
65+ Arg_PVMInstanceId : {
66+ Description : "Instance id to attach the network port to." ,
67+ ForceNew : true ,
68+ Required : true ,
69+ Type : schema .TypeString ,
70+ ValidateFunc : validation .NoZeroValues ,
6571 },
6672
67- //Computed Attributes
68- "macaddress" : {
69- Type : schema .TypeString ,
70- Computed : true ,
73+ // Attributes
74+ Attr_MacAddress : {
75+ Description : "The MAC address of the port." ,
76+ Computed : true ,
77+ Type : schema .TypeString ,
7178 },
72- "network_port_id" : {
73- Type : schema .TypeString ,
74- Computed : true ,
79+ Attr_NetworkPortID : {
80+ Description : "The ID of the port." ,
81+ Computed : true ,
82+ Type : schema .TypeString ,
7583 },
76- "status" : {
77- Type : schema .TypeString ,
78- Computed : true ,
84+ Attr_PublicIP : {
85+ Description : " The public IP associated with the port." ,
86+ Computed : true ,
87+ Type : schema .TypeString ,
7988 },
80- "public_ip" : {
81- Type : schema .TypeString ,
82- Computed : true ,
89+ Attr_Status : {
90+ Description : "The status of the port." ,
91+ Computed : true ,
92+ Type : schema .TypeString ,
8393 },
8494 },
8595 }
@@ -91,13 +101,13 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
91101 if err != nil {
92102 return diag .FromErr (err )
93103 }
94- cloudInstanceID := d .Get (helpers . PICloudInstanceId ).(string )
95- networkname := d .Get (helpers . PINetworkName ).(string )
96- instanceID := d .Get (helpers . PIInstanceId ).(string )
97- description := d .Get (helpers . PINetworkPortDescription ).(string )
104+ cloudInstanceID := d .Get (Arg_CloudInstanceID ).(string )
105+ networkname := d .Get (Arg_NetworkName ).(string )
106+ instanceID := d .Get (Arg_PVMInstanceId ).(string )
107+ description := d .Get (Arg_NetworkPortDescription ).(string )
98108 nwportBody := & models.NetworkPortCreate {Description : description }
99109
100- if v , ok := d .GetOk (helpers . PINetworkPortIPAddress ); ok {
110+ if v , ok := d .GetOk (Arg_NetworkPortIPAddress ); ok {
101111 ipaddress := v .(string )
102112 nwportBody .IPAddress = ipaddress
103113 }
@@ -107,7 +117,7 @@ func resourceIBMPINetworkPortAttachCreate(ctx context.Context, d *schema.Resourc
107117 PvmInstanceID : & instanceID ,
108118 }
109119
110- client := st .NewIBMPINetworkClient (ctx , sess , cloudInstanceID )
120+ client := instance .NewIBMPINetworkClient (ctx , sess , cloudInstanceID )
111121
112122 networkPortResponse , err := client .CreatePort (networkname , nwportBody )
113123 if err != nil {
@@ -152,19 +162,19 @@ func resourceIBMPINetworkPortAttachRead(ctx context.Context, d *schema.ResourceD
152162 networkname := parts [1 ]
153163 portID := parts [2 ]
154164
155- networkC := st .NewIBMPINetworkClient (ctx , sess , cloudInstanceID )
165+ networkC := instance .NewIBMPINetworkClient (ctx , sess , cloudInstanceID )
156166 networkdata , err := networkC .GetPort (networkname , portID )
157167 if err != nil {
158168 return diag .FromErr (err )
159169 }
160170
161- d .Set (helpers . PINetworkPortIPAddress , networkdata .IPAddress )
162- d .Set (helpers . PINetworkPortDescription , networkdata .Description )
163- d .Set (helpers . PIInstanceId , networkdata .PvmInstance .PvmInstanceID )
164- d .Set ("macaddress" , networkdata .MacAddress )
165- d .Set ("status" , networkdata .Status )
166- d .Set ("network_port_id" , networkdata .PortID )
167- d .Set ("public_ip" , networkdata .ExternalIP )
171+ d .Set (Arg_NetworkPortIPAddress , networkdata .IPAddress )
172+ d .Set (Arg_NetworkPortDescription , networkdata .Description )
173+ d .Set (Arg_PVMInstanceId , networkdata .PvmInstance .PvmInstanceID )
174+ d .Set (Attr_MacAddress , networkdata .MacAddress )
175+ d .Set (Attr_Status , networkdata .Status )
176+ d .Set (Attr_NetworkPortID , networkdata .PortID )
177+ d .Set (Attr_PublicIP , networkdata .ExternalIP )
168178
169179 return nil
170180}
@@ -185,7 +195,7 @@ func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.Resourc
185195 networkname := parts [1 ]
186196 portID := parts [2 ]
187197
188- client := st .NewIBMPINetworkClient (ctx , sess , cloudInstanceID )
198+ client := instance .NewIBMPINetworkClient (ctx , sess , cloudInstanceID )
189199
190200 log .Printf ("Calling the delete with the following params delete with cloud instance (%s) and networkid (%s) and portid (%s) " , cloudInstanceID , networkname , portID )
191201 err = client .DeletePort (networkname , portID )
@@ -197,12 +207,12 @@ func resourceIBMPINetworkPortAttachDelete(ctx context.Context, d *schema.Resourc
197207 return nil
198208}
199209
200- func isWaitForIBMPINetworkportAvailable (ctx context.Context , client * st .IBMPINetworkClient , id string , networkname string , timeout time.Duration ) (interface {}, error ) {
210+ func isWaitForIBMPINetworkportAvailable (ctx context.Context , client * instance .IBMPINetworkClient , id string , networkname string , timeout time.Duration ) (interface {}, error ) {
201211 log .Printf ("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available." , id , networkname )
202212
203- stateConf := & resource .StateChangeConf {
204- Pending : []string {"retry" , helpers . PINetworkProvisioning },
205- Target : []string {"DOWN" },
213+ stateConf := & retry .StateChangeConf {
214+ Pending : []string {State_Retry , State_Build },
215+ Target : []string {State_Down },
206216 Refresh : isIBMPINetworkportRefreshFunc (client , id , networkname ),
207217 Timeout : timeout ,
208218 Delay : 10 * time .Second ,
@@ -212,7 +222,7 @@ func isWaitForIBMPINetworkportAvailable(ctx context.Context, client *st.IBMPINet
212222 return stateConf .WaitForStateContext (ctx )
213223}
214224
215- func isIBMPINetworkportRefreshFunc (client * st .IBMPINetworkClient , id , networkname string ) resource .StateRefreshFunc {
225+ func isIBMPINetworkportRefreshFunc (client * instance .IBMPINetworkClient , id , networkname string ) retry .StateRefreshFunc {
216226
217227 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 )
218228 return func () (interface {}, string , error ) {
@@ -221,20 +231,20 @@ func isIBMPINetworkportRefreshFunc(client *st.IBMPINetworkClient, id, networknam
221231 return nil , "" , err
222232 }
223233
224- if * network .Status == "DOWN" {
234+ if strings . ToLower ( * network .Status ) == State_Down {
225235 log .Printf (" The port has been created with the following ip address and attached to an instance " )
226- return network , "DOWN" , nil
236+ return network , State_Down , nil
227237 }
228238
229- return network , helpers . PINetworkProvisioning , nil
239+ return network , State_Build , nil
230240 }
231241}
232- func isWaitForIBMPINetworkPortAttachAvailable (ctx context.Context , client * st .IBMPINetworkClient , id , networkname , instanceid string , timeout time.Duration ) (interface {}, error ) {
242+ func isWaitForIBMPINetworkPortAttachAvailable (ctx context.Context , client * instance .IBMPINetworkClient , id , networkname , instanceid string , timeout time.Duration ) (interface {}, error ) {
233243 log .Printf ("Waiting for Power Network (%s) that was created for Network Zone (%s) to be available." , id , networkname )
234244
235- stateConf := & resource .StateChangeConf {
236- Pending : []string {"retry" , helpers . PINetworkProvisioning },
237- Target : []string {"ACTIVE" },
245+ stateConf := & retry .StateChangeConf {
246+ Pending : []string {State_Retry , State_Build },
247+ Target : []string {State_Active },
238248 Refresh : isIBMPINetworkPortAttachRefreshFunc (client , id , networkname , instanceid ),
239249 Timeout : timeout ,
240250 Delay : 10 * time .Second ,
@@ -244,7 +254,7 @@ func isWaitForIBMPINetworkPortAttachAvailable(ctx context.Context, client *st.IB
244254 return stateConf .WaitForStateContext (ctx )
245255}
246256
247- func isIBMPINetworkPortAttachRefreshFunc (client * st .IBMPINetworkClient , id , networkname , instanceid string ) resource .StateRefreshFunc {
257+ func isIBMPINetworkPortAttachRefreshFunc (client * instance .IBMPINetworkClient , id , networkname , instanceid string ) retry .StateRefreshFunc {
248258
249259 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 )
250260 return func () (interface {}, string , error ) {
@@ -253,11 +263,11 @@ func isIBMPINetworkPortAttachRefreshFunc(client *st.IBMPINetworkClient, id, netw
253263 return nil , "" , err
254264 }
255265
256- if * network .Status == "ACTIVE" && network .PvmInstance .PvmInstanceID == instanceid {
266+ if strings . ToLower ( * network .Status ) == State_Active && network .PvmInstance .PvmInstanceID == instanceid {
257267 log .Printf (" The port has been created with the following ip address and attached to an instance " )
258- return network , "ACTIVE" , nil
268+ return network , State_Active , nil
259269 }
260270
261- return network , helpers . PINetworkProvisioning , nil
271+ return network , State_Build , nil
262272 }
263273}
0 commit comments