Skip to content

Commit

Permalink
feat: building block resource
Browse files Browse the repository at this point in the history
  • Loading branch information
henryde committed Jul 3, 2024
1 parent ed57c9c commit b6a1d66
Show file tree
Hide file tree
Showing 6 changed files with 660 additions and 100 deletions.
80 changes: 74 additions & 6 deletions client/buildingblock.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
package client

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"net/url"
)

const (
MESH_BUILDING_BLOCK_IO_TYPE_STRING = "STRING"
MESH_BUILDING_BLOCK_IO_TYPE_INTEGER = "INTEGER"
MESH_BUILDING_BLOCK_IO_TYPE_BOOLEAN = "BOOLEAN"
MESH_BUILDING_BLOCK_IO_TYPE_SINGLE_SELECT = "SINGLE_SELECT"
MESH_BUILDING_BLOCK_IO_TYPE_FILE = "FILE"
MESH_BUILDING_BLOCK_IO_TYPE_LIST = "LIST"

CONTENT_TYPE_BUILDING_BLOCK = "application/vnd.meshcloud.api.meshbuildingblock.v1.hal+json"
)

type MeshBuildingBlock struct {
Expand Down Expand Up @@ -49,12 +61,25 @@ type MeshBuildingBlockStatus struct {
Outputs []MeshBuildingBlockIO `json:"outputs" tfsdk:"outputs"`
}

func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingBlock, error) {
if c.ensureValidToken() != nil {
return nil, errors.New(ERROR_AUTHENTICATION_FAILURE)
}
type MeshBuildingBlockCreate struct {
ApiVersion string `json:"apiVersion" tfsdk:"api_version"`
Kind string `json:"kind" tfsdk:"kind"`
Metadata MeshBuildingBlockCreateMetadata `json:"metadata" tfsdk:"metadata"`
Spec MeshBuildingBlockSpec `json:"spec" tfsdk:"spec"`
}

type MeshBuildingBlockCreateMetadata struct {
DefinitionUuid string `json:"definitionUuid" tfsdk:"definition_uuid"`
DefinitionVersion int64 `json:"definitionVersion" tfsdk:"definition_version"`
TenantIdentifier string `json:"tenantIdentifier" tfsdk:"tenant_identifier"`
}

func (c *MeshStackProviderClient) urlForBuildingBlock(uuid string) *url.URL {
return c.endpoints.BuildingBlocks.JoinPath(uuid)
}

targetUrl := c.endpoints.BuildingBlocks.JoinPath(uuid)
func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingBlock, error) {
targetUrl := c.urlForBuildingBlock(uuid)
req, err := http.NewRequest("GET", targetUrl.String(), nil)
if err != nil {
return nil, err
Expand Down Expand Up @@ -88,3 +113,46 @@ func (c *MeshStackProviderClient) ReadBuildingBlock(uuid string) (*MeshBuildingB

return &bb, nil
}

func (c *MeshStackProviderClient) CreateBuildingBlock(bb *MeshBuildingBlockCreate) (*MeshBuildingBlock, error) {
payload, err := json.Marshal(bb)
if err != nil {
return nil, err
}

req, err := http.NewRequest("POST", c.endpoints.BuildingBlocks.String(), bytes.NewBuffer(payload))
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", CONTENT_TYPE_BUILDING_BLOCK)
req.Header.Set("Accept", CONTENT_TYPE_BUILDING_BLOCK)

res, err := c.doAuthenticatedRequest(req)
if err != nil {
return nil, err
}

defer res.Body.Close()

data, err := io.ReadAll(res.Body)
if err != nil {
return nil, err
}

if res.StatusCode != 201 {
return nil, fmt.Errorf("unexpected status code: %d, %s", res.StatusCode, data)
}

var createdBb MeshBuildingBlock
err = json.Unmarshal(data, &createdBb)
if err != nil {
return nil, err
}

return &createdBb, nil
}

func (c *MeshStackProviderClient) DeleteBuildingBlock(uuid string) error {
targetUrl := c.urlForBuildingBlock(uuid)
return c.deleteMeshObject(*targetUrl, 202)
}
136 changes: 136 additions & 0 deletions docs/resources/buildingblock.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "meshstack_buildingblock Resource - terraform-provider-meshstack"
subcategory: ""
description: |-
Manage Building Block assignment.
---

# meshstack_buildingblock (Resource)

Manage Building Block assignment.

## Example Usage

```terraform
resource "meshstack_buildingblock" "my_buildingblock" {
metadata = {
definition_uuid = "f012248e-dda9-4763-8706-641a35de6c62"
definition_version = 1
tenant_identifier = "my-workspace.my-project-dev.my-platform.my-location"
}
spec = {
display_name = "my-buildingblock"
inputs = {
name = { value_string = "my-name" }
size = { value_int = 16 }
}
}
}
```

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

### Required

- `metadata` (Attributes) Building Block metadata. (see [below for nested schema](#nestedatt--metadata))
- `spec` (Attributes) Building Block specification. (see [below for nested schema](#nestedatt--spec))

### Read-Only

- `api_version` (String) Building block datatype version
- `kind` (String) meshObject type, always `meshBuildingBlock`.
- `status` (Attributes) Current Building Block status. (see [below for nested schema](#nestedatt--status))

<a id="nestedatt--metadata"></a>
### Nested Schema for `metadata`

Required:

- `definition_uuid` (String) UUID of the Building Block Definition this Building Block is based on.
- `definition_version` (Number) Version number of the Building Block Definition this Building Block is based on
- `tenant_identifier` (String) Full tenant identifier of the tenant this Building Block is assigned to.

Read-Only:

- `created_on` (String) Timestamp of Building Block creation.
- `force_purge` (Boolean) Indicates whether an operator has requested purging of this Building Block.
- `marked_for_deletion_by` (String) For deleted Building Blocks: user who requested deletion.
- `marked_for_deletion_on` (String) For deleted Building Blocks: timestamp of deletion.
- `uuid` (String) UUID which uniquely identifies the Building Block.


<a id="nestedatt--spec"></a>
### Nested Schema for `spec`

Required:

- `display_name` (String) Display name for the Building Block as shown in meshPanel.

Optional:

- `inputs` (Attributes Map) Building Block user inputs. Each input has exactly one value. Use the value attribute that corresponds to the desired input type, e.g. `value_int` to set an integer input, and leave the remaining attributes empty. (see [below for nested schema](#nestedatt--spec--inputs))
- `parent_building_blocks` (Attributes List) List of parent Building Blocks. (see [below for nested schema](#nestedatt--spec--parent_building_blocks))

Read-Only:

- `combined_inputs` (Attributes Map) Contains all Building Block inputs. Each input has exactly one value attribute set according to its' type. (see [below for nested schema](#nestedatt--spec--combined_inputs))

<a id="nestedatt--spec--inputs"></a>
### Nested Schema for `spec.inputs`

Optional:

- `value_bool` (Boolean)
- `value_file` (String)
- `value_int` (Number)
- `value_list` (String) JSON encoded list of objects.
- `value_single_select` (String)
- `value_string` (String)


<a id="nestedatt--spec--parent_building_blocks"></a>
### Nested Schema for `spec.parent_building_blocks`

Required:

- `buildingblock_uuid` (String) UUID of the parent Building Block.
- `definition_uuid` (String) UUID of the parent Building Block definition.


<a id="nestedatt--spec--combined_inputs"></a>
### Nested Schema for `spec.combined_inputs`

Read-Only:

- `value_bool` (Boolean)
- `value_file` (String)
- `value_int` (Number)
- `value_list` (String) JSON encoded list of objects.
- `value_single_select` (String)
- `value_string` (String)



<a id="nestedatt--status"></a>
### Nested Schema for `status`

Read-Only:

- `outputs` (Attributes Map) Building Block outputs. Each output has exactly one value attribute set. (see [below for nested schema](#nestedatt--status--outputs))
- `status` (String) Execution status. One of `WAITING_FOR_DEPENDENT_INPUT`, `WAITING_FOR_OPERATOR_INPUT`, `PENDING`, `IN_PROGRESS`, `SUCCEEDED`, `FAILED`.

<a id="nestedatt--status--outputs"></a>
### Nested Schema for `status.outputs`

Read-Only:

- `value_bool` (Boolean)
- `value_file` (String)
- `value_int` (Number)
- `value_list` (String) JSON encoded list of objects.
- `value_single_select` (String)
- `value_string` (String)
16 changes: 16 additions & 0 deletions examples/resources/meshstack_buildingblock/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
resource "meshstack_buildingblock" "my_buildingblock" {
metadata = {
definition_uuid = "f012248e-dda9-4763-8706-641a35de6c62"
definition_version = 1
tenant_identifier = "my-workspace.my-project-dev.my-platform.my-location"
}

spec = {
display_name = "my-buildingblock"

inputs = {
name = { value_string = "my-name" }
size = { value_int = 16 }
}
}
}
Loading

0 comments on commit b6a1d66

Please sign in to comment.