From 6c13ea13fa87c267341aa92c2585b5565e0e12ac Mon Sep 17 00:00:00 2001 From: Gagan Date: Wed, 4 Dec 2024 13:03:00 +0530 Subject: [PATCH] feat: misc changes (#15) --- client_test.go | 3 ++- environment.go | 16 ++++++++++++++++ environment_test.go | 35 ++++++++++++++++++++++++++++++++--- models.go | 17 +++++++++++++++-- organisation.go | 22 ++++++++++++++++++++++ project_test.go | 2 +- 6 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 organisation.go diff --git a/client_test.go b/client_test.go index f1d3c71..406d4ba 100644 --- a/client_test.go +++ b/client_test.go @@ -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 = ` { @@ -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, diff --git a/environment.go b/environment.go index 961d621..450749b 100644 --- a/environment.go +++ b/environment.go @@ -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) diff --git a/environment_test.go b/environment_test.go index 6a957fe..f2db401 100644 --- a/environment_test.go +++ b/environment_test.go @@ -15,6 +15,7 @@ import ( ) const EnvironmentName = "Development" +const EnvironmentUUID = "4c830509-116d-46b7-804e-98f74d3b000b" func TestGetEnvironment(t *testing.T) { // Given @@ -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 @@ -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) @@ -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) diff --git a/models.go b/models.go index 0922bc3..b5a3ad4 100644 --- a/models.go +++ b/models.go @@ -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 { @@ -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 { @@ -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"` +} diff --git a/organisation.go b/organisation.go new file mode 100644 index 0000000..0ad3756 --- /dev/null +++ b/organisation.go @@ -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 + +} diff --git a/project_test.go b/project_test.go index 16f7ec7..29fa093 100644 --- a/project_test.go +++ b/project_test.go @@ -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,