2015-10-26 15:45:48 +01:00
|
|
|
package vcd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-11-06 11:19:59 +01:00
|
|
|
types "github.com/hmrc/vmware-govcd/types/v56"
|
2015-11-16 21:11:05 +01:00
|
|
|
"log"
|
2015-10-26 15:45:48 +01:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
func resourceVcdFirewallRules() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
|
|
|
Create: resourceVcdFirewallRulesCreate,
|
|
|
|
Delete: resourceFirewallRulesDelete,
|
|
|
|
Read: resourceFirewallRulesRead,
|
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"edge_gateway": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"default_action": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"rule": &schema.Schema{
|
2015-11-16 21:11:05 +01:00
|
|
|
Type: schema.TypeList,
|
2015-10-26 15:45:48 +01:00
|
|
|
Optional: true,
|
|
|
|
ForceNew: true,
|
|
|
|
Elem: &schema.Resource{
|
|
|
|
Schema: map[string]*schema.Schema{
|
|
|
|
"id": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"description": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"policy": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"protocol": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"destination_port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"destination_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"source_port": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
|
|
|
|
"source_ip": &schema.Schema{
|
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceVcdFirewallRulesCreate(d *schema.ResourceData, meta interface{}) error {
|
2015-11-19 00:56:17 +01:00
|
|
|
vcdClient := meta.(*VCDClient)
|
2015-11-16 21:11:05 +01:00
|
|
|
vcdClient.Mutex.Lock()
|
|
|
|
defer vcdClient.Mutex.Unlock()
|
2015-10-26 15:45:48 +01:00
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
|
2015-10-26 15:45:48 +01:00
|
|
|
if err != nil {
|
2015-11-02 20:09:24 +01:00
|
|
|
return fmt.Errorf("Unable to find edge gateway: %s", err)
|
2015-10-26 15:45:48 +01:00
|
|
|
}
|
2015-11-02 20:09:24 +01:00
|
|
|
|
2015-11-19 00:56:17 +01:00
|
|
|
err = retryCall(vcdClient.MaxRetryTimeout, func() error {
|
2015-11-02 20:09:24 +01:00
|
|
|
edgeGateway.Refresh()
|
2015-11-16 21:11:05 +01:00
|
|
|
firewallRules, _ := expandFirewallRules(d, edgeGateway.EdgeGateway)
|
2015-11-02 20:09:24 +01:00
|
|
|
task, err := edgeGateway.CreateFirewallRules(d.Get("default_action").(string), firewallRules)
|
|
|
|
if err != nil {
|
2015-11-16 21:11:05 +01:00
|
|
|
log.Printf("[INFO] Error setting firewall rules: %s", err)
|
2015-11-02 20:09:24 +01:00
|
|
|
return fmt.Errorf("Error setting firewall rules: %#v", err)
|
|
|
|
}
|
2015-11-16 21:11:05 +01:00
|
|
|
|
2015-11-02 20:09:24 +01:00
|
|
|
return task.WaitTaskCompletion()
|
|
|
|
})
|
2015-10-26 15:45:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error completing tasks: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
d.SetId(d.Get("edge_gateway").(string))
|
|
|
|
|
|
|
|
return resourceFirewallRulesRead(d, meta)
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRulesDelete(d *schema.ResourceData, meta interface{}) error {
|
2015-11-19 00:56:17 +01:00
|
|
|
vcdClient := meta.(*VCDClient)
|
2015-11-16 21:11:05 +01:00
|
|
|
vcdClient.Mutex.Lock()
|
|
|
|
defer vcdClient.Mutex.Unlock()
|
2015-10-26 15:45:48 +01:00
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
|
2015-10-26 15:45:48 +01:00
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
firewallRules := deleteFirewallRules(d, edgeGateway.EdgeGateway)
|
2015-10-26 15:45:48 +01:00
|
|
|
defaultAction := edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.DefaultAction
|
|
|
|
task, err := edgeGateway.CreateFirewallRules(defaultAction, firewallRules)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error deleting firewall rules: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = task.WaitTaskCompletion()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error completing tasks: %#v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceFirewallRulesRead(d *schema.ResourceData, meta interface{}) error {
|
2015-11-19 00:56:17 +01:00
|
|
|
vcdClient := meta.(*VCDClient)
|
2015-10-26 15:45:48 +01:00
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
|
2015-10-26 15:45:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Error finding edge gateway: %#v", err)
|
|
|
|
}
|
2015-11-16 21:11:05 +01:00
|
|
|
ruleList := d.Get("rule").([]interface{})
|
2015-10-26 15:45:48 +01:00
|
|
|
firewallRules := *edgeGateway.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService
|
2015-11-16 21:11:05 +01:00
|
|
|
rulesCount := d.Get("rule.#").(int)
|
|
|
|
for i := 0; i < rulesCount; i++ {
|
|
|
|
prefix := fmt.Sprintf("rule.%d", i)
|
|
|
|
if d.Get(prefix+".id").(string) == "" {
|
|
|
|
log.Printf("[INFO] Rule %d has no id. Searching...", i)
|
|
|
|
ruleid, err := matchFirewallRule(d, prefix, firewallRules.FirewallRule)
|
|
|
|
if err == nil {
|
|
|
|
currentRule := ruleList[i].(map[string]interface{})
|
|
|
|
currentRule["id"] = ruleid
|
|
|
|
ruleList[i] = currentRule
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
d.Set("rule", ruleList)
|
2015-10-26 15:45:48 +01:00
|
|
|
d.Set("default_action", firewallRules.DefaultAction)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
func deleteFirewallRules(d *schema.ResourceData, gateway *types.EdgeGateway) []*types.FirewallRule {
|
2015-10-26 15:45:48 +01:00
|
|
|
firewallRules := gateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule
|
2015-11-16 21:11:05 +01:00
|
|
|
rulesCount := d.Get("rule.#").(int)
|
|
|
|
fwrules := make([]*types.FirewallRule, 0, len(firewallRules)-rulesCount)
|
2015-10-26 15:45:48 +01:00
|
|
|
|
|
|
|
for _, f := range firewallRules {
|
|
|
|
keep := true
|
2015-11-16 21:11:05 +01:00
|
|
|
for i := 0; i < rulesCount; i++ {
|
|
|
|
if d.Get(fmt.Sprintf("rule.%d.id", i)).(string) != f.ID {
|
2015-10-26 15:45:48 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
keep = false
|
|
|
|
}
|
|
|
|
if keep {
|
|
|
|
fwrules = append(fwrules, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return fwrules
|
|
|
|
}
|
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
func matchFirewallRule(d *schema.ResourceData, prefix string, rules []*types.FirewallRule) (string, error) {
|
2015-10-26 15:45:48 +01:00
|
|
|
|
|
|
|
for _, m := range rules {
|
2015-11-16 21:11:05 +01:00
|
|
|
if d.Get(prefix+".description").(string) == m.Description &&
|
|
|
|
d.Get(prefix+".policy").(string) == m.Policy &&
|
|
|
|
strings.ToLower(d.Get(prefix+".protocol").(string)) == getProtocol(*m.Protocols) &&
|
|
|
|
strings.ToLower(d.Get(prefix+".destination_port").(string)) == getPortString(m.Port) &&
|
|
|
|
strings.ToLower(d.Get(prefix+".destination_ip").(string)) == strings.ToLower(m.DestinationIP) &&
|
|
|
|
strings.ToLower(d.Get(prefix+".source_port").(string)) == getPortString(m.SourcePort) &&
|
|
|
|
strings.ToLower(d.Get(prefix+".source_ip").(string)) == strings.ToLower(m.SourceIP) {
|
|
|
|
return m.ID, nil
|
2015-10-26 15:45:48 +01:00
|
|
|
}
|
|
|
|
}
|
2015-11-16 21:11:05 +01:00
|
|
|
return "", fmt.Errorf("Unable to find rule")
|
2015-10-26 15:45:48 +01:00
|
|
|
}
|