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

Feature/building blocks #22

Merged
merged 3 commits into from
Jul 5, 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
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 != 200 {
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)
}
2 changes: 1 addition & 1 deletion client/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func (c *MeshStackProviderClient) CreateProject(project *MeshProjectCreate) (*Me
return nil, err
}

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

Expand Down
2 changes: 1 addition & 1 deletion client/tenant.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func (c *MeshStackProviderClient) CreateTenant(tenant *MeshTenantCreate) (*MeshT
return nil, err
}

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

Expand Down
46 changes: 24 additions & 22 deletions docs/data-sources/buildingblock.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
page_title: "meshstack_buildingblock Data Source - terraform-provider-meshstack"
subcategory: ""
description: |-
Query a single Building Block by UUID.
Single Building Block by UUID.
---

# meshstack_buildingblock (Data Source)

Query a single Building Block by UUID.
Single Building Block by UUID.

## Example Usage

Expand All @@ -25,11 +25,11 @@ data "meshstack_buildingblock" "example" {

### Required

- `metadata` (Attributes) Building Block metadata. UUID of the target Building Block must be set here. (see [below for nested schema](#nestedatt--metadata))
- `metadata` (Attributes) Building Block metadata. (see [below for nested schema](#nestedatt--metadata))

### Read-Only

- `api_version` (String) Building Block datatype version
- `api_version` (String) Building block datatype version
- `kind` (String) meshObject type, always `meshBuildingBlock`.
- `spec` (Attributes) Building Block specification. (see [below for nested schema](#nestedatt--spec))
- `status` (Attributes) Current Building Block status. (see [below for nested schema](#nestedatt--status))
Expand All @@ -39,47 +39,48 @@ data "meshstack_buildingblock" "example" {

Required:

- `uuid` (String)
- `uuid` (String) UUID which uniquely identifies the Building Block.

Read-Only:

- `created_on` (String)
- `definition_uuid` (String)
- `definition_version` (Number)
- `force_purge` (Boolean)
- `marked_for_deletion_by` (String)
- `marked_for_deletion_on` (String)
- `tenant_identifier` (String)
- `created_on` (String) Timestamp of Building Block creation.
- `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
- `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.
- `tenant_identifier` (String) Full tenant identifier of the tenant this Building Block is assigned to.


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

Read-Only:

- `display_name` (String)
- `inputs` (Attributes List) List of Building Block inputs. (see [below for nested schema](#nestedatt--spec--inputs))
- `parent_building_blocks` (Attributes List) (see [below for nested schema](#nestedatt--spec--parent_building_blocks))
- `display_name` (String) Display name for the Building Block as shown in meshPanel.
- `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--inputs))
- `parent_building_blocks` (Attributes List) List of parent Building Blocks. (see [below for nested schema](#nestedatt--spec--parent_building_blocks))

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

Read-Only:

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


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

Read-Only:

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



Expand All @@ -88,16 +89,17 @@ Read-Only:

Read-Only:

- `outputs` (Attributes List) List of building block outputs. (see [below for nested schema](#nestedatt--status--outputs))
- `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:

- `key` (String)
- `value_bool` (Boolean)
- `value_file` (String)
- `value_int` (Number)
- `value_list` (String) JSON encoded list of objects.
- `value_single_select` (String)
- `value_string` (String)
- `value_type` (String)
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" {
henryde marked this conversation as resolved.
Show resolved Hide resolved
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
Loading