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

     1  package opc
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/go-oracle-terraform/compute"
     8  	"github.com/hashicorp/terraform/helper/schema"
     9  )
    10  
    11  func resourceOPCIPNetworkExchange() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceOPCIPNetworkExchangeCreate,
    14  		Read:   resourceOPCIPNetworkExchangeRead,
    15  		Delete: resourceOPCIPNetworkExchangeDelete,
    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  				ForceNew: true,
    25  			},
    26  			"description": {
    27  				Type:     schema.TypeString,
    28  				Optional: true,
    29  				ForceNew: true,
    30  			},
    31  			"tags": tagsForceNewSchema(),
    32  			"uri": {
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  		},
    37  	}
    38  }
    39  
    40  func resourceOPCIPNetworkExchangeCreate(d *schema.ResourceData, meta interface{}) error {
    41  	client := meta.(*compute.Client).IPNetworkExchanges()
    42  	input := compute.CreateIPNetworkExchangeInput{
    43  		Name: d.Get("name").(string),
    44  	}
    45  
    46  	log.Printf("[DEBUG] Creating ip network exchange '%s'", input.Name)
    47  	tags := getStringList(d, "tags")
    48  	if len(tags) != 0 {
    49  		input.Tags = tags
    50  	}
    51  
    52  	if description, ok := d.GetOk("description"); ok {
    53  		input.Description = description.(string)
    54  	}
    55  
    56  	info, err := client.CreateIPNetworkExchange(&input)
    57  	if err != nil {
    58  		return fmt.Errorf("Error creating IP Network Exchange: %s", err)
    59  	}
    60  
    61  	d.SetId(info.Name)
    62  	return resourceOPCIPNetworkExchangeRead(d, meta)
    63  }
    64  
    65  func resourceOPCIPNetworkExchangeRead(d *schema.ResourceData, meta interface{}) error {
    66  	client := meta.(*compute.Client).IPNetworkExchanges()
    67  
    68  	log.Printf("[DEBUG] Reading state of IP Network Exchange %s", d.Id())
    69  	getInput := compute.GetIPNetworkExchangeInput{
    70  		Name: d.Id(),
    71  	}
    72  	result, err := client.GetIPNetworkExchange(&getInput)
    73  	if err != nil {
    74  		// IP NetworkExchange does not exist
    75  		if compute.WasNotFoundError(err) {
    76  			d.SetId("")
    77  			return nil
    78  		}
    79  		return fmt.Errorf("Error reading ip network exchange %s: %s", d.Id(), err)
    80  	}
    81  
    82  	d.Set("name", result.Name)
    83  	d.Set("description", result.Description)
    84  	d.Set("uri", result.Uri)
    85  
    86  	if err := setStringList(d, "tags", result.Tags); err != nil {
    87  		return err
    88  	}
    89  
    90  	return nil
    91  }
    92  
    93  func resourceOPCIPNetworkExchangeDelete(d *schema.ResourceData, meta interface{}) error {
    94  	client := meta.(*compute.Client).IPNetworkExchanges()
    95  	name := d.Id()
    96  
    97  	log.Printf("[DEBUG] Deleting IP Network Exchange '%s'", name)
    98  	input := compute.DeleteIPNetworkExchangeInput{
    99  		Name: name,
   100  	}
   101  	if err := client.DeleteIPNetworkExchange(&input); err != nil {
   102  		return fmt.Errorf("Error deleting IP Network Exchange '%s': %+v", name, err)
   103  	}
   104  	return nil
   105  }