github.com/nicgrayson/terraform@v0.4.3-0.20150415203910-c4de50829380/builtin/providers/aws/network_acl_entry.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/awslabs/aws-sdk-go/aws" 8 "github.com/awslabs/aws-sdk-go/service/ec2" 9 ) 10 11 func expandNetworkAclEntries(configured []interface{}, entryType string) ([]*ec2.NetworkACLEntry, error) { 12 entries := make([]*ec2.NetworkACLEntry, 0, len(configured)) 13 for _, eRaw := range configured { 14 data := eRaw.(map[string]interface{}) 15 protocol := data["protocol"].(string) 16 _, ok := protocolIntegers()[protocol] 17 if !ok { 18 return nil, fmt.Errorf("Invalid Protocol %s for rule %#v", protocol, data) 19 } 20 p := extractProtocolInteger(data["protocol"].(string)) 21 e := &ec2.NetworkACLEntry{ 22 Protocol: aws.String(strconv.Itoa(p)), 23 PortRange: &ec2.PortRange{ 24 From: aws.Long(int64(data["from_port"].(int))), 25 To: aws.Long(int64(data["to_port"].(int))), 26 }, 27 Egress: aws.Boolean((entryType == "egress")), 28 RuleAction: aws.String(data["action"].(string)), 29 RuleNumber: aws.Long(int64(data["rule_no"].(int))), 30 CIDRBlock: aws.String(data["cidr_block"].(string)), 31 } 32 entries = append(entries, e) 33 } 34 return entries, nil 35 } 36 37 func flattenNetworkAclEntries(list []*ec2.NetworkACLEntry) []map[string]interface{} { 38 entries := make([]map[string]interface{}, 0, len(list)) 39 40 for _, entry := range list { 41 entries = append(entries, map[string]interface{}{ 42 "from_port": *entry.PortRange.From, 43 "to_port": *entry.PortRange.To, 44 "action": *entry.RuleAction, 45 "rule_no": *entry.RuleNumber, 46 "protocol": *entry.Protocol, 47 "cidr_block": *entry.CIDRBlock, 48 }) 49 } 50 51 return entries 52 53 } 54 55 func extractProtocolInteger(protocol string) int { 56 return protocolIntegers()[protocol] 57 } 58 59 func extractProtocolString(protocol int) string { 60 for key, value := range protocolIntegers() { 61 if value == protocol { 62 return key 63 } 64 } 65 return "" 66 } 67 68 func protocolIntegers() map[string]int { 69 var protocolIntegers = make(map[string]int) 70 protocolIntegers = map[string]int{ 71 "udp": 17, 72 "tcp": 6, 73 "icmp": 1, 74 "all": -1, 75 } 76 return protocolIntegers 77 }