Skip to content

Commit

Permalink
Merge branch 'release/v0.8.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacksiri committed Mar 22, 2024
2 parents 3a95752 + 66b19f1 commit d6ec20b
Show file tree
Hide file tree
Showing 9 changed files with 361 additions and 8 deletions.
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Provision instellar resources.

```terraform
provider "instellar" {
host = "https://web.instellar.app"
host = "https://opsmaru.com"
auth_token = "your-instellar-auth-token"
}
```
Expand Down
44 changes: 44 additions & 0 deletions docs/resources/balancer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "instellar_balancer Resource - terraform-provider-instellar"
subcategory: ""
description: |-
Balancer is what instellar use to balance the load of the incoming requests to the instellar components.
---

# instellar_balancer (Resource)

Balancer is what instellar use to balance the load of the incoming requests to the instellar components.

## Example Usage

```terraform
resource "instellar_cluster" "main" {
name = "some-unique-cluster-name"
provider_name = "aws"
region = "ap-southeast-1"
endpoint = "127.0.0.1:8443"
password_token = "some-password-or-token"
}
resource "instellar_balancer" "this" {
name = "some-balancer"
address = "some.example.com"
cluster_id = instellar_cluster.main.id
}
```

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

### Required

- `address` (String) Balancer Address
- `cluster_id` (String) Which cluster does balancer belong to
- `name` (String) Balancer Name

### Read-Only

- `current_state` (String) Balancer Current State
- `id` (String) Balancer identifier
- `last_updated` (String) Balancer Last Updated
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
provider "instellar" {
host = "https://web.instellar.app"
host = "https://opsmaru.com"
auth_token = "your-instellar-auth-token"
}
13 changes: 13 additions & 0 deletions examples/resources/instellar_balancer/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
resource "instellar_cluster" "main" {
name = "some-unique-cluster-name"
provider_name = "aws"
region = "ap-southeast-1"
endpoint = "127.0.0.1:8443"
password_token = "some-password-or-token"
}

resource "instellar_balancer" "this" {
name = "some-balancer"
address = "some.example.com"
cluster_id = instellar_cluster.main.id
}
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ go 1.20
require (
github.com/google/uuid v1.6.0
github.com/hashicorp/terraform-plugin-docs v0.18.0
github.com/hashicorp/terraform-plugin-framework v1.6.1
github.com/hashicorp/terraform-plugin-framework v1.7.0
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-go v0.22.1
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/hashicorp/terraform-plugin-testing v1.7.0
github.com/upmaru/instellar-go v0.6.4
github.com/upmaru/instellar-go v0.7.1
)

require (
Expand Down
8 changes: 4 additions & 4 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ github.com/hashicorp/terraform-json v0.21.0 h1:9NQxbLNqPbEMze+S6+YluEdXgJmhQykRy
github.com/hashicorp/terraform-json v0.21.0/go.mod h1:qdeBs11ovMzo5puhrRibdD6d2Dq6TyE/28JiU4tIQxk=
github.com/hashicorp/terraform-plugin-docs v0.18.0 h1:2bINhzXc+yDeAcafurshCrIjtdu1XHn9zZ3ISuEhgpk=
github.com/hashicorp/terraform-plugin-docs v0.18.0/go.mod h1:iIUfaJpdUmpi+rI42Kgq+63jAjI8aZVTyxp3Bvk9Hg8=
github.com/hashicorp/terraform-plugin-framework v1.6.1 h1:hw2XrmUu8d8jVL52ekxim2IqDc+2Kpekn21xZANARLU=
github.com/hashicorp/terraform-plugin-framework v1.6.1/go.mod h1:aJI+n/hBPhz1J+77GdgNfk5svW12y7fmtxe/5L5IuwI=
github.com/hashicorp/terraform-plugin-framework v1.7.0 h1:wOULbVmfONnJo9iq7/q+iBOBJul5vRovaYJIu2cY/Pw=
github.com/hashicorp/terraform-plugin-framework v1.7.0/go.mod h1:jY9Id+3KbZ17OMpulgnWLSfwxNVYSoYBQFTgsx044CI=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0 h1:HOjBuMbOEzl7snOdOoUfE2Jgeto6JOjLVQ39Ls2nksc=
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0/go.mod h1:jfHGE/gzjxYz6XoUwi/aYiiKrJDeutQNUtGQXkaHklg=
github.com/hashicorp/terraform-plugin-go v0.22.1 h1:iTS7WHNVrn7uhe3cojtvWWn83cm2Z6ryIUDTRO0EV7w=
Expand Down Expand Up @@ -164,8 +164,8 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
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.6.4 h1:adPEvkJ5TRsOAehDJ9ZSqFoW38WjhNfvSR0kJChSqvc=
github.com/upmaru/instellar-go v0.6.4/go.mod h1:DyU7Pd7Syn6NIbdD5PMGkR1XE+qxGj0UDYwoKZgUxyI=
github.com/upmaru/instellar-go v0.7.1 h1:yMslk+H9uNrzFLflw7ISSX/QkltOaiWIEL6jmIjzmrY=
github.com/upmaru/instellar-go v0.7.1/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
235 changes: 235 additions & 0 deletions instellar/balancer/resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,235 @@
package balancer

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

// instellar client = instc.
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 = &balancerResource{}
_ resource.ResourceWithConfigure = &balancerResource{}
_ resource.ResourceWithImportState = &balancerResource{}
)

