Skip to content

Commit

Permalink
Addes more fix to the provider, updated the civogo lib
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed May 11, 2021
1 parent a049b43 commit df85511
Show file tree
Hide file tree
Showing 17 changed files with 719 additions and 178 deletions.
57 changes: 47 additions & 10 deletions civo/datasource_intances_size.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,24 @@ 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 SizeList 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 dataSourceInstancesSize() *schema.Resource {
Expand All @@ -31,7 +43,32 @@ func getInstancesSizes(m interface{}, extra map[string]interface{}) ([]interface
return nil, fmt.Errorf("[ERR] error retrieving sizes: %s", err)
}

for _, partialSize := range partialSizes {
sizeList := []SizeList{}

for _, v := range partialSizes {
typeName := ""

switch {
case strings.Contains(v.Name, "db"):
typeName = "database"
case strings.Contains(v.Name, "k3s"):
typeName = "kubernetes"
default:
typeName = "instance"
}

sizeList = append(sizeList, SizeList{
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)
}

Expand All @@ -40,14 +77,14 @@ func getInstancesSizes(m interface{}, extra map[string]interface{}) ([]interface

func flattenInstancesSize(size, m interface{}, extra map[string]interface{}) (map[string]interface{}, error) {

s := size.(civogo.InstanceSize)
s := size.(SizeList)

flattenedSize := map[string]interface{}{}
flattenedSize["name"] = s.Name
flattenedSize["nice_name"] = s.NiceName
flattenedSize["cpu_cores"] = s.CPUCores
flattenedSize["ram_mb"] = s.RAMMegabytes
flattenedSize["disk_gb"] = s.DiskGigabytes
flattenedSize["type"] = s.Type
flattenedSize["cpu"] = s.CPU
flattenedSize["ram"] = s.RAM
flattenedSize["disk"] = s.DisK
flattenedSize["description"] = s.Description
flattenedSize["selectable"] = s.Selectable

Expand All @@ -60,19 +97,19 @@ func instancesSizeSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"nice_name": {
"type": {
Type: schema.TypeString,
Computed: true,
},
"cpu_cores": {
"cpu": {
Type: schema.TypeInt,
Computed: true,
},
"ram_mb": {
"ram": {
Type: schema.TypeInt,
Computed: true,
},
"disk_gb": {
"disk": {
Type: schema.TypeInt,
Computed: true,
},
Expand Down
31 changes: 23 additions & 8 deletions civo/datasource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func dataSourceKubernetesCluster() *schema.Resource {
},
"instances": dataSourceInstanceSchema(),
"installed_applications": dataSourceApplicationSchema(),
"pools": dataSourcenodePoolSchema(),
"status": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -118,31 +119,45 @@ func dataSourceInstanceSchema() *schema.Schema {
Type: schema.TypeInt,
Computed: true,
},
"region": {
"status": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
"tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"created_at": {
},
},
}
}

// schema for the node pool in the cluster
func dataSourcenodePoolSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"firewall_id": {
Type: schema.TypeString,
"count": {
Type: schema.TypeInt,
Computed: true,
},
"public_ip": {
"size": {
Type: schema.TypeString,
Computed: true,
},
"tags": {
"instance_names": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"instances": dataSourceInstanceSchema(),
},
},
}
Expand Down
89 changes: 53 additions & 36 deletions civo/datasource_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,26 @@ import (
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

// TemplateDisk is a temporal struct to get all template in one place
type TemplateDisk struct {
ID string
Name string
Version string
Label string
}

// Data source to get from the api a specific template
// using the code of the image
func dataSourceTemplate() *schema.Resource {

dataListConfig := &datalist.ResourceConfig{
RecordSchema: templateSchema(),
RecordSchema: templateSchema(),
ExtraQuerySchema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
},
},
ResultAttributeName: "templates",
FlattenRecord: flattenTemplate,
GetRecords: getTemplates,
Expand All @@ -26,13 +40,41 @@ func dataSourceTemplate() *schema.Resource {
func getTemplates(m interface{}, extra map[string]interface{}) ([]interface{}, error) {
apiClient := m.(*civogo.Client)

templates := []interface{}{}
partialTemplates, err := apiClient.ListTemplates()
if err != nil {
return nil, fmt.Errorf("[ERR] error retrieving all templates: %s", err)
// overwrite the region if is define in the datasource
region, ok := extra["region"].(string)
if !ok {
return nil, fmt.Errorf("unable to find `region` key from query data")
}

if region != "" {
apiClient.Region = region
}

templateDiskList := []TemplateDisk{}

if apiClient.Region == "SVG1" {
templates, err := apiClient.ListTemplates()
if err != nil {
return nil, fmt.Errorf("[ERR] error retrieving all templates: %s", err)
}

for _, v := range templates {
templateDiskList = append(templateDiskList, TemplateDisk{ID: v.ID, Name: v.Name, Version: v.Code, Label: v.ShortDescription})
}
} else {
diskImage, err := apiClient.ListDiskImages()
if err != nil {
return nil, fmt.Errorf("[ERR] error retrieving all Disk Images: %s", err)
}

for _, v := range diskImage {
templateDiskList = append(templateDiskList, TemplateDisk{ID: v.ID, Name: v.Name, Version: v.Version, Label: v.Label})
}

}

for _, partialSize := range partialTemplates {
templates := []interface{}{}
for _, partialSize := range templateDiskList {
templates = append(templates, partialSize)
}

Expand All @@ -41,18 +83,13 @@ func getTemplates(m interface{}, extra map[string]interface{}) ([]interface{}, e

func flattenTemplate(template, m interface{}, extra map[string]interface{}) (map[string]interface{}, error) {

s := template.(civogo.Template)
s := template.(TemplateDisk)

flattenedTemplate := map[string]interface{}{}
flattenedTemplate["id"] = s.ID
flattenedTemplate["code"] = s.Code
flattenedTemplate["name"] = s.Name
flattenedTemplate["volume_id"] = s.VolumeID
flattenedTemplate["image_id"] = s.ImageID
flattenedTemplate["short_description"] = s.ShortDescription
flattenedTemplate["description"] = s.Description
flattenedTemplate["default_username"] = s.DefaultUsername
flattenedTemplate["cloud_config"] = s.CloudConfig
flattenedTemplate["version"] = s.Version
flattenedTemplate["label"] = s.Label

return flattenedTemplate, nil
}
Expand All @@ -64,35 +101,15 @@ func templateSchema() map[string]*schema.Schema {
Type: schema.TypeString,
Computed: true,
},
"code": {
Type: schema.TypeString,
Computed: true,
},
"name": {
Type: schema.TypeString,
Computed: true,
},
"volume_id": {
Type: schema.TypeString,
Computed: true,
},
"image_id": {
Type: schema.TypeString,
Computed: true,
},
"short_description": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"default_username": {
"version": {
Type: schema.TypeString,
Computed: true,
},
"cloud_config": {
"label": {
Type: schema.TypeString,
Computed: true,
},
Expand Down
27 changes: 14 additions & 13 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,20 @@ func Provider() *schema.Provider {
"civo_region": dataSourceRegion(),
},
ResourcesMap: map[string]*schema.Resource{
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
"civo_volume": resourceVolume(),
"civo_volume_attachment": resourceVolumeAttachment(),
"civo_dns_domain_name": resourceDNSDomainName(),
"civo_dns_domain_record": resourceDNSDomainRecord(),
"civo_firewall": resourceFirewall(),
"civo_firewall_rule": resourceFirewallRule(),
"civo_loadbalancer": resourceLoadBalancer(),
"civo_ssh_key": resourceSSHKey(),
"civo_template": resourceTemplate(),
"civo_snapshot": resourceSnapshot(),
"civo_kubernetes_cluster": resourceKubernetesCluster(),
"civo_instance": resourceInstance(),
"civo_network": resourceNetwork(),
"civo_volume": resourceVolume(),
"civo_volume_attachment": resourceVolumeAttachment(),
"civo_dns_domain_name": resourceDNSDomainName(),
"civo_dns_domain_record": resourceDNSDomainRecord(),
"civo_firewall": resourceFirewall(),
"civo_firewall_rule": resourceFirewallRule(),
"civo_loadbalancer": resourceLoadBalancer(),
"civo_ssh_key": resourceSSHKey(),
"civo_template": resourceTemplate(),
"civo_snapshot": resourceSnapshot(),
"civo_kubernetes_cluster": resourceKubernetesCluster(),
"civo_kubernetes_node_pool": resourceKubernetesClusterNodePool(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
Loading

0 comments on commit df85511

Please sign in to comment.