github.com/daveadams/terraform@v0.6.4-0.20160830094355-13ce74975936/builtin/providers/digitalocean/resource_digitalocean_ssh_key.go (about) 1 package digitalocean 2 3 import ( 4 "fmt" 5 "log" 6 "strconv" 7 "strings" 8 9 "github.com/digitalocean/godo" 10 "github.com/hashicorp/terraform/helper/schema" 11 ) 12 13 func resourceDigitalOceanSSHKey() *schema.Resource { 14 return &schema.Resource{ 15 Create: resourceDigitalOceanSSHKeyCreate, 16 Read: resourceDigitalOceanSSHKeyRead, 17 Update: resourceDigitalOceanSSHKeyUpdate, 18 Delete: resourceDigitalOceanSSHKeyDelete, 19 Importer: &schema.ResourceImporter{ 20 State: schema.ImportStatePassthrough, 21 }, 22 23 Schema: map[string]*schema.Schema{ 24 "id": &schema.Schema{ 25 Type: schema.TypeString, 26 Computed: true, 27 }, 28 29 "name": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 }, 33 34 "public_key": &schema.Schema{ 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 StateFunc: func(val interface{}) string { 39 return strings.TrimSpace(val.(string)) 40 }, 41 }, 42 43 "fingerprint": &schema.Schema{ 44 Type: schema.TypeString, 45 Computed: true, 46 }, 47 }, 48 } 49 } 50 51 func resourceDigitalOceanSSHKeyCreate(d *schema.ResourceData, meta interface{}) error { 52 client := meta.(*godo.Client) 53 54 // Build up our creation options 55 opts := &godo.KeyCreateRequest{ 56 Name: d.Get("name").(string), 57 PublicKey: d.Get("public_key").(string), 58 } 59 60 log.Printf("[DEBUG] SSH Key create configuration: %#v", opts) 61 key, _, err := client.Keys.Create(opts) 62 if err != nil { 63 return fmt.Errorf("Error creating SSH Key: %s", err) 64 } 65 66 d.SetId(strconv.Itoa(key.ID)) 67 log.Printf("[INFO] SSH Key: %d", key.ID) 68 69 return resourceDigitalOceanSSHKeyRead(d, meta) 70 } 71 72 func resourceDigitalOceanSSHKeyRead(d *schema.ResourceData, meta interface{}) error { 73 client := meta.(*godo.Client) 74 75 id, err := strconv.Atoi(d.Id()) 76 if err != nil { 77 return fmt.Errorf("invalid SSH key id: %v", err) 78 } 79 80 key, resp, err := client.Keys.GetByID(id) 81 if err != nil { 82 // If the key is somehow already destroyed, mark as 83 // successfully gone 84 if resp != nil && resp.StatusCode == 404 { 85 d.SetId("") 86 return nil 87 } 88 89 return fmt.Errorf("Error retrieving SSH key: %s", err) 90 } 91 92 d.Set("name", key.Name) 93 d.Set("fingerprint", key.Fingerprint) 94 d.Set("public_key", key.PublicKey) 95 96 return nil 97 } 98 99 func resourceDigitalOceanSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error { 100 client := meta.(*godo.Client) 101 102 id, err := strconv.Atoi(d.Id()) 103 if err != nil { 104 return fmt.Errorf("invalid SSH key id: %v", err) 105 } 106 107 var newName string 108 if v, ok := d.GetOk("name"); ok { 109 newName = v.(string) 110 } 111 112 log.Printf("[DEBUG] SSH key update name: %#v", newName) 113 opts := &godo.KeyUpdateRequest{ 114 Name: newName, 115 } 116 _, _, err = client.Keys.UpdateByID(id, opts) 117 if err != nil { 118 return fmt.Errorf("Failed to update SSH key: %s", err) 119 } 120 121 return resourceDigitalOceanSSHKeyRead(d, meta) 122 } 123 124 func resourceDigitalOceanSSHKeyDelete(d *schema.ResourceData, meta interface{}) error { 125 client := meta.(*godo.Client) 126 127 id, err := strconv.Atoi(d.Id()) 128 if err != nil { 129 return fmt.Errorf("invalid SSH key id: %v", err) 130 } 131 132 log.Printf("[INFO] Deleting SSH key: %d", id) 133 _, err = client.Keys.DeleteByID(id) 134 if err != nil { 135 return fmt.Errorf("Error deleting SSH key: %s", err) 136 } 137 138 d.SetId("") 139 return nil 140 }