github.com/ndarilek/terraform@v0.3.8-0.20150320140257-d3135c1b2bac/builtin/providers/aws/network_acl_entry.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"strconv"
     6  
     7  	"github.com/hashicorp/aws-sdk-go/aws"
     8  	"github.com/hashicorp/aws-sdk-go/gen/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.Integer(data["from_port"].(int)),
    25  				To:   aws.Integer(data["to_port"].(int)),
    26  			},
    27  			Egress:     aws.Boolean((entryType == "egress")),
    28  			RuleAction: aws.String(data["action"].(string)),
    29  			RuleNumber: aws.Integer(data["rule_no"].(int)),
    30  			CIDRBlock:  aws.String(data["cidr_block"].(string)),
    31  		}
    32  		entries = append(entries, e)
    33  	}
    34  
    35  	return entries, nil
    36  
    37  }
    38  
    39  func flattenNetworkAclEntries(list []ec2.NetworkACLEntry) []map[string]interface{} {
    40  	entries := make([]map[string]interface{}, 0, len(list))
    41  
    42  	for _, entry := range list {
    43  		entries = append(entries, map[string]interface{}{
    44  			"from_port":  *entry.PortRange.From,
    45  			"to_port":    *entry.PortRange.To,
    46  			"action":     *entry.RuleAction,
    47  			"rule_no":    *entry.RuleNumber,
    48  			"protocol":   *entry.Protocol,
    49  			"cidr_block": *entry.CIDRBlock,
    50  		})
    51  	}
    52  	return entries
    53  
    54  }
    55  
    56  func extractProtocolInteger(protocol string) int {
    57  	return protocolIntegers()[protocol]
    58  }
    59  
    60  func extractProtocolString(protocol int) string {
    61  	for key, value := range protocolIntegers() {
    62  		if value == protocol {
    63  			return key
    64  		}
    65  	}
    66  	return ""
    67  }
    68  
    69  func protocolIntegers() map[string]int {
    70  	var protocolIntegers = make(map[string]int)
    71  	protocolIntegers = map[string]int{
    72  		"udp":  17,
    73  		"tcp":  6,
    74  		"icmp": 1,
    75  		"all":  -1,
    76  	}
    77  	return protocolIntegers
    78  }