Skip to content

Commit

Permalink
Enable query by configuration variable id in order to resolve #215 (#216
Browse files Browse the repository at this point in the history
)
  • Loading branch information
elbaz committed Jan 25, 2022
1 parent 282700a commit 5160c76
Show file tree
Hide file tree
Showing 9 changed files with 204 additions and 63 deletions.
3 changes: 2 additions & 1 deletion client/api_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ type ApiClient struct {
}

type ApiClientInterface interface {
ConfigurationVariables(scope Scope, scopeId string) ([]ConfigurationVariable, error)
ConfigurationVariablesByScope(scope Scope, scopeId string) ([]ConfigurationVariable, error)
ConfigurationVariablesById(id string) (ConfigurationVariable, error)
ConfigurationVariableCreate(params ConfigurationVariableCreateParams) (ConfigurationVariable, error)
ConfigurationVariableUpdate(params ConfigurationVariableUpdateParams) (ConfigurationVariable, error)
ConfigurationVariableDelete(id string) error
Expand Down
21 changes: 18 additions & 3 deletions client/api_client_mock.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 12 additions & 1 deletion client/configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,18 @@ import (
"errors"
)

func (self *ApiClient) ConfigurationVariables(scope Scope, scopeId string) ([]ConfigurationVariable, error) {
func (self *ApiClient) ConfigurationVariablesById(id string) (ConfigurationVariable, error) {
var result ConfigurationVariable

err := self.http.Get("/configuration/"+id, nil, &result)

if err != nil {
return ConfigurationVariable{}, err
}
return result, nil
}

func (self *ApiClient) ConfigurationVariablesByScope(scope Scope, scopeId string) ([]ConfigurationVariable, error) {
organizationId, err := self.organizationId()
if err != nil {
return nil, err
Expand Down
24 changes: 21 additions & 3 deletions client/configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,24 @@ var _ = Describe("Configuration Variable", func() {
IsRequired: &isRequired,
}

Describe("ConfigurationVariablesById", func() {
id := "configurationId"
var found ConfigurationVariable
BeforeEach(func() {
httpCall = mockHttpClient.EXPECT().
Get("/configuration/"+id, nil, gomock.Any()).
Do(func(path string, request interface{}, response *ConfigurationVariable) {
*response = mockConfigurationVariable
})

found, _ = apiClient.ConfigurationVariablesById(id)
})

It("Should return variable", func() {
Expect(found).To(Equal(mockConfigurationVariable))
})
})

Describe("ConfigurationVariableCreate", func() {
var createdConfigurationVariable ConfigurationVariable

Expand Down Expand Up @@ -198,7 +216,7 @@ var _ = Describe("Configuration Variable", func() {
})
})

Describe("ConfigurationVariables", func() {
Describe("ConfigurationVariablesByScope", func() {
var returnedVariables []ConfigurationVariable
mockVariables := []ConfigurationVariable{mockConfigurationVariable}
expectedParams := map[string]string{"organizationId": organizationId}
Expand All @@ -211,7 +229,7 @@ var _ = Describe("Configuration Variable", func() {
Do(func(path string, request interface{}, response *[]ConfigurationVariable) {
*response = mockVariables
})
returnedVariables, _ = apiClient.ConfigurationVariables(ScopeGlobal, "")
returnedVariables, _ = apiClient.ConfigurationVariablesByScope(ScopeGlobal, "")
})

It("Should send GET request with expected params", func() {
Expand Down Expand Up @@ -239,7 +257,7 @@ var _ = Describe("Configuration Variable", func() {
Do(func(path string, request interface{}, response *[]ConfigurationVariable) {
*response = mockVariables
})
returnedVariables, _ = apiClient.ConfigurationVariables(Scope(scope), scopeId)
returnedVariables, _ = apiClient.ConfigurationVariablesByScope(Scope(scope), scopeId)
httpCall.Times(1)
},
Entry("Template Scope", string(ScopeTemplate), "blueprintId"),
Expand Down
13 changes: 11 additions & 2 deletions env0/data_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,22 @@ func getScopeAndId(d *schema.ResourceData) (client.Scope, string) {

func getConfigurationVariable(params ConfigurationVariableParams, meta interface{}) (client.ConfigurationVariable, diag.Diagnostics) {
apiClient := meta.(client.ApiClientInterface)
id, idOk := params.Id, params.Id != ""

if idOk {
variable, err := apiClient.ConfigurationVariablesById(id)
if err != nil {
return client.ConfigurationVariable{}, diag.Errorf("Could not query variable: %v", err)
}
return variable, nil
}

variables, err := apiClient.ConfigurationVariablesByScope(params.Scope, params.ScopeId)

variables, err := apiClient.ConfigurationVariables(params.Scope, params.ScopeId)
if err != nil {
return client.ConfigurationVariable{}, diag.Errorf("Could not query variables: %v", err)
}

id, idOk := params.Id, params.Id != ""
name, nameOk := params.Name, params.Name != ""
typeString, ok := params.configurationType, params.configurationType != ""
type_ := -1
Expand Down
6 changes: 5 additions & 1 deletion env0/data_configuration_variable_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ func TestUnitConfigurationVariableData(t *testing.T) {
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariablesById(configurationVariable.Id).AnyTimes().
Return(configurationVariable, nil)
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
Expand Down Expand Up @@ -107,6 +109,8 @@ func TestUnitConfigurationVariableData(t *testing.T) {
},
},
func(mock *client.MockApiClientInterface) {
mock.EXPECT().ConfigurationVariablesById(configurationVariable.Id).AnyTimes().
Return(configurationVariable, nil)
mock.EXPECT().ConfigurationVariables(client.ScopeGlobal, "").AnyTimes().
Return([]client.ConfigurationVariable{configurationVariable}, nil)
})
Expand All @@ -117,7 +121,7 @@ func TestUnitConfigurationVariableData(t *testing.T) {
resource.TestCase{
Steps: []resource.TestStep{
{
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"id": configurationVariable.Id, "template_id": "template_id"}),
Config: dataSourceConfigCreate(resourceType, resourceName, map[string]interface{}{"template_id": "template_id", "name": configurationVariable.Name}),
Check: checkResources,
},
},
Expand Down
44 changes: 21 additions & 23 deletions env0/resource_configuration_variable.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,36 +198,34 @@ func resourceConfigurationVariableRead(ctx context.Context, d *schema.ResourceDa
apiClient := meta.(client.ApiClientInterface)

id := d.Id()
scope, scopeId := whichScope(d)
variables, err := apiClient.ConfigurationVariables(scope, scopeId)
variable, err := apiClient.ConfigurationVariablesById(id)

if err != nil {
return diag.Errorf("could not get configurationVariable: %v", err)
}
for _, variable := range variables {
if variable.Id == id {
d.Set("name", variable.Name)
d.Set("description", variable.Description)
d.Set("value", variable.Value)
d.Set("is_sensitive", variable.IsSensitive)
if variable.Type != nil && *variable.Type == client.ConfigurationVariableTypeTerraform {
d.Set("type", "terraform")
} else {
d.Set("type", "environment")
}
if variable.Schema != nil {
if len(variable.Schema.Enum) > 0 {
d.Set("enum", variable.Schema.Enum)
}

if variable.Schema.Format != "" {
d.Set("format", variable.Schema.Format)
}
}
d.Set("name", variable.Name)
d.Set("description", variable.Description)
d.Set("value", variable.Value)
d.Set("is_sensitive", variable.IsSensitive)
d.Set("is_read_only", variable.IsReadonly)
d.Set("is_required", variable.IsRequired)
if variable.Type != nil && *variable.Type == client.ConfigurationVariableTypeTerraform {
d.Set("type", "terraform")
} else {
d.Set("type", "environment")
}
if variable.Schema != nil {
if len(variable.Schema.Enum) > 0 {
d.Set("enum", variable.Schema.Enum)
}

return nil
if variable.Schema.Format != "" {
d.Set("format", variable.Schema.Format)
}
}
return diag.Errorf("variable %s not found (under this scope): %v", id, err)

return nil
}

func resourceConfigurationVariableUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
Expand Down
Loading

0 comments on commit 5160c76

Please sign in to comment.