Skip to content

Commit

Permalink
feat: meshstack_tag_definition resource
Browse files Browse the repository at this point in the history
  • Loading branch information
malhussan committed Nov 6, 2024
1 parent 4534653 commit 459fb22
Show file tree
Hide file tree
Showing 8 changed files with 767 additions and 2 deletions.
91 changes: 91 additions & 0 deletions client/tag_definition.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -165,3 +166,93 @@ func (c *MeshStackProviderClient) ReadTagDefinition(name string) (*MeshTagDefini

return &tagDefinition, nil
}

func (c *MeshStackProviderClient) CreateTagDefinition(tagDefinition *MeshTagDefinition) (*MeshTagDefinition, error) {
targetUrl := c.endpoints.TagDefinitions
data, err := json.Marshal(tagDefinition)
if err != nil {
return nil, fmt.Errorf("failed to marshal tag definition: %w", err)
}

fmt.Printf("JSON Payload: %s\n", string(data))

req, err := http.NewRequest("POST", targetUrl.String(), bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("Content-Type", CONTENT_TYPE_TAG_DEFINITION)
req.Header.Set("Accept", CONTENT_TYPE_TAG_DEFINITION)

resp, err := c.doAuthenticatedRequest(req)
if err != nil {
return nil, fmt.Errorf("failed to do authenticated request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusCreated {
return nil, fmt.Errorf("failed to create tag definition: %s", resp.Status)
}

var createdTagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&createdTagDefinition); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return &createdTagDefinition, nil
}

func (c *MeshStackProviderClient) UpdateTagDefinition(tagDefinition *MeshTagDefinition) (*MeshTagDefinition, error) {
targetUrl := c.urlForTagDefinition(tagDefinition.Metadata.Name)
data, err := json.Marshal(tagDefinition)
if err != nil {
return nil, fmt.Errorf("failed to marshal tag definition: %w", err)
}

req, err := http.NewRequest("PUT", targetUrl.String(), bytes.NewBuffer(data))
if err != nil {
return nil, fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("Content-Type", CONTENT_TYPE_TAG_DEFINITION)
req.Header.Set("Accept", CONTENT_TYPE_TAG_DEFINITION)

resp, err := c.doAuthenticatedRequest(req)
if err != nil {
return nil, fmt.Errorf("failed to do authenticated request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("failed to update tag definition: %s", resp.Status)
}

var updatedTagDefinition MeshTagDefinition
if err := json.NewDecoder(resp.Body).Decode(&updatedTagDefinition); err != nil {
return nil, fmt.Errorf("failed to decode response: %w", err)
}

return &updatedTagDefinition, nil
}

func (c *MeshStackProviderClient) DeleteTagDefinition(name string) error {
targetUrl := c.urlForTagDefinition(name)
req, err := http.NewRequest("DELETE", targetUrl.String(), nil)
if err != nil {
return fmt.Errorf("failed to create request: %w", err)
}

req.Header.Set("Accept", CONTENT_TYPE_TAG_DEFINITION)

resp, err := c.doAuthenticatedRequest(req)
if err != nil {
return fmt.Errorf("failed to do authenticated request: %w", err)
}
defer resp.Body.Close()

if resp.StatusCode != http.StatusNoContent {
return fmt.Errorf("failed to delete tag definition: %s", resp.Status)
}

return nil
}
2 changes: 1 addition & 1 deletion docs/resources/project.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ resource "meshstack_project" "example" {
### Read-Only

- `api_version` (String) Project datatype version
- `kind` (String) meshObject type, always `meshBuildingBlock`.
- `kind` (String) meshObject type, always `meshProject`.

<a id="nestedatt--metadata"></a>
### Nested Schema for `metadata`
Expand Down
154 changes: 154 additions & 0 deletions docs/resources/tag_definition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "meshstack_tag_definition Resource - terraform-provider-meshstack"
subcategory: ""
description: |-
Manage tag definitions
---

# meshstack_tag_definition (Resource)

Manage tag definitions

## Example Usage

```terraform
resource "meshstack_tag_definition" "example" {
metadata = {
name = "meshProject.example-key"
}
spec = {
target_kind = "meshProject"
key = "example-key"
display_name = "Example"
value_type = {
email = {
default_value = "default"
validation_regex = ".*"
}
}
description = "Example Description"
sort_order = 0
mandatory = false
immutable = false
restricted = false
}
}
```

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

### Required

- `metadata` (Attributes) Tag definition metadata. Name of the target tag definition must be set here. (see [below for nested schema](#nestedatt--metadata))
- `spec` (Attributes) Tag definition specification. (see [below for nested schema](#nestedatt--spec))

### Read-Only

- `api_version` (String) Tag definition datatype version
- `kind` (String) meshObject type, always `meshTagDefinition`.

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

Required:

- `name` (String)


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

Required:

- `display_name` (String)
- `key` (String)
- `target_kind` (String)
- `value_type` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type))

Optional:

- `description` (String)
- `immutable` (Boolean)
- `mandatory` (Boolean)
- `restricted` (Boolean)
- `sort_order` (Number)

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

Optional:

- `email` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type--email))
- `integer` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type--integer))
- `multi_select` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type--multi_select))
- `number` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type--number))
- `single_select` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type--single_select))
- `string` (Attributes) (see [below for nested schema](#nestedatt--spec--value_type--string))

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

Optional:

- `default_value` (String)
- `validation_regex` (String)


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

Optional:

- `default_value` (Number)


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

Optional:

- `default_value` (List of String)
- `options` (List of String)


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

Optional:

- `default_value` (Number)


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

Optional:

- `default_value` (String)
- `options` (List of String)


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

Optional:

- `default_value` (String)
- `validation_regex` (String)

## Import

Import is supported using the following syntax:

```shell
# import via tag definition name
terraform import 'meshstack_tag_definition.example' 'target_kind.key'

# import the example tag definition
terraform import 'meshstack_tag_definition.example' 'meshProject.example-key'
```
5 changes: 5 additions & 0 deletions examples/resources/meshstack_tag_definition/import.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# import via tag definition name
terraform import 'meshstack_tag_definition.example' 'target_kind.key'

# import the example tag definition
terraform import 'meshstack_tag_definition.example' 'meshProject.example-key'
24 changes: 24 additions & 0 deletions examples/resources/meshstack_tag_definition/resource.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
resource "meshstack_tag_definition" "example" {
metadata = {
name = "meshProject.example-key"
}

spec = {
target_kind = "meshProject"
key = "example-key"

display_name = "Example"

value_type = {
email = {
default_value = "default"
validation_regex = ".*"
}
}
description = "Example Description"
sort_order = 0
mandatory = false
immutable = false
restricted = false
}
}
2 changes: 1 addition & 1 deletion internal/provider/project_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ func (r *projectResource) Schema(_ context.Context, _ resource.SchemaRequest, re
},

"kind": schema.StringAttribute{
MarkdownDescription: "meshObject type, always `meshBuildingBlock`.",
MarkdownDescription: "meshObject type, always `meshProject`.",
Computed: true,
Validators: []validator.String{
stringvalidator.OneOf([]string{"meshProject"}...),
Expand Down
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ func (p *MeshStackProvider) Resources(ctx context.Context) []func() resource.Res
NewProjectUserBindingResource,
NewProjectGroupBindingResource,
NewBuildingBlockResource,
NewTagDefinitionResource,
}
}

Expand Down
Loading

0 comments on commit 459fb22

Please sign in to comment.