Skip to content

Commit

Permalink
Chore: filter out more redundant scopes from ConfigurationVariablesBy… (
Browse files Browse the repository at this point in the history
#753)

* Chore: filter out more redundant scopes from ConfigurationVariablesByScope

* scopeId filter instead of scope type

* added another condition to the 'if'
  • Loading branch information
TomerHeber authored Dec 4, 2023
1 parent e9451dc commit 5a55606
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 63 deletions.
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"),
)
})
})

0 comments on commit 5a55606

Please sign in to comment.