Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PLT-1402:Added ssh_key management support into terrafrom #527

Merged
merged 3 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions docs/data-sources/ssh_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "spectrocloud_ssh_key Data Source - terraform-provider-spectrocloud"
subcategory: ""
description: |-

---

# spectrocloud_ssh_key (Data Source)





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

### Optional

- `context` (String) The context of the cluster profile. Allowed values are `project` or `tenant`. Default value is `project`. If the `project` context is specified, the project name will sourced from the provider configuration parameter [`project_name`](https://registry.terraform.io/providers/spectrocloud/spectrocloud/latest/docs#schema).
- `id` (String) The Id of the SSH key resource.
- `name` (String) The name of the SSH key resource.

### Read-Only

- `ssh_key` (String, Sensitive) The SSH key value.
55 changes: 55 additions & 0 deletions docs/resources/ssh_key.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
---
page_title: "spectrocloud_ssh_key Resource - terraform-provider-spectrocloud"
subcategory: ""
description: |-

---

# spectrocloud_ssh_key (Resource)



You can learn more about managing ssh key by reviewing the [Create and Manage DNS Mappings](https://docs.spectrocloud.com/clusters/cluster-management/ssh-keys/) guide.

## Example Usage

An example of creating an ssh key assets in project or tenant context.

```hcl
data "spectrocloud_ssh_key" "ssh_project" {
name = "test-tf-ssh"
context = "project"
}

resource "spectrocloud_ssh_key" "ssh_tenant" {
name = "ssh-dev-1"
context = "tenant"
ssh_key = data.spectrocloud_ssh_key.ssh_project.ssh_key
}
```

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

### Required

- `name` (String) The name of the SSH key resource.
- `ssh_key` (String, Sensitive) The SSH key value.

### Optional

- `context` (String) The context of the cluster profile. Allowed values are `project` or `tenant`. Default value is `project`. If the `project` context is specified, the project name will sourced from the provider configuration parameter [`project_name`](https://registry.terraform.io/providers/spectrocloud/spectrocloud/latest/docs#schema).
- `timeouts` (Block, Optional) (see [below for nested schema](#nestedblock--timeouts))

### Read-Only

- `id` (String) The ID of this resource.

<a id="nestedblock--timeouts"></a>
### Nested Schema for `timeouts`

Optional:

- `create` (String)
- `delete` (String)
- `update` (String)
14 changes: 14 additions & 0 deletions examples/data-sources/spectrocloud_ssh_key/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_providers {
spectrocloud = {
version = ">= 0.1"
source = "spectrocloud/spectrocloud"
}
}
}

provider "spectrocloud" {
host = var.sc_host
api_key = var.sc_api_key
project_name = var.sc_project_name
}
10 changes: 10 additions & 0 deletions examples/data-sources/spectrocloud_ssh_key/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
data "spectrocloud_ssh_key" "ssh_project" {
name = "test-tf-ssh"
context = "project"
}

resource "spectrocloud_ssh_key" "ssh_tenant" {
name = "ssh-dev-1"
context = "tenant"
ssh_key = data.spectrocloud_ssh_key.ssh_project.ssh_key
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Spectro Cloud credentials
sc_host = "{Enter Spectro Cloud API Host}" #e.g: api.spectrocloud.com (for SaaS)
sc_api_key = "{Enter Spectro Cloud API Key}"
sc_project_name = "{Enter Spectro Cloud Project Name}" #e.g: Default
13 changes: 13 additions & 0 deletions examples/data-sources/spectrocloud_ssh_key/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
variable "sc_host" {
description = "Spectro Cloud Endpoint"
default = "api.spectrocloud.com"
}

variable "sc_api_key" {
description = "Spectro Cloud API key"
}

variable "sc_project_name" {
description = "Spectro Cloud Project (e.g: Default)"
default = "Default"
}
14 changes: 14 additions & 0 deletions examples/resources/spectrocloud_ssh_key/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
terraform {
required_providers {
spectrocloud = {
version = ">= 0.1"
source = "spectrocloud/spectrocloud"
}
}
}

provider "spectrocloud" {
host = var.sc_host
api_key = var.sc_api_key
project_name = var.sc_project_name
}
10 changes: 10 additions & 0 deletions examples/resources/spectrocloud_ssh_key/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
resource "spectrocloud_ssh_key" "ssh_project" {
name = "ssh-dev-1-project"
context = "project"
ssh_key = var.ssh_key_value
}
resource "spectrocloud_ssh_key" "ssh_tenant" {
name = "ssh-dev-1"
context = "tenant"
ssh_key = var.ssh_key_value
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# Spectro Cloud credentials
sc_host = "{Enter Spectro Cloud API Host}" #e.g: api.spectrocloud.com (for SaaS)
sc_api_key = "{Enter Spectro Cloud API Key}"
sc_project_name = "{Enter Spectro Cloud Project Name}" #e.g: Default
18 changes: 18 additions & 0 deletions examples/resources/spectrocloud_ssh_key/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
variable "sc_host" {
description = "Spectro Cloud Endpoint"
default = "api.spectrocloud.com"
}

variable "sc_api_key" {
description = "Spectro Cloud API key"
}

variable "sc_project_name" {
description = "Spectro Cloud Project (e.g: Default)"
default = "Default"
}

variable "ssh_key_value" {
description = "ssh key value"
default = "ssh-rsa ...... == [email protected]"
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/robfig/cron v1.2.0
github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368
github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d
github.com/spectrocloud/palette-sdk-go v0.0.0-20241018151815-6925f4d3d199
github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb
github.com/stretchr/testify v1.9.0
gotest.tools v2.2.0+incompatible
k8s.io/api v0.23.5
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -600,8 +600,8 @@ github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368 h1:eY0BOyEbGu
github.com/spectrocloud/gomi v1.14.1-0.20240214074114-c19394812368/go.mod h1:LlZ9We4kDaELYi7Is0SVmnySuDhwphJLS6ZT4wXxFIk=
github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d h1:OMRbHxMJ1a+G1BYzvUYuMM0wLkYJPdnEOFx16faQ/UY=
github.com/spectrocloud/hapi v1.14.1-0.20240214071352-81f589b1d86d/go.mod h1:MktpRPnSXDTHsQrFSD+daJFQ1zMLSR+1gWOL31jVvWE=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241018151815-6925f4d3d199 h1:YlU+FfzCWusdufvR7IanDHg5ZURM0Z79enWpD36ohf0=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241018151815-6925f4d3d199/go.mod h1:dSlNvDS0qwUWTbrYI6P8x981mcbbRHFrBg67v5zl81U=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb h1:LVeVFAMVdZRhtn1VY3DnDi32ts90r8/RXP5+1RZBZEA=
github.com/spectrocloud/palette-sdk-go v0.0.0-20241022161234-3782615736bb/go.mod h1:dSlNvDS0qwUWTbrYI6P8x981mcbbRHFrBg67v5zl81U=
github.com/spf13/afero v1.1.2/go.mod h1:j4pytiNVoe2o6bmDsKpLACNPDBIoEAkihy7loJ1B0CQ=
github.com/spf13/afero v1.2.2/go.mod h1:9ZxEEn6pIJ8Rxe320qSDBk6AsU0r9pR7Q4OcevTdifk=
github.com/spf13/afero v1.6.0/go.mod h1:Ai8FlHk4v/PARR026UzYexafAt9roJ7LcLMAmO6Z93I=
Expand Down
73 changes: 73 additions & 0 deletions spectrocloud/data_source_ssh_key.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package spectrocloud

import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/spectrocloud/palette-sdk-go/api/models"
)

func dataSourceSSHKey() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceSSHKeyRead,

Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
ConflictsWith: []string{"name"},
Description: "The Id of the SSH key resource.",
},
"name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "The name of the SSH key resource.",
},
"ssh_key": {
Type: schema.TypeString,
Computed: true,
Sensitive: true,
Description: "The SSH key value.",
},
"context": {
Type: schema.TypeString,
Optional: true,
Default: "project",
ValidateFunc: validation.StringInSlice([]string{"project", "tenant"}, false),
Description: "The context of the cluster profile. Allowed values are `project` or `tenant`. " +
"Default value is `project`. " + PROJECT_NAME_NUANCE,
},
},
}
}

func dataSourceSSHKeyRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
sshKeyContext := d.Get("context").(string)
c := getV1ClientWithResourceContext(m, sshKeyContext)
var diags diag.Diagnostics
id := d.Get("id").(string)
name := d.Get("name").(string)
var sshKey *models.V1UserAssetSSH
var err error
if id != "" {
sshKey, err = c.GetSSHKey(d.Id())
} else if name != "" {
sshKey, err = c.GetSSHKeyByName(name)
}
if err != nil {
return diag.FromErr(err)
}
d.SetId(sshKey.Metadata.UID)
err = d.Set("name", sshKey.Metadata.Name)
if err != nil {
return nil
}
err = d.Set("ssh_key", sshKey.Spec.PublicKey)
if err != nil {
return nil
}
return diags
}
2 changes: 2 additions & 0 deletions spectrocloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ func New(_ string) func() *schema.Provider {

"spectrocloud_workspace": resourceWorkspace(),
"spectrocloud_alert": resourceAlert(),
"spectrocloud_ssh_key": resourceSSHKey(),
},
DataSourcesMap: map[string]*schema.Resource{
"spectrocloud_user": dataSourceUser(),
Expand Down Expand Up @@ -176,6 +177,7 @@ func New(_ string) func() *schema.Provider {

"spectrocloud_private_cloud_gateway": dataSourcePCG(),
"spectrocloud_ippool": dataSourcePrivateCloudGatewayIpPool(),
"spectrocloud_ssh_key": dataSourceSSHKey(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
Loading
Loading