github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/softlayer/resource_softlayer_ssh_key.go (about) 1 package softlayer 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/hashicorp/terraform/helper/schema" 10 datatypes "github.com/maximilien/softlayer-go/data_types" 11 ) 12 13 func resourceSoftLayerSSHKey() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceSoftLayerSSHKeyCreate, 16 Read: resourceSoftLayerSSHKeyRead, 17 Update: resourceSoftLayerSSHKeyUpdate, 18 Delete: resourceSoftLayerSSHKeyDelete, 19 Exists: resourceSoftLayerSSHKeyExists, 20 21 Schema: map[string]*schema.Schema{ 22 "id": &schema.Schema{ 23 Type: schema.TypeInt, 24 Computed: true, 25 }, 26 27 "name": &schema.Schema{ 28 Type: schema.TypeString, 29 Required: true, 30 }, 31 32 "public_key": &schema.Schema{ 33 Type: schema.TypeString, 34 Required: true, 35 ForceNew: true, 36 }, 37 38 "fingerprint": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 43 "notes": &schema.Schema{ 44 Type: schema.TypeString, 45 Optional: true, 46 Default: nil, 47 }, 48 }, 49 } 50 } 51 52 func resourceSoftLayerSSHKeyCreate(d *schema.ResourceData, meta interface{}) error { 53 client := meta.(*Client).sshKeyService 54 55 // Build up our creation options 56 opts := datatypes.SoftLayer_Security_Ssh_Key{ 57 Label: d.Get("name").(string), 58 Key: d.Get("public_key").(string), 59 } 60 61 if notes, ok := d.GetOk("notes"); ok { 62 opts.Notes = notes.(string) 63 } 64 65 res, err := client.CreateObject(opts) 66 if err != nil { 67 return fmt.Errorf("Error creating SSH Key: %s", err) 68 } 69 70 d.SetId(strconv.Itoa(res.Id)) 71 log.Printf("[INFO] SSH Key: %d", res.Id) 72 73 return resourceSoftLayerSSHKeyRead(d, meta) 74 } 75 76 func resourceSoftLayerSSHKeyRead(d *schema.ResourceData, meta interface{}) error { 77 client := meta.(*Client).sshKeyService 78 79 keyId, _ := strconv.Atoi(d.Id()) 80 81 key, err := client.GetObject(keyId) 82 if err != nil { 83 // If the key is somehow already destroyed, mark as 84 // succesfully gone 85 if strings.Contains(err.Error(), "404 Not Found") { 86 d.SetId("") 87 return nil 88 } 89 90 return fmt.Errorf("Error retrieving SSH key: %s", err) 91 } 92 93 d.Set("id", key.Id) 94 d.Set("name", key.Label) 95 d.Set("public_key", strings.TrimSpace(key.Key)) 96 d.Set("fingerprint", key.Fingerprint) 97 d.Set("notes", key.Notes) 98 99 return nil 100 } 101 102 func resourceSoftLayerSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error { 103 client := meta.(*Client).sshKeyService 104 105 keyId, _ := strconv.Atoi(d.Id()) 106 107 key, err := client.GetObject(keyId) 108 if err != nil { 109 return fmt.Errorf("Error retrieving SSH key: %s", err) 110 } 111 112 if d.HasChange("name") { 113 key.Label = d.Get("name").(string) 114 } 115 116 if d.HasChange("notes") { 117 key.Notes = d.Get("notes").(string) 118 } 119 120 _, err = client.EditObject(keyId, key) 121 if err != nil { 122 return fmt.Errorf("Error editing SSH key: %s", err) 123 } 124 return nil 125 } 126 127 func resourceSoftLayerSSHKeyDelete(d *schema.ResourceData, meta interface{}) error { 128 client := meta.(*Client).sshKeyService 129 130 id, err := strconv.Atoi(d.Id()) 131 if err != nil { 132 return fmt.Errorf("Error deleting SSH Key: %s", err) 133 } 134 135 log.Printf("[INFO] Deleting SSH key: %d", id) 136 _, err = client.DeleteObject(id) 137 if err != nil { 138 return fmt.Errorf("Error deleting SSH key: %s", err) 139 } 140 141 d.SetId("") 142 return nil 143 } 144 145 func resourceSoftLayerSSHKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) { 146 client := meta.(*Client).sshKeyService 147 148 if client == nil { 149 return false, fmt.Errorf("The client was nil.") 150 } 151 152 keyId, err := strconv.Atoi(d.Id()) 153 if err != nil { 154 return false, fmt.Errorf("Not a valid ID, must be an integer: %s", err) 155 } 156 157 result, err := client.GetObject(keyId) 158 return result.Id == keyId && err == nil, nil 159 }