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

feat: Add project resource and data source #41

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
1 change: 1 addition & 0 deletions docs/data-sources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ A representation of a cluster you can deploy to.
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).
- `name` (String) Human-readable name of this cluster, that also translates to cloud resource name.
- `node_pools` (Attributes Map) Map of node pool specs managed by this cluster, where the key is name of the node pool and value contains the spec. (see [below for nested schema](#nestedatt--node_pools))
- `project_id` (String) ID of the project that this cluster belongs to.
- `protect` (Boolean) If set to `true` then this cluster cannot be deleted.
- `provider_id` (String) Provider used to create this cluster.
- `tags` (Map of String) Key-value tags used to filter clusters.
Expand Down
54 changes: 54 additions & 0 deletions docs/data-sources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "plural_project Data Source - terraform-provider-plural"
subcategory: ""
description: |-

---

# plural_project (Data Source)





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

### Optional

- `bindings` (Attributes) Read and write policies of this project. (see [below for nested schema](#nestedatt--bindings))
- `description` (String) Description of this project.
- `id` (String) Internal identifier of this project.
- `name` (String) Human-readable name of this project.

### Read-Only

- `default` (Boolean)

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

Optional:

- `read` (Attributes Set) Read policies of this project. (see [below for nested schema](#nestedatt--bindings--read))
- `write` (Attributes Set) Write policies of this project. (see [below for nested schema](#nestedatt--bindings--write))

<a id="nestedatt--bindings--read"></a>
### Nested Schema for `bindings.read`

Optional:

- `group_id` (String)
- `id` (String)
- `user_id` (String)


<a id="nestedatt--bindings--write"></a>
### Nested Schema for `bindings.write`

Optional:

- `group_id` (String)
- `id` (String)
- `user_id` (String)
1 change: 1 addition & 0 deletions docs/resources/cluster.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ A representation of a cluster you can deploy to.
- `helm_values` (String) Additional Helm values you'd like to use in deployment agent Helm installs. This is useful for BYOK clusters that need to use custom images or other constructs.
- `kubeconfig` (Attributes) (see [below for nested schema](#nestedatt--kubeconfig))
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).
- `project_id` (String) ID of the project that this cluster belongs to.
- `protect` (Boolean) If set to `true` then this cluster cannot be deleted.
- `tags` (Map of String) Key-value tags used to filter clusters.

Expand Down
1 change: 1 addition & 0 deletions docs/resources/infrastructure_stack.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ description: |-
- `environment` (Attributes Set) Defines environment variables for the stack. (see [below for nested schema](#nestedatt--environment))
- `files` (Map of String) File path-content map.
- `job_spec` (Attributes) Repository information used to pull stack. (see [below for nested schema](#nestedatt--job_spec))
- `project_id` (String) ID of the project that this stack belongs to.

### Read-Only

Expand Down
57 changes: 57 additions & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "plural_project Resource - terraform-provider-plural"
subcategory: ""
description: |-

---

# plural_project (Resource)





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

### Required

- `name` (String) Human-readable name of this project.

### Optional

- `bindings` (Attributes) Read and write policies of this project. (see [below for nested schema](#nestedatt--bindings))
- `description` (String) Description of this project.

### Read-Only

- `default` (Boolean)
- `id` (String) Internal identifier of this project.

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

Optional:

- `read` (Attributes Set) Read policies of this project. (see [below for nested schema](#nestedatt--bindings--read))
- `write` (Attributes Set) Write policies of this project. (see [below for nested schema](#nestedatt--bindings--write))

<a id="nestedatt--bindings--read"></a>
### Nested Schema for `bindings.read`

Optional:

- `group_id` (String)
- `id` (String)
- `user_id` (String)


<a id="nestedatt--bindings--write"></a>
### Nested Schema for `bindings.write`

Optional:

- `group_id` (String)
- `id` (String)
- `user_id` (String)
59 changes: 59 additions & 0 deletions example/project/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
terraform {
required_providers {
plural = {
source = "pluralsh/plural"
version = "0.2.1"
}
}
}

provider "plural" {
use_cli = true
}

data "plural_project" "default" {
name = "default"
}

data "plural_cluster" "mgmt" {
handle = "mgmt"
}

data "plural_git_repository" "tf-hello" {
url = "https://github.com/zreigz/tf-hello.git"
}

resource "random_string" "random" {
length = 5
upper = false
special = false
}

# resource "plural_project" "test" {
# name = "test-${random_string.random.result}"
# description = "test project created by terraform"
# }
#
# resource "plural_cluster" "byok" {
# name = "byok-${random_string.random.result}"
# project_id = data.plural_project.default.id
# kubeconfig = {
# # Required, can be sourced from environment variables
# # export PLURAL_KUBE_CONFIG_PATH to read from local file
# }
# }

resource "plural_infrastructure_stack" "tf-hello" {
name = "tf-hello-${random_string.random.result}"
type = "TERRAFORM"
cluster_id = data.plural_cluster.mgmt.id
repository = {
id = data.plural_git_repository.tf-hello.id
ref = "main"
folder = "terraform"
}
configuration = {
image = "ghcr.io/pluralsh/harness"
version = "sha-e9b2089-terraform-1.8"
}
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ require (
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
github.com/hashicorp/terraform-plugin-log v0.9.0
github.com/mitchellh/go-homedir v1.1.0
github.com/pluralsh/console-client-go v0.5.16
github.com/pluralsh/console-client-go v0.8.0
github.com/pluralsh/plural-cli v0.8.5-0.20240216094552-efc34ee6de37
github.com/pluralsh/polly v0.1.7
github.com/samber/lo v1.38.1
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -856,8 +856,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
github.com/pluralsh/console-client-go v0.5.16 h1:AHBnis/xUdXbUclNH1GUwcqAsCOzHzOtwetKOIS0dTM=
github.com/pluralsh/console-client-go v0.5.16/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/console-client-go v0.8.0 h1:wCX92bN5EPW8g58neebJPFOm0YQKPCdLBJItSku4dec=
github.com/pluralsh/console-client-go v0.8.0/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA=
github.com/pluralsh/gqlclient v1.11.0/go.mod h1:qSXKUlio1F2DRPy8el4oFYsmpKbkUYspgPB87T4it5I=
github.com/pluralsh/plural-cli v0.8.5-0.20240216094552-efc34ee6de37 h1:DBnaKvKmbTbKwbkrh/2gJBwyHYfaXdxeT3UGh+94K4g=
Expand Down
34 changes: 17 additions & 17 deletions internal/common/cluster_bindings.go → internal/common/bindings.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,34 +10,34 @@ import (
console "github.com/pluralsh/console-client-go"
)

type ClusterBindings struct {
type Bindings struct {
Read types.Set `tfsdk:"read"`
Write types.Set `tfsdk:"write"`
}

func (cb *ClusterBindings) ReadAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
func (cb *Bindings) ReadAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
if cb == nil {
return nil
}

return clusterPolicyBindingAttributes(cb.Read, ctx, d)
return policyBindingAttributes(cb.Read, ctx, d)
}

func (cb *ClusterBindings) WriteAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
func (cb *Bindings) WriteAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
if cb == nil {
return nil
}

return clusterPolicyBindingAttributes(cb.Write, ctx, d)
return policyBindingAttributes(cb.Write, ctx, d)
}

func clusterPolicyBindingAttributes(bindings types.Set, ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
func policyBindingAttributes(bindings types.Set, ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
if bindings.IsNull() {
return nil
}

result := make([]*console.PolicyBindingAttributes, 0, len(bindings.Elements()))
elements := make([]ClusterPolicyBinding, len(bindings.Elements()))
elements := make([]PolicyBinding, len(bindings.Elements()))
d.Append(bindings.ElementsAs(ctx, &elements, false)...)

for _, binding := range elements {
Expand All @@ -51,16 +51,16 @@ func clusterPolicyBindingAttributes(bindings types.Set, ctx context.Context, d d
return result
}

func (cb *ClusterBindings) From(readBindings []*console.PolicyBindingFragment, writeBindings []*console.PolicyBindingFragment, ctx context.Context, d diag.Diagnostics) {
func (cb *Bindings) From(readBindings []*console.PolicyBindingFragment, writeBindings []*console.PolicyBindingFragment, ctx context.Context, d diag.Diagnostics) {
if cb == nil {
return
}

cb.Read = clusterBindingsFrom(readBindings, cb.Read, ctx, d)
cb.Write = clusterBindingsFrom(writeBindings, cb.Write, ctx, d)
cb.Read = bindingsFrom(readBindings, cb.Read, ctx, d)
cb.Write = bindingsFrom(writeBindings, cb.Write, ctx, d)
}

func clusterBindingsFrom(bindings []*console.PolicyBindingFragment, config types.Set, ctx context.Context, d diag.Diagnostics) types.Set {
func bindingsFrom(bindings []*console.PolicyBindingFragment, config types.Set, ctx context.Context, d diag.Diagnostics) types.Set {
if len(bindings) == 0 {
// Rewriting config to state to avoid inconsistent result errors.
// This could happen, for example, when sending "nil" to API and "[]" is returned as a result.
Expand All @@ -69,7 +69,7 @@ func clusterBindingsFrom(bindings []*console.PolicyBindingFragment, config types

values := make([]attr.Value, len(bindings))
for i, binding := range bindings {
value := ClusterPolicyBinding{
value := PolicyBinding{
ID: types.StringPointerValue(binding.ID),
}

Expand All @@ -81,29 +81,29 @@ func clusterBindingsFrom(bindings []*console.PolicyBindingFragment, config types
value.GroupID = types.StringValue(binding.Group.ID)
}

objValue, diags := types.ObjectValueFrom(ctx, ClusterPolicyBindingAttrTypes, value)
objValue, diags := types.ObjectValueFrom(ctx, PolicyBindingAttrTypes, value)
values[i] = objValue
d.Append(diags...)
}

setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: ClusterPolicyBindingAttrTypes}, values)
setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: PolicyBindingAttrTypes}, values)
d.Append(diags...)
return setValue
}

type ClusterPolicyBinding struct {
type PolicyBinding struct {
GroupID types.String `tfsdk:"group_id"`
ID types.String `tfsdk:"id"`
UserID types.String `tfsdk:"user_id"`
}

var ClusterPolicyBindingAttrTypes = map[string]attr.Type{
var PolicyBindingAttrTypes = map[string]attr.Type{
"group_id": types.StringType,
"id": types.StringType,
"user_id": types.StringType,
}

func (cpb *ClusterPolicyBinding) Attributes() *console.PolicyBindingAttributes {
func (cpb *PolicyBinding) Attributes() *console.PolicyBindingAttributes {
return &console.PolicyBindingAttributes{
ID: cpb.ID.ValueStringPointer(),
UserID: cpb.UserID.ValueStringPointer(),
Expand Down
14 changes: 14 additions & 0 deletions internal/common/project.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package common

import (
"github.com/hashicorp/terraform-plugin-framework/types"
gqlclient "github.com/pluralsh/console-client-go"
)

func ProjectFrom(project *gqlclient.TinyProjectFragment) types.String {
if project != nil {
return types.StringValue(project.ID)
}

return types.StringNull()
}
5 changes: 5 additions & 0 deletions internal/datasource/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,11 @@ func (d *clusterDataSource) Schema(_ context.Context, _ datasource.SchemaRequest
MarkdownDescription: "Human-readable name of this cluster, that also translates to cloud resource name.",
Computed: true,
},
"project_id": schema.StringAttribute{
Description: "ID of the project that this cluster belongs to.",
MarkdownDescription: "ID of the project that this cluster belongs to.",
Computed: true,
},
"handle": schema.StringAttribute{
MarkdownDescription: "A short, unique human-readable name used to identify this cluster. Does not necessarily map to the cloud resource name.",
Optional: true,
Expand Down
2 changes: 2 additions & 0 deletions internal/datasource/cluster_model.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ type cluster struct {
InsertedAt types.String `tfsdk:"inserted_at"`
Name types.String `tfsdk:"name"`
Handle types.String `tfsdk:"handle"`
ProjectId types.String `tfsdk:"project_id"`
DesiredVersion types.String `tfsdk:"desired_version"`
ProviderId types.String `tfsdk:"provider_id"`
Cloud types.String `tfsdk:"cloud"`
Expand All @@ -37,6 +38,7 @@ func (c *cluster) From(cl *console.ClusterFragment, ctx context.Context, d diag.
c.InsertedAt = types.StringPointerValue(cl.InsertedAt)
c.Name = types.StringValue(cl.Name)
c.Handle = types.StringPointerValue(cl.Handle)
c.ProjectId = common.ProjectFrom(cl.Project)
c.DesiredVersion = types.StringPointerValue(cl.Version)
c.Protect = types.BoolPointerValue(cl.Protect)
c.Tags = common.TagsFrom(cl.Tags, c.Tags, d)
Expand Down
Loading
Loading