github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_ssh_key.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 resourceOPCSSHKey() *schema.Resource {
    11  	return &schema.Resource{
    12  		Create: resourceOPCSSHKeyCreate,
    13  		Read:   resourceOPCSSHKeyRead,
    14  		Update: resourceOPCSSHKeyUpdate,
    15  		Delete: resourceOPCSSHKeyDelete,
    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  
    27  			"key": {
    28  				Type:     schema.TypeString,
    29  				Required: true,
    30  			},
    31  
    32  			"enabled": {
    33  				Type:     schema.TypeBool,
    34  				Optional: true,
    35  				Default:  true,
    36  			},
    37  		},
    38  	}
    39  }
    40  
    41  func resourceOPCSSHKeyCreate(d *schema.ResourceData, meta interface{}) error {
    42  	client := meta.(*compute.Client).SSHKeys()
    43  
    44  	name := d.Get("name").(string)
    45  	key := d.Get("key").(string)
    46  	enabled := d.Get("enabled").(bool)
    47  
    48  	input := compute.CreateSSHKeyInput{
    49  		Name:    name,
    50  		Key:     key,
    51  		Enabled: enabled,
    52  	}
    53  	info, err := client.CreateSSHKey(&input)
    54  	if err != nil {
    55  		return fmt.Errorf("Error creating ssh key %s: %s", name, err)
    56  	}
    57  
    58  	d.SetId(info.Name)
    59  
    60  	return resourceOPCSSHKeyRead(d, meta)
    61  }
    62  
    63  func resourceOPCSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error {
    64  	client := meta.(*compute.Client).SSHKeys()
    65  
    66  	name := d.Get("name").(string)
    67  	key := d.Get("key").(string)
    68  	enabled := d.Get("enabled").(bool)
    69  
    70  	input := compute.UpdateSSHKeyInput{
    71  		Name:    name,
    72  		Key:     key,
    73  		Enabled: enabled,
    74  	}
    75  	_, err := client.UpdateSSHKey(&input)
    76  	if err != nil {
    77  		return fmt.Errorf("Error updating ssh key %s: %s", name, err)
    78  	}
    79  
    80  	return resourceOPCSSHKeyRead(d, meta)
    81  }
    82  
    83  func resourceOPCSSHKeyRead(d *schema.ResourceData, meta interface{}) error {
    84  	client := meta.(*compute.Client).SSHKeys()
    85  	name := d.Id()
    86  
    87  	input := compute.GetSSHKeyInput{
    88  		Name: name,
    89  	}
    90  	result, err := client.GetSSHKey(&input)
    91  	if err != nil {
    92  		if compute.WasNotFoundError(err) {
    93  			d.SetId("")
    94  			return nil
    95  		}
    96  		return fmt.Errorf("Error reading ssh key %s: %s", name, err)
    97  	}
    98  
    99  	d.Set("name", result.Name)
   100  	d.Set("key", result.Key)
   101  	d.Set("enabled", result.Enabled)
   102  
   103  	return nil
   104  }
   105  
   106  func resourceOPCSSHKeyDelete(d *schema.ResourceData, meta interface{}) error {
   107  	client := meta.(*compute.Client).SSHKeys()
   108  	name := d.Id()
   109  
   110  	input := compute.DeleteSSHKeyInput{
   111  		Name: name,
   112  	}
   113  	if err := client.DeleteSSHKey(&input); err != nil {
   114  		return fmt.Errorf("Error deleting ssh key %s: %s", name, err)
   115  	}
   116  
   117  	return nil
   118  }