Skip to content

Commit

Permalink
Merge branch 'release/v0.3.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed May 31, 2023
2 parents d7bdd0f + 5ca9551 commit fec467c
Show file tree
Hide file tree
Showing 8 changed files with 373 additions and 13 deletions.
30 changes: 30 additions & 0 deletions docs/resources/node.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "instellar_node Resource - terraform-provider-instellar"
subcategory: ""
description: |-
Node management
---

# instellar_node (Resource)

Node management



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

### Required

- `cluster_id` (String) Cluster ID
- `public_ip` (String) Public IP of the node
- `slug` (String) Node slug

### Read-Only

- `current_state` (String) Current state
- `id` (String) Node identifier
- `last_updated` (String) Timestamp of terraform update


2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ require (
github.com/hashicorp/terraform-plugin-go v0.15.0
github.com/hashicorp/terraform-plugin-log v0.8.0
github.com/hashicorp/terraform-plugin-testing v1.2.0
github.com/upmaru/instellar-go v0.2.6
github.com/upmaru/instellar-go v0.2.9
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,8 @@ github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.2/go.mod h1:R6va5+xMeoiuVRoj+gSkQ7d3FALtqAAGI1FQKckRals=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/upmaru/instellar-go v0.2.6 h1:zzIdCPMmAreButJc5o7mlTcw29JNjqGTUuM298q4YaA=
github.com/upmaru/instellar-go v0.2.6/go.mod h1:DyU7Pd7Syn6NIbdD5PMGkR1XE+qxGj0UDYwoKZgUxyI=
github.com/upmaru/instellar-go v0.2.9 h1:fBjXLY1W8F9LeWzWyUwLTBZbV5ahtK4fCztk4BFfxb8=
github.com/upmaru/instellar-go v0.2.9/go.mod h1:DyU7Pd7Syn6NIbdD5PMGkR1XE+qxGj0UDYwoKZgUxyI=
github.com/vmihailenco/msgpack v3.3.3+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
github.com/vmihailenco/msgpack v4.0.4+incompatible h1:dSLoQfGFAo3F6OoNhwUmLwVgaUXK79GlxNBwueZn0xI=
github.com/vmihailenco/msgpack v4.0.4+incompatible/go.mod h1:fy3FlTQTDXWkZ7Bh6AcGMlsjHatGryHQYUTf1ShIgkk=
Expand Down
8 changes: 7 additions & 1 deletion instellar/cluster/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,11 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
return
}

_, err := r.client.UpdateCluster(plan.ID.ValueString())
clusterParams := instc.ClusterParams{
CredentialEndpoint: plan.Endpoint.ValueString(),
}

_, err := r.client.UpdateCluster(plan.ID.ValueString(), clusterParams)
if err != nil {
resp.Diagnostics.AddError(
"Error updating instellar cluster",
Expand All @@ -217,6 +221,8 @@ func (r *clusterResource) Update(ctx context.Context, req resource.UpdateRequest
)
}

plan.Slug = types.StringValue(cluster.Data.Attributes.Slug)
plan.Endpoint = types.StringValue(cluster.Data.Attributes.Endpoint)
plan.CurrentState = types.StringValue(cluster.Data.Attributes.CurrentState)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

Expand Down
36 changes: 27 additions & 9 deletions instellar/cluster/resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,11 @@ func TestAccClusterResource(t *testing.T) {
ProtoV6ProviderFactories: acceptance.TestAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: acceptance.ProviderConfig + fmt.Sprintf(`
resource "instellar_cluster" "test" {
name = "%s"
provider_name = "aws"
region = "ap-southeast-1"
endpoint = "127.0.0.1:8443"
password_token = "some-password-or-token"
}
`, clusterNameSlug),
Config: buildConfig(clusterNameSlug, "127.0.0.1:8443"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("instellar_cluster.test", "name", clusterNameSlug),
resource.TestCheckResourceAttr("instellar_cluster.test", "endpoint", "127.0.0.1:8443"),

// Verify computed attribute fields.
resource.TestCheckResourceAttr("instellar_cluster.test", "slug", clusterNameSlug),
resource.TestCheckResourceAttr("instellar_cluster.test", "current_state", "connecting"),
Expand All @@ -44,6 +38,30 @@ func TestAccClusterResource(t *testing.T) {
ImportStateVerify: true,
ImportStateVerifyIgnore: []string{"last_updated", "password_token", "current_state"},
},
{
Config: buildConfig(clusterNameSlug, "38.43.56.78:8443"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("instellar_cluster.test", "name", clusterNameSlug),
resource.TestCheckResourceAttr("instellar_cluster.test", "endpoint", "38.43.56.78:8443"),
// Verify computed attribute fields.
resource.TestCheckResourceAttr("instellar_cluster.test", "slug", clusterNameSlug),
// Verify dynamic vlaues have value set
resource.TestCheckResourceAttrSet("instellar_cluster.test", "id"),
resource.TestCheckResourceAttrSet("instellar_cluster.test", "last_updated"),
),
},
},
})
}

