github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/openstack/resource_openstack_compute_keypair_v2.go (about)

     1  package openstack
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  	"github.com/rackspace/gophercloud/openstack/compute/v2/extensions/keypairs"
     9  )
    10  
    11  func resourceComputeKeypairV2() *schema.Resource {
    12  	return &schema.Resource{
    13  		Create: resourceComputeKeypairV2Create,
    14  		Read:   resourceComputeKeypairV2Read,
    15  		Delete: resourceComputeKeypairV2Delete,
    16  
    17  		Schema: map[string]*schema.Schema{
    18  			"region": &schema.Schema{
    19  				Type:        schema.TypeString,
    20  				Required:    true,
    21  				ForceNew:    true,
    22  				DefaultFunc: envDefaultFuncAllowMissing("OS_REGION_NAME"),
    23  			},
    24  			"name": &schema.Schema{
    25  				Type:     schema.TypeString,
    26  				Required: true,
    27  				ForceNew: true,
    28  			},
    29  			"public_key": &schema.Schema{
    30  				Type:     schema.TypeString,
    31  				Optional: true,
    32  				ForceNew: true,
    33  			},
    34  		},
    35  	}
    36  }
    37  
    38  func resourceComputeKeypairV2Create(d *schema.ResourceData, meta interface{}) error {
    39  	config := meta.(*Config)
    40  	computeClient, err := config.computeV2Client(d.Get("region").(string))
    41  	if err != nil {
    42  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    43  	}
    44  
    45  	createOpts := keypairs.CreateOpts{
    46  		Name:      d.Get("name").(string),
    47  		PublicKey: d.Get("public_key").(string),
    48  	}
    49  
    50  	log.Printf("[DEBUG] Create Options: %#v", createOpts)
    51  	kp, err := keypairs.Create(computeClient, createOpts).Extract()
    52  	if err != nil {
    53  		return fmt.Errorf("Error creating OpenStack keypair: %s", err)
    54  	}
    55  
    56  	d.SetId(kp.Name)
    57  
    58  	return resourceComputeKeypairV2Read(d, meta)
    59  }
    60  
    61  func resourceComputeKeypairV2Read(d *schema.ResourceData, meta interface{}) error {
    62  	config := meta.(*Config)
    63  	computeClient, err := config.computeV2Client(d.Get("region").(string))
    64  	if err != nil {
    65  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    66  	}
    67  
    68  	kp, err := keypairs.Get(computeClient, d.Id()).Extract()
    69  	if err != nil {
    70  		return CheckDeleted(d, err, "keypair")
    71  	}
    72  
    73  	d.Set("name", kp.Name)
    74  	d.Set("public_key", kp.PublicKey)
    75  
    76  	return nil
    77  }
    78  
    79  func resourceComputeKeypairV2Delete(d *schema.ResourceData, meta interface{}) error {
    80  	config := meta.(*Config)
    81  	computeClient, err := config.computeV2Client(d.Get("region").(string))
    82  	if err != nil {
    83  		return fmt.Errorf("Error creating OpenStack compute client: %s", err)
    84  	}
    85  
    86  	err = keypairs.Delete(computeClient, d.Id()).ExtractErr()
    87  	if err != nil {
    88  		return fmt.Errorf("Error deleting OpenStack keypair: %s", err)
    89  	}
    90  	d.SetId("")
    91  	return nil
    92  }