-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Now can create, update or delete a ssh resource - Update to civogo v0.2.0 BREAKING CHANGE: No Signed-off-by: Alejandro JNM <[email protected]>
- Loading branch information
1 parent
834be2e
commit 04c84fe
Showing
5 changed files
with
108 additions
and
6 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
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,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 | ||
} |
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