diff --git a/civo/datasource_kubernetes_cluster.go b/civo/datasource_kubernetes_cluster.go new file mode 100644 index 00000000..7ebd33a7 --- /dev/null +++ b/civo/datasource_kubernetes_cluster.go @@ -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 +} diff --git a/civo/datasource_kubernetes_cluster_test.go b/civo/datasource_kubernetes_cluster_test.go new file mode 100644 index 00000000..98ad174c --- /dev/null +++ b/civo/datasource_kubernetes_cluster_test.go @@ -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) +} diff --git a/civo/import_dns_domain_name_test.go b/civo/import_dns_domain_name_test.go new file mode 100644 index 00000000..58e1fab5 --- /dev/null +++ b/civo/import_dns_domain_name_test.go @@ -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), + }, + }, + }) +} diff --git a/civo/provider.go b/civo/provider.go index cd8d60d5..a327a0ad 100644 --- a/civo/provider.go +++ b/civo/provider.go @@ -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(), diff --git a/civo/resource_dns_domain_name.go b/civo/resource_dns_domain_name.go index cc1b8b3c..c005c9d3 100644 --- a/civo/resource_dns_domain_name.go +++ b/civo/resource_dns_domain_name.go @@ -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 { diff --git a/website/docs/d/kubernetes_cluster.html.md b/website/docs/d/kubernetes_cluster.html.md new file mode 100644 index 00000000..579fc85e --- /dev/null +++ b/website/docs/d/kubernetes_cluster.html.md @@ -0,0 +1,76 @@ +--- +layout: "civo" +page_title: "Civo: civo_kubernetes_cluster" +sidebar_current: "docs-civo-datasource-kubernetes-cluster" +description: |- + Get a Civo Kubernetes cluster resource. +--- + +# civo_kubernetes_cluster + +Provides a Civo Kubernetes cluster data source. + +**Note:** This data source returns a single kubernetes cluster. When specifying a `name`, an +error is triggered if more than one kubernetes Cluster is found. + +## Example Usage + +Get the Kubernetes Cluster by name: + +```hcl +data "civo_kubernetes_cluster" "my-cluster" { + name = "my-super-cluster" +} + +output "kubernetes_cluster_output" { + value = data.civo_kubernetes_cluster.my-cluster.master_ip +} +``` + +Get the Kubernetes Cluster by id: + +```hcl +data "civo_kubernetes_cluster" "my-cluster" { + name = "40ac97ee-b82b-4231-9b60-079c7e2e5d79" +} +``` +## Argument Reference + +One of following the arguments must be provided: + +* `id` - (Optional) The ID of the kubernetes Cluster +* `name` - (Optional) The name of the kubernetes Cluster. + +## Attributes Reference + +The following attributes are exported: + +* `id` - A unique ID that can be used to identify and reference a Kubernetes cluster. +* `name` - The name of your cluster,. +* `num_target_nodes` - The size of the Kubernetes cluster. +* `target_nodes_size` - The size of each node. +* `kubernetes_version` - The version of Kubernetes. +* `tags` - A list of tags. +* `applications` - A list of application installed. +* `instances` - In addition to the arguments provided, these additional attributes about the cluster's default node instance are exported. + - `hostname` - The hostname of the instance. + - `size` - The size of the instance. + - `region` - The region where instance are. + - `status` - The status of the instance. + - `created_at` - The date where the instances was created. + - `firewall_id` - The firewall id assigned to the instance + - `public_ip` - The public ip of the instances, only available if the instances is the master + - `tags` - The tag of the instances +* `installed_applications` - A unique ID that can be used to identify and reference a Kubernetes cluster. + - `application` - The name of the application + - `version` - The version of the application + - `installed` - if installed or not + - `category` - The category of the application +* `status` - The status of Kubernetes cluster. +* `ready` -If the Kubernetes cluster is ready. +* `kubeconfig` - A representation of the Kubernetes cluster's kubeconfig in yaml format. +* `api_endpoint` - The base URL of the API server on the Kubernetes master node. +* `master_ip` - The Ip of the Kubernetes master node. +* `dns_entry` - The unique dns entry for the cluster in this case point to the master. +* `built_at` - The date where the Kubernetes cluster was build. +* `created_at` - The date where the Kubernetes cluster was create. \ No newline at end of file