github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_acl.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 resourceOPCACL() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceOPCACLCreate,
    14  		Read:   resourceOPCACLRead,
    15  		Update: resourceOPCACLUpdate,
    16  		Delete: resourceOPCACLDelete,
    17  		Importer: &schema.ResourceImporter{
    18  			State: schema.ImportStatePassthrough,
    19  		},
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"name": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"description": {
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  			},
    31  			"enabled": {
    32  				Type:     schema.TypeBool,
    33  				Optional: true,
    34  				Default:  true,
    35  			},
    36  			"tags": {
    37  				Type:     schema.TypeList,
    38  				Optional: true,
    39  				Elem:     &schema.Schema{Type: schema.TypeString},
    40  				ForceNew: true,
    41  			},
    42  			"uri": {
    43  				Type:     schema.TypeString,
    44  				Computed: true,
    45  			},
    46  		},
    47  	}
    48  }
    49  
    50  func resourceOPCACLCreate(d *schema.ResourceData, meta interface{}) error {
    51  	log.Printf("[DEBUG] Resource state: %#v", d.State())
    52  
    53  	log.Print("[DEBUG] Creating acl")
    54  
    55  	client := meta.(*compute.Client).ACLs()
    56  	input := compute.CreateACLInput{
    57  		Name:    d.Get("name").(string),
    58  		Enabled: d.Get("enabled").(bool),
    59  	}
    60  
    61  	tags := getStringList(d, "tags")
    62  	if len(tags) != 0 {
    63  		input.Tags = tags
    64  	}
    65  
    66  	if description, ok := d.GetOk("description"); ok {
    67  		input.Description = description.(string)
    68  	}
    69  
    70  	info, err := client.CreateACL(&input)
    71  	if err != nil {
    72  		return fmt.Errorf("Error creating ACL: %s", err)
    73  	}
    74  
    75  	d.SetId(info.Name)
    76  	return resourceOPCACLRead(d, meta)
    77  }
    78  
    79  func resourceOPCACLRead(d *schema.ResourceData, meta interface{}) error {
    80  	log.Printf("[DEBUG] Resource state: %#v", d.State())
    81  	client := meta.(*compute.Client).ACLs()
    82  
    83  	log.Printf("[DEBUG] Reading state of ip reservation %s", d.Id())
    84  	getInput := compute.GetACLInput{
    85  		Name: d.Id(),
    86  	}
    87  	result, err := client.GetACL(&getInput)
    88  	if err != nil {
    89  		// ACL does not exist
    90  		if compute.WasNotFoundError(err) {
    91  			d.SetId("")
    92  			return nil
    93  		}
    94  		return fmt.Errorf("Error reading acl %s: %s", d.Id(), err)
    95  	}
    96  
    97  	log.Printf("[DEBUG] Read state of acl %s: %#v", d.Id(), result)
    98  	d.Set("name", result.Name)
    99  	d.Set("enabled", result.Enabled)
   100  	d.Set("description", result.Description)
   101  	d.Set("uri", result.URI)
   102  	if err := setStringList(d, "tags", result.Tags); err != nil {
   103  		return err
   104  	}
   105  	return nil
   106  }
   107  
   108  func resourceOPCACLUpdate(d *schema.ResourceData, meta interface{}) error {
   109  	log.Printf("[DEBUG] Resource state: %#v", d.State())
   110  
   111  	log.Print("[DEBUG] Updating acl")
   112  
   113  	client := meta.(*compute.Client).ACLs()
   114  	input := compute.UpdateACLInput{
   115  		Name:    d.Get("name").(string),
   116  		Enabled: d.Get("enabled").(bool),
   117  	}
   118  
   119  	tags := getStringList(d, "tags")
   120  	if len(tags) != 0 {
   121  		input.Tags = tags
   122  	}
   123  
   124  	if description, ok := d.GetOk("description"); ok {
   125  		input.Description = description.(string)
   126  	}
   127  
   128  	info, err := client.UpdateACL(&input)
   129  	if err != nil {
   130  		return fmt.Errorf("Error updating ACL: %s", err)
   131  	}
   132  
   133  	d.SetId(info.Name)
   134  	return resourceOPCACLRead(d, meta)
   135  }
   136  
   137  func resourceOPCACLDelete(d *schema.ResourceData, meta interface{}) error {
   138  	log.Printf("[DEBUG] Resource state: %#v", d.State())
   139  	client := meta.(*compute.Client).ACLs()
   140  	name := d.Id()
   141  
   142  	log.Printf("[DEBUG] Deleting ACL: %v", name)
   143  
   144  	input := compute.DeleteACLInput{
   145  		Name: name,
   146  	}
   147  	if err := client.DeleteACL(&input); err != nil {
   148  		return fmt.Errorf("Error deleting ACL")
   149  	}
   150  	return nil
   151  }