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

Chore: filter out more redundant scopes from ConfigurationVariablesBy… #753

Merged
merged 5 commits into from
Dec 4, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
22 changes: 6 additions & 16 deletions client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ func (client *ApiClient) ConfigurationVariablesById(id string) (ConfigurationVar
return result, nil
}

func filterOutConfigurationVariables(variables []ConfigurationVariable, scope Scope) []ConfigurationVariable {
filteredVariables := []ConfigurationVariable{}
for _, variable := range variables {
if variable.Scope != scope {
filteredVariables = append(filteredVariables, variable)
}
}
return filteredVariables
}

func (client *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId string) ([]ConfigurationVariable, error) {
organizationId, err := client.OrganizationId()
if err != nil {
Expand All @@ -120,15 +110,15 @@ func (client *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId stri
return []ConfigurationVariable{}, err
}

// The API returns global and template scopes for environment (and other) scopes. Filter them out.
if scope != ScopeGlobal {
result = filterOutConfigurationVariables(result, ScopeGlobal)
if scope != ScopeTemplate {
result = filterOutConfigurationVariables(result, ScopeTemplate)
// The API returns variables of upper scopes. Filter them out.
var filteredVariables []ConfigurationVariable
for _, variable := range result {
if scope == variable.Scope {
filteredVariables = append(filteredVariables, variable)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

that's not good enough 😅
as per the explanation in the issue #751

There'd be several variables from the same scope "type" from a different specific scope.
you'd get more variables from the API in the PROJECT scope, but which are NOT defined on the requested projectId.

Does that make sense?

Copy link
Contributor

Choose a reason for hiding this comment

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

Each variable has a scope and a scopeId.
The scope can be ENVIRONMENT, PROJECT, etc.
The scopeIds are uuids fir the specific environment, project, etc. respectfully.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

so.. should I filter instead based on scope ID? (instead of scope).

Copy link
Contributor

@Wassap124 Wassap124 Nov 22, 2023

Choose a reason for hiding this comment

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

Exactly

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

updated.

}

return result, nil
return filteredVariables, nil
}

func (client *ApiClient) ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error) {
Expand Down
52 changes: 5 additions & 47 deletions client/configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,23 +38,6 @@ var _ = Describe("Configuration Variable", func() {
Regex: "regex",
}

mockGlobalConfigurationVariable := ConfigurationVariable{
Id: "config-var-id-789",
Name: "configName",
Description: "configDescription",
Value: "configValue",
OrganizationId: organizationId,
IsSensitive: &isSensitive,
Scope: ScopeGlobal,
Type: &varType,
ScopeId: "project-123",
UserId: "user|123",
Schema: &schema,
IsReadOnly: &isReadOnly,
IsRequired: &isRequired,
Regex: "regex",
}

mockTemplateConfigurationVariable := ConfigurationVariable{
Id: "config-var-id-1111",
Name: "ignore",
Expand Down Expand Up @@ -277,9 +260,11 @@ var _ = Describe("Configuration Variable", func() {
})

Describe("ConfigurationVariablesByScope", func() {
scopeId := "scope-id"

var returnedVariables []ConfigurationVariable
mockVariables := []ConfigurationVariable{mockConfigurationVariable, mockGlobalConfigurationVariable, mockTemplateConfigurationVariable}
expectedParams := map[string]string{"organizationId": organizationId}
mockVariables := []ConfigurationVariable{mockTemplateConfigurationVariable}
expectedParams := map[string]string{"organizationId": organizationId, "blueprintId": scopeId}

BeforeEach(func() {
mockOrganizationIdCall(organizationId)
Expand All @@ -289,7 +274,7 @@ var _ = Describe("Configuration Variable", func() {
Do(func(path string, request interface{}, response *[]ConfigurationVariable) {
*response = mockVariables
})
returnedVariables, _ = apiClient.ConfigurationVariablesByScope(ScopeGlobal, "")
returnedVariables, _ = apiClient.ConfigurationVariablesByScope(ScopeTemplate, scopeId)
})

It("Should send GET request with expected params", func() {
Expand All @@ -303,32 +288,5 @@ var _ = Describe("Configuration Variable", func() {
It("Should return variables", func() {
Expect(returnedVariables).To(Equal(mockVariables))
})

DescribeTable("Different Scopes",
func(scope string, expectedFieldName string) {
scopeId := expectedFieldName + "-id"
expectedParams := map[string]string{
"organizationId": organizationId,
expectedFieldName: scopeId,
}

httpCall = mockHttpClient.EXPECT().
Get("/configuration", expectedParams, gomock.Any()).
Do(func(path string, request interface{}, response *[]ConfigurationVariable) {
*response = mockVariables
})
returnedVariables, _ = apiClient.ConfigurationVariablesByScope(Scope(scope), scopeId)
if scope == string(ScopeTemplate) {
Expect(returnedVariables).To((Equal([]ConfigurationVariable{mockConfigurationVariable, mockTemplateConfigurationVariable})))
} else {
Expect(returnedVariables).To((Equal([]ConfigurationVariable{mockConfigurationVariable})))
}
httpCall.Times(1)
},
Entry("Template Scope", string(ScopeTemplate), "blueprintId"),
Entry("Project Scope", string(ScopeProject), "projectId"),
Entry("Environment Scope", string(ScopeEnvironment), "environmentId"),
Entry("Project Scope", string(ScopeDeploymentLog), "deploymentLogId"),
)
})
})
Loading