-
Notifications
You must be signed in to change notification settings - Fork 657
/
resource_ibm_is_virtual_endpoint_gateway_ip.go
221 lines (208 loc) · 6.7 KB
/
resource_ibm_is_virtual_endpoint_gateway_ip.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
211
212
213
214
215
216
217
218
219
220
221
// Copyright IBM Corp. 2017, 2021 All Rights Reserved.
// Licensed under the Mozilla Public License v2.0
package vpc
import (
"fmt"
"log"
"time"
"github.com/IBM-Cloud/terraform-provider-ibm/ibm/flex"
"github.com/IBM/vpc-go-sdk/vpcv1"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
const (
isVirtualEndpointGatewayID = "gateway"
isVirtualEndpointGatewayIPID = "reserved_ip"
isVirtualEndpointGatewayIPName = "name"
isVirtualEndpointGatewayIPAddress = "address"
isVirtualEndpointGatewayIPResourceType = "resource_type"
isVirtualEndpointGatewayIPAutoDelete = "auto_delete"
isVirtualEndpointGatewayIPCreatedAt = "created_at"
isVirtualEndpointGatewayIPTarget = "target"
isVirtualEndpointGatewayIPTargetID = "id"
isVirtualEndpointGatewayIPTargetName = "name"
isVirtualEndpointGatewayIPTargetResourceType = "resource_type"
)
func ResourceIBMISEndpointGatewayIP() *schema.Resource {
return &schema.Resource{
Create: resourceIBMisVirtualEndpointGatewayIPCreate,
Read: resourceIBMisVirtualEndpointGatewayIPRead,
Delete: resourceIBMisVirtualEndpointGatewayIPDelete,
Exists: resourceIBMisVirtualEndpointGatewayIPExists,
Importer: &schema.ResourceImporter{},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
},
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Endpoint gateway ID",
},
isVirtualEndpointGatewayIPID: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Endpoint gateway IP id",
},
isVirtualEndpointGatewayIPName: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP name",
},
isVirtualEndpointGatewayIPResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP resource type",
},
isVirtualEndpointGatewayIPCreatedAt: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP created date and time",
},
isVirtualEndpointGatewayIPAutoDelete: {
Type: schema.TypeBool,
Computed: true,
Description: "Endpoint gateway IP auto delete",
},
isVirtualEndpointGatewayIPAddress: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway IP address",
},
isVirtualEndpointGatewayIPTarget: {
Type: schema.TypeList,
Computed: true,
Description: "Endpoint gateway detail",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
isVirtualEndpointGatewayIPTargetID: {
Type: schema.TypeString,
Computed: true,
Description: "The IPs target id",
},
isVirtualEndpointGatewayIPTargetName: {
Type: schema.TypeString,
Computed: true,
Description: "The IPs target name",
},
isVirtualEndpointGatewayIPTargetResourceType: {
Type: schema.TypeString,
Computed: true,
Description: "Endpoint gateway resource type",
},
},
},
},
},
}
}
func resourceIBMisVirtualEndpointGatewayIPCreate(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
gatewayID := d.Get(isVirtualEndpointGatewayID).(string)
ipID := d.Get(isVirtualEndpointGatewayIPID).(string)
opt := sess.NewAddEndpointGatewayIPOptions(gatewayID, ipID)
_, response, err := sess.AddEndpointGatewayIP(opt)
if err != nil {
log.Printf("Add Endpoint Gateway failed: %v", response)
return err
}
d.SetId(fmt.Sprintf("%s/%s", gatewayID, ipID))
return resourceIBMisVirtualEndpointGatewayIPRead(d, meta)
}
func resourceIBMisVirtualEndpointGatewayIPRead(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
gatewayID := parts[0]
ipID := parts[1]
opt := sess.NewGetEndpointGatewayIPOptions(gatewayID, ipID)
result, response, err := sess.GetEndpointGatewayIP(opt)
if err != nil {
log.Printf("Get Endpoint Gateway IP failed: %v", response)
return err
}
d.Set(isVirtualEndpointGatewayIPID, result.ID)
d.Set(isVirtualEndpointGatewayIPName, result.Name)
d.Set(isVirtualEndpointGatewayIPAddress, result.Address)
d.Set(isVirtualEndpointGatewayIPCreatedAt, (result.CreatedAt).String())
d.Set(isVirtualEndpointGatewayIPResourceType, result.ResourceType)
d.Set(isVirtualEndpointGatewayIPAutoDelete, result.AutoDelete)
d.Set(isVirtualEndpointGatewayIPTarget,
flattenEndpointGatewayIPTarget(result.Target.(*vpcv1.ReservedIPTarget)))
return nil
}
func resourceIBMisVirtualEndpointGatewayIPDelete(d *schema.ResourceData, meta interface{}) error {
sess, err := vpcClient(meta)
if err != nil {
return err
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return err
}
gatewayID := parts[0]
ipID := parts[1]
opt := sess.NewRemoveEndpointGatewayIPOptions(gatewayID, ipID)
response, err := sess.RemoveEndpointGatewayIP(opt)
if err != nil && response.StatusCode != 404 {
log.Printf("Remove Endpoint Gateway IP failed: %v", response)
return err
}
d.SetId("")
return nil
}
func resourceIBMisVirtualEndpointGatewayIPExists(d *schema.ResourceData, meta interface{}) (bool, error) {
sess, err := vpcClient(meta)
if err != nil {
return false, err
}
parts, err := flex.IdParts(d.Id())
if err != nil {
return false, err
}
if len(parts) != 2 {
return false, fmt.Errorf("[ERROR] Incorrect ID %s: ID should be a combination of gatewayID/ipID", d.Id())
}
gatewayID := parts[0]
ipID := parts[1]
opt := sess.NewGetEndpointGatewayIPOptions(gatewayID, ipID)
_, response, err := sess.GetEndpointGatewayIP(opt)
if err != nil {
if response != nil && response.StatusCode == 404 {
log.Printf("Endpoint Gateway IP does not exist.")
return false, nil
}
log.Printf("Error : %s", response)
return false, err
}
return true, nil
}
func flattenEndpointGatewayIPTarget(target *vpcv1.ReservedIPTarget) interface{} {
targetSlice := []interface{}{}
targetOutput := map[string]string{}
if target == nil {
return targetOutput
}
if target.ID != nil {
targetOutput[isVirtualEndpointGatewayIPTargetID] = *target.ID
}
if target.Name != nil {
targetOutput[isVirtualEndpointGatewayIPTargetName] = *target.Name
}
if target.ResourceType != nil {
targetOutput[isVirtualEndpointGatewayIPTargetResourceType] = *target.ResourceType
}
targetSlice = append(targetSlice, targetOutput)
return targetSlice
}