Skip to content

Commit

Permalink
feat: Add sitehost_ssh_key resource for creating, modifying and del…
Browse files Browse the repository at this point in the history
…eting SSH Keys
  • Loading branch information
MatthewPierson committed Mar 12, 2024
1 parent 55a4114 commit 41679ad
Show file tree
Hide file tree
Showing 17 changed files with 501 additions and 22 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# Changelog
All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

## [v1.2.0] 2024-03-13
### Added
- Added `sitehost_ssh_key` resource.

### Updated
- Updated GoSH version to v0.3.4.

## [v1.1.0] 2023-03-27
### Added
- Added `sitehost_dns_zone` resource.
Expand Down
2 changes: 1 addition & 1 deletion GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ HOSTNAME=hashicorp.com
NAMESPACE=sh
NAME=sitehost
BINARY=terraform-provider-${NAME}
VERSION=1.1.0
VERSION=1.2.0
OS_ARCH=linux_amd64
SRC := go.sum $(shell git ls-files -cmo --exclude-standard -- "*.go")
TESTABLE := ./...
Expand Down
10 changes: 5 additions & 5 deletions docs/data-sources/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ description: |-

### Required

- `image` (String)
- `label` (String)
- `location` (String)
- `product_code` (String)
- `image` (String) An Image ID to deploy the Disk from. The complete list of images ID you can see in our official documentation.
- `label` (String) Each Server is assigned a single public IPv4 address upon creation.
- `location` (String) This is the location where the Server was deployed. This cannot be changed without opening a support ticket.
- `product_code` (String) The product code of the server to be deployed, determining the price and size.

### Optional

- `name` (String)
- `name` (String) The `name` is the ID and is provided for a Server.

### Read-Only

Expand Down
33 changes: 33 additions & 0 deletions docs/data-sources/ssh_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sitehost_ssh_key Data Source - terraform-provider-sitehost"
subcategory: ""
description: |-
---

# sitehost_ssh_key (Data Source)





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `content` (String) The `content` is the contents of the public key.
- `label` (String) The `label` is the name of the SSH Key, and is displayed in CP.

### Optional

- `custom_image_access` (String) `custom_image_access` determines whether the key can be used to access custom images.

### Read-Only

- `date_added` (String) The `date_added` is the date/time when the SSH Key was added.
- `date_updated` (String) The `date_updated` is the date/time when the SSH Key was updated.
- `id` (String) The `id` is the ID of the SSH Key within SiteHost's systems.


