2015-10-26 15:45:48 +01:00
|
|
|
package vcd
|
|
|
|
|
|
|
|
import (
|
2015-11-16 21:11:05 +01:00
|
|
|
"fmt"
|
2015-11-02 17:39:56 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2015-11-16 21:11:05 +01:00
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2015-11-06 11:19:59 +01:00
|
|
|
types "github.com/hmrc/vmware-govcd/types/v56"
|
2015-10-26 15:45:48 +01:00
|
|
|
"strconv"
|
2015-11-02 17:39:56 +01:00
|
|
|
"time"
|
2015-10-26 15:45:48 +01:00
|
|
|
)
|
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
func expandIPRange(configured []interface{}) types.IPRanges {
|
2015-10-26 15:45:48 +01:00
|
|
|
ipRange := make([]*types.IPRange, 0, len(configured))
|
|
|
|
|
|
|
|
for _, ipRaw := range configured {
|
|
|
|
data := ipRaw.(map[string]interface{})
|
|
|
|
|
|
|
|
ip := types.IPRange{
|
|
|
|
StartAddress: data["start_address"].(string),
|
|
|
|
EndAddress: data["end_address"].(string),
|
|
|
|
}
|
|
|
|
|
|
|
|
ipRange = append(ipRange, &ip)
|
|
|
|
}
|
|
|
|
|
|
|
|
ipRanges := types.IPRanges{
|
|
|
|
IPRange: ipRange,
|
|
|
|
}
|
|
|
|
|
2015-11-10 19:39:58 +01:00
|
|
|
return ipRanges
|
2015-10-26 15:45:48 +01:00
|
|
|
}
|
|
|
|
|
2015-11-16 21:11:05 +01:00
|
|
|
func expandFirewallRules(d *schema.ResourceData, gateway *types.EdgeGateway) ([]*types.FirewallRule, error) {
|
2015-10-26 15:45:48 +01:00
|
|
|
//firewallRules := make([]*types.FirewallRule, 0, len(configured))
|
|
|
|
firewallRules := gateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule
|
|
|
|
|
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)
|
2015-10-26 15:45:48 +01:00
|
|
|
|
|
|
|
var protocol *types.FirewallRuleProtocols
|
2015-11-16 21:11:05 +01:00
|
|
|
switch d.Get(prefix + ".protocol").(string) {
|
2015-10-26 15:45:48 +01:00
|
|
|
case "tcp":
|
|
|
|
protocol = &types.FirewallRuleProtocols{
|
|
|
|
TCP: true,
|
|
|
|
}
|
|
|
|
case "udp":
|
|
|
|
protocol = &types.FirewallRuleProtocols{
|
|
|
|
UDP: true,
|
|
|
|
}
|
|
|
|
case "icmp":
|
|
|
|
protocol = &types.FirewallRuleProtocols{
|
|
|
|
ICMP: true,
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
protocol = &types.FirewallRuleProtocols{
|
|
|
|
Any: true,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rule := &types.FirewallRule{
|
|
|
|
//ID: strconv.Itoa(len(configured) - i),
|
|
|
|
IsEnabled: true,
|
|
|
|
MatchOnTranslate: false,
|
2015-11-16 21:11:05 +01:00
|
|
|
Description: d.Get(prefix + ".description").(string),
|
|
|
|
Policy: d.Get(prefix + ".policy").(string),
|
2015-10-26 15:45:48 +01:00
|
|
|
Protocols: protocol,
|
2015-11-16 21:11:05 +01:00
|
|
|
Port: getNumericPort(d.Get(prefix + ".destination_port")),
|
|
|
|
DestinationPortRange: d.Get(prefix + ".destination_port").(string),
|
|
|
|
DestinationIP: d.Get(prefix + ".destination_ip").(string),
|
|
|
|
SourcePort: getNumericPort(d.Get(prefix + ".source_port")),
|
|
|
|
SourcePortRange: d.Get(prefix + ".source_port").(string),
|
|
|
|
SourceIP: d.Get(prefix + ".source_ip").(string),
|
2015-10-26 15:45:48 +01:00
|
|
|
EnableLogging: false,
|
|
|
|
}
|
|
|
|
firewallRules = append(firewallRules, rule)
|
|
|
|
}
|
|
|
|
|
|
|
|
return firewallRules, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getProtocol(protocol types.FirewallRuleProtocols) string {
|
|
|
|
if protocol.TCP {
|
|
|
|
return "tcp"
|
|
|
|
}
|
|
|
|
if protocol.UDP {
|
|
|
|
return "udp"
|
|
|
|
}
|
|
|
|
if protocol.ICMP {
|
|
|
|
return "icmp"
|
|
|
|
}
|
|
|
|
return "any"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getNumericPort(portrange interface{}) int {
|
|
|
|
i, err := strconv.Atoi(portrange.(string))
|
|
|
|
if err != nil {
|
|
|
|
return -1
|
|
|
|
}
|
|
|
|
return i
|
|
|
|
}
|
|
|
|
|
|
|
|
func getPortString(port int) string {
|
|
|
|
if port == -1 {
|
|
|
|
return "any"
|
|
|
|
}
|
|
|
|
portstring := strconv.Itoa(port)
|
|
|
|
return portstring
|
|
|
|
}
|
2015-11-02 17:39:56 +01:00
|
|
|
|
2015-11-19 00:56:17 +01:00
|
|
|
func retryCall(seconds int, f resource.RetryFunc) error {
|
|
|
|
return resource.Retry(time.Duration(seconds)*time.Second, f)
|
2015-11-02 17:39:56 +01:00
|
|
|
}
|