forked from hashicorp/terraform
/
resource_vnic_set.go
169 lines (139 loc) · 3.62 KB
/
resource_vnic_set.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
package opc
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceOPCVNICSet() *schema.Resource {
return &schema.Resource{
Create: resourceOPCVNICSetCreate,
Read: resourceOPCVNICSetRead,
Update: resourceOPCVNICSetUpdate,
Delete: resourceOPCVNICSetDelete,
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,
},
"applied_acls": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"virtual_nics": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"tags": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
}
}
func resourceOPCVNICSetCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).VirtNICSets()
name := d.Get("name").(string)
desc, descOk := d.GetOk("description")
input := &compute.CreateVirtualNICSetInput{
Name: name,
}
if descOk {
input.Description = desc.(string)
}
acls := getStringList(d, "applied_acls")
if len(acls) != 0 {
input.AppliedACLs = acls
}
vnics := getStringList(d, "virtual_nics")
if len(vnics) != 0 {
input.VirtualNICs = vnics
}
tags := getStringList(d, "tags")
if len(tags) != 0 {
input.Tags = tags
}
vnicSet, err := client.CreateVirtualNICSet(input)
if err != nil {
return fmt.Errorf("Error creating Virtual NIC Set: %s", err)
}
d.SetId(vnicSet.Name)
return resourceOPCVNICSetRead(d, meta)
}
func resourceOPCVNICSetRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).VirtNICSets()
name := d.Id()
input := &compute.GetVirtualNICSetInput{
Name: name,
}
res, err := client.GetVirtualNICSet(input)
if err != nil {
if compute.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading Virtual NIC Set '%s': %s", name, err)
}
d.Set("name", res.Name)
d.Set("description", res.Description)
if err := setStringList(d, "applied_acls", res.AppliedACLs); err != nil {
return err
}
if err := setStringList(d, "virtual_nics", res.VirtualNICs); err != nil {
return err
}
if err := setStringList(d, "tags", res.Tags); err != nil {
return err
}
return nil
}
func resourceOPCVNICSetUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).VirtNICSets()
name := d.Id()
desc, descOk := d.GetOk("description")
input := &compute.UpdateVirtualNICSetInput{
Name: name,
}
if descOk {
input.Description = desc.(string)
}
acls := getStringList(d, "applied_acls")
if len(acls) != 0 {
input.AppliedACLs = acls
}
vnics := getStringList(d, "virtual_nics")
if len(vnics) != 0 {
input.VirtualNICs = vnics
}
tags := getStringList(d, "tags")
if len(tags) != 0 {
input.Tags = tags
}
info, err := client.UpdateVirtualNICSet(input)
if err != nil {
return fmt.Errorf("Error updating Virtual NIC Set: %s", err)
}
d.SetId(info.Name)
return resourceOPCVNICSetRead(d, meta)
}
func resourceOPCVNICSetDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*compute.Client).VirtNICSets()
name := d.Id()
input := &compute.DeleteVirtualNICSetInput{
Name: name,
}
if err := client.DeleteVirtualNICSet(input); err != nil {
return fmt.Errorf("Error deleting Virtual NIC Set '%s': %s", name, err)
}
return nil
}