2014-11-30 12:38:45 +01:00
|
|
|
package aws
|
|
|
|
|
|
|
|
import (
|
2014-12-08 11:48:39 +01:00
|
|
|
"fmt"
|
2015-02-20 19:22:26 +01:00
|
|
|
"github.com/mitchellh/goamz/ec2"
|
2014-11-30 12:38:45 +01:00
|
|
|
)
|
|
|
|
|
2014-12-08 11:48:39 +01:00
|
|
|
func expandNetworkAclEntries(configured []interface{}, entryType string) ([]ec2.NetworkAclEntry, error) {
|
2014-11-30 12:38:45 +01:00
|
|
|
entries := make([]ec2.NetworkAclEntry, 0, len(configured))
|
|
|
|
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))
|
|
|
|
e := ec2.NetworkAclEntry{
|
2014-12-01 09:49:05 +01:00
|
|
|
Protocol: p,
|
2014-11-30 12:38:45 +01:00
|
|
|
PortRange: ec2.PortRange{
|
2014-12-01 09:49:05 +01:00
|
|
|
From: data["from_port"].(int),
|
|
|
|
To: data["to_port"].(int),
|
2014-11-30 12:38:45 +01:00
|
|
|
},
|
2014-12-01 09:49:05 +01:00
|
|
|
Egress: (entryType == "egress"),
|
2014-11-30 12:38:45 +01:00
|
|
|
RuleAction: data["action"].(string),
|
|
|
|
RuleNumber: data["rule_no"].(int),
|
2014-12-01 09:49:05 +01:00
|
|
|
CidrBlock: 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
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func flattenNetworkAclEntries(list []ec2.NetworkAclEntry) []map[string]interface{} {
|
|
|
|
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{}{
|
2014-12-01 09:49:05 +01:00
|
|
|
"from_port": entry.PortRange.From,
|
|
|
|
"to_port": entry.PortRange.To,
|
|
|
|
"action": entry.RuleAction,
|
|
|
|
"rule_no": entry.RuleNumber,
|
|
|
|
"protocol": extractProtocolString(entry.Protocol),
|
|
|
|
"cidr_block": entry.CidrBlock,
|
|
|
|
})
|
|
|
|
}
|
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
|
|
|
|
}
|