forked from terraform-providers/terraform-provider-opc
/
resource_ip_network.go
186 lines (150 loc) · 4.3 KB
/
resource_ip_network.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
package opc
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/client"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCIPNetwork() *schema.Resource {
return &schema.Resource{
Create: resourceOPCIPNetworkCreate,
Read: resourceOPCIPNetworkRead,
Update: resourceOPCIPNetworkUpdate,
Delete: resourceOPCIPNetworkDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip_address_prefix": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateIPPrefixCIDR,
},
"ip_network_exchange": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"public_napt_enabled": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"uri": {
Type: schema.TypeString,
Computed: true,
},
"tags": tagsOptionalSchema(),
},
}
}
func resourceOPCIPNetworkCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*OPCClient).computeClient.IPNetworks()
// Get required attributes
name := d.Get("name").(string)
ipPrefix := d.Get("ip_address_prefix").(string)
// public_napt_enabled is not required, but bool type allows it to be unspecified
naptEnabled := d.Get("public_napt_enabled").(bool)
input := &compute.CreateIPNetworkInput{
Name: name,
IPAddressPrefix: ipPrefix,
PublicNaptEnabled: naptEnabled,
}
// Get Optional attributes
if desc, ok := d.GetOk("description"); ok && desc != nil {
input.Description = desc.(string)
}
if ipEx, ok := d.GetOk("ip_network_exchange"); ok && ipEx != nil {
input.IPNetworkExchange = ipEx.(string)
}
tags := getStringList(d, "tags")
if len(tags) != 0 {
input.Tags = tags
}
info, err := client.CreateIPNetwork(input)
if err != nil {
return fmt.Errorf("Error creating IP Network '%s': %v", name, err)
}
d.SetId(info.Name)
return resourceOPCIPNetworkRead(d, meta)
}
func resourceOPCIPNetworkRead(d *schema.ResourceData, meta interface{}) error {
computeClient := meta.(*OPCClient).computeClient.IPNetworks()
name := d.Id()
input := &compute.GetIPNetworkInput{
Name: name,
}
result, err := computeClient.GetIPNetwork(input)
if err != nil {
if client.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading IP Network '%s': %v", name, err)
}
if result == nil {
d.SetId("")
return nil
}
d.Set("name", result.Name)
d.Set("ip_address_prefix", result.IPAddressPrefix)
d.Set("ip_network_exchanged", result.IPNetworkExchange)
d.Set("description", result.Description)
d.Set("public_napt_enabled", result.PublicNaptEnabled)
d.Set("uri", result.Uri)
if err := setStringList(d, "tags", result.Tags); err != nil {
return err
}
return nil
}
func resourceOPCIPNetworkUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*OPCClient).computeClient.IPNetworks()
// Get required attributes
name := d.Get("name").(string)
ipPrefix := d.Get("ip_address_prefix").(string)
// public_napt_enabled is not required, but bool type allows it to be unspecified
naptEnabled := d.Get("public_napt_enabled").(bool)
input := &compute.UpdateIPNetworkInput{
Name: name,
IPAddressPrefix: ipPrefix,
PublicNaptEnabled: naptEnabled,
}
// Get Optional attributes
desc, descOk := d.GetOk("description")
if descOk {
input.Description = desc.(string)
}
ipEx, ipExOk := d.GetOk("ip_network_exchange")
if ipExOk {
input.IPNetworkExchange = ipEx.(string)
}
tags := getStringList(d, "tags")
if len(tags) != 0 {
input.Tags = tags
}
info, err := client.UpdateIPNetwork(input)
if err != nil {
return fmt.Errorf("Error updating IP Network '%s': %v", name, err)
}
d.SetId(info.Name)
return resourceOPCIPNetworkRead(d, meta)
}
func resourceOPCIPNetworkDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*OPCClient).computeClient.IPNetworks()
name := d.Id()
input := &compute.DeleteIPNetworkInput{
Name: name,
}
if err := client.DeleteIPNetwork(input); err != nil {
return fmt.Errorf("Error deleting IP Network '%s': %v", name, err)
}
return nil
}