func NewBalancerResource() resource.Resource {
return &balancerResource{}
}

type balancerResource struct {
client *instc.Client
}

type balancerResourceModel struct {
ID types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Address types.String `tfsdk:"address"`
CurrentState types.String `tfsdk:"current_state"`
ClusterID types.String `tfsdk:"cluster_id"`
LastUpdated types.String `tfsdk:"last_updated"`
}

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

func (r *balancerResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Balancer is what instellar use to balance the load of the incoming requests to the instellar components.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Description: "Balancer identifier",
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Description: "Balancer Name",
Required: true,
},
"address": schema.StringAttribute{
Description: "Balancer Address",
Required: true,
},
"current_state": schema.StringAttribute{
Description: "Balancer Current State",
Computed: true,
},
"cluster_id": schema.StringAttribute{
Description: "Which cluster does balancer belong to",
Required: true,
},
"last_updated": schema.StringAttribute{
Description: "Balancer Last Updated",
Computed: true,
},
},
}
}

func (r *balancerResource) 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 *balancerResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan balancerResourceModel
diags := req.Plan.Get(ctx, &plan)

resp.Diagnostics.Append(diags...)

if resp.Diagnostics.HasError() {
return
}

balancerParams := instc.BalancerParams{
Name: plan.Name.ValueString(),
Address: plan.Address.ValueString(),
}

balancer, err := r.client.CreateBalancer(plan.ClusterID.ValueString(), balancerParams)

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

plan.ID = types.StringValue(strconv.Itoa(balancer.Data.Attributes.ID))
plan.CurrentState = types.StringValue(balancer.Data.Attributes.CurrentState)
plan.ClusterID = types.StringValue(strconv.Itoa(balancer.Data.Attributes.ClusterID))
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 *balancerResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state balancerResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

balancer, err := r.client.GetBalancer(state.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error reading uplink",
"Could not read uplink id "+state.ID.ValueString()+": "+err.Error(),
)
}

state.Name = types.StringValue(balancer.Data.Attributes.Name)
state.Address = types.StringValue(balancer.Data.Attributes.Address)
state.CurrentState = types.StringValue(balancer.Data.Attributes.CurrentState)
state.ClusterID = types.StringValue(strconv.Itoa(balancer.Data.Attributes.ClusterID))

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

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

balancerParams := instc.BalancerParams{
Name: plan.Name.ValueString(),
Address: plan.Address.ValueString(),
}

_, err := r.client.UpdateBalancer(plan.ID.ValueString(), balancerParams)

if err != nil {
resp.Diagnostics.AddError(
"Error updating balancer",
"Could not update balancer, unexpected error: "+err.Error(),
)
return
}

balancer, err := r.client.GetBalancer(plan.ID.ValueString())

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

plan.Name = types.StringValue(balancer.Data.Attributes.Name)
plan.Address = types.StringValue(balancer.Data.Attributes.Address)
plan.CurrentState = types.StringValue(balancer.Data.Attributes.CurrentState)
plan.ClusterID = types.StringValue(strconv.Itoa(balancer.Data.Attributes.ClusterID))
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 *balancerResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state *balancerResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

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

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

0 comments on commit d6ec20b

Please sign in to comment.