github.com/pmcatominey/terraform@v0.7.0-rc2.0.20160708105029-1401a52a5cc5/builtin/providers/cloudstack/resource_cloudstack_ssh_keypair.go (about) 1 package cloudstack 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/hashicorp/terraform/helper/pathorcontents" 9 "github.com/hashicorp/terraform/helper/schema" 10 "github.com/xanzy/go-cloudstack/cloudstack" 11 ) 12 13 func resourceCloudStackSSHKeyPair() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceCloudStackSSHKeyPairCreate, 16 Read: resourceCloudStackSSHKeyPairRead, 17 Delete: resourceCloudStackSSHKeyPairDelete, 18 19 Schema: map[string]*schema.Schema{ 20 "name": &schema.Schema{ 21 Type: schema.TypeString, 22 Required: true, 23 ForceNew: true, 24 }, 25 26 "public_key": &schema.Schema{ 27 Type: schema.TypeString, 28 Optional: true, 29 ForceNew: true, 30 }, 31 32 "project": &schema.Schema{ 33 Type: schema.TypeString, 34 Optional: true, 35 ForceNew: true, 36 }, 37 38 "private_key": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 43 "fingerprint": &schema.Schema{ 44 Type: schema.TypeString, 45 Computed: true, 46 }, 47 }, 48 } 49 } 50 51 func resourceCloudStackSSHKeyPairCreate(d *schema.ResourceData, meta interface{}) error { 52 cs := meta.(*cloudstack.CloudStackClient) 53 54 name := d.Get("name").(string) 55 publicKey := d.Get("public_key").(string) 56 57 if publicKey != "" { 58 // Register supplied key 59 key, _, err := pathorcontents.Read(publicKey) 60 if err != nil { 61 return fmt.Errorf("Error reading the public key: %v", err) 62 } 63 64 p := cs.SSH.NewRegisterSSHKeyPairParams(name, string(key)) 65 66 // If there is a project supplied, we retrieve and set the project id 67 if err := setProjectid(p, cs, d); err != nil { 68 return err 69 } 70 71 _, err = cs.SSH.RegisterSSHKeyPair(p) 72 if err != nil { 73 return err 74 } 75 } else { 76 // No key supplied, must create one and return the private key 77 p := cs.SSH.NewCreateSSHKeyPairParams(name) 78 79 // If there is a project supplied, we retrieve and set the project id 80 if err := setProjectid(p, cs, d); err != nil { 81 return err 82 } 83 84 r, err := cs.SSH.CreateSSHKeyPair(p) 85 if err != nil { 86 return err 87 } 88 d.Set("private_key", r.Privatekey) 89 } 90 91 log.Printf("[DEBUG] Key pair successfully generated at Cloudstack") 92 d.SetId(name) 93 94 return resourceCloudStackSSHKeyPairRead(d, meta) 95 } 96 97 func resourceCloudStackSSHKeyPairRead(d *schema.ResourceData, meta interface{}) error { 98 cs := meta.(*cloudstack.CloudStackClient) 99 100 log.Printf("[DEBUG] looking for key pair with name %s", d.Id()) 101 102 p := cs.SSH.NewListSSHKeyPairsParams() 103 p.SetName(d.Id()) 104 105 // If there is a project supplied, we retrieve and set the project id 106 if err := setProjectid(p, cs, d); err != nil { 107 return err 108 } 109 110 r, err := cs.SSH.ListSSHKeyPairs(p) 111 if err != nil { 112 return err 113 } 114 if r.Count == 0 { 115 log.Printf("[DEBUG] Key pair %s does not exist", d.Id()) 116 d.SetId("") 117 return nil 118 } 119 120 //SSHKeyPair name is unique in a cloudstack account so dont need to check for multiple 121 d.Set("name", r.SSHKeyPairs[0].Name) 122 d.Set("fingerprint", r.SSHKeyPairs[0].Fingerprint) 123 124 return nil 125 } 126 127 func resourceCloudStackSSHKeyPairDelete(d *schema.ResourceData, meta interface{}) error { 128 cs := meta.(*cloudstack.CloudStackClient) 129 130 // Create a new parameter struct 131 p := cs.SSH.NewDeleteSSHKeyPairParams(d.Id()) 132 133 // If there is a project supplied, we retrieve and set the project id 134 if err := setProjectid(p, cs, d); err != nil { 135 return err 136 } 137 138 // Remove the SSH Keypair 139 _, err := cs.SSH.DeleteSSHKeyPair(p) 140 if err != nil { 141 // This is a very poor way to be told the ID does no longer exist :( 142 if strings.Contains(err.Error(), fmt.Sprintf( 143 "A key pair with name '%s' does not exist for account", d.Id())) { 144 return nil 145 } 146 147 return fmt.Errorf("Error deleting key pair: %s", err) 148 } 149 150 return nil 151 }