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 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
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 scopeId == variable.ScopeId && scope == variable.Scope {
filteredVariables = append(filteredVariables, variable)
}
}

return result, nil
return filteredVariables, nil
}

func (client *ApiClient) ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error) {
Expand Down
53 changes: 6 additions & 47 deletions client/configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,30 +38,14 @@ 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",
Description: "ignore",
Value: "ignore",
OrganizationId: organizationId,
Scope: ScopeTemplate,
ScopeId: "scope-id",
}

Describe("ConfigurationVariable", func() {
Expand Down Expand Up @@ -277,9 +261,11 @@ var _ = Describe("Configuration Variable", func() {
})

Describe("ConfigurationVariablesByScope", func() {
scopeId := mockTemplateConfigurationVariable.ScopeId

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 +275,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 +289,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"),
)
})
})