github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_vnic_set.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 resourceOPCVNICSet() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCVNICSetCreate,
    13  		Read:   resourceOPCVNICSetRead,
    14  		Update: resourceOPCVNICSetUpdate,
    15  		Delete: resourceOPCVNICSetDelete,
    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  			},
    30  			"applied_acls": {
    31  				Type:     schema.TypeList,
    32  				Optional: true,
    33  				Elem:     &schema.Schema{Type: schema.TypeString},
    34  			},
    35  			"virtual_nics": {
    36  				Type:     schema.TypeList,
    37  				Optional: true,
    38  				Elem:     &schema.Schema{Type: schema.TypeString},
    39  			},
    40  			"tags": {
    41  				Type:     schema.TypeList,
    42  				Optional: true,
    43  				Elem:     &schema.Schema{Type: schema.TypeString},
    44  			},
    45  		},
    46  	}
    47  }
    48  
    49  func resourceOPCVNICSetCreate(d *schema.ResourceData, meta interface{}) error {
    50  	client := meta.(*compute.Client).VirtNICSets()
    51  
    52  	name := d.Get("name").(string)
    53  	desc, descOk := d.GetOk("description")
    54  
    55  	input := &compute.CreateVirtualNICSetInput{
    56  		Name: name,
    57  	}
    58  
    59  	if descOk {
    60  		input.Description = desc.(string)
    61  	}
    62  
    63  	acls := getStringList(d, "applied_acls")
    64  	if len(acls) != 0 {
    65  		input.AppliedACLs = acls
    66  	}
    67  
    68  	vnics := getStringList(d, "virtual_nics")
    69  	if len(vnics) != 0 {
    70  		input.VirtualNICs = vnics
    71  	}
    72  
    73  	tags := getStringList(d, "tags")
    74  	if len(tags) != 0 {
    75  		input.Tags = tags
    76  	}
    77  
    78  	vnicSet, err := client.CreateVirtualNICSet(input)
    79  	if err != nil {
    80  		return fmt.Errorf("Error creating Virtual NIC Set: %s", err)
    81  	}
    82  
    83  	d.SetId(vnicSet.Name)
    84  
    85  	return resourceOPCVNICSetRead(d, meta)
    86  }
    87  
    88  func resourceOPCVNICSetRead(d *schema.ResourceData, meta interface{}) error {
    89  	client := meta.(*compute.Client).VirtNICSets()
    90  
    91  	name := d.Id()
    92  	input := &compute.GetVirtualNICSetInput{
    93  		Name: name,
    94  	}
    95  
    96  	res, err := client.GetVirtualNICSet(input)
    97  	if err != nil {
    98  		if compute.WasNotFoundError(err) {
    99  			d.SetId("")
   100  			return nil
   101  		}
   102  		return fmt.Errorf("Error reading Virtual NIC Set '%s': %s", name, err)
   103  	}
   104  
   105  	d.Set("name", res.Name)
   106  	d.Set("description", res.Description)
   107  	if err := setStringList(d, "applied_acls", res.AppliedACLs); err != nil {
   108  		return err
   109  	}
   110  	if err := setStringList(d, "virtual_nics", res.VirtualNICs); err != nil {
   111  		return err
   112  	}
   113  	if err := setStringList(d, "tags", res.Tags); err != nil {
   114  		return err
   115  	}
   116  	return nil
   117  }
   118  
   119  func resourceOPCVNICSetUpdate(d *schema.ResourceData, meta interface{}) error {
   120  	client := meta.(*compute.Client).VirtNICSets()
   121  
   122  	name := d.Id()
   123  	desc, descOk := d.GetOk("description")
   124  
   125  	input := &compute.UpdateVirtualNICSetInput{
   126  		Name: name,
   127  	}
   128  
   129  	if descOk {
   130  		input.Description = desc.(string)
   131  	}
   132  
   133  	acls := getStringList(d, "applied_acls")
   134  	if len(acls) != 0 {
   135  		input.AppliedACLs = acls
   136  	}
   137  
   138  	vnics := getStringList(d, "virtual_nics")
   139  	if len(vnics) != 0 {
   140  		input.VirtualNICs = vnics
   141  	}
   142  
   143  	tags := getStringList(d, "tags")
   144  	if len(tags) != 0 {
   145  		input.Tags = tags
   146  	}
   147  
   148  	info, err := client.UpdateVirtualNICSet(input)
   149  	if err != nil {
   150  		return fmt.Errorf("Error updating Virtual NIC Set: %s", err)
   151  	}
   152  
   153  	d.SetId(info.Name)
   154  	return resourceOPCVNICSetRead(d, meta)
   155  }
   156  
   157  func resourceOPCVNICSetDelete(d *schema.ResourceData, meta interface{}) error {
   158  	client := meta.(*compute.Client).VirtNICSets()
   159  
   160  	name := d.Id()
   161  	input := &compute.DeleteVirtualNICSetInput{
   162  		Name: name,
   163  	}
   164  
   165  	if err := client.DeleteVirtualNICSet(input); err != nil {
   166  		return fmt.Errorf("Error deleting Virtual NIC Set '%s': %s", name, err)
   167  	}
   168  	return nil
   169  }