github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/vcd/structure.go (about)

     1  package vcd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/resource"
     6  	"github.com/hashicorp/terraform/helper/schema"
     7  	types "github.com/hmrc/vmware-govcd/types/v56"
     8  	"strconv"
     9  	"time"
    10  )
    11  
    12  func expandIPRange(configured []interface{}) types.IPRanges {
    13  	ipRange := make([]*types.IPRange, 0, len(configured))
    14  
    15  	for _, ipRaw := range configured {
    16  		data := ipRaw.(map[string]interface{})
    17  
    18  		ip := types.IPRange{
    19  			StartAddress: data["start_address"].(string),
    20  			EndAddress:   data["end_address"].(string),
    21  		}
    22  
    23  		ipRange = append(ipRange, &ip)
    24  	}
    25  
    26  	ipRanges := types.IPRanges{
    27  		IPRange: ipRange,
    28  	}
    29  
    30  	return ipRanges
    31  }
    32  
    33  func expandFirewallRules(d *schema.ResourceData, gateway *types.EdgeGateway) ([]*types.FirewallRule, error) {
    34  	//firewallRules := make([]*types.FirewallRule, 0, len(configured))
    35  	firewallRules := gateway.Configuration.EdgeGatewayServiceConfiguration.FirewallService.FirewallRule
    36  
    37  	rulesCount := d.Get("rule.#").(int)
    38  	for i := 0; i < rulesCount; i++ {
    39  		prefix := fmt.Sprintf("rule.%d", i)
    40  
    41  		var protocol *types.FirewallRuleProtocols
    42  		switch d.Get(prefix + ".protocol").(string) {
    43  		case "tcp":
    44  			protocol = &types.FirewallRuleProtocols{
    45  				TCP: true,
    46  			}
    47  		case "udp":
    48  			protocol = &types.FirewallRuleProtocols{
    49  				UDP: true,
    50  			}
    51  		case "icmp":
    52  			protocol = &types.FirewallRuleProtocols{
    53  				ICMP: true,
    54  			}
    55  		default:
    56  			protocol = &types.FirewallRuleProtocols{
    57  				Any: true,
    58  			}
    59  		}
    60  		rule := &types.FirewallRule{
    61  			//ID: strconv.Itoa(len(configured) - i),
    62  			IsEnabled:            true,
    63  			MatchOnTranslate:     false,
    64  			Description:          d.Get(prefix + ".description").(string),
    65  			Policy:               d.Get(prefix + ".policy").(string),
    66  			Protocols:            protocol,
    67  			Port:                 getNumericPort(d.Get(prefix + ".destination_port")),
    68  			DestinationPortRange: d.Get(prefix + ".destination_port").(string),
    69  			DestinationIP:        d.Get(prefix + ".destination_ip").(string),
    70  			SourcePort:           getNumericPort(d.Get(prefix + ".source_port")),
    71  			SourcePortRange:      d.Get(prefix + ".source_port").(string),
    72  			SourceIP:             d.Get(prefix + ".source_ip").(string),
    73  			EnableLogging:        false,
    74  		}
    75  		firewallRules = append(firewallRules, rule)
    76  	}
    77  
    78  	return firewallRules, nil
    79  }
    80  
    81  func getProtocol(protocol types.FirewallRuleProtocols) string {
    82  	if protocol.TCP {
    83  		return "tcp"
    84  	}
    85  	if protocol.UDP {
    86  		return "udp"
    87  	}
    88  	if protocol.ICMP {
    89  		return "icmp"
    90  	}
    91  	return "any"
    92  }
    93  
    94  func getNumericPort(portrange interface{}) int {
    95  	i, err := strconv.Atoi(portrange.(string))
    96  	if err != nil {
    97  		return -1
    98  	}
    99  	return i
   100  }
   101  
   102  func getPortString(port int) string {
   103  	if port == -1 {
   104  		return "any"
   105  	}
   106  	portstring := strconv.Itoa(port)
   107  	return portstring
   108  }
   109  
   110  func retryCall(seconds int, f resource.RetryFunc) error {
   111  	return resource.Retry(time.Duration(seconds)*time.Second, f)
   112  }