Skip to content

Commit

Permalink
Added support for Database (#162)
Browse files Browse the repository at this point in the history
  • Loading branch information
satakshigarg committed Jan 24, 2023
1 parent eaacce9 commit 2af28da
Show file tree
Hide file tree
Showing 13 changed files with 616 additions and 14 deletions.
122 changes: 122 additions & 0 deletions civo/datasource_database.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
package civo

import (
"context"
"log"
"strings"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

// Data source to get from the api a specific Database
// using the id or the name
func dataSourceDatabase() *schema.Resource {
return &schema.Resource{
Description: strings.Join([]string{
"Get information of an Database for use in other resources. This data source provides all of the Database's properties as configured on your Civo account.",
"Note: This data source returns a single Database. When specifying a name, an error will be raised if more than one Databases with the same name found.",
}, "\n\n"),
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
Description: "The ID of the Database",
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
Description: "The name of the Database",
},
"size": {
Type: schema.TypeString,
Computed: true,
Description: "Size of the database",
},
"nodes": {
Type: schema.TypeInt,
Computed: true,
Description: "Count of nodes",
},
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "The region of an existing Database",
},
"network_id": {
Type: schema.TypeString,
Computed: true,
Description: "The network id of the Database",
},
"firewall_id": {
Type: schema.TypeString,
Computed: true,
Description: "The firewall id of the Database",
},
"username": {
Type: schema.TypeString,
Computed: true,
Description: "The username of the database",
},
"password": {
Type: schema.TypeString,
Computed: true,
Description: "The password of the database",
},
"status": {
Type: schema.TypeString,
Computed: true,
Description: "The status of the database",
},
},
ReadContext: dataSourceDatabaseRead,
}
}

func dataSourceDatabaseRead(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
if region, ok := d.GetOk("region"); ok {
apiClient.Region = region.(string)
}

var foundDatabase *civogo.Database

if name, ok := d.GetOk("name"); ok {
log.Printf("[INFO] Getting the Database by name")
database, err := apiClient.FindDatabase(name.(string))
if err != nil {
return diag.Errorf("[ERR] failed to retrive Database: %s", err)
}

foundDatabase = database
}

if id, ok := d.GetOk("id"); ok {
log.Printf("[INFO] Getting the Database by id")
database, err := apiClient.FindDatabase(id.(string))
if err != nil {
return diag.Errorf("[ERR] failed to retrive Database: %s", err)
}

foundDatabase = database
}

d.SetId(foundDatabase.ID)
d.Set("name", foundDatabase.Name)
d.Set("region", apiClient.Region)
d.Set("size", foundDatabase.Size)
d.Set("nodes", foundDatabase.Nodes)
d.Set("network_id", foundDatabase.NetworkID)
d.Set("firewall_id", foundDatabase.FirewallID)
d.Set("username", foundDatabase.Username)
d.Set("password", foundDatabase.Password)
d.Set("status", foundDatabase.Status)

return nil
}
43 changes: 43 additions & 0 deletions civo/datasource_database_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package civo

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccDataSourceCivoDatabase_basic(t *testing.T) {
datasourceName := "data.civo_database.foobar"
name := acctest.RandomWithPrefix("database")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoDatabaseConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttrSet(datasourceName, "size"),
resource.TestCheckResourceAttrSet(datasourceName, "nodes"),
resource.TestCheckResourceAttr(datasourceName, "status", "Ready"),
),
},
},
})
}

func testAccDataSourceCivoDatabaseConfig(name string) string {
return fmt.Sprintf(`
resource "civo_database" "foobar" {
name = "%s"
size = "g3.db.xsmall"
nodes = 2
}
data "civo_database" "foobar" {
name = civo_database.foobar.name
}`, name)
}
5 changes: 4 additions & 1 deletion civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ package civo

import (
"fmt"
"log"

"github.com/civo/civogo"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"log"
)

var (
Expand Down Expand Up @@ -58,6 +59,7 @@ func Provider() *schema.Provider {
"civo_object_store_credential": dataSourceObjectStoreCredential(),
"civo_region": dataSourceRegion(),
"civo_reserved_ip": dataSourceReservedIP(),
"civo_database": dataSourceDatabase(),
// "civo_snapshot": dataSourceSnapshot(),
},
ResourcesMap: map[string]*schema.Resource{
Expand All @@ -76,6 +78,7 @@ func Provider() *schema.Provider {
"civo_instance_reserved_ip_assignment": resourceInstanceReservedIPAssignment(),
"civo_object_store": resourceObjectStore(),
"civo_object_store_credential": resourceObjectStoreCredential(),
"civo_database": resourceDatabase(),
// "civo_loadbalancer": resourceLoadBalancer(),
// "civo_template": resourceTemplate(),
// "civo_snapshot": resourceSnapshot(),
Expand Down
Loading

0 comments on commit 2af28da

Please sign in to comment.