github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/vcd/structure.go (about)

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