func buildConfig(clusterNameSlug string, endpoint string) string {
return acceptance.ProviderConfig + fmt.Sprintf(`
resource "instellar_cluster" "test" {
name = "%s"
provider_name = "aws"
region = "ap-southeast-1"
endpoint = "%s"
password_token = "some-password-or-token"
}
`, clusterNameSlug, endpoint)
}
230 changes: 230 additions & 0 deletions instellar/node/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,230 @@
package node

import (
"context"
"fmt"
"strconv"
"time"

instc "github.com/upmaru/instellar-go"

"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
)

var (
_ resource.Resource = &nodeResource{}
_ resource.ResourceWithConfigure = &nodeResource{}
_ resource.ResourceWithImportState = &nodeResource{}
)

func NewNodeResource() resource.Resource {
return &nodeResource{}
}

type nodeResource struct {
client *instc.Client
}

type nodeResourceModel struct {
ID types.String `tfsdk:"id"`
Slug types.String `tfsdk:"slug"`
ClusterID types.String `tfsdk:"cluster_id"`
PublicIP types.String `tfsdk:"public_ip"`
CurrentState types.String `tfsdk:"current_state"`
LastUpdated types.String `tfsdk:"last_updated"`
}

func (r *nodeResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_node"
}

func (r *nodeResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Node management",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Node identifier",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"slug": schema.StringAttribute{
Description: "Node slug",
Required: true,
},
"current_state": schema.StringAttribute{
Description: "Current state",
Computed: true,
},
"cluster_id": schema.StringAttribute{
Description: "Cluster ID",
Required: true,
},
"public_ip": schema.StringAttribute{
Description: "Public IP of the node",
Required: true,
},
"last_updated": schema.StringAttribute{
Description: "Timestamp of terraform update",
Computed: true,
},
},
}
}

func (r *nodeResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*instc.Client)

if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf(
"Expected *instc.Client, got: %T. Please report this issue to the provider developers.",
req.ProviderData,
),
)
return
}

r.client = client
}

func (r *nodeResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan nodeResourceModel
diags := req.Plan.Get(ctx, &plan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

nodeParams := instc.NodeParams{
PublicIP: plan.PublicIP.ValueString(),
}

node, err := r.client.CreateNode(plan.ClusterID.ValueString(), plan.Slug.ValueString(), nodeParams)

if err != nil {
resp.Diagnostics.AddError(
"Error creating node",
"Could not create node, unexpected error: "+err.Error(),
)
return
}

plan.ID = types.StringValue(strconv.Itoa(node.Data.Attributes.ID))
plan.CurrentState = types.StringValue(node.Data.Attributes.CurrentState)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}
}

func (r *nodeResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state nodeResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

node, err := r.client.GetNode(state.ID.ValueString())

if err != nil {
resp.Diagnostics.AddError(
"Error reading node",
"Could not read node id "+state.ID.ValueString()+": "+err.Error(),
)
return
}

state.Slug = types.StringValue(node.Data.Attributes.Slug)
state.CurrentState = types.StringValue(node.Data.Attributes.CurrentState)
state.ClusterID = types.StringValue(strconv.Itoa(node.Data.Attributes.ClusterID))
state.PublicIP = types.StringValue(node.Data.Attributes.PublicIP)

diags = resp.State.Set(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (r *nodeResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan nodeResourceModel
diags := req.Plan.Get(ctx, &plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

nodeParams := instc.NodeParams{
PublicIP: plan.PublicIP.ValueString(),
}

_, err := r.client.UpdateNode(plan.ClusterID.ValueString(), plan.Slug.ValueString(), nodeParams)
if err != nil {
resp.Diagnostics.AddError(
"Error updating node",
"Could not update node, unexpected error: "+err.Error(),
)
return
}

node, err := r.client.GetNode(plan.ID.ValueString())

if err != nil {
resp.Diagnostics.AddError(
"Error reading node",
"Could not read node ID "+plan.ID.ValueString()+": "+err.Error(),
)
}

plan.Slug = types.StringValue(node.Data.Attributes.Slug)
plan.CurrentState = types.StringValue(node.Data.Attributes.CurrentState)
plan.PublicIP = types.StringValue(node.Data.Attributes.PublicIP)
plan.LastUpdated = types.StringValue(time.Now().Format(time.RFC850))

diags = resp.State.Set(ctx, plan)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}

func (r *nodeResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state nodeResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

_, err := r.client.DeleteNode(state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error deleting node",
"Could not delete node, unexpected error: "+err.Error(),
)
return
}
}

func (r *nodeResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
Loading

0 comments on commit fec467c

Please sign in to comment.