-
Notifications
You must be signed in to change notification settings - Fork 646
/
resource_ibm_pi_network_port.go
210 lines (172 loc) · 6.12 KB
/
resource_ibm_pi_network_port.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package power
import (
"context"
"fmt"
"log"
"time"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
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/power/models"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/conns"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
)
func ResourceIBMPINetworkPort() *schema.Resource {
return &schema.Resource{
CreateContext: resourceIBMPINetworkPortCreate,
ReadContext: resourceIBMPINetworkPortRead,
UpdateContext: resourceIBMPINetworkPortUpdate,
DeleteContext: resourceIBMPINetworkPortDelete,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(60 * time.Minute),
Delete: schema.DefaultTimeout(60 * time.Minute),
},
Schema: map[string]*schema.Schema{
helpers.PINetworkName: {
Type: schema.TypeString,
Required: true,
},
helpers.PICloudInstanceId: {
Type: schema.TypeString,
Required: true,
},
helpers.PINetworkPortDescription: {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
helpers.PINetworkPortIPAddress: {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
//Computed Attributes
"macaddress": {
Type: schema.TypeString,
Computed: true,
},
"portid": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceIBMPINetworkPortCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := d.Get(helpers.PICloudInstanceId).(string)
networkname := d.Get(helpers.PINetworkName).(string)
description := d.Get(helpers.PINetworkPortDescription).(string)
ipaddress := d.Get(helpers.PINetworkPortIPAddress).(string)
nwportBody := &models.NetworkPortCreate{Description: description}
if ipaddress != "" {
log.Printf("IP address provided. ")
nwportBody.IPAddress = ipaddress
}
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
d.SetId(fmt.Sprintf("%s/%s/%s", cloudInstanceID, networkname, IBMPINetworkPortID))
_, err = isWaitForIBMPINetworkPortAvailable(ctx, client, IBMPINetworkPortID, networkname, d.Timeout(schema.TimeoutCreate))
if err != nil {
return diag.FromErr(err)
}
return resourceIBMPINetworkPortRead(ctx, d, meta)
}
func resourceIBMPINetworkPortRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
sess, err := meta.(conns.ClientSession).IBMPISession()
if err != nil {
return diag.FromErr(err)
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := parts[0]
networkname := parts[1]
portID := parts[2]
networkC := st.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("macaddress", networkdata.MacAddress)
d.Set("status", networkdata.Status)
d.Set("portid", networkdata.PortID)
d.Set("public_ip", networkdata.ExternalIP)
return nil
}
func resourceIBMPINetworkPortUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
return nil
}
func resourceIBMPINetworkPortDelete(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 {
return diag.FromErr(err)
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return diag.FromErr(err)
}
cloudInstanceID := parts[0]
networkname := parts[1]
portID := parts[2]
client := st.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)
if err != nil {
return diag.FromErr(err)
}
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 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.PortID != nil {
//if network.State == "available" {
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
}
}