Skip to content

Commit

Permalink
chore: add skeleton for buildingblock resource
Browse files Browse the repository at this point in the history
  • Loading branch information
j0g3sc committed Nov 21, 2023
1 parent 1326220 commit 831901c
Show file tree
Hide file tree
Showing 11 changed files with 250 additions and 181 deletions.
30 changes: 0 additions & 30 deletions docs/data-sources/scaffolding_example.md

This file was deleted.

3 changes: 0 additions & 3 deletions examples/data-sources/scaffolding_example/data-source.tf

This file was deleted.

6 changes: 4 additions & 2 deletions examples/provider/provider.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
provider "scaffolding" {
# example configuration here
provider "meshstack" {
endpoint = "meshfed.url"
apikey = "API_KEY"
apisecret = "API_SECRET"
}
21 changes: 21 additions & 0 deletions examples/resources/buildingblocks/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
resource "buildingblock" "example" {
definition_uuid = "some-definition-uuid"
definition_version = 1
tenant_identifier = "tenantIdentifier"

name = "some-name"
display_name = "displayname"

inputs = {
key_1 = "value"
key_2 = 5
key_3 = true
}

parents = [
{
definition_uuid = "some-parent-definition-uuid"
uuid = "some-parent-block-uuid"
}
]
}
3 changes: 0 additions & 3 deletions examples/resources/scaffolding_example/resource.tf

This file was deleted.

57 changes: 57 additions & 0 deletions internal/provider/buildingblock_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ datasource.DataSource = &BuildingBlockDataSource{}

func NewBuildingBlockSource() datasource.DataSource {
return &BuildingBlockDataSource{}
}

type BuildingBlockDataSource struct {
client *MeshStackProviderClient
}

type BuildingBlockDataSourceModel struct {
}

func (d *BuildingBlockDataSource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_buildingblock"
}

func (d *BuildingBlockDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "BuildingBlock data source",

Attributes: map[string]schema.Attribute{},
}
}

func (d *BuildingBlockDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*MeshStackProviderClient)

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

return
}

d.client = client
}

func (d *BuildingBlockDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
}
162 changes: 162 additions & 0 deletions internal/provider/buildingblock_resource.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package provider

import (
"context"
"fmt"

"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"
)

// Ensure provider defined types fully satisfy framework interfaces.
var _ resource.Resource = &BuildingBlockResource{}
var _ resource.ResourceWithImportState = &BuildingBlockResource{}

func NewBuildingBlockResource() resource.Resource {
return &BuildingBlockResource{}
}

type BuildingBlockResource struct {
client *MeshStackProviderClient
}

type BuildingBlockResourceModel struct {
Uuid types.String `tfsdk:"uuid"`
DefinitionUUid types.String `tfsdk:"definition_uuid"`
DefinitionVersion types.Int64 `tfsdk:"definition_version"`
TenantIdentifier types.String `tfsdk:"tenant_identifier"`
Name types.String `tfsdk:"name"`
DisplayName types.String `tfsdk:"display_name"`
Inputs types.Map `tfsdk:"inputs"`
Parents types.Set `tfsdk:"parents"`
}

func (r *BuildingBlockResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_buildingblock"
}

func (r *BuildingBlockResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Building Block",

Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
Computed: true,
MarkdownDescription: "UUID of the Building Block (generated)",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"definition_uuid": schema.StringAttribute{
Required: true,
MarkdownDescription: "The UUID of the Building Block definition, that is used for this Building Block.",
},
"definition_version": schema.Int64Attribute{
Required: true,
MarkdownDescription: "Version number of the Building Block definition, that is used for this Building Block.",
},
"tenant_identifier": schema.StringAttribute{
Required: true,
MarkdownDescription: "The identifier of the tenant, this Building Block belongs to.",
},
"name": schema.StringAttribute{
Required: true,
MarkdownDescription: "The name of the Building Block.",
},
"display_name": schema.StringAttribute{
Required: true,
MarkdownDescription: "The display name of the Building Block.",
},
"inputs": schema.StringAttribute{
Optional: true,
MarkdownDescription: "Assigned input values for the Building Block.",
},
"parents": schema.SetAttribute{
Optional: true,
MarkdownDescription: "The Building Blocks, that are parents of this Building Block.",
},
},
}
}

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

client, ok := req.ProviderData.(*MeshStackProviderClient)

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

return
}

r.client = client
}

func (r *BuildingBlockResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data BuildingBlockResourceModel

// read from PLAN
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

// TODO use client to create resource

// save to STATE
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *BuildingBlockResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data BuildingBlockResourceModel

// read from STATE
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

// save to STATE
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *BuildingBlockResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data BuildingBlockResourceModel

// read from PLAN
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

// TODO use client to update resource

// save to STATE
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}

func (r *BuildingBlockResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data BuildingBlockResourceModel

// read from STATE
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}

// TODO use client to delete resource
}

func (r *BuildingBlockResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("uuid"), req, resp)
}
104 changes: 0 additions & 104 deletions internal/provider/example_data_source.go

This file was deleted.

Loading

0 comments on commit 831901c

Please sign in to comment.