-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathresource_firewall_exclusion.go
154 lines (124 loc) · 3.86 KB
/
resource_firewall_exclusion.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
package main
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/sky-uk/gonsx"
"github.com/sky-uk/gonsx/api/firewallexclusion"
"log"
)
func getMember(moid string, nsxclient *gonsx.NSXClient) (*firewallexclusion.Member, error) {
getAllAPI := firewallexclusion.NewGetAll()
err := nsxclient.Do(getAllAPI)
if err != nil {
return nil, err
}
if getAllAPI.StatusCode() != 200 {
return nil, fmt.Errorf("Status code: %d, Response: %s", getAllAPI.StatusCode(), getAllAPI.ResponseObject())
}
member := getAllAPI.GetResponse().FilterByMOID(moid)
if member.MOID == "" {
return nil, nil
}
return member, nil
}
func resourceFirewallExclusion() *schema.Resource {
return &schema.Resource{
Create: resourceFirewallExclusionCreate,
Read: resourceFirewallExclusionRead,
Delete: resourceFirewallExclusionDelete,
Schema: map[string]*schema.Schema{
"moid": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceFirewallExclusionCreate(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var moid string
// Gather the attributes for the resource.
if v, ok := d.GetOk("moid"); ok {
moid = v.(string)
} else {
return fmt.Errorf("moid argument is required")
}
// Create the API, use it and check for errors.
log.Printf(fmt.Sprintf("[DEBUG] member.NewCreate(%s)", moid))
createAPI := firewallexclusion.NewCreate(moid)
err := nsxclient.Do(createAPI)
if err != nil {
return fmt.Errorf("Error: %v", err)
}
if createAPI.StatusCode() != 200 {
return fmt.Errorf("%s", createAPI.ResponseObject())
}
// If we get here, everything is OK. Set the ID for the Terraform state
// and return the response from the READ method.
d.SetId(moid)
return resourceFirewallExclusionRead(d, meta)
}
func resourceFirewallExclusionRead(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var moid string
// Gather the attributes for the resource.
if v, ok := d.GetOk("moid"); ok {
moid = v.(string)
} else {
return fmt.Errorf("moid argument is required")
}
// See if we can find our specifically named resource within the list of
// of firewall exclusions
log.Printf(fmt.Sprintf("[DEBUG] api.GetResponse().FilterByMOID(\"%s\")", moid))
memberObject, err := getMember(moid, nsxclient)
if err != nil {
return err
}
// If the resource has been removed manually, notify Terraform of this fact.
if memberObject == nil {
d.SetId("")
}
return nil
}
func resourceFirewallExclusionDelete(d *schema.ResourceData, meta interface{}) error {
nsxclient := meta.(*gonsx.NSXClient)
var moid string
// Gather the attributes for the resource.
if v, ok := d.GetOk("moid"); ok {
moid = v.(string)
} else {
return fmt.Errorf("moid argument is required")
}
// Gather all the resources that are associated with the specified
// moid.
log.Printf(fmt.Sprintf("[DEBUG] member.NewGetAll(%s)", moid))
api := firewallexclusion.NewGetAll()
err := nsxclient.Do(api)
if err != nil {
return err
}
// See if we can find our specifically named resource within the list of
// resources associated with the moid.
log.Printf(fmt.Sprintf("[DEBUG] api.GetResponse().FilterByMOID(\"%s\").MOID", moid))
memberObject, err := getMember(moid, nsxclient)
id := memberObject.MOID
log.Printf(fmt.Sprintf("[DEBUG] id := %s", id))
// If the resource has been removed manually, notify Terraform of this fact.
if id == "" {
d.SetId("")
return nil
}
// If we got here, the resource exists, so we attempt to delete it.
deleteAPI := firewallexclusion.NewDelete(id)
err = nsxclient.Do(deleteAPI)
if err != nil {
return err
}
// If we got here, the resource had existed, we deleted it and there was
// no error. Notify Terraform of this fact and return successful
// completion.
d.SetId("")
log.Printf(fmt.Sprintf("[DEBUG] id %s deleted.", id))
return nil
}