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: misc changes to support environment, project resource, and organization data resource in the Terraform provider #15

Merged
merged 1 commit into from
Dec 4, 2024
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
3 changes: 2 additions & 1 deletion client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ const GetProjectResponseJson = `
const ProjectID int64 = 10
const ProjectUUID = "cba035f8-d801-416f-a985-ce6e05acbe13"
const ProjectName = "project-1"
const OrganisationID = 10
const OrganisationID int64 = 10

const CreateFeatureResponseJson = `
{
Expand Down Expand Up @@ -1278,6 +1278,7 @@ const EnvironmentID int64 = 100
const EnvironmentAPIKey = "environment_api_key"
const EnvironmentJson = `{
"id": 100,
"uuid": "4c830509-116d-46b7-804e-98f74d3b000b",
"name": "Development",
"api_key": "environment_api_key",
"description": null,
Expand Down
16 changes: 16 additions & 0 deletions environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,22 @@ func (c *Client) GetEnvironment(apiKey string) (*Environment, error) {

return &environment, nil
}
func (c *Client) GetEnvironmentByUUID(uuid string) (*Environment, error) {
url := fmt.Sprintf("%s/environments/get-by-uuid/%s/", c.baseURL, uuid)
environment := Environment{}
resp, err := c.client.R().
SetResult(&environment).Get(url)

if err != nil {
return nil, err
}

if !resp.IsSuccess() {
return nil, fmt.Errorf("flagsmithapi: Error getting environment: %s", resp)
}

return &environment, nil
}
func (c *Client) CreateEnvironment(environment *Environment) error {
url := fmt.Sprintf("%s/environments/", c.baseURL)
resp, err := c.client.R().SetBody(environment).SetResult(environment).Post(url)
Expand Down
35 changes: 32 additions & 3 deletions environment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import (
)

const EnvironmentName = "Development"
const EnvironmentUUID = "4c830509-116d-46b7-804e-98f74d3b000b"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the same UUID as before. Could we DRY it up?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For that, I'd have to dynamically generate the JSON (i.e., it can't be a global variable). In the end, it will end up looking uglier.


func TestGetEnvironment(t *testing.T) {
// Given
Expand Down Expand Up @@ -42,7 +43,35 @@ func TestGetEnvironment(t *testing.T) {
assert.Equal(t, EnvironmentID, environment.ID)
assert.Equal(t, "Development", environment.Name)
assert.Equal(t, EnvironmentAPIKey, environment.APIKey)
assert.Equal(t, ProjectID, environment.Project)
assert.Equal(t, ProjectID, environment.ProjectID)
}
func TestGetEnvironmentByUUID(t *testing.T) {
// Given
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
assert.Equal(t, fmt.Sprintf("/api/v1/environments/get-by-uuid/%s/", EnvironmentUUID), req.URL.Path)
assert.Equal(t, "GET", req.Method)
assert.Equal(t, "Api-Key "+MasterAPIKey, req.Header.Get("Authorization"))

rw.Header().Set("Content-Type", "application/json")
_, err := io.WriteString(rw, EnvironmentJson)
assert.NoError(t, err)
}))
defer server.Close()

client := flagsmithapi.NewClient(MasterAPIKey, server.URL+"/api/v1")

// When
environment, err := client.GetEnvironmentByUUID(EnvironmentUUID)

// Then
// assert that we did not receive an error
assert.NoError(t, err)

// assert that the environment is as expected
assert.Equal(t, EnvironmentID, environment.ID)
assert.Equal(t, "Development", environment.Name)
assert.Equal(t, EnvironmentAPIKey, environment.APIKey)
assert.Equal(t, ProjectID, environment.ProjectID)
}
func TestCreateEnvironment(t *testing.T) {
// Given
Expand Down Expand Up @@ -75,7 +104,7 @@ func TestCreateEnvironment(t *testing.T) {
environment := &flagsmithapi.Environment{
Name: EnvironmentName,
Description: "This is a test environment",
Project: ProjectID,
ProjectID: ProjectID,
}
err := client.CreateEnvironment(environment)

Expand Down Expand Up @@ -124,7 +153,7 @@ func TestUpdateEnvironment(t *testing.T) {
ID: EnvironmentID,
Name: EnvironmentName,
Description: "Updated environment description",
Project: ProjectID,
ProjectID: ProjectID,
APIKey: EnvironmentAPIKey,
}
err := client.UpdateEnvironment(environment)
Expand Down
17 changes: 15 additions & 2 deletions models.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ type Project struct {
HideDisabledFlags bool `json:"hide_disabled_flags,omitempty"`
PreventFlagDefaults bool `json:"prevent_flag_defaults,omitempty"`
OnlyAllowLowerCaseFeatureNames bool `json:"only_allow_lower_case_feature_names,omitempty"`
FeatureNameRegex bool `json:"feature_name_regex,omitempty"`
FeatureNameRegex string `json:"feature_name_regex,omitempty"`
StaleFlagsLimitDays int64 `json:"stale_flags_limit_days,omitempty"`
EnableRealtimeUpdates bool `json:"enable_realtime_updates,omitempty"`
}

type FeatureMultivariateOption struct {
Expand Down Expand Up @@ -212,16 +214,18 @@ type FeatureSegment struct {

type Environment struct {
ID int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name"`
APIKey string `json:"api_key,omitempty"`
Description string `json:"description"`
Project int64 `json:"project"`
ProjectID int64 `json:"project"`
AllowClientTraits bool `json:"allow_client_traits,omitempty"`
BannerText string `json:"banner_text,omitempty"`
BannerColour string `json:"banner_colour,omitempty"`
HideDisabledFlags bool `json:"hide_disabled_flags,omitempty"`
HideSensitiveData bool `json:"hide_sensitive_data,omitempty"`
UseIdentityCompositeKeyForHashing bool `json:"use_identity_composite_key_for_hashing,omitempty"`
MinimumChangeRequestApprovals int64 `json:"minimum_change_request_approvals,omitempty"`
}

type Tag struct {
Expand Down Expand Up @@ -257,3 +261,12 @@ type Trait struct {
BooleanValue *bool `json:"boolean_value,omitempty"`
FloatValue *float64 `json:"float_value,omitempty"`
}

type Organisation struct {
ID int64 `json:"id,omitempty"`
UUID string `json:"uuid,omitempty"`
Name string `json:"name"`
Force2FA bool `json:"force_2fa"`
RestrictProjectCreateToAdmin bool `json:"restrict_project_create_to_admin"`
PersistTraitData bool `json:"persist_trait_data"`
}
22 changes: 22 additions & 0 deletions organisation.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package flagsmithapi

import (
"fmt"
)

func (c *Client) GetOrganisationByUUID(orgUUID string) (*Organisation, error) {
url := fmt.Sprintf("%s/organisations/get-by-uuid/%s/", c.baseURL, orgUUID)
organisation := Organisation{}
resp, err := c.client.R().
SetResult(&organisation).
Get(url)

if err != nil {
return nil, err
}
if !resp.IsSuccess() {
return nil, fmt.Errorf("flagsmithapi: Error getting organisation: %s", resp)
}
return &organisation, nil

}
2 changes: 1 addition & 1 deletion project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestGetProject(t *testing.T) {

}

func TestCreateProject(t *testing.T) {
func TestCreateProjectByUUID(t *testing.T) {
// Given
project := flagsmithapi.Project{
Name: ProjectName,
Expand Down
Loading