forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_acl.go
151 lines (127 loc) · 3.38 KB
/
resource_acl.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
package opc
import (
"fmt"
"log"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCACL() *schema.Resource {
return &schema.Resource{
Create: resourceOPCACLCreate,
Read: resourceOPCACLRead,
Update: resourceOPCACLUpdate,
Delete: resourceOPCACLDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"tags": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
ForceNew: true,
},
"uri": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceOPCACLCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
log.Print("[DEBUG] Creating acl")
client := meta.(*compute.Client).ACLs()
input := compute.CreateACLInput{
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
}
tags := getStringList(d, "tags")
if len(tags) != 0 {
input.Tags = tags
}
if description, ok := d.GetOk("description"); ok {
input.Description = description.(string)
}
info, err := client.CreateACL(&input)
if err != nil {
return fmt.Errorf("Error creating ACL: %s", err)
}
d.SetId(info.Name)
return resourceOPCACLRead(d, meta)
}
func resourceOPCACLRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
client := meta.(*compute.Client).ACLs()
log.Printf("[DEBUG] Reading state of ip reservation %s", d.Id())
getInput := compute.GetACLInput{
Name: d.Id(),
}
result, err := client.GetACL(&getInput)
if err != nil {
// ACL does not exist
if compute.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading acl %s: %s", d.Id(), err)
}
log.Printf("[DEBUG] Read state of acl %s: %#v", d.Id(), result)
d.Set("name", result.Name)
d.Set("enabled", result.Enabled)
d.Set("description", result.Description)
d.Set("uri", result.URI)
if err := setStringList(d, "tags", result.Tags); err != nil {
return err
}
return nil
}
func resourceOPCACLUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
log.Print("[DEBUG] Updating acl")
client := meta.(*compute.Client).ACLs()
input := compute.UpdateACLInput{
Name: d.Get("name").(string),
Enabled: d.Get("enabled").(bool),
}
tags := getStringList(d, "tags")
if len(tags) != 0 {
input.Tags = tags
}
if description, ok := d.GetOk("description"); ok {
input.Description = description.(string)
}
info, err := client.UpdateACL(&input)
if err != nil {
return fmt.Errorf("Error updating ACL: %s", err)
}
d.SetId(info.Name)
return resourceOPCACLRead(d, meta)
}
func resourceOPCACLDelete(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Resource state: %#v", d.State())
client := meta.(*compute.Client).ACLs()
name := d.Id()
log.Printf("[DEBUG] Deleting ACL: %v", name)
input := compute.DeleteACLInput{
Name: name,
}
if err := client.DeleteACL(&input); err != nil {
return fmt.Errorf("Error deleting ACL")
}
return nil
}