Skip to content

Commit

Permalink
Update the database (#178)
Browse files Browse the repository at this point in the history
* 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
alejandrojnm committed Jun 5, 2023
1 parent d204163 commit 0c24127
Show file tree
Hide file tree
Showing 17 changed files with 470 additions and 326 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ jobs:
build:
strategy:
matrix:
go-version: [1.17.x, 1.18.x]
go-version: [1.20.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ jobs:
test:
strategy:
matrix:
go-version: [1.17.x, 1.18.x]
go-version: [1.20.x]
os: [ubuntu-latest]
runs-on: ${{ matrix.os }}
steps:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.18.x
go-version: 1.20.x

- name: Install and run tfplugindocs CLI
run: |
Expand Down
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

0 comments on commit 0c24127

Please sign in to comment.