-
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.
- Loading branch information
1 parent
eaacce9
commit 2af28da
Showing
13 changed files
with
616 additions
and
14 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
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 | ||
} |
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,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) | ||
} |
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
Oops, something went wrong.