Skip to content

Commit 72b6db2

Browse files
authored
Merge pull request #41 from pluralsh/marcin/prod-2233-add-support-for-projects-to-terraform-provider
feat: Add project resource and data source
2 parents 6b54ee0 + 7d1b7ee commit 72b6db2

File tree

21 files changed

+625
-33
lines changed

21 files changed

+625
-33
lines changed

docs/data-sources/cluster.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ A representation of a cluster you can deploy to.
2828
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).
2929
- `name` (String) Human-readable name of this cluster, that also translates to cloud resource name.
3030
- `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))
31+
- `project_id` (String) ID of the project that this cluster belongs to.
3132
- `protect` (Boolean) If set to `true` then this cluster cannot be deleted.
3233
- `provider_id` (String) Provider used to create this cluster.
3334
- `tags` (Map of String) Key-value tags used to filter clusters.

docs/data-sources/project.md

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "plural_project Data Source - terraform-provider-plural"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# plural_project (Data Source)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Optional
19+
20+
- `bindings` (Attributes) Read and write policies of this project. (see [below for nested schema](#nestedatt--bindings))
21+
- `description` (String) Description of this project.
22+
- `id` (String) Internal identifier of this project.
23+
- `name` (String) Human-readable name of this project.
24+
25+
### Read-Only
26+
27+
- `default` (Boolean)
28+
29+
<a id="nestedatt--bindings"></a>
30+
### Nested Schema for `bindings`
31+
32+
Optional:
33+
34+
- `read` (Attributes Set) Read policies of this project. (see [below for nested schema](#nestedatt--bindings--read))
35+
- `write` (Attributes Set) Write policies of this project. (see [below for nested schema](#nestedatt--bindings--write))
36+
37+
<a id="nestedatt--bindings--read"></a>
38+
### Nested Schema for `bindings.read`
39+
40+
Optional:
41+
42+
- `group_id` (String)
43+
- `id` (String)
44+
- `user_id` (String)
45+
46+
47+
<a id="nestedatt--bindings--write"></a>
48+
### Nested Schema for `bindings.write`
49+
50+
Optional:
51+
52+
- `group_id` (String)
53+
- `id` (String)
54+
- `user_id` (String)

docs/resources/cluster.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ A representation of a cluster you can deploy to.
2828
- `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.
2929
- `kubeconfig` (Attributes) (see [below for nested schema](#nestedatt--kubeconfig))
3030
- `metadata` (String) Arbitrary JSON metadata to store user-specific state of this cluster (e.g. IAM roles for add-ons).
31+
- `project_id` (String) ID of the project that this cluster belongs to.
3132
- `protect` (Boolean) If set to `true` then this cluster cannot be deleted.
3233
- `tags` (Map of String) Key-value tags used to filter clusters.
3334

docs/resources/infrastructure_stack.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ description: |-
3232
- `environment` (Attributes Set) Defines environment variables for the stack. (see [below for nested schema](#nestedatt--environment))
3333
- `files` (Map of String) File path-content map.
3434
- `job_spec` (Attributes) Repository information used to pull stack. (see [below for nested schema](#nestedatt--job_spec))
35+
- `project_id` (String) ID of the project that this stack belongs to.
3536

3637
### Read-Only
3738

docs/resources/project.md

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "plural_project Resource - terraform-provider-plural"
4+
subcategory: ""
5+
description: |-
6+
7+
---
8+
9+
# plural_project (Resource)
10+
11+
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Human-readable name of this project.
21+
22+
### Optional
23+
24+
- `bindings` (Attributes) Read and write policies of this project. (see [below for nested schema](#nestedatt--bindings))
25+
- `description` (String) Description of this project.
26+
27+
### Read-Only
28+
29+
- `default` (Boolean)
30+
- `id` (String) Internal identifier of this project.
31+
32+
<a id="nestedatt--bindings"></a>
33+
### Nested Schema for `bindings`
34+
35+
Optional:
36+
37+
- `read` (Attributes Set) Read policies of this project. (see [below for nested schema](#nestedatt--bindings--read))
38+
- `write` (Attributes Set) Write policies of this project. (see [below for nested schema](#nestedatt--bindings--write))
39+
40+
<a id="nestedatt--bindings--read"></a>
41+
### Nested Schema for `bindings.read`
42+
43+
Optional:
44+
45+
- `group_id` (String)
46+
- `id` (String)
47+
- `user_id` (String)
48+
49+
50+
<a id="nestedatt--bindings--write"></a>
51+
### Nested Schema for `bindings.write`
52+
53+
Optional:
54+
55+
- `group_id` (String)
56+
- `id` (String)
57+
- `user_id` (String)

example/project/main.tf

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
terraform {
2+
required_providers {
3+
plural = {
4+
source = "pluralsh/plural"
5+
version = "0.2.1"
6+
}
7+
}
8+
}
9+
10+
provider "plural" {
11+
use_cli = true
12+
}
13+
14+
data "plural_project" "default" {
15+
name = "default"
16+
}
17+
18+
data "plural_cluster" "mgmt" {
19+
handle = "mgmt"
20+
}
21+
22+
data "plural_git_repository" "tf-hello" {
23+
url = "https://github.com/zreigz/tf-hello.git"
24+
}
25+
26+
resource "random_string" "random" {
27+
length = 5
28+
upper = false
29+
special = false
30+
}
31+
32+
# resource "plural_project" "test" {
33+
# name = "test-${random_string.random.result}"
34+
# description = "test project created by terraform"
35+
# }
36+
#
37+
# resource "plural_cluster" "byok" {
38+
# name = "byok-${random_string.random.result}"
39+
# project_id = data.plural_project.default.id
40+
# kubeconfig = {
41+
# # Required, can be sourced from environment variables
42+
# # export PLURAL_KUBE_CONFIG_PATH to read from local file
43+
# }
44+
# }
45+
46+
resource "plural_infrastructure_stack" "tf-hello" {
47+
name = "tf-hello-${random_string.random.result}"
48+
type = "TERRAFORM"
49+
cluster_id = data.plural_cluster.mgmt.id
50+
repository = {
51+
id = data.plural_git_repository.tf-hello.id
52+
ref = "main"
53+
folder = "terraform"
54+
}
55+
configuration = {
56+
image = "ghcr.io/pluralsh/harness"
57+
version = "sha-e9b2089-terraform-1.8"
58+
}
59+
}

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ require (
1212
github.com/hashicorp/terraform-plugin-framework-validators v0.12.0
1313
github.com/hashicorp/terraform-plugin-log v0.9.0
1414
github.com/mitchellh/go-homedir v1.1.0
15-
github.com/pluralsh/console-client-go v0.5.16
15+
github.com/pluralsh/console-client-go v0.8.0
1616
github.com/pluralsh/plural-cli v0.8.5-0.20240216094552-efc34ee6de37
1717
github.com/pluralsh/polly v0.1.7
1818
github.com/samber/lo v1.38.1

go.sum

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -856,8 +856,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
856856
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
857857
github.com/pkg/sftp v1.10.1/go.mod h1:lYOWFsE0bwd1+KfKJaKeuokY15vzFx25BLbzYYoAxZI=
858858
github.com/pkg/sftp v1.13.1/go.mod h1:3HaPG6Dq1ILlpPZRO0HVMrsydcdLt6HRDccSgb87qRg=
859-
github.com/pluralsh/console-client-go v0.5.16 h1:AHBnis/xUdXbUclNH1GUwcqAsCOzHzOtwetKOIS0dTM=
860-
github.com/pluralsh/console-client-go v0.5.16/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
859+
github.com/pluralsh/console-client-go v0.8.0 h1:wCX92bN5EPW8g58neebJPFOm0YQKPCdLBJItSku4dec=
860+
github.com/pluralsh/console-client-go v0.8.0/go.mod h1:eyCiLA44YbXiYyJh8303jk5JdPkt9McgCo5kBjk4lKo=
861861
github.com/pluralsh/gqlclient v1.11.0 h1:FfXW7FiEJLHOfTAa7NxDb8jb3aMZNIpCAcG+bg8uHYA=
862862
github.com/pluralsh/gqlclient v1.11.0/go.mod h1:qSXKUlio1F2DRPy8el4oFYsmpKbkUYspgPB87T4it5I=
863863
github.com/pluralsh/plural-cli v0.8.5-0.20240216094552-efc34ee6de37 h1:DBnaKvKmbTbKwbkrh/2gJBwyHYfaXdxeT3UGh+94K4g=

internal/common/cluster_bindings.go renamed to internal/common/bindings.go

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,34 @@ import (
1010
console "github.com/pluralsh/console-client-go"
1111
)
1212

13-
type ClusterBindings struct {
13+
type Bindings struct {
1414
Read types.Set `tfsdk:"read"`
1515
Write types.Set `tfsdk:"write"`
1616
}
1717

18-
func (cb *ClusterBindings) ReadAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
18+
func (cb *Bindings) ReadAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
1919
if cb == nil {
2020
return nil
2121
}
2222

23-
return clusterPolicyBindingAttributes(cb.Read, ctx, d)
23+
return policyBindingAttributes(cb.Read, ctx, d)
2424
}
2525

26-
func (cb *ClusterBindings) WriteAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
26+
func (cb *Bindings) WriteAttributes(ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
2727
if cb == nil {
2828
return nil
2929
}
3030

31-
return clusterPolicyBindingAttributes(cb.Write, ctx, d)
31+
return policyBindingAttributes(cb.Write, ctx, d)
3232
}
3333

34-
func clusterPolicyBindingAttributes(bindings types.Set, ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
34+
func policyBindingAttributes(bindings types.Set, ctx context.Context, d diag.Diagnostics) []*console.PolicyBindingAttributes {
3535
if bindings.IsNull() {
3636
return nil
3737
}
3838

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

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

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

59-
cb.Read = clusterBindingsFrom(readBindings, cb.Read, ctx, d)
60-
cb.Write = clusterBindingsFrom(writeBindings, cb.Write, ctx, d)
59+
cb.Read = bindingsFrom(readBindings, cb.Read, ctx, d)
60+
cb.Write = bindingsFrom(writeBindings, cb.Write, ctx, d)
6161
}
6262

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

7070
values := make([]attr.Value, len(bindings))
7171
for i, binding := range bindings {
72-
value := ClusterPolicyBinding{
72+
value := PolicyBinding{
7373
ID: types.StringPointerValue(binding.ID),
7474
}
7575

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

84-
objValue, diags := types.ObjectValueFrom(ctx, ClusterPolicyBindingAttrTypes, value)
84+
objValue, diags := types.ObjectValueFrom(ctx, PolicyBindingAttrTypes, value)
8585
values[i] = objValue
8686
d.Append(diags...)
8787
}
8888

89-
setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: ClusterPolicyBindingAttrTypes}, values)
89+
setValue, diags := types.SetValue(basetypes.ObjectType{AttrTypes: PolicyBindingAttrTypes}, values)
9090
d.Append(diags...)
9191
return setValue
9292
}
9393

94-
type ClusterPolicyBinding struct {
94+
type PolicyBinding struct {
9595
GroupID types.String `tfsdk:"group_id"`
9696
ID types.String `tfsdk:"id"`
9797
UserID types.String `tfsdk:"user_id"`
9898
}
9999

100-
var ClusterPolicyBindingAttrTypes = map[string]attr.Type{
100+
var PolicyBindingAttrTypes = map[string]attr.Type{
101101
"group_id": types.StringType,
102102
"id": types.StringType,
103103
"user_id": types.StringType,
104104
}
105105

106-
func (cpb *ClusterPolicyBinding) Attributes() *console.PolicyBindingAttributes {
106+
func (cpb *PolicyBinding) Attributes() *console.PolicyBindingAttributes {
107107
return &console.PolicyBindingAttributes{
108108
ID: cpb.ID.ValueStringPointer(),
109109
UserID: cpb.UserID.ValueStringPointer(),

internal/common/project.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package common
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
gqlclient "github.com/pluralsh/console-client-go"
6+
)
7+
8+
func ProjectFrom(project *gqlclient.TinyProjectFragment) types.String {
9+
if project != nil {
10+
return types.StringValue(project.ID)
11+
}
12+
13+
return types.StringNull()
14+
}

0 commit comments

Comments
 (0)