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

     1  package vcd
     2  
     3  import (
     4  	"fmt"
     5  	"github.com/hashicorp/terraform/helper/schema"
     6  )
     7  
     8  func resourceVcdSNAT() *schema.Resource {
     9  	return &schema.Resource{
    10  		Create: resourceVcdSNATCreate,
    11  		Delete: resourceVcdSNATDelete,
    12  		Read:   resourceVcdSNATRead,
    13  
    14  		Schema: map[string]*schema.Schema{
    15  			"edge_gateway": &schema.Schema{
    16  				Type:     schema.TypeString,
    17  				Required: true,
    18  				ForceNew: true,
    19  			},
    20  
    21  			"external_ip": &schema.Schema{
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  				ForceNew: true,
    25  			},
    26  
    27  			"internal_ip": &schema.Schema{
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  		},
    33  	}
    34  }
    35  
    36  func resourceVcdSNATCreate(d *schema.ResourceData, meta interface{}) error {
    37  	vcdClient := meta.(*VCDClient)
    38  	// Multiple VCD components need to run operations on the Edge Gateway, as
    39  	// the edge gatway will throw back an error if it is already performing an
    40  	// operation we must wait until we can aquire a lock on the client
    41  	vcdClient.Mutex.Lock()
    42  	defer vcdClient.Mutex.Unlock()
    43  
    44  	// Creating a loop to offer further protection from the edge gateway erroring
    45  	// due to being busy eg another person is using another client so wouldn't be
    46  	// constrained by out lock. If the edge gateway reurns with a busy error, wait
    47  	// 3 seconds and then try again. Continue until a non-busy error or success
    48  	edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
    49  	if err != nil {
    50  		return fmt.Errorf("Unable to find edge gateway: %#v", err)
    51  	}
    52  
    53  	err = retryCall(vcdClient.MaxRetryTimeout, func() error {
    54  		task, err := edgeGateway.AddNATMapping("SNAT", d.Get("internal_ip").(string),
    55  			d.Get("external_ip").(string),
    56  			"any")
    57  		if err != nil {
    58  			return fmt.Errorf("Error setting SNAT rules: %#v", err)
    59  		}
    60  		return task.WaitTaskCompletion()
    61  	})
    62  	if err != nil {
    63  		return err
    64  	}
    65  
    66  	d.SetId(d.Get("internal_ip").(string))
    67  	return nil
    68  }
    69  
    70  func resourceVcdSNATRead(d *schema.ResourceData, meta interface{}) error {
    71  	vcdClient := meta.(*VCDClient)
    72  	e, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
    73  
    74  	if err != nil {
    75  		return fmt.Errorf("Unable to find edge gateway: %#v", err)
    76  	}
    77  
    78  	var found bool
    79  
    80  	for _, r := range e.EdgeGateway.Configuration.EdgeGatewayServiceConfiguration.NatService.NatRule {
    81  		if r.RuleType == "SNAT" &&
    82  			r.GatewayNatRule.OriginalIP == d.Id() {
    83  			found = true
    84  			d.Set("external_ip", r.GatewayNatRule.TranslatedIP)
    85  		}
    86  	}
    87  
    88  	if !found {
    89  		d.SetId("")
    90  	}
    91  
    92  	return nil
    93  }
    94  
    95  func resourceVcdSNATDelete(d *schema.ResourceData, meta interface{}) error {
    96  	vcdClient := meta.(*VCDClient)
    97  	// Multiple VCD components need to run operations on the Edge Gateway, as
    98  	// the edge gatway will throw back an error if it is already performing an
    99  	// operation we must wait until we can aquire a lock on the client
   100  	vcdClient.Mutex.Lock()
   101  	defer vcdClient.Mutex.Unlock()
   102  
   103  	edgeGateway, err := vcdClient.OrgVdc.FindEdgeGateway(d.Get("edge_gateway").(string))
   104  	if err != nil {
   105  		return fmt.Errorf("Unable to find edge gateway: %#v", err)
   106  	}
   107  
   108  	err = retryCall(vcdClient.MaxRetryTimeout, func() error {
   109  		task, err := edgeGateway.RemoveNATMapping("SNAT", d.Get("internal_ip").(string),
   110  			d.Get("external_ip").(string),
   111  			"")
   112  		if err != nil {
   113  			return fmt.Errorf("Error setting SNAT rules: %#v", err)
   114  		}
   115  		return task.WaitTaskCompletion()
   116  	})
   117  	if err != nil {
   118  		return err
   119  	}
   120  
   121  	return nil
   122  }