Skip to content

Commit

Permalink
feat: Add project, environment resource and orgnanisation data resour…
Browse files Browse the repository at this point in the history
…ce (#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
  • Loading branch information
gagantrivedi authored Dec 6, 2024
1 parent 28a1b23 commit dc4ce8a
Show file tree
Hide file tree
Showing 16 changed files with 1,215 additions and 81 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ jobs:
FLAGSMITH_ENVIRONMENT_ID: 18143
FLAGSMITH_FEATURE_ID: 25142
FLAGSMITH_PROJECT_UUID: 97907de4-66d4-4d78-aae3-04f5a4518a55
FLAGSMITH_PROJECT_ID: 7721
FLAGSMITH_ORGANISATION_UUID: 0ee0578e-f2b8-467d-ba49-4cda324cea91
FLAGSMITH_ORGANISATION_ID: 6880
TF_ACC: "1"
run: go test -v -cover ./flagsmith/
timeout-minutes: 10
20 changes: 20 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
## 0.9.0
FEATURES:
* Add resource `flagsmith_project`
* Add resource `flagsmith_environment`
* Add data resource `flagsmith_organisation`


## 0.8.2
ENHANCEMENTS:
* resource(flagsmith_tag): Make `tag_colour` optional

## 0.8.1
BUG FIXES
fix https://github.com/Flagsmith/terraform-provider-flagsmith/issues/155


## 0.8.0
NOTES:
* This Go module(and related dependencies) has been updated to GO 1.21 as per the Go Support policy

## 0.7.0
FEATURES:
* Add resource `flagsmith_tag`
Expand Down
28 changes: 28 additions & 0 deletions docs/data-sources/organisation.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "flagsmith_organisation Data Source - terraform-provider-flagsmith"
subcategory: ""
description: |-
Flagsmith Organisation
---

# flagsmith_organisation (Data Source)

Flagsmith Organisation



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

### Required

- `uuid` (String) UUID of the organisation

### Read-Only

- `force_2fa` (Boolean) If true, signup will require 2FA
- `id` (Number) ID of the organisation
- `name` (String) Name of the organisation
- `persist_trait_data` (Boolean) If false, trait data for this organisation identities will not stored
- `restrict_project_create_to_admin` (Boolean) If true, only organisation admin can create projects
38 changes: 38 additions & 0 deletions docs/resources/environment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "flagsmith_environment Resource - terraform-provider-flagsmith"
subcategory: ""
description: |-
Flagsmith Environment
---

# flagsmith_environment (Resource)

Flagsmith Environment



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

### Required

- `name` (String) Name of the environment
- `project_id` (Number) ID of the project

### Optional

- `allow_client_traits` (Boolean) Allows clients using the client API key to set traits.
- `banner_colour` (String) hex code for the UI banner colour
- `banner_text` (String) Banner text to display in the UI
- `description` (String) Description of the environment
- `hide_disabled_flags` (Boolean) If true will exclude flags from SDK which are disabled
- `hide_sensitive_data` (Boolean) If true, will hide sensitive data(e.g: traits, description etc) from the SDK endpoints
- `minimum_change_request_approvals` (Number) Minimum number of approvals required for a change request
- `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)

### Read-Only

- `api_key` (String) Client side API Key
- `id` (Number) ID of the environment
- `uuid` (String) UUID of the environment
35 changes: 35 additions & 0 deletions docs/resources/project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "flagsmith_project Resource - terraform-provider-flagsmith"
subcategory: ""
description: |-
Flagsmith Project
---

# flagsmith_project (Resource)

Flagsmith Project



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

### Required

- `name` (String) Name of the project
- `organisation_id` (Number) ID of the organisation project belongs to

### Optional

- `enable_realtime_updates` (Boolean) Enable this to trigger a realtime(sse) event whenever the value of a flag changes
- `feature_name_regex` (String) Used for validating feature names
- `hide_disabled_flags` (Boolean) If true will exclude flags from SDK which are disabled
- `only_allow_lower_case_feature_names` (Boolean) Used by UI to validate feature names
- `prevent_flag_defaults` (Boolean) Prevent defaults from being set in all environments when creating a feature.
- `stale_flags_limit_days` (Number) Number of days without modification in any environment before a flag is considered stale.

### Read-Only

- `id` (Number) ID of the project
- `uuid` (String) UUID of the project
96 changes: 96 additions & 0 deletions flagsmith/data_source_organisation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package flagsmith

import (
"context"
"fmt"
"github.com/Flagsmith/flagsmith-go-api-client"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)

// Ensure provider defined types fully satisfy framework interfaces
var _ datasource.DataSource = &organisationDataResource{}

func newOrganisationDataResource() datasource.DataSource {
return &organisationDataResource{}
}

type organisationDataResource struct {
client *flagsmithapi.Client
}

func (o *organisationDataResource) Metadata(ctx context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_organisation"
}

func (o *organisationDataResource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}

client, ok := req.ProviderData.(*flagsmithapi.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *flagsmithapi.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}

o.client = client
}
func (o *organisationDataResource) Schema(ctx context.Context, req datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Flagsmith Organisation",

Attributes: map[string]schema.Attribute{
"uuid": schema.StringAttribute{
Required: true,
MarkdownDescription: "UUID of the organisation",
},
"id": schema.Int64Attribute{
Computed: true,
MarkdownDescription: "ID of the organisation",
},
"name": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Name of the organisation",
},
"force_2fa": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "If true, signup will require 2FA",
},
"persist_trait_data": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "If false, trait data for this organisation identities will not stored",
},
"restrict_project_create_to_admin": schema.BoolAttribute{
Computed: true,
MarkdownDescription: "If true, only organisation admin can create projects",
},
},
}
}
func (o *organisationDataResource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var data OrganisationResourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)

// Early return if the state is wrong
if diags.HasError() {
return
}

organisation, err := o.client.GetOrganisationByUUID(data.UUID.ValueString())
if err != nil {
panic(err)

}
resourceData := MakeOrganisationResourceDataFromClientOrganisation(organisation)

diags = resp.State.Set(ctx, &resourceData)
resp.Diagnostics.Append(diags...)

}
42 changes: 42 additions & 0 deletions flagsmith/data_source_organisation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package flagsmith_test

import (
"fmt"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"testing"
"strconv"
)

func TestAccOrganisationDataResource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactories,
Steps: []resource.TestStep{
{
Config: testAccOrganisationDataResourceConfig(organisationUUID()),
Check: resource.ComposeAggregateTestCheckFunc(

resource.TestCheckResourceAttr("data.flagsmith_organisation.test_org", "id", strconv.Itoa( organisationID())),
resource.TestCheckResourceAttr("data.flagsmith_organisation.test_org", "uuid", organisationUUID()),

resource.TestCheckResourceAttrSet("data.flagsmith_organisation.test_org", "name"),

),
},
},
})
}


func testAccOrganisationDataResourceConfig(organisationUUID string) string {
return fmt.Sprintf(`
provider "flagsmith" {
}
data "flagsmith_organisation" "test_org" {
uuid = "%s"
}
`,organisationUUID)
}
Loading

0 comments on commit dc4ce8a

Please sign in to comment.