forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_security_ip_list.go
135 lines (116 loc) · 3.75 KB
/
resource_security_ip_list.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
package opc
import (
"fmt"
"log"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCSecurityIPList() *schema.Resource {
return &schema.Resource{
Create: resourceOPCSecurityIPListCreate,
Read: resourceOPCSecurityIPListRead,
Update: resourceOPCSecurityIPListUpdate,
Delete: resourceOPCSecurityIPListDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"ip_entries": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func resourceOPCSecurityIPListCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
client := meta.(*compute.Client).SecurityIPLists()
ipEntries := d.Get("ip_entries").([]interface{})
ipEntryStrings := []string{}
for _, entry := range ipEntries {
ipEntryStrings = append(ipEntryStrings, entry.(string))
}
input := compute.CreateSecurityIPListInput{
Name: d.Get("name").(string),
SecIPEntries: ipEntryStrings,
}
if description, ok := d.GetOk("description"); ok {
input.Description = description.(string)
}
log.Printf("[DEBUG] Creating security IP list with %+v", input)
info, err := client.CreateSecurityIPList(&input)
if err != nil {
return fmt.Errorf("Error creating security IP list %s: %s", input.Name, err)
}
d.SetId(info.Name)
return resourceOPCSecurityIPListRead(d, meta)
}
func resourceOPCSecurityIPListRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
client := meta.(*compute.Client).SecurityIPLists()
name := d.Id()
log.Printf("[DEBUG] Reading state of security IP list %s", name)
input := compute.GetSecurityIPListInput{
Name: name,
}
result, err := client.GetSecurityIPList(&input)
if err != nil {
// Security IP List does not exist
if compute.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading security IP list %s: %s", name, err)
}
log.Printf("[DEBUG] Read state of security IP list %s: %#v", name, result)
d.Set("name", result.Name)
d.Set("ip_entries", result.SecIPEntries)
d.Set("description", result.Description)
return nil
}
func resourceOPCSecurityIPListUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
client := meta.(*compute.Client).SecurityIPLists()
ipEntries := d.Get("ip_entries").([]interface{})
ipEntryStrings := []string{}
for _, entry := range ipEntries {
ipEntryStrings = append(ipEntryStrings, entry.(string))
}
input := compute.UpdateSecurityIPListInput{
Name: d.Get("name").(string),
SecIPEntries: ipEntryStrings,
}
if description, ok := d.GetOk("description"); ok {
input.Description = description.(string)
}
log.Printf("[DEBUG] Updating security IP list with %+v", input)
info, err := client.UpdateSecurityIPList(&input)
if err != nil {
return fmt.Errorf("Error updating security IP list %s: %s", input.Name, err)
}
d.SetId(info.Name)
return resourceOPCSecurityIPListRead(d, meta)
}
func resourceOPCSecurityIPListDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
client := meta.(*compute.Client).SecurityIPLists()
name := d.Id()
log.Printf("[DEBUG] Deleting security IP list %s", name)
input := compute.DeleteSecurityIPListInput{
Name: name,
}
if err := client.DeleteSecurityIPList(&input); err != nil {
return fmt.Errorf("Error deleting security IP list %s: %s", name, err)
}
return nil
}