2014-12-10 22:20:52 +01:00
|
|
|
package cloudstack
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-12-03 11:10:42 +01:00
|
|
|
"sync"
|
|
|
|
"time"
|
2014-12-10 22:20:52 +01:00
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
"github.com/hashicorp/go-multierror"
|
2014-12-10 22:20:52 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
|
|
|
"github.com/xanzy/go-cloudstack/cloudstack"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceCloudStackFirewall() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceCloudStackFirewallCreate,
|
|
|
|
Read: resourceCloudStackFirewallRead,
|
|
|
|
Update: resourceCloudStackFirewallUpdate,
|
|
|
|
Delete: resourceCloudStackFirewallDelete,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"ipaddress": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
2015-01-15 10:04:09 +01:00
|
|
|
"managed": &schema.Schema{
|
|
|
|
Type: schema.TypeBool,
|
|
|
|
Optional: true,
|
|
|
|
Default: false,
|
|
|
|
},
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
"rule": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
2015-01-16 13:26:43 +01:00
|
|
|
Optional: true,
|
2014-12-10 22:20:52 +01:00
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
2015-12-03 11:10:42 +01:00
|
|
|
"cidr_list": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
|
|
|
Set: schema.HashString,
|
|
|
|
},
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
"source_cidr": &schema.Schema{
|
2015-12-03 11:10:42 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Deprecated: "Please use the `cidr_list` field instead",
|
2014-12-10 22:20:52 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"icmp_type": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"icmp_code": &schema.Schema{
|
|
|
|
Type: schema.TypeInt,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"ports": &schema.Schema{
|
|
|
|
Type: schema.TypeSet,
|
|
|
|
Optional: true,
|
|
|
|
Elem: &schema.Schema{Type: schema.TypeString},
|
2015-12-03 11:10:42 +01:00
|
|
|
Set: schema.HashString,
|
2014-12-10 22:20:52 +01:00
|
|
|
},
|
|
|
|
|
|
|
|
"uuids": &schema.Schema{
|
|
|
|
Type: schema.TypeMap,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackFirewallCreate(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2015-01-16 13:26:43 +01:00
|
|
|
// Make sure all required parameters are there
|
|
|
|
if err := verifyFirewallParams(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// Retrieve the ipaddress ID
|
|
|
|
ipaddressid, e := retrieveID(cs, "ipaddress", d.Get("ipaddress").(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
if e != nil {
|
|
|
|
return e.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need to set this upfront in order to be able to save a partial state
|
2015-01-15 10:04:09 +01:00
|
|
|
d.SetId(ipaddressid)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Create all rules that are configured
|
2015-12-03 11:10:42 +01:00
|
|
|
if nrs := d.Get("rule").(*schema.Set); nrs.Len() > 0 {
|
2014-12-10 22:20:52 +01:00
|
|
|
// Create an empty schema.Set to hold all rules
|
2015-12-03 11:10:42 +01:00
|
|
|
rules := resourceCloudStackFirewall().Schema["rule"].ZeroValue().(*schema.Set)
|
|
|
|
|
|
|
|
err := createFirewallRules(d, meta, rules, nrs)
|
|
|
|
|
|
|
|
// We need to update this first to preserve the correct state
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
2015-12-03 11:10:42 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return resourceCloudStackFirewallRead(d, meta)
|
|
|
|
}
|
|
|
|
func createFirewallRules(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{},
|
|
|
|
rules *schema.Set,
|
|
|
|
nrs *schema.Set) error {
|
|
|
|
var errs *multierror.Error
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(nrs.Len())
|
|
|
|
|
|
|
|
sem := make(chan struct{}, 10)
|
|
|
|
for _, rule := range nrs.List() {
|
|
|
|
// Put in a tiny sleep here to avoid DoS'ing the API
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
go func(rule map[string]interface{}) {
|
|
|
|
defer wg.Done()
|
|
|
|
sem <- struct{}{}
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Create a single rule
|
2015-12-03 11:10:42 +01:00
|
|
|
err := createFirewallRule(d, meta, rule)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
// If we have at least one UUID, we need to save the rule
|
|
|
|
if len(rule["uuids"].(map[string]interface{})) > 0 {
|
|
|
|
rules.Add(rule)
|
|
|
|
}
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2015-12-03 11:10:42 +01:00
|
|
|
errs = multierror.Append(errs, err)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
2015-12-03 11:10:42 +01:00
|
|
|
|
|
|
|
<-sem
|
|
|
|
}(rule.(map[string]interface{}))
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return errs.ErrorOrNil()
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
func createFirewallRule(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{},
|
|
|
|
rule map[string]interface{}) error {
|
2014-12-10 22:20:52 +01:00
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
uuids := rule["uuids"].(map[string]interface{})
|
|
|
|
|
2015-01-15 10:04:09 +01:00
|
|
|
// Make sure all required rule parameters are there
|
|
|
|
if err := verifyFirewallRuleParams(d, rule); err != nil {
|
2014-12-10 22:20:52 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parameter struct
|
2015-01-15 10:04:09 +01:00
|
|
|
p := cs.Firewall.NewCreateFirewallRuleParams(d.Id(), rule["protocol"].(string))
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Set the CIDR list
|
2015-12-03 11:10:42 +01:00
|
|
|
p.SetCidrlist(retrieveCidrList(rule))
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// If the protocol is ICMP set the needed ICMP parameters
|
|
|
|
if rule["protocol"].(string) == "icmp" {
|
|
|
|
p.SetIcmptype(rule["icmp_type"].(int))
|
|
|
|
p.SetIcmpcode(rule["icmp_code"].(int))
|
|
|
|
|
|
|
|
r, err := cs.Firewall.CreateFirewallRule(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2015-12-03 11:10:42 +01:00
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
uuids["icmp"] = r.Id
|
|
|
|
rule["uuids"] = uuids
|
|
|
|
}
|
|
|
|
|
|
|
|
// If protocol is not ICMP, loop through all ports
|
|
|
|
if rule["protocol"].(string) != "icmp" {
|
|
|
|
if ps := rule["ports"].(*schema.Set); ps.Len() > 0 {
|
|
|
|
|
|
|
|
// Create an empty schema.Set to hold all processed ports
|
2015-12-03 11:10:42 +01:00
|
|
|
ports := &schema.Set{F: schema.HashString}
|
|
|
|
|
|
|
|
// Define a regexp for parsing the port
|
|
|
|
re := regexp.MustCompile(`^(\d+)(?:-(\d+))?$`)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
for _, port := range ps.List() {
|
2015-12-03 11:10:42 +01:00
|
|
|
if _, ok := uuids[port.(string)]; ok {
|
|
|
|
ports.Add(port)
|
|
|
|
rule["ports"] = ports
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
m := re.FindStringSubmatch(port.(string))
|
|
|
|
|
|
|
|
startPort, err := strconv.Atoi(m[1])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
endPort := startPort
|
|
|
|
if m[2] != "" {
|
|
|
|
endPort, err = strconv.Atoi(m[2])
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
p.SetStartport(startPort)
|
|
|
|
p.SetEndport(endPort)
|
|
|
|
|
|
|
|
r, err := cs.Firewall.CreateFirewallRule(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ports.Add(port)
|
|
|
|
rule["ports"] = ports
|
|
|
|
|
|
|
|
uuids[port.(string)] = r.Id
|
|
|
|
rule["uuids"] = uuids
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackFirewallRead(d *schema.ResourceData, meta interface{}) error {
|
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
|
2015-11-24 18:01:12 +01:00
|
|
|
// Get all the rules from the running environment
|
|
|
|
p := cs.Firewall.NewListFirewallRulesParams()
|
|
|
|
p.SetIpaddressid(d.Id())
|
|
|
|
p.SetListall(true)
|
|
|
|
|
|
|
|
l, err := cs.Firewall.ListFirewallRules(p)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make a map of all the rules so we can easily find a rule
|
|
|
|
ruleMap := make(map[string]*cloudstack.FirewallRule, l.Count)
|
|
|
|
for _, r := range l.FirewallRules {
|
|
|
|
ruleMap[r.Id] = r
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Create an empty schema.Set to hold all rules
|
2015-12-03 11:10:42 +01:00
|
|
|
rules := resourceCloudStackFirewall().Schema["rule"].ZeroValue().(*schema.Set)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Read all rules that are configured
|
|
|
|
if rs := d.Get("rule").(*schema.Set); rs.Len() > 0 {
|
|
|
|
for _, rule := range rs.List() {
|
|
|
|
rule := rule.(map[string]interface{})
|
|
|
|
uuids := rule["uuids"].(map[string]interface{})
|
|
|
|
|
|
|
|
if rule["protocol"].(string) == "icmp" {
|
|
|
|
id, ok := uuids["icmp"]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the rule
|
2015-11-24 18:01:12 +01:00
|
|
|
r, ok := ruleMap[id.(string)]
|
|
|
|
if !ok {
|
|
|
|
delete(uuids, "icmp")
|
|
|
|
continue
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-11-24 18:01:12 +01:00
|
|
|
// Delete the known rule so only unknown rules remain in the ruleMap
|
|
|
|
delete(ruleMap, id.(string))
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Update the values
|
|
|
|
rule["protocol"] = r.Protocol
|
|
|
|
rule["icmp_type"] = r.Icmptype
|
|
|
|
rule["icmp_code"] = r.Icmpcode
|
2015-12-03 11:10:42 +01:00
|
|
|
setCidrList(rule, r.Cidrlist)
|
2014-12-10 22:20:52 +01:00
|
|
|
rules.Add(rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If protocol is not ICMP, loop through all ports
|
|
|
|
if rule["protocol"].(string) != "icmp" {
|
|
|
|
if ps := rule["ports"].(*schema.Set); ps.Len() > 0 {
|
|
|
|
|
|
|
|
// Create an empty schema.Set to hold all ports
|
2015-12-03 11:10:42 +01:00
|
|
|
ports := &schema.Set{F: schema.HashString}
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// Loop through all ports and retrieve their info
|
|
|
|
for _, port := range ps.List() {
|
|
|
|
id, ok := uuids[port.(string)]
|
|
|
|
if !ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the rule
|
2015-11-24 18:01:12 +01:00
|
|
|
r, ok := ruleMap[id.(string)]
|
|
|
|
if !ok {
|
|
|
|
delete(uuids, port.(string))
|
|
|
|
continue
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-11-24 18:01:12 +01:00
|
|
|
// Delete the known rule so only unknown rules remain in the ruleMap
|
|
|
|
delete(ruleMap, id.(string))
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Update the values
|
|
|
|
rule["protocol"] = r.Protocol
|
2015-12-03 11:10:42 +01:00
|
|
|
setCidrList(rule, r.Cidrlist)
|
2014-12-10 22:20:52 +01:00
|
|
|
ports.Add(port)
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there is at least one port found, add this rule to the rules set
|
|
|
|
if ports.Len() > 0 {
|
|
|
|
rule["ports"] = ports
|
|
|
|
rules.Add(rule)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:04:09 +01:00
|
|
|
// If this is a managed firewall, add all unknown rules into a single dummy rule
|
2015-01-16 13:26:43 +01:00
|
|
|
managed := d.Get("managed").(bool)
|
2015-11-24 18:01:12 +01:00
|
|
|
if managed && len(ruleMap) > 0 {
|
|
|
|
for uuid := range ruleMap {
|
2015-12-03 11:10:42 +01:00
|
|
|
// We need to create and add a dummy value to a schema.Set as the
|
|
|
|
// cidr_list is a required field and thus needs a value
|
|
|
|
cidrs := &schema.Set{F: schema.HashString}
|
|
|
|
cidrs.Add(uuid)
|
|
|
|
|
2015-12-01 00:36:33 +01:00
|
|
|
// Make a dummy rule to hold the unknown UUID
|
|
|
|
rule := map[string]interface{}{
|
2015-12-03 11:10:42 +01:00
|
|
|
"cidr_list": cidrs,
|
|
|
|
"protocol": uuid,
|
|
|
|
"uuids": map[string]interface{}{uuid: uuid},
|
2015-12-01 00:36:33 +01:00
|
|
|
}
|
2015-01-15 10:04:09 +01:00
|
|
|
|
2015-12-01 00:36:33 +01:00
|
|
|
// Add the dummy rule to the rules set
|
|
|
|
rules.Add(rule)
|
2015-01-15 10:04:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
if rules.Len() > 0 {
|
|
|
|
d.Set("rule", rules)
|
2015-01-16 13:26:43 +01:00
|
|
|
} else if !managed {
|
2014-12-10 22:20:52 +01:00
|
|
|
d.SetId("")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackFirewallUpdate(d *schema.ResourceData, meta interface{}) error {
|
2015-01-16 13:26:43 +01:00
|
|
|
// Make sure all required parameters are there
|
|
|
|
if err := verifyFirewallParams(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Check if the rule set as a whole has changed
|
|
|
|
if d.HasChange("rule") {
|
|
|
|
o, n := d.GetChange("rule")
|
|
|
|
ors := o.(*schema.Set).Difference(n.(*schema.Set))
|
|
|
|
nrs := n.(*schema.Set).Difference(o.(*schema.Set))
|
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
// We need to start with a rule set containing all the rules we
|
|
|
|
// already have and want to keep. Any rules that are not deleted
|
|
|
|
// correctly and any newly created rules, will be added to this
|
|
|
|
// set to make sure we end up in a consistent state
|
|
|
|
rules := o.(*schema.Set).Intersection(n.(*schema.Set))
|
|
|
|
|
|
|
|
// First loop through all the old rules and delete them
|
|
|
|
if ors.Len() > 0 {
|
|
|
|
err := deleteFirewallRules(d, meta, rules, ors)
|
|
|
|
|
|
|
|
// We need to update this first to preserve the correct state
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
// Then loop through all the new rules and create them
|
|
|
|
if nrs.Len() > 0 {
|
|
|
|
err := createFirewallRules(d, meta, rules, nrs)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
// We need to update this first to preserve the correct state
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resourceCloudStackFirewallRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceCloudStackFirewallDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-12-03 11:10:42 +01:00
|
|
|
// Create an empty rule set to hold all rules that where
|
|
|
|
// not deleted correctly
|
|
|
|
rules := resourceCloudStackFirewall().Schema["rule"].ZeroValue().(*schema.Set)
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Delete all rules
|
2015-12-03 11:10:42 +01:00
|
|
|
if ors := d.Get("rule").(*schema.Set); ors.Len() > 0 {
|
|
|
|
err := deleteFirewallRules(d, meta, rules, ors)
|
|
|
|
|
|
|
|
// We need to update this first to preserve the correct state
|
|
|
|
d.Set("rule", rules)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func deleteFirewallRules(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{},
|
|
|
|
rules *schema.Set,
|
|
|
|
ors *schema.Set) error {
|
|
|
|
var errs *multierror.Error
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(ors.Len())
|
|
|
|
|
|
|
|
sem := make(chan struct{}, 10)
|
|
|
|
for _, rule := range ors.List() {
|
|
|
|
// Put a sleep here to avoid DoS'ing the API
|
|
|
|
time.Sleep(500 * time.Millisecond)
|
|
|
|
|
|
|
|
go func(rule map[string]interface{}) {
|
|
|
|
defer wg.Done()
|
|
|
|
sem <- struct{}{}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Delete a single rule
|
2015-12-03 11:10:42 +01:00
|
|
|
err := deleteFirewallRule(d, meta, rule)
|
2014-12-10 22:20:52 +01:00
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
// If we have at least one UUID, we need to save the rule
|
|
|
|
if len(rule["uuids"].(map[string]interface{})) > 0 {
|
|
|
|
rules.Add(rule)
|
|
|
|
}
|
2014-12-10 22:20:52 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2015-12-03 11:10:42 +01:00
|
|
|
errs = multierror.Append(errs, err)
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
2015-12-03 11:10:42 +01:00
|
|
|
|
|
|
|
<-sem
|
|
|
|
}(rule.(map[string]interface{}))
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
return errs.ErrorOrNil()
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 11:10:42 +01:00
|
|
|
func deleteFirewallRule(
|
|
|
|
d *schema.ResourceData,
|
|
|
|
meta interface{},
|
|
|
|
rule map[string]interface{}) error {
|
2014-12-10 22:20:52 +01:00
|
|
|
cs := meta.(*cloudstack.CloudStackClient)
|
|
|
|
uuids := rule["uuids"].(map[string]interface{})
|
|
|
|
|
|
|
|
for k, id := range uuids {
|
2015-01-15 11:29:04 +01:00
|
|
|
// We don't care about the count here, so just continue
|
|
|
|
if k == "#" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
// Create the parameter struct
|
|
|
|
p := cs.Firewall.NewDeleteFirewallRuleParams(id.(string))
|
|
|
|
|
|
|
|
// Delete the rule
|
|
|
|
if _, err := cs.Firewall.DeleteFirewallRule(p); err != nil {
|
|
|
|
|
2015-10-05 14:05:21 +02:00
|
|
|
// This is a very poor way to be told the ID does no longer exist :(
|
2014-12-10 22:20:52 +01:00
|
|
|
if strings.Contains(err.Error(), fmt.Sprintf(
|
|
|
|
"Invalid parameter id value=%s due to incorrect long value format, "+
|
|
|
|
"or entity does not exist", id.(string))) {
|
|
|
|
delete(uuids, k)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete the UUID of this rule
|
|
|
|
delete(uuids, k)
|
2015-12-03 11:10:42 +01:00
|
|
|
rule["uuids"] = uuids
|
2014-12-10 22:20:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-16 13:26:43 +01:00
|
|
|
func verifyFirewallParams(d *schema.ResourceData) error {
|
|
|
|
managed := d.Get("managed").(bool)
|
|
|
|
_, rules := d.GetOk("rule")
|
|
|
|
|
|
|
|
if !rules && !managed {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"You must supply at least one 'rule' when not using the 'managed' firewall feature")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 10:04:09 +01:00
|
|
|
func verifyFirewallRuleParams(d *schema.ResourceData, rule map[string]interface{}) error {
|
2015-12-03 11:10:42 +01:00
|
|
|
cidrList := rule["cidr_list"].(*schema.Set)
|
|
|
|
sourceCidr := rule["source_cidr"].(string)
|
|
|
|
if cidrList.Len() == 0 && sourceCidr == "" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Parameter cidr_list is a required parameter")
|
|
|
|
}
|
|
|
|
if cidrList.Len() > 0 && sourceCidr != "" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Parameter source_cidr is deprecated and cannot be used together with cidr_list")
|
|
|
|
}
|
|
|
|
|
2014-12-10 22:20:52 +01:00
|
|
|
protocol := rule["protocol"].(string)
|
|
|
|
if protocol != "tcp" && protocol != "udp" && protocol != "icmp" {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"%s is not a valid protocol. Valid options are 'tcp', 'udp' and 'icmp'", protocol)
|
|
|
|
}
|
|
|
|
|
|
|
|
if protocol == "icmp" {
|
|
|
|
if _, ok := rule["icmp_type"]; !ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Parameter icmp_type is a required parameter when using protocol 'icmp'")
|
|
|
|
}
|
|
|
|
if _, ok := rule["icmp_code"]; !ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Parameter icmp_code is a required parameter when using protocol 'icmp'")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if _, ok := rule["ports"]; !ok {
|
|
|
|
return fmt.Errorf(
|
|
|
|
"Parameter port is a required parameter when using protocol 'tcp' or 'udp'")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|