Skip to content

Commit

Permalink
Added kubernetes cluster data source
Browse files Browse the repository at this point in the history
Signed-off-by: Alejandro JNM <alejandrojnm@gmail.com>
  • Loading branch information
alejandrojnm committed Jul 6, 2020
1 parent 92073ab commit bbb1821
Show file tree
Hide file tree
Showing 6 changed files with 401 additions and 1 deletion.
209 changes: 209 additions & 0 deletions civo/datasource_kubernetes_cluster.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package civo

import (
"fmt"
"log"

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

// Data source to get from the api a specific instance
// using the id or the hostname
func dataSourceKubernetesCluster() *schema.Resource {
return &schema.Resource{
Read: dataSourceKubernetesClusterRead,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validation.NoZeroValues,
ExactlyOneOf: []string{"id", "name"},
},
// computed attributes
"num_target_nodes": {
Type: schema.TypeInt,
Computed: true,
},
"target_nodes_size": {
Type: schema.TypeString,
Computed: true,
},
"kubernetes_version": {
Type: schema.TypeString,
Computed: true,
},
"tags": {
Type: schema.TypeString,
Computed: true,
},
"applications": {
Type: schema.TypeString,
Computed: true,
},
"instances": dataSourceInstanceSchema(),
"installed_applications": dataSourceApplicationSchema(),
"status": {
Type: schema.TypeString,
Computed: true,
},
"ready": {
Type: schema.TypeBool,
Computed: true,
},
"kubeconfig": {
Type: schema.TypeString,
Computed: true,
},
"api_endpoint": {
Type: schema.TypeString,
Computed: true,
},
"master_ip": {
Type: schema.TypeString,
Computed: true,
},
"dns_entry": {
Type: schema.TypeString,
Computed: true,
},
"built_at": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

// schema for the instances
func dataSourceInstanceSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"hostname": {
Type: schema.TypeString,
Computed: true,
},
"size": {
Type: schema.TypeString,
Computed: true,
},
"region": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"firewall_id": {
Type: schema.TypeString,
Computed: true,
},
"public_ip": {
Type: schema.TypeString,
Computed: true,
},
"tags": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
},
},
}
}

// schema for the application in the cluster
func dataSourceApplicationSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"application": {
Type: schema.TypeString,
Computed: true,
},
"version": {
Type: schema.TypeString,
Computed: true,
},
"installed": {
Type: schema.TypeBool,
Computed: true,
},
"category": {
Type: schema.TypeString,
Computed: true,
},
},
},
}
}

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

var foundCluster *civogo.KubernetesCluster

if id, ok := d.GetOk("id"); ok {
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)
}

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)
}

foundCluster = kubeCluster
}

d.SetId(foundCluster.ID)
d.Set("name", foundCluster.Name)
d.Set("num_target_nodes", foundCluster.NumTargetNode)
d.Set("target_nodes_size", foundCluster.TargetNodeSize)
d.Set("kubernetes_version", foundCluster.KubernetesVersion)
d.Set("tags", foundCluster.Tags)
d.Set("status", foundCluster.Status)
d.Set("ready", foundCluster.Ready)
d.Set("kubeconfig", foundCluster.KubeConfig)
d.Set("api_endpoint", foundCluster.APIEndPoint)
d.Set("master_ip", foundCluster.MasterIP)
d.Set("dns_entry", foundCluster.DNSEntry)
d.Set("built_at", foundCluster.BuiltAt.UTC().String())
d.Set("created_at", foundCluster.CreatedAt.UTC().String())

if err := d.Set("instances", flattenInstances(foundCluster.Instances)); err != nil {
return fmt.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 nil
}
81 changes: 81 additions & 0 deletions civo/datasource_kubernetes_cluster_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package civo

import (
"fmt"
"testing"

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

func TestAccDataSourceCivoKubernetesCluster_basic(t *testing.T) {
datasourceName := "data.civo_kubernetes_cluster.foobar"
name := acctest.RandomWithPrefix("k8s")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoKubernetesClusterConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttr(datasourceName, "num_target_nodes", "2"),
resource.TestCheckResourceAttr(datasourceName, "target_nodes_size", "g2.small"),
resource.TestCheckResourceAttrSet(datasourceName, "kubeconfig"),
resource.TestCheckResourceAttrSet(datasourceName, "api_endpoint"),
resource.TestCheckResourceAttrSet(datasourceName, "master_ip"),
),
},
},
})
}

func TestAccDataSourceCivoKubernetesClusterByID_basic(t *testing.T) {
datasourceName := "data.civo_kubernetes_cluster.foobar"
name := acctest.RandomWithPrefix("k8s")

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceCivoKubernetesClusterByIDConfig(name),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(datasourceName, "name", name),
resource.TestCheckResourceAttr(datasourceName, "num_target_nodes", "2"),
resource.TestCheckResourceAttr(datasourceName, "target_nodes_size", "g2.small"),
resource.TestCheckResourceAttrSet(datasourceName, "kubeconfig"),
resource.TestCheckResourceAttrSet(datasourceName, "api_endpoint"),
resource.TestCheckResourceAttrSet(datasourceName, "master_ip"),
),
},
},
})
}

func testAccDataSourceCivoKubernetesClusterConfig(name string) string {
return fmt.Sprintf(`
resource "civo_kubernetes_cluster" "my-cluster" {
name = "%s"
num_target_nodes = 2
}
data "civo_kubernetes_cluster" "foobar" {
name = civo_kubernetes_cluster.my-cluster.name
}
`, name)
}

func testAccDataSourceCivoKubernetesClusterByIDConfig(name string) string {
return fmt.Sprintf(`
resource "civo_kubernetes_cluster" "my-cluster" {
name = "%s"
num_target_nodes = 2
}
data "civo_kubernetes_cluster" "foobar" {
id = civo_kubernetes_cluster.my-cluster.id
}
`, name)
}
33 changes: 33 additions & 0 deletions civo/import_dns_domain_name_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package civo

import (
"testing"

"fmt"

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

func TestAccCivoDNSDomainName_importBasic(t *testing.T) {
resourceName := "civo_dns_domain_name.foobar"
domainName := fmt.Sprintf("foobar-test-terraform-%s.com", acctest.RandString(10))

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckCivoDNSDomainNameDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckCivoDNSDomainNameConfigBasic(domainName),
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
ImportStateId: fmt.Sprint(domainName),
},
},
})
}
1 change: 1 addition & 0 deletions civo/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"civo_template": dataSourceTemplate(),
"civo_kubernetes_version": dataSourceKubernetesVersion(),
"civo_kubernetes_cluster": dataSourceKubernetesCluster(),
"civo_instances_size": dataSourceInstancesSize(),
"civo_instances": dataSourceInstances(),
"civo_instance": dataSourceInstance(),
Expand Down
2 changes: 1 addition & 1 deletion civo/resource_dns_domain_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func resourceDNSDomainNameDelete(d *schema.ResourceData, m interface{}) error {
func resourceDNSDomainImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
apiClient := m.(*civogo.Client)

log.Printf("[INFO] Searching the domain %s", d.Get("name").(string))
log.Printf("[INFO] Searching the domain %s", d.Id())
resp, err := apiClient.GetDNSDomain(d.Id())
if err != nil {
if resp != nil {
Expand Down
Loading

0 comments on commit bbb1821

Please sign in to comment.