-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added a new datasource for the civo size (#118)
* Updated the size of the k3s cluster to a new version g4s.kube.medium by default * Added a check to see if the size is available * Added the new data source civo_size * Fixed error with the format of the code
- Loading branch information
1 parent
0a3bd81
commit 8ed3687
Showing
9 changed files
with
313 additions
and
11 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
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
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,137 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/civo/civogo" | ||
"github.com/civo/terraform-provider-civo/internal/datalist" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
// SizeList is a temporal struct to save all size | ||
type Size struct { | ||
Name string | ||
Description string | ||
Type string | ||
CPU int | ||
RAM int | ||
DisK int | ||
Selectable bool | ||
} | ||
|
||
// Data source to get and filter all instances size | ||
// use to define the size in resourceInstance | ||
func dataSourceSize() *schema.Resource { | ||
dataListConfig := &datalist.ResourceConfig{ | ||
Description: "Retrieves information about the sizes that Civo supports, with the ability to filter the results.", | ||
RecordSchema: SizeSchema(), | ||
ResultAttributeName: "sizes", | ||
FlattenRecord: flattenSize, | ||
GetRecords: getSizes, | ||
} | ||
|
||
return datalist.NewResource(dataListConfig) | ||
|
||
} | ||
|
||
func getSizes(m interface{}, extra map[string]interface{}) ([]interface{}, error) { | ||
apiClient := m.(*civogo.Client) | ||
|
||
sizes := []interface{}{} | ||
partialSizes, err := apiClient.ListInstanceSizes() | ||
if err != nil { | ||
return nil, fmt.Errorf("[ERR] error retrieving sizes: %s", err) | ||
} | ||
|
||
sizeList := []Size{} | ||
|
||
for _, v := range partialSizes { | ||
if !v.Selectable { | ||
continue | ||
} | ||
|
||
typeName := "" | ||
|
||
switch { | ||
case strings.Contains(v.Name, "db"): | ||
typeName = "database" | ||
case strings.Contains(v.Name, "kube") || strings.Contains(v.Name, "k3s"): | ||
typeName = "kubernetes" | ||
default: | ||
typeName = "instance" | ||
} | ||
|
||
sizeList = append(sizeList, Size{ | ||
Name: v.Name, | ||
Description: v.Description, | ||
Type: typeName, | ||
CPU: v.CPUCores, | ||
RAM: v.RAMMegabytes, | ||
DisK: v.DiskGigabytes, | ||
Selectable: v.Selectable, | ||
}) | ||
} | ||
|
||
for _, partialSize := range sizeList { | ||
sizes = append(sizes, partialSize) | ||
} | ||
|
||
return sizes, nil | ||
} | ||
|
||
func flattenSize(size, m interface{}, extra map[string]interface{}) (map[string]interface{}, error) { | ||
|
||
s := size.(Size) | ||
|
||
flattenedSize := map[string]interface{}{} | ||
flattenedSize["name"] = s.Name | ||
flattenedSize["type"] = s.Type | ||
flattenedSize["cpu"] = s.CPU | ||
flattenedSize["ram"] = s.RAM | ||
flattenedSize["disk"] = s.DisK | ||
flattenedSize["description"] = s.Description | ||
flattenedSize["selectable"] = s.Selectable | ||
|
||
return flattenedSize, nil | ||
} | ||
|
||
func SizeSchema() map[string]*schema.Schema { | ||
return map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "The name of the instance size", | ||
}, | ||
"type": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "A human name of the instance size", | ||
}, | ||
"cpu": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "Total of CPU in the instance", | ||
}, | ||
"ram": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "Total of RAM of the instance", | ||
}, | ||
"disk": { | ||
Type: schema.TypeInt, | ||
Computed: true, | ||
Description: "The instance size of SSD", | ||
}, | ||
"description": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Description: "A description of the instance size", | ||
}, | ||
"selectable": { | ||
Type: schema.TypeBool, | ||
Computed: true, | ||
Description: "If can use the instance size", | ||
}, | ||
} | ||
} |
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,136 @@ | ||
package civo | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform" | ||
) | ||
|
||
func TestAccDataSourceCivoSize_basic(t *testing.T) { | ||
datasourceName := "data.civo_size.foobar" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceCivoSizeConfig(), | ||
Check: resource.ComposeAggregateTestCheckFunc( | ||
testAccCheckDataSourceCivoSizeExist(datasourceName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccDataSourceCivoSize_WithFilterAndSort(t *testing.T) { | ||
datasourceName := "data.civo_size.foobar" | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceCivoSizeConfigWhitFilterAndSort(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccCheckDataSourceCivoSizeExist(datasourceName), | ||
testAccCheckDataSourceCivoSizeFilteredAndSorted(datasourceName), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccCheckDataSourceCivoSizeExist(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["sizes.#"] | ||
total, err := strconv.Atoi(rawTotal) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if total < 1 { | ||
return fmt.Errorf("No Civo sizes retrieved") | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccCheckDataSourceCivoSizeFilteredAndSorted(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["sizes.#"] | ||
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 | ||
} | ||
|
||
var prevCPUCore float64 | ||
for i := 0; i < total; i++ { | ||
name := rs.Primary.Attributes[fmt.Sprintf("sizes.%d.name", i)] | ||
if !stringInSlice(name, []string{"g2.large", "g2.xlarge", "g2.2xlarge"}) { | ||
return fmt.Errorf("Name is not in expected test filter values") | ||
} | ||
|
||
CPUCore, _ := strconv.ParseFloat(rs.Primary.Attributes[fmt.Sprintf("sizes.%d.cpu_cores", i)], 64) | ||
if prevCPUCore > 0 { | ||
return fmt.Errorf("Sizes is not sorted by CPU Core in descending order") | ||
} | ||
prevCPUCore = CPUCore | ||
|
||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccDataSourceCivoSizeConfig() string { | ||
return ` | ||
data "civo_size" "foobar" { | ||
} | ||
` | ||
} | ||
|
||
func testAccDataSourceCivoSizeConfigWhitFilterAndSort() string { | ||
return ` | ||
data "civo_size" "foobar" { | ||
filter { | ||
key = "name" | ||
values = ["large"] | ||
} | ||
sort { | ||
key = "cpu_cores" | ||
direction = "desc" | ||
} | ||
} | ||
` | ||
} |
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
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
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
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
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,21 @@ | ||
data "civo_size" "small" { | ||
filter { | ||
key = "name" | ||
values = ["g3.small"] | ||
match_by = "re" | ||
} | ||
|
||
filter { | ||
key = "type" | ||
values = ["instance"] | ||
} | ||
|
||
} | ||
|
||
resource "civo_instance" "my-test-instance" { | ||
hostname = "foo.com" | ||
tags = ["python", "nginx"] | ||
notes = "this is a note for the server" | ||
size = element(data.civo_size.small.sizes, 0).name | ||
disk_image = element(data.civo_disk_image.debian.diskimages, 0).id | ||
} |