Skip to content

Commit dc4ce8a

Browse files
authored
feat: Add project, environment resource and orgnanisation data resource (#168)
* feat: Add organisation data source * feat: Add project resource * feat: Add environment resource * Add test data * Add docs * !fixup: Add project resource * bump terraform-plugin-docs * bump terraform-plugin-sdk/v2 * bump terraform-plugin-framework-validators * update changelog * remove outdated comment
1 parent 28a1b23 commit dc4ce8a

16 files changed

+1215
-81
lines changed

.github/workflows/test.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,9 @@ jobs:
8282
FLAGSMITH_ENVIRONMENT_ID: 18143
8383
FLAGSMITH_FEATURE_ID: 25142
8484
FLAGSMITH_PROJECT_UUID: 97907de4-66d4-4d78-aae3-04f5a4518a55
85+
FLAGSMITH_PROJECT_ID: 7721
86+
FLAGSMITH_ORGANISATION_UUID: 0ee0578e-f2b8-467d-ba49-4cda324cea91
87+
FLAGSMITH_ORGANISATION_ID: 6880
8588
TF_ACC: "1"
8689
run: go test -v -cover ./flagsmith/
8790
timeout-minutes: 10

CHANGELOG.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,23 @@
1+
## 0.9.0
2+
FEATURES:
3+
* Add resource `flagsmith_project`
4+
* Add resource `flagsmith_environment`
5+
* Add data resource `flagsmith_organisation`
6+
7+
8+
## 0.8.2
9+
ENHANCEMENTS:
10+
* resource(flagsmith_tag): Make `tag_colour` optional
11+
12+
## 0.8.1
13+
BUG FIXES
14+
fix https://github.com/Flagsmith/terraform-provider-flagsmith/issues/155
15+
16+
17+
## 0.8.0
18+
NOTES:
19+
* This Go module(and related dependencies) has been updated to GO 1.21 as per the Go Support policy
20+
121
## 0.7.0
222
FEATURES:
323
* Add resource `flagsmith_tag`

docs/data-sources/organisation.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "flagsmith_organisation Data Source - terraform-provider-flagsmith"
4+
subcategory: ""
5+
description: |-
6+
Flagsmith Organisation
7+
---
8+
9+
# flagsmith_organisation (Data Source)
10+
11+
Flagsmith Organisation
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `uuid` (String) UUID of the organisation
21+
22+
### Read-Only
23+
24+
- `force_2fa` (Boolean) If true, signup will require 2FA
25+
- `id` (Number) ID of the organisation
26+
- `name` (String) Name of the organisation
27+
- `persist_trait_data` (Boolean) If false, trait data for this organisation identities will not stored
28+
- `restrict_project_create_to_admin` (Boolean) If true, only organisation admin can create projects

docs/resources/environment.md

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "flagsmith_environment Resource - terraform-provider-flagsmith"
4+
subcategory: ""
5+
description: |-
6+
Flagsmith Environment
7+
---
8+
9+
# flagsmith_environment (Resource)
10+
11+
Flagsmith Environment
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Name of the environment
21+
- `project_id` (Number) ID of the project
22+
23+
### Optional
24+
25+
- `allow_client_traits` (Boolean) Allows clients using the client API key to set traits.
26+
- `banner_colour` (String) hex code for the UI banner colour
27+
- `banner_text` (String) Banner text to display in the UI
28+
- `description` (String) Description of the environment
29+
- `hide_disabled_flags` (Boolean) If true will exclude flags from SDK which are disabled
30+
- `hide_sensitive_data` (Boolean) If true, will hide sensitive data(e.g: traits, description etc) from the SDK endpoints
31+
- `minimum_change_request_approvals` (Number) Minimum number of approvals required for a change request
32+
- `use_identity_composite_key_for_hashing` (Boolean) Enable this to have consistent multivariate and percentage split evaluations across all SDKs (in local and server side mode)
33+
34+
### Read-Only
35+
36+
- `api_key` (String) Client side API Key
37+
- `id` (Number) ID of the environment
38+
- `uuid` (String) UUID of the environment

docs/resources/project.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "flagsmith_project Resource - terraform-provider-flagsmith"
4+
subcategory: ""
5+
description: |-
6+
Flagsmith Project
7+
---
8+
9+
# flagsmith_project (Resource)
10+
11+
Flagsmith Project
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `name` (String) Name of the project
21+
- `organisation_id` (Number) ID of the organisation project belongs to
22+
23+
### Optional
24+
25+
- `enable_realtime_updates` (Boolean) Enable this to trigger a realtime(sse) event whenever the value of a flag changes
26+
- `feature_name_regex` (String) Used for validating feature names
27+
- `hide_disabled_flags` (Boolean) If true will exclude flags from SDK which are disabled
28+
- `only_allow_lower_case_feature_names` (Boolean) Used by UI to validate feature names
29+
- `prevent_flag_defaults` (Boolean) Prevent defaults from being set in all environments when creating a feature.
30+
- `stale_flags_limit_days` (Number) Number of days without modification in any environment before a flag is considered stale.
31+
32+
### Read-Only
33+
34+
- `id` (Number) ID of the project
35+
- `uuid` (String) UUID of the project
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package flagsmith
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"github.com/Flagsmith/flagsmith-go-api-client"
7+
"github.com/hashicorp/terraform-plugin-framework/datasource"
8+
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
9+
)
10+
11+
// Ensure provider defined types fully satisfy framework interfaces
12+
var _ datasource.DataSource = &organisationDataResource{}
13+
14+
func newOrganisationDataResource() datasource.DataSource {
15+
return &organisationDataResource{}
16+
}
17+
18+
type organisationDataResource struct {
19+
client *flagsmithapi.Client
20+
}
21+
22+
func (o *organisationDataResource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
23+
resp.TypeName = req.ProviderTypeName + "_organisation"
24+
}
25+
26+
func (o *organisationDataResource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
27+
// Prevent panic if the provider has not been configured.
28+
if req.ProviderData == nil {
29+
return
30+
}
31+
32+
client, ok := req.ProviderData.(*flagsmithapi.Client)
33+
if !ok {
34+
resp.Diagnostics.AddError(
35+
"Unexpected Resource Configure Type",
36+
fmt.Sprintf("Expected *flagsmithapi.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
37+
)
38+
return
39+
}
40+
41+
o.client = client
42+
}
43+
func (o *organisationDataResource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
44+
resp.Schema = schema.Schema{
45+
// This description is used by the documentation generator and the language server.
46+
MarkdownDescription: "Flagsmith Organisation",
47+
48+
Attributes: map[string]schema.Attribute{
49+
"uuid": schema.StringAttribute{
50+
Required: true,
51+
MarkdownDescription: "UUID of the organisation",
52+
},
53+
"id": schema.Int64Attribute{
54+
Computed: true,
55+
MarkdownDescription: "ID of the organisation",
56+
},
57+
"name": schema.StringAttribute{
58+
Computed: true,
59+
MarkdownDescription: "Name of the organisation",
60+
},
61+
"force_2fa": schema.BoolAttribute{
62+
Computed: true,
63+
MarkdownDescription: "If true, signup will require 2FA",
64+
},
65+
"persist_trait_data": schema.BoolAttribute{
66+
Computed: true,
67+
MarkdownDescription: "If false, trait data for this organisation identities will not stored",
68+
},
69+
"restrict_project_create_to_admin": schema.BoolAttribute{
70+
Computed: true,
71+
MarkdownDescription: "If true, only organisation admin can create projects",
72+
},
73+
},
74+
}
75+
}
76+
func (o *organisationDataResource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
77+
var data OrganisationResourceData
78+
diags := req.Config.Get(ctx, &data)
79+
resp.Diagnostics.Append(diags...)
80+
81+
// Early return if the state is wrong
82+
if diags.HasError() {
83+
return
84+
}
85+
86+
organisation, err := o.client.GetOrganisationByUUID(data.UUID.ValueString())
87+
if err != nil {
88+
panic(err)
89+
90+
}
91+
resourceData := MakeOrganisationResourceDataFromClientOrganisation(organisation)
92+
93+
diags = resp.State.Set(ctx, &resourceData)
94+
resp.Diagnostics.Append(diags...)
95+
96+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package flagsmith_test
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
7+
"testing"
8+
"strconv"
9+
)
10+
11+
func TestAccOrganisationDataResource(t *testing.T) {
12+
resource.Test(t, resource.TestCase{
13+
PreCheck: func() { testAccPreCheck(t) },
14+
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
15+
Steps: []resource.TestStep{
16+
{
17+
Config: testAccOrganisationDataResourceConfig(organisationUUID()),
18+
Check: resource.ComposeAggregateTestCheckFunc(
19+
20+
resource.TestCheckResourceAttr("data.flagsmith_organisation.test_org", "id", strconv.Itoa( organisationID())),
21+
resource.TestCheckResourceAttr("data.flagsmith_organisation.test_org", "uuid", organisationUUID()),
22+
23+
resource.TestCheckResourceAttrSet("data.flagsmith_organisation.test_org", "name"),
24+
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
32+
func testAccOrganisationDataResourceConfig(organisationUUID string) string {
33+
return fmt.Sprintf(`
34+
provider "flagsmith" {
35+
}
36+
37+
data "flagsmith_organisation" "test_org" {
38+
uuid = "%s"
39+
}
40+
41+
`,organisationUUID)
42+
}

0 commit comments

Comments
 (0)