2014-11-30 12:38:45 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-12-08 11:48:39 +01:00
|
|
|
"fmt"
|
2015-03-11 21:01:07 +01:00
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/hashicorp/aws-sdk-go/aws"
|
|
|
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
2014-11-30 12:38:45 +01:00
|
|
|
)
|
|
|
|
|
2015-03-11 21:01:07 +01:00
|
|
|
func expandNetworkAclEntries(configured []interface{}, entryType string) ([]ec2.NetworkACLEntry, error) {
|
|
|
|
entries := make([]ec2.NetworkACLEntry, 0, len(configured))
|
2014-11-30 12:38:45 +01:00
|
|
|
for _, eRaw := range configured {
|
|
|
|
data := eRaw.(map[string]interface{})
|
2014-12-08 11:48:39 +01:00
|
|
|
protocol := data["protocol"].(string)
|
|
|
|
_, ok := protocolIntegers()[protocol]
|
2015-02-20 19:22:26 +01:00
|
|
|
if !ok {
|
2014-12-08 11:48:39 +01:00
|
|
|
return nil, fmt.Errorf("Invalid Protocol %s for rule %#v", protocol, data)
|
|
|
|
}
|
2014-11-30 12:38:45 +01:00
|
|
|
p := extractProtocolInteger(data["protocol"].(string))
|
2015-03-11 21:01:07 +01:00
|
|
|
e := ec2.NetworkACLEntry{
|
|
|
|
Protocol: aws.String(strconv.Itoa(p)),
|
|
|
|
PortRange: &ec2.PortRange{
|
|
|
|
From: aws.Integer(data["from_port"].(int)),
|
|
|
|
To: aws.Integer(data["to_port"].(int)),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2015-03-11 21:01:07 +01:00
|
|
|
Egress: aws.Boolean((entryType == "egress")),
|
|
|
|
RuleAction: aws.String(data["action"].(string)),
|
|
|
|
RuleNumber: aws.Integer(data["rule_no"].(int)),
|
|
|
|
CIDRBlock: aws.String(data["cidr_block"].(string)),
|
2014-11-30 12:38:45 +01:00
|
|
|
}
|
|
|
|
entries = append(entries, e)
|
|
|
|
}
|
|
|
|
|
2014-12-08 11:48:39 +01:00
|
|
|
return entries, nil
|
2014-11-30 12:38:45 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-03-11 21:01:07 +01:00
|
|
|
func flattenNetworkAclEntries(list []ec2.NetworkACLEntry) []map[string]interface{} {
|
2014-11-30 12:38:45 +01:00
|
|
|
entries := make([]map[string]interface{}, 0, len(list))
|
|
|
|
|
2014-12-01 09:49:05 +01:00
|
|
|
for _, entry := range list {
|
2014-11-30 12:38:45 +01:00
|
|
|
entries = append(entries, map[string]interface{}{
|
2015-03-11 21:01:07 +01:00
|
|
|
"from_port": *entry.PortRange.From,
|
|
|
|
"to_port": *entry.PortRange.To,
|
|
|
|
"action": *entry.RuleAction,
|
|
|
|
"rule_no": *entry.RuleNumber,
|
|
|
|
"protocol": *entry.Protocol,
|
|
|
|
"cidr_block": *entry.CIDRBlock,
|
2014-12-01 09:49:05 +01:00
|
|
|
})
|
|
|
|
}
|
2014-11-30 12:38:45 +01:00
|
|
|
return entries
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractProtocolInteger(protocol string) int {
|
|
|
|
return protocolIntegers()[protocol]
|
|
|
|
}
|
|
|
|
|
|
|
|
func extractProtocolString(protocol int) string {
|
|
|
|
for key, value := range protocolIntegers() {
|
2014-12-01 09:49:05 +01:00
|
|
|
if value == protocol {
|
2014-11-30 12:38:45 +01:00
|
|
|
return key
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2014-12-01 09:49:05 +01:00
|
|
|
func protocolIntegers() map[string]int {
|
|
|
|
var protocolIntegers = make(map[string]int)
|
2014-11-30 12:38:45 +01:00
|
|
|
protocolIntegers = map[string]int{
|
2014-12-01 09:49:05 +01:00
|
|
|
"udp": 17,
|
|
|
|
"tcp": 6,
|
|
|
|
"icmp": 1,
|
2015-02-20 19:22:26 +01:00
|
|
|
"all": -1,
|
2014-11-30 12:38:45 +01:00
|
|
|
}
|
|
|
|
return protocolIntegers
|
|
|
|
}
|