Skip to content

Commit

Permalink
Updated the terraform SDK in all resources (#125)
Browse files Browse the repository at this point in the history
* Updated the terraform SDK in all resources and datasource, fixed bugs in the Kubernetes resource and Kubernetes node pool
* Reformat code

Close #124
  • Loading branch information
alejandrojnm committed Apr 1, 2022
1 parent 2634c80 commit 7d1fbfc
Show file tree
Hide file tree
Showing 26 changed files with 617 additions and 368 deletions.
11 changes: 6 additions & 5 deletions civo/datasource_dns_domain_name.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package civo

import (
"fmt"
"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"
)
Expand All @@ -18,7 +19,7 @@ func dataSourceDNSDomainName() *schema.Resource {
"Get information on a domain. This data source provides the name and the id.",
"An error will be raised if the provided domain name is not in your Civo account.",
}, "\n\n"),
Read: dataSourceDNSDomainNameRead,
ReadContext: dataSourceDNSDomainNameRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand All @@ -37,7 +38,7 @@ func dataSourceDNSDomainName() *schema.Resource {
}
}

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

var foundDomain *civogo.DNSDomain
Expand All @@ -46,15 +47,15 @@ func dataSourceDNSDomainNameRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] Getting the domain by id")
domain, err := apiClient.FindDNSDomain(id.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive domain: %s", err)
return diag.Errorf("[ERR] failed to retrive domain: %s", err)
}

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

foundDomain = image
Expand Down
10 changes: 6 additions & 4 deletions civo/datasource_dns_domain_record.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package civo

import (
"context"
"fmt"
"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"
)
Expand All @@ -17,7 +19,7 @@ func dataSourceDNSDomainRecord() *schema.Resource {
"Get information on a DNS record. This data source provides the name, TTL, and zone file as configured on your Civo account.",
"An error will be raised if the provided domain name or record are not in your Civo account.",
}, "\n\n"),
Read: dataSourceDNSDomainRecordRead,
ReadContext: dataSourceDNSDomainRecordRead,
Schema: map[string]*schema.Schema{
"domain_id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -71,19 +73,19 @@ func dataSourceDNSDomainRecord() *schema.Resource {
}
}

func dataSourceDNSDomainRecordRead(d *schema.ResourceData, m interface{}) error {
func dataSourceDNSDomainRecordRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
apiClient := m.(*civogo.Client)
domain := d.Get("domain_id").(string)
name := d.Get("name").(string)

allRecords, err := apiClient.ListDNSRecords(domain)
if err != nil {
return fmt.Errorf("error retrieving all domain records: %s", err)
return diag.Errorf("error retrieving all domain records: %s", err)
}

record, err := getRecordByName(allRecords, name)
if err != nil {
return err
return diag.Errorf("Error retrieving records by name: %s", err)
}

d.SetId(record.ID)
Expand Down
11 changes: 6 additions & 5 deletions civo/datasource_firewall.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package civo

import (
"fmt"
"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"
)
Expand All @@ -19,7 +20,7 @@ func dataSourceFirewall() *schema.Resource {
"This data source provides all of the firewall's properties as configured on your Civo account.",
"Firewalls may be looked up by id or name, and you can optionally pass region if you want to make a lookup for an expecific firewall inside that region.",
}, "\n\n"),
Read: dataSourceFirewallRead,
ReadContext: dataSourceFirewallRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -50,7 +51,7 @@ func dataSourceFirewall() *schema.Resource {
}
}

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

// overwrite the region if is define in the datasource
Expand All @@ -64,15 +65,15 @@ func dataSourceFirewallRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] Getting the firewall by id")
firewall, err := apiClient.FindFirewall(id.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive firewall: %s", err)
return diag.Errorf("[ERR] failed to retrive firewall: %s", err)
}

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

foundFirewall = firewall
Expand Down
11 changes: 6 additions & 5 deletions civo/datasource_instance.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package civo

