github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_route.go (about)

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"github.com/hashicorp/go-oracle-terraform/compute"
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceOPCRoute() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCRouteCreate,
    13  		Read:   resourceOPCRouteRead,
    14  		Update: resourceOPCRouteUpdate,
    15  		Delete: resourceOPCRouteDelete,
    16  		Importer: &schema.ResourceImporter{
    17  			State: schema.ImportStatePassthrough,
    18  		},
    19  
    20  		Schema: map[string]*schema.Schema{
    21  			"name": {
    22  				Type:     schema.TypeString,
    23  				Required: true,
    24  			},
    25  
    26  			"description": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  			},
    30  
    31  			"admin_distance": {
    32  				Type:         schema.TypeInt,
    33  				Optional:     true,
    34  				ValidateFunc: validateAdminDistance,
    35  			},
    36  
    37  			"ip_address_prefix": {
    38  				Type:         schema.TypeString,
    39  				Required:     true,
    40  				ValidateFunc: validateIPPrefixCIDR,
    41  			},
    42  
    43  			"next_hop_vnic_set": {
    44  				Type:     schema.TypeString,
    45  				Required: true,
    46  			},
    47  
    48  			"tags": tagsOptionalSchema(),
    49  		},
    50  	}
    51  }
    52  
    53  func resourceOPCRouteCreate(d *schema.ResourceData, meta interface{}) error {
    54  	client := meta.(*compute.Client).Routes()
    55  
    56  	// Get Required attributes
    57  	name := d.Get("name").(string)
    58  	ipPrefix := d.Get("ip_address_prefix").(string)
    59  	nextHop := d.Get("next_hop_vnic_set").(string)
    60  
    61  	// Start populating input struct
    62  	input := &compute.CreateRouteInput{
    63  		Name:            name,
    64  		IPAddressPrefix: ipPrefix,
    65  		NextHopVnicSet:  nextHop,
    66  	}
    67  
    68  	// Get Optional Attributes
    69  	desc, descOk := d.GetOk("description")
    70  	if descOk {
    71  		input.Description = desc.(string)
    72  	}
    73  
    74  	dist, distOk := d.GetOk("admin_distance")
    75  	if distOk {
    76  		input.AdminDistance = dist.(int)
    77  	}
    78  
    79  	tags := getStringList(d, "tags")
    80  	if len(tags) != 0 {
    81  		input.Tags = tags
    82  	}
    83  
    84  	// Create Route
    85  	info, err := client.CreateRoute(input)
    86  	if err != nil {
    87  		return fmt.Errorf("Error creating route '%s': %v", name, err)
    88  	}
    89  
    90  	d.SetId(info.Name)
    91  
    92  	return resourceOPCRouteRead(d, meta)
    93  }
    94  
    95  func resourceOPCRouteRead(d *schema.ResourceData, meta interface{}) error {
    96  	client := meta.(*compute.Client).Routes()
    97  
    98  	name := d.Id()
    99  	input := &compute.GetRouteInput{
   100  		Name: name,
   101  	}
   102  
   103  	res, err := client.GetRoute(input)
   104  	if err != nil {
   105  		if compute.WasNotFoundError(err) {
   106  			d.SetId("")
   107  			return nil
   108  		}
   109  		return fmt.Errorf("Error reading route '%s': %v", name, err)
   110  	}
   111  
   112  	d.Set("name", res.Name)
   113  	d.Set("admin_distance", res.AdminDistance)
   114  	d.Set("ip_address_prefix", res.IPAddressPrefix)
   115  	d.Set("next_hop_vnic_set", res.NextHopVnicSet)
   116  	d.Set("description", res.Description)
   117  	if err := setStringList(d, "tags", res.Tags); err != nil {
   118  		return err
   119  	}
   120  	return nil
   121  }
   122  
   123  func resourceOPCRouteUpdate(d *schema.ResourceData, meta interface{}) error {
   124  	client := meta.(*compute.Client).Routes()
   125  
   126  	// Get Required attributes
   127  	name := d.Get("name").(string)
   128  	ipPrefix := d.Get("ip_address_prefix").(string)
   129  	nextHop := d.Get("next_hop_vnic_set").(string)
   130  
   131  	// Start populating input struct
   132  	input := &compute.UpdateRouteInput{
   133  		Name:            name,
   134  		IPAddressPrefix: ipPrefix,
   135  		NextHopVnicSet:  nextHop,
   136  	}
   137  
   138  	// Get Optional Attributes
   139  	desc, descOk := d.GetOk("description")
   140  	if descOk {
   141  		input.Description = desc.(string)
   142  	}
   143  
   144  	dist, distOk := d.GetOk("admin_distance")
   145  	if distOk {
   146  		input.AdminDistance = dist.(int)
   147  	}
   148  
   149  	tags := getStringList(d, "tags")
   150  	if len(tags) != 0 {
   151  		input.Tags = tags
   152  	}
   153  
   154  	// Create Route
   155  	info, err := client.UpdateRoute(input)
   156  	if err != nil {
   157  		return fmt.Errorf("Error creating route '%s': %v", name, err)
   158  	}
   159  
   160  	d.SetId(info.Name)
   161  
   162  	return resourceOPCRouteRead(d, meta)
   163  }
   164  
   165  func resourceOPCRouteDelete(d *schema.ResourceData, meta interface{}) error {
   166  	client := meta.(*compute.Client).Routes()
   167  
   168  	name := d.Id()
   169  	input := &compute.DeleteRouteInput{
   170  		Name: name,
   171  	}
   172  	if err := client.DeleteRoute(input); err != nil {
   173  		return fmt.Errorf("Error deleting route '%s': %v", name, err)
   174  	}
   175  	return nil
   176  }