2016-02-10 21:30:32 +01:00
|
|
|
package triton
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/terraform/helper/schema"
|
2017-03-31 00:25:27 +02:00
|
|
|
"github.com/joyent/triton-go"
|
2016-02-10 21:30:32 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
func resourceKey() *schema.Resource {
|
|
|
|
return &schema.Resource{
|
2017-03-31 00:25:27 +02:00
|
|
|
Create: resourceKeyCreate,
|
|
|
|
Exists: resourceKeyExists,
|
|
|
|
Read: resourceKeyRead,
|
|
|
|
Delete: resourceKeyDelete,
|
|
|
|
Timeouts: fastResourceTimeout,
|
2016-05-05 01:09:38 +02:00
|
|
|
Importer: &schema.ResourceImporter{
|
2017-03-31 00:25:27 +02:00
|
|
|
State: schema.ImportStatePassthrough,
|
2016-05-05 01:09:38 +02:00
|
|
|
},
|
2016-02-10 21:30:32 +01:00
|
|
|
|
|
|
|
Schema: map[string]*schema.Schema{
|
2017-03-31 00:25:27 +02:00
|
|
|
"name": {
|
|
|
|
Description: "Name of the key (generated from the key comment if not set)",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Optional: true,
|
|
|
|
Computed: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
2017-03-31 00:25:27 +02:00
|
|
|
"key": {
|
|
|
|
Description: "Content of public key from disk in OpenSSH format",
|
2016-02-10 21:30:32 +01:00
|
|
|
Type: schema.TypeString,
|
|
|
|
Required: true,
|
|
|
|
ForceNew: true,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func resourceKeyCreate(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
if keyName := d.Get("name").(string); keyName == "" {
|
2016-02-10 21:30:32 +01:00
|
|
|
parts := strings.SplitN(d.Get("key").(string), " ", 3)
|
|
|
|
if len(parts) == 3 {
|
|
|
|
d.Set("name", parts[2])
|
|
|
|
} else {
|
2017-03-31 00:25:27 +02:00
|
|
|
return errors.New("No key name specified, and key material has no comment")
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
_, err := client.Keys().CreateKey(&triton.CreateKeyInput{
|
2016-02-10 21:30:32 +01:00
|
|
|
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))
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return resourceKeyRead(d, meta)
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceKeyExists(d *schema.ResourceData, meta interface{}) (bool, error) {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
_, err := client.Keys().GetKey(&triton.GetKeyInput{
|
|
|
|
KeyName: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return true, nil
|
2016-02-10 21:30:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func resourceKeyRead(d *schema.ResourceData, meta interface{}) error {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
key, err := client.Keys().GetKey(&triton.GetKeyInput{
|
|
|
|
KeyName: d.Id(),
|
|
|
|
})
|
2016-02-10 21:30:32 +01:00
|
|
|
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 {
|
2017-03-31 00:25:27 +02:00
|
|
|
client := meta.(*triton.Client)
|
2016-02-10 21:30:32 +01:00
|
|
|
|
2017-03-31 00:25:27 +02:00
|
|
|
return client.Keys().DeleteKey(&triton.DeleteKeyInput{
|
|
|
|
KeyName: d.Id(),
|
|
|
|
})
|
2016-05-05 01:09:38 +02:00
|
|
|
}
|