github.com/minamijoyo/terraform@v0.7.8-0.20161029001309-18b3736ba44b/builtin/providers/cloudstack/resource_cloudstack_static_route.go (about)

     1  package cloudstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"strings"
     7  
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  	"github.com/xanzy/go-cloudstack/cloudstack"
    10  )
    11  
    12  func resourceCloudStackStaticRoute() *schema.Resource {
    13  	return &schema.Resource{
    14  		Create: resourceCloudStackStaticRouteCreate,
    15  		Read:   resourceCloudStackStaticRouteRead,
    16  		Delete: resourceCloudStackStaticRouteDelete,
    17  
    18  		Schema: map[string]*schema.Schema{
    19  			"cidr": &schema.Schema{
    20  				Type:     schema.TypeString,
    21  				Required: true,
    22  				ForceNew: true,
    23  			},
    24  
    25  			"gateway_id": &schema.Schema{
    26  				Type:     schema.TypeString,
    27  				Required: true,
    28  				ForceNew: true,
    29  			},
    30  		},
    31  	}
    32  }
    33  
    34  func resourceCloudStackStaticRouteCreate(d *schema.ResourceData, meta interface{}) error {
    35  	cs := meta.(*cloudstack.CloudStackClient)
    36  
    37  	// Create a new parameter struct
    38  	p := cs.VPC.NewCreateStaticRouteParams(
    39  		d.Get("cidr").(string),
    40  		d.Get("gateway_id").(string),
    41  	)
    42  
    43  	// Create the new private gateway
    44  	r, err := cs.VPC.CreateStaticRoute(p)
    45  	if err != nil {
    46  		return fmt.Errorf("Error creating static route for %s: %s", d.Get("cidr").(string), err)
    47  	}
    48  
    49  	d.SetId(r.Id)
    50  
    51  	return resourceCloudStackStaticRouteRead(d, meta)
    52  }
    53  
    54  func resourceCloudStackStaticRouteRead(d *schema.ResourceData, meta interface{}) error {
    55  	cs := meta.(*cloudstack.CloudStackClient)
    56  
    57  	// Get the virtual machine details
    58  	staticroute, count, err := cs.VPC.GetStaticRouteByID(d.Id())
    59  	if err != nil {
    60  		if count == 0 {
    61  			log.Printf("[DEBUG] Static route %s does no longer exist", d.Id())
    62  			d.SetId("")
    63  			return nil
    64  		}
    65  
    66  		return err
    67  	}
    68  
    69  	d.Set("cidr", staticroute.Cidr)
    70  
    71  	return nil
    72  }
    73  
    74  func resourceCloudStackStaticRouteDelete(d *schema.ResourceData, meta interface{}) error {
    75  	cs := meta.(*cloudstack.CloudStackClient)
    76  
    77  	// Create a new parameter struct
    78  	p := cs.VPC.NewDeleteStaticRouteParams(d.Id())
    79  
    80  	// Delete the private gateway
    81  	_, err := cs.VPC.DeleteStaticRoute(p)
    82  	if err != nil {
    83  		// This is a very poor way to be told the ID does no longer exist :(
    84  		if strings.Contains(err.Error(), fmt.Sprintf(
    85  			"Invalid parameter id value=%s due to incorrect long value format, "+
    86  				"or entity does not exist", d.Id())) {
    87  			return nil
    88  		}
    89  
    90  		return fmt.Errorf("Error deleting static route for %s: %s", d.Get("cidr").(string), err)
    91  	}
    92  
    93  	return nil
    94  }