github.com/nathanielks/terraform@v0.6.1-0.20170509030759-13e1a62319dc/builtin/providers/opc/resource_ip_association.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 resourceOPCIPAssociation() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCIPAssociationCreate,
    13  		Read:   resourceOPCIPAssociationRead,
    14  		Delete: resourceOPCIPAssociationDelete,
    15  		Importer: &schema.ResourceImporter{
    16  			State: schema.ImportStatePassthrough,
    17  		},
    18  
    19  		Schema: map[string]*schema.Schema{
    20  			"name": {
    21  				Type:     schema.TypeString,
    22  				Optional: true,
    23  				Computed: true,
    24  			},
    25  
    26  			"vcable": {
    27  				Type:     schema.TypeString,
    28  				Required: true,
    29  				ForceNew: true,
    30  			},
    31  
    32  			"parent_pool": {
    33  				Type:     schema.TypeString,
    34  				Required: true,
    35  				ForceNew: true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceOPCIPAssociationCreate(d *schema.ResourceData, meta interface{}) error {
    42  	vCable := d.Get("vcable").(string)
    43  	parentPool := d.Get("parent_pool").(string)
    44  
    45  	client := meta.(*compute.Client).IPAssociations()
    46  	input := compute.CreateIPAssociationInput{
    47  		ParentPool: parentPool,
    48  		VCable:     vCable,
    49  	}
    50  	info, err := client.CreateIPAssociation(&input)
    51  	if err != nil {
    52  		return fmt.Errorf("Error creating ip association between vcable %s and parent pool %s: %s", vCable, parentPool, err)
    53  	}
    54  
    55  	d.SetId(info.Name)
    56  
    57  	return resourceOPCIPAssociationRead(d, meta)
    58  }
    59  
    60  func resourceOPCIPAssociationRead(d *schema.ResourceData, meta interface{}) error {
    61  	client := meta.(*compute.Client).IPAssociations()
    62  
    63  	name := d.Id()
    64  	input := compute.GetIPAssociationInput{
    65  		Name: name,
    66  	}
    67  	result, err := client.GetIPAssociation(&input)
    68  	if err != nil {
    69  		// IP Association does not exist
    70  		if compute.WasNotFoundError(err) {
    71  			d.SetId("")
    72  			return nil
    73  		}
    74  		return fmt.Errorf("Error reading ip association '%s': %s", name, err)
    75  	}
    76  
    77  	d.Set("name", result.Name)
    78  	d.Set("parent_pool", result.ParentPool)
    79  	d.Set("vcable", result.VCable)
    80  
    81  	return nil
    82  }
    83  
    84  func resourceOPCIPAssociationDelete(d *schema.ResourceData, meta interface{}) error {
    85  	client := meta.(*compute.Client).IPAssociations()
    86  
    87  	name := d.Id()
    88  	input := compute.DeleteIPAssociationInput{
    89  		Name: name,
    90  	}
    91  	if err := client.DeleteIPAssociation(&input); err != nil {
    92  		return fmt.Errorf("Error deleting ip association '%s': %s", name, err)
    93  	}
    94  
    95  	return nil
    96  }