Skip to content

Commit

Permalink
feat: meshstack_tag_definition data source (GET)
Browse files Browse the repository at this point in the history
  • Loading branch information
malhussan committed Nov 5, 2024
1 parent 2a22ed8 commit 4534653
Show file tree
Hide file tree
Showing 6 changed files with 359 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client/tag_definition.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,9 @@ func (c *MeshStackProviderClient) ReadTagDefinition(name string) (*MeshTagDefini
return nil, err
}

resp, err := c.httpClient.Do(req)
req.Header.Set("Accept", CONTENT_TYPE_TAG_DEFINITION)

resp, err := c.doAuthenticatedRequest(req)
if err != nil {
return nil, err
}
Expand Down
126 changes: 126 additions & 0 deletions docs/data-sources/tag_definition.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "meshstack_tag_definition Data Source - terraform-provider-meshstack"
subcategory: ""
description: |-
A single tag definition by name.
---

# meshstack_tag_definition (Data Source)

A single tag definition by name.

## Example Usage

```terraform
data "meshstack_tag_definition" "example" {
name = "meshWorkspace.costCenter"
}
```

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

### Required

- `name` (String) The name of the tag definition.

### Read-Only

- `tag_definition` (Attributes) Tag definition details (see [below for nested schema](#nestedatt--tag_definition))

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

Read-Only:

- `api_version` (String) API Version of meshTagDefinition datatype. Matches the version part provided within the Accept request header.
- `kind` (String) As a common meshObject structure exists, every meshObject has a kind. This is always meshTagDefinition for this endpoint.
- `metadata` (Attributes) Always contains the 'name' to uniquely identify the meshTagDefinition. (see [below for nested schema](#nestedatt--tag_definition--metadata))
- `spec` (Attributes) Specification for the meshTagDefinition. (see [below for nested schema](#nestedatt--tag_definition--spec))

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

Read-Only:

- `name` (String) Must be of the form $targetKind.$key since tag definitions must be non-conflicting.


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

Read-Only:

- `description` (String) The detailed description of the tag.
- `display_name` (String) The display name of the tag.
- `immutable` (Boolean) Indicates whether the tag value is not editable after initially set.
- `key` (String) The key of the tag.
- `mandatory` (Boolean) Indicates whether the tag is mandatory.
- `restricted` (Boolean) Indicates whether only admins can edit this tag.
- `sort_order` (Number) The sort order for this tag when displayed in the UI. meshPanel sorts tags in ascending order.
- `target_kind` (String) The kind of meshObject this tag is defined for. Must be one of: meshProject, meshWorkspace, meshLandingZone, meshPaymentMethod or meshBuildingBlockDefinition.
- `value_type` (Attributes) The TagValueType of the tag. Must define exactly one of the available types. (see [below for nested schema](#nestedatt--tag_definition--spec--value_type))

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

Read-Only:

- `email` (Attributes) email address, represented as JSON string (see [below for nested schema](#nestedatt--tag_definition--spec--value_type--email))
- `integer` (Attributes) an integer, represented as a JSON number (see [below for nested schema](#nestedatt--tag_definition--spec--value_type--integer))
- `multi_select` (Attributes) one or multiple strings from a list of options, represented as a JSON array (see [below for nested schema](#nestedatt--tag_definition--spec--value_type--multi_select))
- `number` (Attributes) a decimal number, represented as a JSON number (see [below for nested schema](#nestedatt--tag_definition--spec--value_type--number))
- `single_select` (Attributes) a string from a list of options, represented as a JSON string (see [below for nested schema](#nestedatt--tag_definition--spec--value_type--single_select))
- `string` (Attributes) string, represented as JSON string (see [below for nested schema](#nestedatt--tag_definition--spec--value_type--string))

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

Read-Only:

- `default_value` (String) The default value of the tag.
- `validation_regex` (String) The regex pattern that the tag value must match.


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

Read-Only:

- `default_value` (Number) The default value of the tag.


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

Read-Only:

- `default_value` (List of String) The default value of the tag.
- `options` (List of String) The allowed options for the tag as a string[]


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

Read-Only:

- `default_value` (Number) The default value of the tag.


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

Read-Only:

- `default_value` (String) The default value of the tag.
- `options` (List of String) The allowed options for the tag as a string[]


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

Read-Only:

- `default_value` (String) The default value of the tag.
- `validation_regex` (String) The regex pattern that the tag value must match.
6 changes: 6 additions & 0 deletions docs/data-sources/tag_definitions.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,13 @@ description: |-

Tag definitions for the entire organization.

## Example Usage

```terraform
data "meshstack_tag_definitions" "all" {
# no attributes for filtering are supported at the moment
}
```

<!-- schema generated by tfplugindocs -->
## Schema
Expand Down
3 changes: 3 additions & 0 deletions examples/data-sources/meshstack_tag_definition/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
data "meshstack_tag_definition" "example" {
name = "meshWorkspace.costCenter"
}
1 change: 1 addition & 0 deletions internal/provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ func (p *MeshStackProvider) DataSources(ctx context.Context) []func() datasource
NewProjectUserBindingDataSource,
NewProjectGroupBindingDataSource,
NewTenantDataSource,
NewTagDefinitionDataSource,
NewTagDefinitionsDataSource,
}
}
Expand Down
220 changes: 220 additions & 0 deletions internal/provider/tag_definition_data_source.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,220 @@
package provider

import (
"context"
"fmt"

"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/meshcloud/terraform-provider-meshstack/client"
)

func NewTagDefinitionDataSource() datasource.DataSource {
return &tagDefinitionDataSource{}
}

type tagDefinitionDataSource struct {
client *client.MeshStackProviderClient
}

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

func (d *tagDefinitionDataSource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "A single tag definition by name.",

Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "The name of the tag definition.",
Required: true,
},
"tag_definition": schema.SingleNestedAttribute{
MarkdownDescription: "Tag definition details",
Computed: true,
Attributes: map[string]schema.Attribute{
"kind": schema.StringAttribute{
MarkdownDescription: "As a common meshObject structure exists, every meshObject has a kind. This is always meshTagDefinition for this endpoint.",
Computed: true,
},
"api_version": schema.StringAttribute{
MarkdownDescription: "API Version of meshTagDefinition datatype. Matches the version part provided within the Accept request header.",
Computed: true,
},
"metadata": schema.SingleNestedAttribute{
MarkdownDescription: "Always contains the 'name' to uniquely identify the meshTagDefinition.",
Computed: true,
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
MarkdownDescription: "Must be of the form $targetKind.$key since tag definitions must be non-conflicting.",
Computed: true,
},
},
},
"spec": schema.SingleNestedAttribute{
MarkdownDescription: "Specification for the meshTagDefinition.",
Computed: true,
Attributes: map[string]schema.Attribute{
"target_kind": schema.StringAttribute{
MarkdownDescription: "The kind of meshObject this tag is defined for. Must be one of: meshProject, meshWorkspace, meshLandingZone, meshPaymentMethod or meshBuildingBlockDefinition.",
Computed: true,
},
"key": schema.StringAttribute{
MarkdownDescription: "The key of the tag.",
Computed: true,
},
"value_type": schema.SingleNestedAttribute{
MarkdownDescription: "The TagValueType of the tag. Must define exactly one of the available types.",
Computed: true,
Attributes: map[string]schema.Attribute{
"string": schema.SingleNestedAttribute{
MarkdownDescription: "string, represented as JSON string",
Computed: true,
Attributes: map[string]schema.Attribute{
"default_value": schema.StringAttribute{
MarkdownDescription: "The default value of the tag.",
Computed: true,
},
"validation_regex": schema.StringAttribute{
MarkdownDescription: "The regex pattern that the tag value must match.",
Computed: true,
},
},
},
"email": schema.SingleNestedAttribute{
MarkdownDescription: "email address, represented as JSON string",
Computed: true,
Attributes: map[string]schema.Attribute{
"default_value": schema.StringAttribute{
MarkdownDescription: "The default value of the tag.",
Computed: true,
},
"validation_regex": schema.StringAttribute{
MarkdownDescription: "The regex pattern that the tag value must match.",
Computed: true,
},
},
},
"integer": schema.SingleNestedAttribute{
MarkdownDescription: "an integer, represented as a JSON number",
Computed: true,
Attributes: map[string]schema.Attribute{
"default_value": schema.Int64Attribute{
MarkdownDescription: "The default value of the tag.",
Computed: true,
},
},
},
"number": schema.SingleNestedAttribute{
MarkdownDescription: "a decimal number, represented as a JSON number",
Computed: true,
Attributes: map[string]schema.Attribute{
"default_value": schema.Float64Attribute{
MarkdownDescription: "The default value of the tag.",
Computed: true,
},
},
},
"single_select": schema.SingleNestedAttribute{
MarkdownDescription: "a string from a list of options, represented as a JSON string",
Computed: true,
Attributes: map[string]schema.Attribute{
"options": schema.ListAttribute{
MarkdownDescription: "The allowed options for the tag as a string[]",
Computed: true,
ElementType: types.StringType,
},
"default_value": schema.StringAttribute{
MarkdownDescription: "The default value of the tag.",
Computed: true,
},
},
},
"multi_select": schema.SingleNestedAttribute{
MarkdownDescription: "one or multiple strings from a list of options, represented as a JSON array",
Computed: true,
Attributes: map[string]schema.Attribute{
"options": schema.ListAttribute{
MarkdownDescription: "The allowed options for the tag as a string[]",
Computed: true,
ElementType: types.StringType,
},
"default_value": schema.ListAttribute{
MarkdownDescription: "The default value of the tag.",
Computed: true,
ElementType: types.StringType,
},
},
},
},
},
"description": schema.StringAttribute{
MarkdownDescription: "The detailed description of the tag.",
Computed: true,
},
"display_name": schema.StringAttribute{
MarkdownDescription: "The display name of the tag.",
Computed: true,
},
"sort_order": schema.Int64Attribute{
MarkdownDescription: "The sort order for this tag when displayed in the UI. meshPanel sorts tags in ascending order.",
Computed: true,
},
"mandatory": schema.BoolAttribute{
MarkdownDescription: "Indicates whether the tag is mandatory.",
Computed: true,
},
"immutable": schema.BoolAttribute{
MarkdownDescription: "Indicates whether the tag value is not editable after initially set.",
Computed: true,
},
"restricted": schema.BoolAttribute{
MarkdownDescription: "Indicates whether only admins can edit this tag.",
Computed: true,
},
},
},
},
},
},
}
}

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

client, ok := req.ProviderData.(*client.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 *tagDefinitionDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var name string
diags := req.Config.GetAttribute(ctx, path.Root("name"), &name)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}

tag, err := d.client.ReadTagDefinition(name)
if err != nil {
resp.Diagnostics.AddError("Unable to read meshTagDefinition", err.Error())
return
}

resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("tag_definition"), &tag)...)
}

0 comments on commit 4534653

Please sign in to comment.