-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the new data source ssh key to get one ssh from the civo cloud
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
- Loading branch information
1 parent
255aaf4
commit e32df07
Showing
3 changed files
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/helper/validation" | ||
"log" | ||
) | ||
|
||
// Data source to get from the api a specific ssh key | ||
// using the id or the name | ||
func dataSourceSSHKey() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceSSHKeyRead, | ||
Schema: map[string]*schema.Schema{ | ||
"id": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
ExactlyOneOf: []string{"id", "name"}, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.NoZeroValues, | ||
ExactlyOneOf: []string{"id", "name"}, | ||
}, | ||
// Computed resource | ||
"fingerprint": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceSSHKeyRead(d *schema.ResourceData, m interface{}) error { | ||
apiClient := m.(*civogo.Client) | ||
|
||
var searchBy string | ||
|
||
if id, ok := d.GetOk("id"); ok { | ||
log.Printf("[INFO] Getting the ssh key by id") | ||
searchBy = id.(string) | ||
} else if name, ok := d.GetOk("name"); ok { | ||
log.Printf("[INFO] Getting the ssh key by label") | ||
searchBy = name.(string) | ||
} | ||
|
||
sshKey, err := apiClient.FindSSHKey(searchBy) | ||
if err != nil { | ||
fmt.Errorf("[ERR] failed to retrive network: %s", err) | ||
return err | ||
} | ||
|
||
d.SetId(sshKey.ID) | ||
d.Set("name", sshKey.Name) | ||
d.Set("fingerprint", sshKey.Fingerprint) | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
--- | ||
layout: "civo" | ||
page_title: "Civo: civo_ssh_key" | ||
sidebar_current: "docs-civo-datasource-ssh-key" | ||
description: |- | ||
Get information on a ssh key. | ||
--- | ||
|
||
# civo_ssh_key | ||
|
||
Get information on a ssh key. This data source provides the name, | ||
and fingerprint as configured on your Civo account. This is useful if | ||
the ssh key in question is not managed by Terraform or you need to utilize any | ||
of the keys data. | ||
|
||
An error is triggered if the provided ssh key name does not exist. | ||
|
||
## Example Usage | ||
|
||
Get the ssh key: | ||
|
||
```hcl | ||
data "civo_ssh_key" "example" { | ||
name = "example" | ||
} | ||
resource "civo_instance" "my-test-instance" { | ||
hostname = "foo.com" | ||
tags = ["python", "nginx"] | ||
notes = "this is a note for the server" | ||
size = element(data.civo_instances_size.small.sizes, 0).name | ||
template = element(data.civo_template.debian.templates, 0).id | ||
sshkey_id = data.civo_ssh_key.example.id | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
The following arguments are supported: | ||
|
||
* `id` - (Optional) The ID of the ssh key. | ||
* `name` - (Optional) The name of the ssh key. | ||
|
||
## Attributes Reference | ||
|
||
The following attributes are exported: | ||
|
||
* `id`: The ID of the ssh key. | ||
* `name`: The name of the ssh key. | ||
* `fingerprint`: The fingerprint of the public key of the ssh key. |