Skip to content

Commit

Permalink
feat: Add ssh resource
Browse files Browse the repository at this point in the history
- Now can create, update or delete a ssh resource
- Update to civogo v0.2.0

BREAKING CHANGE: No

Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Mar 24, 2020
1 parent 834be2e commit 04c84fe
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 6 deletions.
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ Progress
-~~Instances~~
-~~Networks~~
-~~Volumes~~
-~~Firewalls~~
-~~Load balancers~~
-~~SSH keys~~
-~~Domain names~~
-~~Domain records~~
- Regions
- Quotas
- Sizes
-~~Domain names~~
-~~Domain records~~
- Kubernetes Clusters
- Kubernetes Applications
- Load balancers
- SSH keys
- Snapshots
- Firewalls
- Templates
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func Provider() terraform.ResourceProvider {
"civo_firewall": resourceFirewall(),
"civo_firewall_rule": resourceFirewallRule(),
"civo_loadbalancer": resourceLoadBalancer(),
"civo_ssh_key": resourceSSHKey(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
99 changes: 99 additions & 0 deletions civo/resource_ssh.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package civo

import (
"fmt"
"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)

func resourceSSHKey() *schema.Resource {
fmt.Print()
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "a string that will be the reference for the SSH key.",
ValidateFunc: validateName,
},
"public_key": {
Type: schema.TypeString,
Required: true,
Description: "a string containing the SSH public key.",
Sensitive: true,
ForceNew: true,
},
"fingerprint": {
Type: schema.TypeString,
Computed: true,
Description: "a string containing the SSH finger print.",
},
},
Create: resourceSSHKeyCreate,
Read: resourceSSHKeyRead,
Update: resourceSSHKeyUpdate,
Delete: resourceSSHKeyDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
}
}

func resourceSSHKeyCreate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

sshKey, err := apiClient.NewSSHKey(d.Get("name").(string), d.Get("public_key").(string))
if err != nil {
fmt.Errorf("[WARN] failed to create a new ssh key: %s", err)
return err
}

d.SetId(sshKey.ID)

return resourceSSHKeyRead(d, m)
}

func resourceSSHKeyRead(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

sshKey, err := apiClient.FindSSHKey(d.Id())
if err != nil {
if sshKey != nil {
d.SetId("")
return nil
}

return fmt.Errorf("[WARN] error retrieving ssh key: %s", err)
}

d.Set("name", sshKey.Name)
d.Set("fingerprint", sshKey.Fingerprint)

return nil
}

func resourceSSHKeyUpdate(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

if d.HasChange("name") {
if d.Get("name").(string) != "" {
_, err := apiClient.UpdateSSHKey(d.Get("name").(string), d.Id())
if err != nil {
log.Printf("[WARN] an error occurred while trying to rename the ssh key (%s)", d.Id())
}
}
}

return resourceSSHKeyRead(d, m)
}

func resourceSSHKeyDelete(d *schema.ResourceData, m interface{}) error {
apiClient := m.(*civogo.Client)

_, err := apiClient.DeleteSSHKey(d.Id())
if err != nil {
log.Printf("[INFO] civo ssh key (%s) was delete", d.Id())
}
return nil
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/civo/terraform-provider-civo
require (
cloud.google.com/go v0.54.0 // indirect
github.com/aws/aws-sdk-go v1.29.22 // indirect
github.com/civo/civogo v0.1.9
github.com/civo/civogo v0.2.0
github.com/fatih/color v1.9.0 // indirect
github.com/hashicorp/go-getter v1.4.1 // indirect
github.com/hashicorp/go-hclog v0.12.1 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ github.com/civo/civogo v0.1.8 h1:s+jxqbYznLBtOuq677APFlxZysC8lIUn2KY/Ro53dWU=
github.com/civo/civogo v0.1.8/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/civo/civogo v0.1.9 h1:UAjrqsbaJ/N7HjM7fH2r2h5iA6CIrcuCStiqE17XUgM=
github.com/civo/civogo v0.1.9/go.mod h1:QwPxaDertZ7sxu3Fdibu/JdXBJV3RqYZrh76YEbGLKg=
github.com/civo/civogo v0.2.0 h1:O+NTsf6Fz0yhlbwM35hV1r9iPk8R3DwP8QbdBcdniNE=
github.com/civo/civogo v0.2.0/go.mod h1:SR0ZOhABfQHjgNQE3UyfX4gaYsrfslkPFRFMx5P29rg=
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
Expand Down

0 comments on commit 04c84fe

Please sign in to comment.