-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Update the database, added the datasource for list database versions and removed deprecated resource * Update the example for the civo_database_version * Update to use 1.20 in the lint and build * Fix the fmt in the files
- Loading branch information
1 parent
d204163
commit 0c24127
Showing
17 changed files
with
470 additions
and
326 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"github.com/civo/civogo" | ||
"github.com/civo/terraform-provider-civo/internal/datalist" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// DatabaseVersion is a temporal struct to save all versions | ||
type DatabaseVersion struct { | ||
Engine string | ||
Version string | ||
Default bool | ||
} | ||
|
||
// Data source to get and filter all database version | ||
// use to define the engine and version in resourceDatabase | ||
func dataDatabaseVersion() *schema.Resource { | ||
dataListConfig := &datalist.ResourceConfig{ | ||
Description: "Retrieves information about the database versions that Civo supports, with the ability to filter the results.", | ||
RecordSchema: versionSchema(), | ||
ResultAttributeName: "versions", | ||
FlattenRecord: flattenVersion, | ||
GetRecords: getVersion, | ||
} | ||
|
||
return datalist.NewResource(dataListConfig) | ||
} | ||
|
||
func getVersion(m interface{}, _ map[string]interface{}) ([]interface{}, error) { | ||
apiClient := m.(*civogo.Client) | ||
|
||
versions := []interface{}{} | ||
partialVersions, err := apiClient.ListDBVersions() | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERR] error retrieving version: %s", err) | ||
} | ||
|
||
versionList := []DatabaseVersion{} | ||
for k, v := range partialVersions { | ||
for _, version := range v { | ||
versionList = append(versionList, DatabaseVersion{ | ||
Engine: k, | ||
Version: version.SoftwareVersion, | ||
Default: version.Default, | ||
}) | ||
} | ||
} | ||
|
||
for _, version := range versionList { | ||
versions = append(versions, version) | ||
} | ||
|
||
return versions, nil | ||
} | ||
|
||
func flattenVersion(versions, _ interface{}, _ map[string]interface{}) (map[string]interface{}, error) { | ||
s := versions.(DatabaseVersion) | ||
|
||
flattenedSize := map[string]interface{}{} | ||
flattenedSize["engine"] = s.Engine | ||
flattenedSize["version"] = s.Version | ||
flattenedSize["default"] = s.Default | ||
|
||
return flattenedSize, nil | ||
} | ||
|
||
func versionSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"engine": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The engine of the database", | ||
}, | ||
"version": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The version of the database", | ||
}, | ||
"default": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "If the version is the default", | ||
}, | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccDataSourceCivoDatabaseVersion_basic(t *testing.T) { | ||
datasourceName := "data.civo_database_version.foobar" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceCivoDatabaseVersionConfig(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckDataSourceDatabaseVersionExist(datasourceName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceCivoDatabaseVersion_WithFilterAndSort(t *testing.T) { | ||
datasourceName := "data.civo_database_version.foobar" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceCivoDatabaseVersionWhitFilterAndSort(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDataSourceDatabaseVersionExist(datasourceName), | ||
testAccCheckDataSourceCivoDatabaseVersionFilteredAndSorted(datasourceName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckDataSourceDatabaseVersionExist(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
|
||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
if rs.Primary.ID == "" { | ||
return fmt.Errorf("No Record ID is set") | ||
} | ||
|
||
rawTotal := rs.Primary.Attributes["versions.#"] | ||
total, err := strconv.Atoi(rawTotal) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if total < 1 { | ||
return fmt.Errorf("No Civo database version retrieved") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckDataSourceCivoDatabaseVersionFilteredAndSorted(n string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
rs, ok := s.RootModule().Resources[n] | ||
|
||
if !ok { | ||
return fmt.Errorf("Not found: %s", n) | ||
} | ||
|
||
rawTotal := rs.Primary.Attributes["versions.#"] | ||
total, err := strconv.Atoi(rawTotal) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
stringInSlice := func(value string, slice []string) bool { | ||
for _, item := range slice { | ||
if item == value { | ||
return true | ||
} | ||
} | ||
return false | ||
} | ||
|
||
for i := 0; i < total; i++ { | ||
name := rs.Primary.Attributes[fmt.Sprintf("versions.%d.engine", i)] | ||
if !stringInSlice(name, []string{"Mysql", "PostgreSQL"}) { | ||
return fmt.Errorf("engine is not in expected test filter values") | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccDataSourceCivoDatabaseVersionConfig() string { | ||
return ` | ||
data "civo_database_version" "foobar" { | ||
} | ||
` | ||
} | ||
|
||
func testAccDataSourceCivoDatabaseVersionWhitFilterAndSort() string { | ||
return ` | ||
data "civo_database_version" "foobar" { | ||
filter { | ||
key = "engine" | ||
values = ["mysql"] | ||
} | ||
sort { | ||
key = "engine" | ||
direction = "desc" | ||
} | ||
} | ||
` | ||
} |
Oops, something went wrong.