terraform/builtin/providers/triton/resource_key.go

119 lines
2.5 KiB
Go
Raw Normal View History

package triton
import (
"errors"
"strings"
"github.com/hashicorp/terraform/helper/schema"
"github.com/joyent/gosdc/cloudapi"
)
var (
// ErrNoKeyComment will be returned when the key name cannot be generated from
// the key comment and is not otherwise specified.
ErrNoKeyComment = errors.New("no key comment found to use as a name (and none specified)")
)
func resourceKey() *schema.Resource {
return &schema.Resource{
Create: resourceKeyCreate,
Exists: resourceKeyExists,
Read: resourceKeyRead,
Delete: resourceKeyDelete,
Importer: &schema.ResourceImporter{
State: resourceKeyImporter,
},
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Description: "name of this key (will be generated from the key comment, if not set and comment present)",
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"key": &schema.Schema{
Description: "content of public key from disk",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceKeyCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
if d.Get("name").(string) == "" {
parts := strings.SplitN(d.Get("key").(string), " ", 3)
if len(parts) == 3 {
d.Set("name", parts[2])
} else {
return ErrNoKeyComment
}
}
_, err := client.CreateKey(cloudapi.CreateKeyOpts{
Name: d.Get("name").(string),
Key: d.Get("key").(string),
})
if err != nil {
return err
}
provider/triton: Id trying to be used before being set ``` Before === RUN TestAccTritonKey_basic --- FAIL: TestAccTritonKey_basic (1.60s) testing.go:265: Step 0 error: Error applying: 1 error(s) occurred: * triton_key.test: failed to get key with name: caused by: failed unmarshaling/decoding the response body: [{"name":"phinze","fingerprint":"ee:5e:f8:bb:67:a6:60:21:36:5c:73:ce:c6:8b:4d:69","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC/zeXD4lIgk7Pm5r8qIM+U9qBHQE8a9aUXCsQd++SlA/hnBXeOLUpSm9vtuGFkknA1A4oQBvp07ngTFUw4y2ZD8GAIimmKywEetc/f2nnAkzxhbQ9C1OX5U1FbGUv+p8u60nB8ZZ/a4SgKA3IloVQ7HXjQmZKOsutE+IrT5FTviHrp6f7bwIhMkZXAuHWmkHlaIYub0TIgvbXujk6bqTGQq+G1yAeOLm2KSv6oU0v1A0aFYKBmcMVdj587fR0D91+DwsUBpmGeGyMBlA7x7eKFCS7Xme9sb9TC4VvkDg1xNIwZHo1ybkFih+l9DBMR7sbgfzeE6AwDxwY3/do7Uqwv phinze"},{"name":"jen20","fingerprint":"1b:bc:29:48:89:af:72:63:f0:83:b8:11:b6:4d:ff:3f","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQC6f/7h11O0R4nHY4T9Xd4z2GCN9dPVQYZFvHLbQjwoxIiXYkqy0RVAJlmB9vFuCyxMfS2Hvn9NVhDOQhlXnc1poRaSp98t0uAfFtKfqfGHt5vGrLrA9ACAkPTx5STEryp5/wV9aElcGje4sU2XeFIr0Rf6FPptxGLqXw44yq0X5DVcFg+axov123ErOIMSLNJ7TMdZCh3bqbpE7MnwyBEh69WUwjMr1OfW1AlX0O3RnIHa6HkLCoYnjj4pa3eDKeG0NjAOjbDSqxg8hLVb/+2YA7oaOhZzqhSLmKOZrnFgMSGv90rBbhHzCYTTHNP4NvSGTjPbYzUB/6I6M9l7QD5T James@jn-mpb13"},{"name":"stack72","fingerprint":"40:b3:59:04:7e:e0:08:cd:08:70:7e:a5:9c:07:22:4d","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDGgEagvBiKCc68r2kw7EKyosZs+ibMAQU4as+zOgTFbpHR6iSshAbRUjXXGuSVMmy5GhepnBHdT68hBXD5ny2NcyQPMV2RjSTtzFFWUoazwpUdZiFf4G+fEfK6mLjla1TtHcetCqyjuc1N5+CMWiGypQLDXPtfJKAgNU0RiwXP1SK4zDA8JIl5CcTGtE4ok+mtFFXRIQAoVyAo0u6XM8uToTq/7zH1NKe12pPgcaTthNeZBUiNTaq+BJuS2Y/ws72a7fUsXD5Cc9kvAZQe/QKmMWHOkw8qW8cSDQoWfH2awneZnxNrt3HKPye5IJqbPWhQlpAH32kwvT6DJ0ZAEkZr triton"},{"name":"Acceptance Tests","fingerprint":"b5:28:91:3a:3b:0b:34:72:d4:a6:95:62:40:20:f4:1c","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDSbVS0L6gCEfrpguFuNrsRls6cX12khHRVqoipL//UbvdVk1YaJuxHqfHoa+wf+ICTqOqrdxmLXk4VnFuEdECKkNzbNwa0P0QAVC1RLVEv1WHlOf5cFn+imJHt2bis3nHGz4kat1ZAMiKcDYTWGOH4Hn0HS32kW4oVqhINeeifb0uPkBej7YfD0Cs2Ihk5siZ4A2eejlZpw+X7tV9UnDBf4i/3bEnp4owPdSZjfyEJ1KJD1GiFYp3xf+pDEn9hLYOLmUFH2U3+bY+LglILzCRdXKQH5R2qAP5EGVprl5W3QmFlUpoYxGERB1feJZKGqCFQzNZzfvuSHkhxxXdVlOSX James@jn-mpb13"},{"name":"acctest-1882518303167625406","fingerprint":"af:ee:3c:aa:94:e1:08:93:f8:c0:9f:67:62:e5:e7:e9","key":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDL18KJIe8N7FxcgOMtabo10qZEDyYUSlOpsh/EYrugQCQHMKuNytog1lhFNZNk4LGNAz5L8/btG9+/axY/PfundbjR3SXt0hupAGQIVHuygWTr7foj5iGhckrEM+r3eMCXqoCnIFLhDZLDcq/zN2MxNbqDKcWSYmc8ul9dZWuiQpKOL+0nNXjhYA8Ewu+07kVAtsZD0WfvnAUjxmYb3rB15eBWk7gLxHrOPfZpeDSvOOX2bmzikpLn+L5NKrJsLrzO6hU/rpxD4OTHLULcsnIts3lYH8hShU8uY5ry94PBzdix++se3pUGvNSe967fKlHw3Ymh9nE/LJDQnzTNyFMj James@jn-mpb13"}] caused by: unexpected type *cloudapi.Key ``` ``` After === RUN TestAccTritonKey_basic --- PASS: TestAccTritonKey_basic (17.14s) ```
2016-08-31 00:19:04 +02:00
d.SetId(d.Get("name").(string))
err = resourceKeyRead(d, meta)
if err != nil {
return err
}
return nil
}
func resourceKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
client := meta.(*cloudapi.Client)
keys, err := client.ListKeys()
if err != nil {
return false, err
}
for _, key := range keys {
if key.Name == d.Id() {
return true, nil
}
}
return false, nil
}
func resourceKeyRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
key, err := client.GetKey(d.Id())
if err != nil {
return err
}
d.Set("name", key.Name)
d.Set("key", key.Key)
return nil
}
func resourceKeyDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*cloudapi.Client)
if err := client.DeleteKey(d.Get("name").(string)); err != nil {
return err
}
return nil
}
func resourceKeyImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
return []*schema.ResourceData{d}, nil
}