Skip to content

Commit

Permalink
Added the new data source ssh key to get one ssh from the civo cloud
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 21, 2020
1 parent 255aaf4 commit e32df07
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 0 deletions.
62 changes: 62 additions & 0 deletions civo/datasource_ssh.go
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
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func Provider() terraform.ResourceProvider {
"civo_network": dataSourceNetwork(),
"civo_volume": dataSourceVolume(),
"civo_loadbalancer": dataSourceLoadBalancer(),
"civo_ssh_key": dataSourceSSHKey(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
Expand Down
50 changes: 50 additions & 0 deletions website/docs/d/ssh_key.html.markdown
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.

0 comments on commit e32df07

Please sign in to comment.