github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_security_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 resourceOPCSecurityAssociation() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCSecurityAssociationCreate,
    13  		Read:   resourceOPCSecurityAssociationRead,
    14  		Delete: resourceOPCSecurityAssociationDelete,
    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  				ForceNew: true,
    25  			},
    26  
    27  			"vcable": {
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  				ForceNew: true,
    31  			},
    32  
    33  			"seclist": {
    34  				Type:     schema.TypeString,
    35  				Required: true,
    36  				ForceNew: true,
    37  			},
    38  		},
    39  	}
    40  }
    41  
    42  func resourceOPCSecurityAssociationCreate(d *schema.ResourceData, meta interface{}) error {
    43  	client := meta.(*compute.Client).SecurityAssociations()
    44  
    45  	name := d.Get("name").(string)
    46  	vcable := d.Get("vcable").(string)
    47  	seclist := d.Get("seclist").(string)
    48  
    49  	input := compute.CreateSecurityAssociationInput{
    50  		Name:    name,
    51  		SecList: seclist,
    52  		VCable:  vcable,
    53  	}
    54  	info, err := client.CreateSecurityAssociation(&input)
    55  	if err != nil {
    56  		return fmt.Errorf("Error creating security association between vcable %s and security list %s: %s", vcable, seclist, err)
    57  	}
    58  
    59  	d.SetId(info.Name)
    60  
    61  	return resourceOPCSecurityAssociationRead(d, meta)
    62  }
    63  
    64  func resourceOPCSecurityAssociationRead(d *schema.ResourceData, meta interface{}) error {
    65  	client := meta.(*compute.Client).SecurityAssociations()
    66  
    67  	name := d.Id()
    68  
    69  	input := compute.GetSecurityAssociationInput{
    70  		Name: name,
    71  	}
    72  	result, err := client.GetSecurityAssociation(&input)
    73  	if err != nil {
    74  		// Security Association does not exist
    75  		if compute.WasNotFoundError(err) {
    76  			d.SetId("")
    77  			return nil
    78  		}
    79  		return fmt.Errorf("Error reading security association %s: %s", name, err)
    80  	}
    81  
    82  	d.Set("name", result.Name)
    83  	d.Set("seclist", result.SecList)
    84  	d.Set("vcable", result.VCable)
    85  
    86  	return nil
    87  }
    88  
    89  func resourceOPCSecurityAssociationDelete(d *schema.ResourceData, meta interface{}) error {
    90  	client := meta.(*compute.Client).SecurityAssociations()
    91  
    92  	name := d.Id()
    93  
    94  	input := compute.DeleteSecurityAssociationInput{
    95  		Name: name,
    96  	}
    97  	if err := client.DeleteSecurityAssociation(&input); err != nil {
    98  		return fmt.Errorf("Error deleting Security Association '%s': %v", name, err)
    99  	}
   100  	return nil
   101  }