github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/packet/resource_packet_ssh_key.go (about) 1 package packet 2 3 import ( 4 "github.com/hashicorp/terraform/helper/schema" 5 "github.com/packethost/packngo" 6 ) 7 8 func resourcePacketSSHKey() *schema.Resource { 9 return &schema.Resource{ 10 Create: resourcePacketSSHKeyCreate, 11 Read: resourcePacketSSHKeyRead, 12 Update: resourcePacketSSHKeyUpdate, 13 Delete: resourcePacketSSHKeyDelete, 14 15 Schema: map[string]*schema.Schema{ 16 "id": &schema.Schema{ 17 Type: schema.TypeString, 18 Computed: true, 19 }, 20 21 "name": &schema.Schema{ 22 Type: schema.TypeString, 23 Required: true, 24 }, 25 26 "public_key": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 ForceNew: true, 30 }, 31 32 "fingerprint": &schema.Schema{ 33 Type: schema.TypeString, 34 Computed: true, 35 }, 36 37 "created": &schema.Schema{ 38 Type: schema.TypeString, 39 Computed: true, 40 }, 41 42 "updated": &schema.Schema{ 43 Type: schema.TypeString, 44 Computed: true, 45 }, 46 }, 47 } 48 } 49 50 func resourcePacketSSHKeyCreate(d *schema.ResourceData, meta interface{}) error { 51 client := meta.(*packngo.Client) 52 53 createRequest := &packngo.SSHKeyCreateRequest{ 54 Label: d.Get("name").(string), 55 Key: d.Get("public_key").(string), 56 } 57 58 key, _, err := client.SSHKeys.Create(createRequest) 59 if err != nil { 60 return friendlyError(err) 61 } 62 63 d.SetId(key.ID) 64 65 return resourcePacketSSHKeyRead(d, meta) 66 } 67 68 func resourcePacketSSHKeyRead(d *schema.ResourceData, meta interface{}) error { 69 client := meta.(*packngo.Client) 70 71 key, _, err := client.SSHKeys.Get(d.Id()) 72 if err != nil { 73 err = friendlyError(err) 74 75 // If the key is somehow already destroyed, mark as 76 // succesfully gone 77 if isNotFound(err) { 78 d.SetId("") 79 return nil 80 } 81 82 return err 83 } 84 85 d.Set("id", key.ID) 86 d.Set("name", key.Label) 87 d.Set("public_key", key.Key) 88 d.Set("fingerprint", key.FingerPrint) 89 d.Set("created", key.Created) 90 d.Set("updated", key.Updated) 91 92 return nil 93 } 94 95 func resourcePacketSSHKeyUpdate(d *schema.ResourceData, meta interface{}) error { 96 client := meta.(*packngo.Client) 97 98 updateRequest := &packngo.SSHKeyUpdateRequest{ 99 ID: d.Get("id").(string), 100 Label: d.Get("name").(string), 101 Key: d.Get("public_key").(string), 102 } 103 104 _, _, err := client.SSHKeys.Update(updateRequest) 105 if err != nil { 106 return friendlyError(err) 107 } 108 109 return resourcePacketSSHKeyRead(d, meta) 110 } 111 112 func resourcePacketSSHKeyDelete(d *schema.ResourceData, meta interface{}) error { 113 client := meta.(*packngo.Client) 114 115 _, err := client.SSHKeys.Delete(d.Id()) 116 if err != nil { 117 return friendlyError(err) 118 } 119 120 d.SetId("") 121 return nil 122 }