import (
"fmt"
"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"
)
Expand All @@ -18,7 +19,7 @@ func dataSourceInstance() *schema.Resource {
"Get information on an instance for use in other resources. This data source provides all of the instance's properties as configured on your Civo account.",
"Note: This data source returns a single instance. When specifying a hostname, an error will be raised if more than one instances found.",
}, "\n\n"),
Read: dataSourceInstanceRead,
ReadContext: dataSourceInstanceRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -140,7 +141,7 @@ func dataSourceInstance() *schema.Resource {
}
}

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

// overwrite the region if is define in the datasource
Expand All @@ -154,15 +155,15 @@ func dataSourceInstanceRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[INFO] Getting the instance by id")
image, err := apiClient.FindInstance(id.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive instance: %s", err)
return diag.Errorf("[ERR] failed to retrive instance: %s", err)
}

foundImage = image
} else if hostname, ok := d.GetOk("hostname"); ok {
log.Printf("[INFO] Getting the instance by hostname")
image, err := apiClient.FindInstance(hostname.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive instance: %s", err)
return diag.Errorf("[ERR] failed to retrive instance: %s", err)
}

foundImage = image
Expand Down
63 changes: 52 additions & 11 deletions civo/datasource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package civo

import (
"fmt"
"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"
)
Expand All @@ -18,7 +19,7 @@ func dataSourceKubernetesCluster() *schema.Resource {
"Provides a Civo Kubernetes cluster data source.",
"Note: This data source returns a single Kubernetes cluster. When specifying a name, an error will be raised if more than one Kubernetes cluster found.",
}, "\n\n"),
Read: dataSourceKubernetesClusterRead,
ReadContext: dataSourceKubernetesClusterRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand All @@ -42,11 +43,13 @@ func dataSourceKubernetesCluster() *schema.Resource {
// computed attributes
"num_target_nodes": {
Type: schema.TypeInt,
Deprecated: "This field is deprecated and will be removed in a future version of the provider",
Computed: true,
Description: "The size of the Kubernetes cluster",
},
"target_nodes_size": {
Type: schema.TypeString,
Deprecated: "This field is deprecated and will be removed in a future version of the provider",
Computed: true,
Description: "The size of each node",
},
Expand Down Expand Up @@ -172,7 +175,7 @@ func dataSourcenodePoolSchema() *schema.Schema {
Computed: true,
Description: "The ID of the pool",
},
"count": {
"node_count": {
Type: schema.TypeInt,
Computed: true,
Description: "The size of the pool",
Expand Down Expand Up @@ -226,7 +229,7 @@ func dataSourceApplicationSchema() *schema.Schema {
}
}

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

// overwrite the region if is define in the datasource
Expand All @@ -240,15 +243,14 @@ func dataSourceKubernetesClusterRead(d *schema.ResourceData, m interface{}) erro
log.Printf("[INFO] Getting the kubernetes Cluster by id")
kubeCluster, err := apiClient.FindKubernetesCluster(id.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive kubernetes cluster: %s", err)
return diag.Errorf("[ERR] failed to retrive kubernetes cluster: %s", err)
}

foundCluster = kubeCluster
} else if name, ok := d.GetOk("name"); ok {
log.Printf("[INFO] Getting the kubernetes Cluster by name")
kubeCluster, err := apiClient.FindKubernetesCluster(name.(string))
if err != nil {
return fmt.Errorf("[ERR] failed to retrive kubernetes cluster: %s", err)
return diag.Errorf("[ERR] failed to retrive kubernetes cluster: %s", err)
}

foundCluster = kubeCluster
Expand All @@ -269,17 +271,56 @@ func dataSourceKubernetesClusterRead(d *schema.ResourceData, m interface{}) erro
d.Set("dns_entry", foundCluster.DNSEntry)
d.Set("created_at", foundCluster.CreatedAt.UTC().String())

if err := d.Set("pools", flattenNodePool(foundCluster)); err != nil {
return fmt.Errorf("[ERR] error retrieving the pools for kubernetes cluster error: %#v", err)
if err := d.Set("pools", dsflattenNodePool(foundCluster)); err != nil {
return diag.Errorf("[ERR] error retrieving the pools for kubernetes cluster error: %#v", err)
}

if err := d.Set("instances", flattenInstances(foundCluster.Instances)); err != nil {
return fmt.Errorf("[ERR] error retrieving the instances for kubernetes cluster error: %#v", err)
return diag.Errorf("[ERR] error retrieving the instances for kubernetes cluster error: %#v", err)
}

if err := d.Set("installed_applications", flattenInstalledApplication(foundCluster.InstalledApplications)); err != nil {
return fmt.Errorf("[ERR] error retrieving the installed application for kubernetes cluster error: %#v", err)
return diag.Errorf("[ERR] error retrieving the installed application for kubernetes cluster error: %#v", err)
}

return nil
}

// function to flatten all instances inside the cluster
func dsflattenNodePool(cluster *civogo.KubernetesCluster) []interface{} {

if cluster.Pools == nil {
return nil
}

flattenedPool := make([]interface{}, 0)
for _, pool := range cluster.Pools {
flattenedPoolInstance := make([]interface{}, 0)
for _, v := range pool.Instances {

rawPoolInstance := map[string]interface{}{
"hostname": v.Hostname,
"size": pool.Size,
"cpu_cores": v.CPUCores,
"ram_mb": v.RAMMegabytes,
"disk_gb": v.DiskGigabytes,
"status": v.Status,
"tags": v.Tags,
}
flattenedPoolInstance = append(flattenedPoolInstance, rawPoolInstance)
}
instanceName := append(pool.InstanceNames, pool.InstanceNames...)

rawPool := map[string]interface{}{
"id": pool.ID,
"node_count": pool.Count,
"size": pool.Size,
"instance_names": instanceName,
"instances": flattenedPoolInstance,
}

flattenedPool = append(flattenedPool, rawPool)
}

return flattenedPool
}
16 changes: 11 additions & 5 deletions civo/datasource_loadbalancer.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package civo

import (
"fmt"
"context"
"log"
"strings"

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

Expand All @@ -17,7 +18,7 @@ func dataSourceLoadBalancer() *schema.Resource {
"Get information on a load balancer for use in other resources. This data source provides all of the load balancers properties as configured on your Civo account.",
"An error will be raised if the provided load balancer name does not exist in your Civo account.",
}, "\n\n"),
Read: dataSourceLoadBalancerRead,
ReadContext: dataSourceLoadBalancerRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand All @@ -29,6 +30,11 @@ func dataSourceLoadBalancer() *schema.Resource {
Optional: true,
Description: "The name of the load balancer (You can find this name from service annotations 'kubernetes.civo.com/loadbalancer-name')",
},
"region": {
Type: schema.TypeString,
Optional: true,
Description: "The region of the load balancer, if you delcare this field, the datasource will use this value instead of the one defined in the provider",
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -117,7 +123,7 @@ func dataSourceLoadBalancer() *schema.Resource {
}
}

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

// overwrite the region if is define in the datasource
Expand All @@ -137,7 +143,7 @@ func dataSourceLoadBalancerRead(d *schema.ResourceData, m interface{}) error {

lb, err := apiClient.FindLoadBalancer(searchBy)
if err != nil {
return fmt.Errorf("[ERR] failed to retrive LoadBalancer: %s", err)
return diag.Errorf("[ERR] failed to retrive LoadBalancer: %s", err)
}

d.SetId(lb.ID)
Expand All @@ -154,7 +160,7 @@ func dataSourceLoadBalancerRead(d *schema.ResourceData, m interface{}) error {
d.Set("state", lb.State)

if err := d.Set("backends", flattenLoadBalancerBackend(lb.Backends)); err != nil {
return fmt.Errorf("[ERR] error retrieving the backends for load balancer error: %#v", err)
return diag.Errorf("[ERR] error retrieving the backends for load balancer error: %#v", err)
}

return nil
Expand Down
Loading

0 comments on commit 7d1fbfc

Please sign in to comment.