10 changes: 8 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,19 @@ provider "sitehost" {
api_endpoint = "https://api.sitehost.nz/1.1/"
}
# Create a web server
# Create an SSH Key
resource "sitehost_ssh_key" "key" {
label = "My New SSH Key"
content = "ssh-rsa AAAAB3..."
}
# Create a web server with the SSH Key we just created
resource "sitehost_server" "web" {
label = "webserver"
location = "AKLCITY"
product_code = "XENLIT"
image = "ubuntu-xenial.amd64"
ssh_keys = []
ssh_keys = [sitehost_ssh_key.key.content]
}
```

Expand Down
18 changes: 10 additions & 8 deletions docs/resources/server.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "sitehost_server Resource - terraform-provider-sitehost"
subcategory: ""
description: Provides a SiteHost Server resource. This can be used to create, modify, and delete Servers.
Expand All @@ -21,24 +22,25 @@ resource "sitehost_server" "web" {
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `image` (String) The Server image slug.
- `label` (String) The Server label.
- `location` (String) The region to start in.
- `product_code` (String) The unique slug that identifies the type of Server.
- `image` (String) An Image ID to deploy the Disk from. The complete list of images ID you can see in our official documentation.
- `label` (String) The SiteHost's label is for display purposes only.
- `location` (String) This is the location where the Server was deployed. This cannot be changed without opening a support ticket.
- `product_code` (String) The product code of the server to be deployed, determining the price and size.

### Optional

- `ips` (List of String) Assign specific IPs for this Server.
- `name` (String) The name (ID) of the Server.
- `ssh_keys` (List of String) The SSH public keys will added to Server.
- `ips` (List of String) Each Server is assigned a single public IPv4 address upon creation.
- `name` (String) The `name` is the ID and is provided for a Server.
- `ssh_keys` (List of String) A list of SSH public keys to deploy for the root user on the newly created Server.

### Read-Only

- `id` (String) The ID of this resource.
- `password` (String, Sensitive) The root/administrator password.
- `password` (String, Sensitive) The password that will be assigned to the 'root' user account.


36 changes: 36 additions & 0 deletions docs/resources/ssh_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
---
page_title: "sitehost_ssh_key Resource - terraform-provider-sitehost"
subcategory: ""
description: Provides a SiteHost SSH Key resource. This can be used to create, modify, and delete SSH Keys.

---

# sitehost_ssh_key (Resource)

Provides a SiteHost SSH Key resource. This can be used to create, modify, and delete SSH Keys.

## Example Usage
```hcl
# Create an SSH Key
resource "sitehost_ssh_key" "key" {
label = "My New SSH Key"
content = "ssh-rsa AAAAB3..."
}
```

## Schema

### Required

- `label` (String) The SSH Key label.
- `content` (String, Sensitive) The string content of your SSH Public Key.

### Optional

- `custom_image_access` (String) Whether or not the SSH Key will have access to custom images (1: True, 0: False).

### Read-Only

- `id` (String) The ID of the SSH Key.
- `date_added` (String) The timestamp for when the SSH Key was created.
- `date_updated` (String) The timestamp for when the SSH key was last updated. Defaults to the `date_added` value.
37 changes: 37 additions & 0 deletions examples/ssh_key/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# SiteHost SSH Key

This example adds an SSH Key to your account, for use when creating new servers.

> Note: To run this example, first configure your SiteHost provider as described in <https://kb.sitehost.nz/developers/terraform-provider>.
## Prerequisites

Your `Client ID` and `API Key` values can be generated<https://cp.sitehost.nz/api/list-keys> by clicking "Add API Key". You can find out how to do this in our [knowledge base](https://kb.sitehost.nz/developers/api).

You will need to export your SiteHost Client ID and API Key as an environment variable:

```sh
export TF_VAR_sitehost_client_id="Put your SiteHost Client ID here"
export TF_VAR_sitehost_api_key="Put your SiteHost API key here"
export TF_VAR_ssh_key_label="Put the SSH Key label here"
export TF_VAR_ssh_key_data="Put the contents of your SSH Public Key here"
```

## Run this example

From the `examples/ssh_key` directory.

```sh
terraform init
terraform apply
```

> The SSH Key should be added
## Destroy the Resources

Clean up by removing all the resources that were created in one command:

```sh
terraform destroy
```
19 changes: 19 additions & 0 deletions examples/ssh_key/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
terraform {
required_providers {
sitehost = {
source = "sitehostnz/sitehost"
version = "~> 1.1.1"
}
}
}

provider "sitehost" {
client_id = var.sitehost_client_id
api_key = var.sitehost_api_key
api_endpoint = "https://api.sitehost.nz/1.1/"
}

resource "sitehost_ssh_key" "sitehost_ssh_key_create" {
label = var.ssh_key_label
content = var.ssh_key_data
}
15 changes: 15 additions & 0 deletions examples/ssh_key/variable.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "sitehost_client_id" {
description = "SiteHost API v1.1 Client ID"
}

variable "sitehost_api_key" {
description = "SiteHost API v1.1 API Key"
}

variable "ssh_key_label" {
description = "Label for the SSH Key"
}

variable "ssh_key_data" {
description = "Content of the SSH Public Key"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/terraform-plugin-docs v0.9.0
github.com/hashicorp/terraform-plugin-sdk/v2 v2.15.0
github.com/ory/go-acc v0.2.8
github.com/sitehostnz/gosh v0.3.2
github.com/sitehostnz/gosh v0.3.4
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -672,8 +672,8 @@ github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6Mwd
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/sitehostnz/gosh v0.3.2 h1:BvDMC9YYj4vCLa76DQYNYtICPi2OyAGG1jBpGIYz8Yk=
github.com/sitehostnz/gosh v0.3.2/go.mod h1:dra8Cc6DfQU2NNzdFf0ObgZhvFdn9Pifq2FiPqEw/ls=
github.com/sitehostnz/gosh v0.3.4 h1:3uUVtNHHyzRg0zpMiYwVhd/UOYmwzGcLN6fb+Bf0Bew=
github.com/sitehostnz/gosh v0.3.4/go.mod h1:dra8Cc6DfQU2NNzdFf0ObgZhvFdn9Pifq2FiPqEw/ls=
github.com/sivchari/containedctx v1.0.2 h1:0hLQKpgC53OVF1VT7CeoFHk9YKstur1XOgfYIc1yrHI=
github.com/sivchari/containedctx v1.0.2/go.mod h1:PwZOeqm4/DLoJOqMSIJs3aKqXRX4YO+uXww087KZ7Bw=
github.com/sivchari/nosnakecase v1.7.0 h1:7QkpWIRMe8x25gckkFd2A5Pi6Ymo0qgr4JrhGt95do8=
Expand Down
9 changes: 6 additions & 3 deletions sitehost/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/sitehostnz/terraform-provider-sitehost/sitehost/helper"
"github.com/sitehostnz/terraform-provider-sitehost/sitehost/info"
"github.com/sitehostnz/terraform-provider-sitehost/sitehost/server"
sshkey "github.com/sitehostnz/terraform-provider-sitehost/sitehost/ssh_key"
)

// New returns a schema.Provider for SiteHost.
Expand All @@ -37,15 +38,17 @@ func New(version string) func() *schema.Provider {
},
},
DataSourcesMap: map[string]*schema.Resource{
"sitehost_server": server.DataSource(),
"sitehost_api": info.DataSource(),
"sitehost_stack": stack.DataSource(),
"sitehost_server": server.DataSource(),
"sitehost_api": info.DataSource(),
"sitehost_stack": stack.DataSource(),
"sitehost_ssh_key": sshkey.DataSource(),
// "sitehost_stack_database": database.DataSource(),
},
ResourcesMap: map[string]*schema.Resource{
"sitehost_server": server.Resource(),
"sitehost_dns_zone": dns.ZoneResource(),
"sitehost_dns_record": dns.RecordResource(),
"sitehost_ssh_key": sshkey.Resource(),
},
}

Expand Down
47 changes: 47 additions & 0 deletions sitehost/ssh_key/datasource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package sshkey

import (
"context"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
sshkey "github.com/sitehostnz/gosh/pkg/api/ssh/key"
"github.com/sitehostnz/terraform-provider-sitehost/sitehost/helper"
)

// DataSource returns a schema with the function to read Server resource.
func DataSource() *schema.Resource {
recordSchema := sshKeyDataSourceSchema()

return &schema.Resource{
ReadContext: readDataSource,
Schema: recordSchema,
}
}

// readDataSource is a function to read an SSH Key.
func readDataSource(_ context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conf, ok := meta.(*helper.CombinedConfig)
if !ok {
return diag.Errorf("failed to convert meta object")
}

client := sshkey.New(conf.Client)

resp, err := client.Get(context.Background(), sshkey.GetRequest{
ID: d.Id(),
})
if err != nil {
return diag.Errorf("Error retrieving SSH Key: %s", err)
}

if !resp.Status {
return diag.Errorf("Error retrieving SSH Key: %s", resp.Msg)
}

if diagErr := setData(resp, d); diagErr != nil {
return diagErr
}

return nil
}
Loading

0 comments on commit 41679ad

Please sign in to comment.