Skip to content

Commit

Permalink
Updated to v2 of the SDK of terraform and add support for muti region
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Apr 19, 2021
1 parent 508fc67 commit a049b43
Show file tree
Hide file tree
Showing 43 changed files with 294 additions and 243 deletions.
4 changes: 2 additions & 2 deletions civo/datasource_dns_domain_name_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoDnsDomainName(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions civo/datasource_dns_domain_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoDNSDomainRecord_basic(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions civo/datasource_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoInstance_basic(t *testing.T) {
Expand Down
111 changes: 61 additions & 50 deletions civo/datasource_instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func dataSourceInstances() *schema.Resource {
ExtraQuerySchema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Required: false,
Optional: true,
},
},
ResultAttributeName: "instances",
Expand All @@ -31,14 +31,19 @@ func getDataSourceInstances(m interface{}, extra map[string]interface{}) ([]inte
apiClient := m.(*civogo.Client)

// overwrite the region if is define in the datasource
if region, ok := extra["region"]; ok {
apiClient.Region = region.(string)
region, ok := extra["region"].(string)
if !ok {
return nil, fmt.Errorf("unable to find `region` key from query data")
}

instance := []interface{}{}
if region != "" {
apiClient.Region = region
}

var instance []interface{}
partialInstances, err := apiClient.ListInstances(1, 200)
if err != nil {
return nil, fmt.Errorf("[ERR] error retrieving sizes: %s", err)
return nil, fmt.Errorf("[ERR] error retrieving instances: %s", err)
}

for _, partialInstance := range partialInstances.Items {
Expand All @@ -50,11 +55,17 @@ func getDataSourceInstances(m interface{}, extra map[string]interface{}) ([]inte

func flattenDataSourceInstances(instance, m interface{}, extra map[string]interface{}) (map[string]interface{}, error) {

region, ok := extra["region"].(string)
if !ok {
return nil, fmt.Errorf("unable to find `region` key from query data")
}

i := instance.(civogo.Instance)

flattenedInstance := map[string]interface{}{}
flattenedInstance["id"] = i.ID
flattenedInstance["hostname"] = i.Hostname
flattenedInstance["region"] = region
flattenedInstance["reverse_dns"] = i.ReverseDNS
flattenedInstance["size"] = i.Size
flattenedInstance["cpu_cores"] = i.CPUCores
Expand All @@ -81,93 +92,93 @@ func flattenDataSourceInstances(instance, m interface{}, extra map[string]interf
func instancesSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "id of the instance",
},
"hostname": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "hostname of the instance",
},
"region": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "region of the instance",
},
"reverse_dns": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "reverse DNS of the instance",
},
"size": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "size of the instance",
},
"cpu_cores": {
Type: schema.TypeInt,
Computed: true,
Type: schema.TypeInt,
Description: "CPU of the instance",
},
"ram_mb": {
Type: schema.TypeInt,
Computed: true,
Type: schema.TypeInt,
Description: "RAM of the instance",
},
"disk_gb": {
Type: schema.TypeInt,
Computed: true,
Type: schema.TypeInt,
Description: "SSD of the instance",
},
"network_id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "netwoerk id of the instance",
},
"template": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "template of the instance",
},
"initial_user": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "initial user of the instance",
},
"notes": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "note of the instance",
},
"sshkey_id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "sshkey id of the instance",
},
"firewall_id": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "firewall id of the instance",
},
"tags": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "tags of the instance",
},
"script": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "script of the instance",
},
"initial_password": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "initial password of the instance",
},
"private_ip": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "private ip of the instance",
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "public ip of the instance",
},
"pseudo_ip": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "pseudo ip of the instance",
},
"status": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "status of the instance",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Type: schema.TypeString,
Description: "creation date of the instance",
},
}
}
4 changes: 2 additions & 2 deletions civo/datasource_instances_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoInstances_basic(t *testing.T) {
Expand Down
14 changes: 7 additions & 7 deletions civo/datasource_intances_size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"testing"

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

func TestAccDataSourceCivoInstanceSize_basic(t *testing.T) {
Expand Down Expand Up @@ -101,7 +101,7 @@ func testAccCheckDataSourceCivoInstanceSizeFilteredAndSorted(n string) resource.
}

CPUCore, _ := strconv.ParseFloat(rs.Primary.Attributes[fmt.Sprintf("sizes.%d.cpu_cores", i)], 64)
if prevCPUCore > 0 && prevCPUCore < prevCPUCore {
if prevCPUCore > 0 {
return fmt.Errorf("Sizes is not sorted by CPU Core in descending order")
}
prevCPUCore = CPUCore
Expand All @@ -113,14 +113,14 @@ func testAccCheckDataSourceCivoInstanceSizeFilteredAndSorted(n string) resource.
}

func testAccDataSourceCivoInstanceSizeConfig() string {
return fmt.Sprintf(`
return `
data "civo_instances_size" "foobar" {
}
`)
`
}

func testAccDataSourceCivoInstanceSizeConfigWhitFilterAndSort() string {
return fmt.Sprintf(`
return `
data "civo_instances_size" "foobar" {
filter {
key = "name"
Expand All @@ -132,5 +132,5 @@ data "civo_instances_size" "foobar" {
direction = "desc"
}
}
`)
`
}
4 changes: 2 additions & 2 deletions civo/datasource_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoKubernetesCluster_basic(t *testing.T) {
Expand Down
12 changes: 6 additions & 6 deletions civo/datasource_kubernetes_version_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import (
"strconv"
"testing"

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

func TestAccDataSourceCivoKubernetesVersion_basic(t *testing.T) {
Expand Down Expand Up @@ -106,19 +106,19 @@ func testAccCheckDataSourceCivoKubernetesVersionFiltered(n string) resource.Test
}

func testAccDataSourceCivoKubernetesVersionConfig() string {
return fmt.Sprintf(`
return `
data "civo_kubernetes_version" "foobar" {
}
`)
`
}

func testAccDataSourceCivoKubernetesVersionConfigWhitFilter() string {
return fmt.Sprintf(`
return `
data "civo_kubernetes_version" "foobar" {
filter {
key = "type"
values = ["stable"]
}
}
`)
`
}
4 changes: 2 additions & 2 deletions civo/datasource_loadbalancer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoLoadBalancer_basic(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions civo/datasource_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoNetwork_basic(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions civo/datasource_snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ import (
"fmt"
"testing"

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

func TestAccDataSourceCivoSnapshots_basic(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions civo/datasource_ssh_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import (
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"golang.org/x/crypto/ssh"
)

Expand Down
Loading

0 comments on commit a049b43

Please sign in to comment.