github.com/chalford/terraform@v0.3.7-0.20150113080010-a78c69a8c81f/builtin/providers/aws/network_acl_entry.go (about)

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