This repository has been archived by the owner on Mar 25, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
resource_security_application.go
160 lines (138 loc) · 3.88 KB
/
resource_security_application.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
package opc
import (
"fmt"
"github.com/hashicorp/go-oracle-terraform/client"
"github.com/hashicorp/go-oracle-terraform/compute"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
)
func resourceOPCSecurityApplication() *schema.Resource {
return &schema.Resource{
Create: resourceOPCSecurityApplicationCreate,
Read: resourceOPCSecurityApplicationRead,
Delete: resourceOPCSecurityApplicationDelete,
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,
ForceNew: true,
},
"protocol": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateIPProtocol,
ForceNew: true,
},
"dport": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"icmptype": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.Echo),
string(compute.Reply),
string(compute.TTL),
string(compute.TraceRoute),
string(compute.Unreachable),
}, true),
ForceNew: true,
},
"icmpcode": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.StringInSlice([]string{
string(compute.Admin),
string(compute.Df),
string(compute.Host),
string(compute.Network),
string(compute.Port),
string(compute.Protocol),
}, true),
ForceNew: true,
},
},
}
}
func resourceOPCSecurityApplicationCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
protocol := d.Get("protocol").(string)
dport := d.Get("dport").(string)
icmptype := d.Get("icmptype").(string)
icmpcode := d.Get("icmpcode").(string)
description := d.Get("description").(string)
computeClient, err := meta.(*Client).getComputeClient()
if err != nil {
return err
}
resClient := computeClient.SecurityApplications()
input := compute.CreateSecurityApplicationInput{
Name: name,
Description: description,
Protocol: compute.SecurityApplicationProtocol(protocol),
DPort: dport,
ICMPCode: compute.SecurityApplicationICMPCode(icmpcode),
ICMPType: compute.SecurityApplicationICMPType(icmptype),
}
info, err := resClient.CreateSecurityApplication(&input)
if err != nil {
return fmt.Errorf("Error creating security application %s: %s", name, err)
}
d.SetId(info.Name)
return resourceOPCSecurityApplicationRead(d, meta)
}
func resourceOPCSecurityApplicationRead(d *schema.ResourceData, meta interface{}) error {
computeClient, err := meta.(*Client).getComputeClient()
if err != nil {
return err
}
resClient := computeClient.SecurityApplications()
name := d.Id()
input := compute.GetSecurityApplicationInput{
Name: name,
}
result, err := resClient.GetSecurityApplication(&input)
if err != nil {
if client.WasNotFoundError(err) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading security application %s: %s", name, err)
}
if result == nil {
d.SetId("")
return nil
}
d.Set("name", result.Name)
d.Set("protocol", result.Protocol)
d.Set("dport", result.DPort)
d.Set("icmptype", result.ICMPType)
d.Set("icmpcode", result.ICMPCode)
d.Set("description", result.Description)
return nil
}
func resourceOPCSecurityApplicationDelete(d *schema.ResourceData, meta interface{}) error {
computeClient, err := meta.(*Client).getComputeClient()
if err != nil {
return err
}
resClient := computeClient.SecurityApplications()
name := d.Id()
input := compute.DeleteSecurityApplicationInput{
Name: name,
}
if err := resClient.DeleteSecurityApplication(&input); err != nil {
return fmt.Errorf("Error deleting security application '%s': %s", name, err)
}
return nil
}