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

Refactor DBaaS resources #251

Merged
merged 6 commits into from
May 15, 2024
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
23 changes: 18 additions & 5 deletions selectel/data_source_selectel_dbaas_configuration_parameter_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ func dataSourceDBaaSConfigurationParameterV1() *schema.Resource {
Type: schema.TypeString,
},
},
"invalid_values": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"is_restart_required": {
Type: schema.TypeBool,
Computed: true,
Expand Down Expand Up @@ -188,6 +195,15 @@ func filterConfigurationParametersByName(configurationParameters []dbaas.Configu
return filteredConfigurationParameters
}

func convertListParametersTypes(parameters []interface{}) []string {
parameterList := make([]string, len(parameters))
for i, value := range parameters {
parameterList[i] = convertFieldToStringByType(value)
}

return parameterList
}

func flattenDBaaSConfigurationParameters(configurationParameters []dbaas.ConfigurationParameter) []interface{} {
configurationParametersList := make([]interface{}, len(configurationParameters))
for i, param := range configurationParameters {
Expand All @@ -200,11 +216,8 @@ func flattenDBaaSConfigurationParameters(configurationParameters []dbaas.Configu
configurationParametersMap["min"] = convertFieldToStringByType(param.Min)
configurationParametersMap["max"] = convertFieldToStringByType(param.Max)
configurationParametersMap["default_value"] = convertFieldToStringByType(param.DefaultValue)
choicesList := make([]string, len(param.Choices))
for i, choice := range param.Choices {
choicesList[i] = convertFieldToStringByType(choice)
}
configurationParametersMap["choices"] = choicesList
configurationParametersMap["choices"] = convertListParametersTypes(param.Choices)
configurationParametersMap["invalid_values"] = convertListParametersTypes(param.InvalidValues)
configurationParametersMap["is_restart_required"] = param.IsRestartRequired
configurationParametersMap["is_changeable"] = param.IsChangeable

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestAccDBaaSConfigurationParametersV1Basic(t *testing.T) {
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.max", "2147483647"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.default_value", "32768"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.choices.#", "0"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.invalid_values.#", "0"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.is_restart_required", "false"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.is_changeable", "true"),
),
Expand All @@ -60,6 +61,7 @@ func TestAccDBaaSConfigurationParametersV1Basic(t *testing.T) {
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.choices.0", "origin"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.choices.1", "replica"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.choices.2", "local"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.invalid_values.#", "0"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.is_restart_required", "false"),
resource.TestCheckResourceAttr("data.selectel_dbaas_configuration_parameter_v1.configuration_param_tf_acc_test_1", "configuration_parameters.0.is_changeable", "true"),
),
Expand Down
31 changes: 31 additions & 0 deletions selectel/data_source_selectel_dbaas_flavor_v1.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type flavorSearchFilter struct {
vcpus int
ram int
disk int
flSize string
datastoreTypeID string
}

Expand Down Expand Up @@ -56,6 +57,10 @@ func dataSourceDBaaSFlavorV1() *schema.Resource {
Type: schema.TypeInt,
Computed: true,
},
"fl_size": {
Type: schema.TypeString,
Computed: true,
},
"datastore_type_ids": {
Type: schema.TypeList,
Computed: true,
Expand Down Expand Up @@ -83,6 +88,10 @@ func dataSourceDBaaSFlavorV1() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
},
"fl_size": {
Type: schema.TypeString,
Optional: true,
},
"datastore_type_id": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -118,6 +127,7 @@ func dataSourceDBaaSFlavorV1Read(ctx context.Context, d *schema.ResourceData, me
flavors = filterFlavorByVcpus(flavors, filter.vcpus)
flavors = filterFlavorByRAM(flavors, filter.ram)
flavors = filterFlavorByDisk(flavors, filter.disk)
flavors = filterFlavorByFlSize(flavors, filter.flSize)
flavors = filterFlavorByDatastoreTypeID(flavors, filter.datastoreTypeID)

flavorsFlatten := flattenDBaaSFlavors(flavors)
Expand Down Expand Up @@ -156,6 +166,11 @@ func expandFlavorSearchFilter(filterSet *schema.Set) (flavorSearchFilter, error)
filter.disk = disk.(int)
}

flSize, ok := resourceFilterMap["fl_size"]
if ok {
filter.flSize = flSize.(string)
}

datastoreTypeID, ok := resourceFilterMap["datastore_type_id"]
if ok {
filter.datastoreTypeID = datastoreTypeID.(string)
Expand Down Expand Up @@ -209,6 +224,21 @@ func filterFlavorByDisk(flavors []dbaas.FlavorResponse, disk int) []dbaas.Flavor
return filteredFlavors
}

func filterFlavorByFlSize(flavors []dbaas.FlavorResponse, flSize string) []dbaas.FlavorResponse {
if flSize == "" {
return flavors
}

var filteredFlavors []dbaas.FlavorResponse
for _, f := range flavors {
if f.FlSize == flSize {
filteredFlavors = append(filteredFlavors, f)
}
}

return filteredFlavors
}

func filterFlavorByDatastoreTypeID(flavors []dbaas.FlavorResponse, datastoreTypeID string) []dbaas.FlavorResponse {
if datastoreTypeID == "" {
return flavors
Expand Down Expand Up @@ -236,6 +266,7 @@ func flattenDBaaSFlavors(flavors []dbaas.FlavorResponse) []interface{} {
flavorMap["vcpus"] = flavor.Vcpus
flavorMap["ram"] = flavor.RAM
flavorMap["disk"] = flavor.Disk
flavorMap["fl_size"] = flavor.FlSize
flavorMap["datastore_type_ids"] = flavor.DatastoreTypeIDs

flavorsList[i] = flavorMap
Expand Down
2 changes: 2 additions & 0 deletions selectel/data_source_selectel_dbaas_flavor_v1_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func TestAccDBaaSFlavorsV1Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.vcpus"),
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.ram"),
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.disk"),
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.fl_size"),
),
},
{
Expand All @@ -46,6 +47,7 @@ func TestAccDBaaSFlavorsV1Basic(t *testing.T) {
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.vcpus"),
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.ram"),
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.disk"),
resource.TestCheckResourceAttrSet("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.fl_size"),
resource.TestCheckResourceAttr("data.selectel_dbaas_flavor_v1.flavor_tf_acc_test_1", "flavors.0.datastore_type_ids.#", "1"),
),
},
Expand Down
Loading
Loading