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

Update the database #178

Merged
merged 4 commits into from
Jun 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
12 changes: 12 additions & 0 deletions civo/datasource_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,16 @@ func dataSourceDatabase() *schema.Resource {
Computed: true,
Description: "Size of the database",
},
"engine": {
Type: schema.TypeString,
Computed: true,
Description: "The engine of the database",
},
"version": {
Type: schema.TypeString,
Computed: true,
Description: "The version of the database",
},
"nodes": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -111,6 +121,8 @@ func dataSourceDatabaseRead(_ context.Context, d *schema.ResourceData, m interfa
d.Set("name", foundDatabase.Name)
d.Set("region", apiClient.Region)
d.Set("size", foundDatabase.Size)
d.Set("engine", foundDatabase.Software)
d.Set("version", foundDatabase.SoftwareVersion)
d.Set("nodes", foundDatabase.Nodes)
d.Set("network_id", foundDatabase.NetworkID)
d.Set("firewall_id", foundDatabase.FirewallID)
Expand Down
4 changes: 4 additions & 0 deletions civo/datasource_database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ func TestAccDataSourceCivoDatabase_basic(t *testing.T) {
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttrSet(datasourceName, "size"),
resource.TestCheckResourceAttrSet(datasourceName, "nodes"),
resource.TestCheckResourceAttrSet(datasourceName, "engine"),
resource.TestCheckResourceAttrSet(datasourceName, "version"),
resource.TestCheckResourceAttr(datasourceName, "status", "Ready"),
),
},
Expand All @@ -34,6 +36,8 @@ func testAccDataSourceCivoDatabaseConfig(name string) string {
resource "civo_database" "foobar" {
name = "%s"
size = "g3.db.xsmall"
engine = "Postgres"
version = "13"
nodes = 2
}

Expand Down
87 changes: 87 additions & 0 deletions civo/datasource_database_version.go
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",
},
}
}
128 changes: 128 additions & 0 deletions civo/datasource_database_version_test.go
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"
}
}
`
}
Loading