From af98ac9c6f5b3ee31bf8e6020edb8f713ab61a06 Mon Sep 17 00:00:00 2001 From: rawmind0 Date: Mon, 8 Jul 2019 19:08:18 +0200 Subject: [PATCH 1/4] Added node pool data sources. Go and test files --- rancher2/data_source_rancher2_node_pool.go | 92 +++++++++++++++++++ .../data_source_rancher2_node_pool_test.go | 86 +++++++++++++++++ rancher2/provider.go | 5 +- rancher2/resource_rancher2_node_pool_test.go | 19 +++- 4 files changed, 195 insertions(+), 7 deletions(-) create mode 100644 rancher2/data_source_rancher2_node_pool.go create mode 100644 rancher2/data_source_rancher2_node_pool_test.go diff --git a/rancher2/data_source_rancher2_node_pool.go b/rancher2/data_source_rancher2_node_pool.go new file mode 100644 index 000000000..1fdce2a08 --- /dev/null +++ b/rancher2/data_source_rancher2_node_pool.go @@ -0,0 +1,92 @@ +package rancher2 + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" +) + +func dataSourceRancher2NodePool() *schema.Resource { + return &schema.Resource{ + Read: dataSourceRancher2NodePoolRead, + + Schema: map[string]*schema.Schema{ + "cluster_id": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "name": &schema.Schema{ + Type: schema.TypeString, + Required: true, + }, + "node_template_id": &schema.Schema{ + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "hostname_prefix": &schema.Schema{ + Type: schema.TypeString, + Computed: true, + }, + "quantity": &schema.Schema{ + Type: schema.TypeInt, + Computed: true, + }, + "control_plane": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + "etcd": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + "worker": &schema.Schema{ + Type: schema.TypeBool, + Computed: true, + }, + "annotations": &schema.Schema{ + Type: schema.TypeMap, + Computed: true, + }, + "labels": &schema.Schema{ + Type: schema.TypeMap, + Computed: true, + }, + }, + } +} + +func dataSourceRancher2NodePoolRead(d *schema.ResourceData, meta interface{}) error { + client, err := meta.(*Config).ManagementClient() + if err != nil { + return err + } + + clusterID := d.Get("cluster_id").(string) + name := d.Get("name").(string) + nodeTemplateID := d.Get("node_template_id").(string) + + filters := map[string]interface{}{ + "clusterId": clusterID, + "name": name, + } + if len(nodeTemplateID) > 0 { + filters["nodeTemplateId"] = nodeTemplateID + } + listOpts := NewListOpts(filters) + + nodePools, err := client.NodePool.List(listOpts) + if err != nil { + return err + } + + count := len(nodePools.Data) + if count <= 0 { + return fmt.Errorf("[ERROR] node pool with name \"%s\" on cluster ID \"%s\" not found", name, clusterID) + } + if count > 1 { + return fmt.Errorf("[ERROR] found %d node pool with name \"%s\" on cluster ID \"%s\"", count, name, clusterID) + } + + return flattenNodePool(d, &nodePools.Data[0]) +} diff --git a/rancher2/data_source_rancher2_node_pool_test.go b/rancher2/data_source_rancher2_node_pool_test.go new file mode 100644 index 000000000..d6e34b39b --- /dev/null +++ b/rancher2/data_source_rancher2_node_pool_test.go @@ -0,0 +1,86 @@ +package rancher2 + +import ( + "testing" + + "github.com/hashicorp/terraform/helper/resource" +) + +const ( + testAccRancher2NodePoolDataSourceType = "rancher2_node_pool" +) + +var ( + testAccCheckRancher2NodePoolDataSourceConfig string +) + +func init() { + testAccCheckRancher2NodePoolDataSourceConfig = ` +resource "rancher2_cluster" "foo" { + name = "foo-custom" + description = "Terraform node pool cluster acceptance test" + rke_config { + network { + plugin = "canal" + } + } +} + +resource "rancher2_cloud_credential" "foo" { + name = "foo" + description= "Terraform cloudCredential acceptance test" + amazonec2_credential_config { + access_key = "XXXXXXXXXXXXXXXXXXXX" + secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + } +} + +resource "rancher2_node_template" "foo" { + name = "foo" + description = "Terraform node pool acceptance test" + cloud_credential_id = "${rancher2_cloud_credential.foo.id}" + amazonec2_config { + ami = "ami-XXXXXXXXXXXXXXX" + region = "XX-west-1" + security_group = ["XXXXXXXX"] + subnet_id = "subnet-XXXXXXXX" + vpc_id = "vpc-XXXXXXXX" + zone = "a" + } +} + +resource "rancher2_node_pool" "foo" { + cluster_id = "${rancher2_cluster.foo.id}" + name = "foo" + hostname_prefix = "foo-cluster-0" + node_template_id = "${rancher2_node_template.foo.id}" + quantity = 1 + control_plane = true + etcd = true + worker = true +} + +data "` + testAccRancher2NodePoolDataSourceType + `" "foo" { + name = "${rancher2_node_pool.foo.name}" + cluster_id = "${rancher2_node_pool.foo.cluster_id}" +} +` +} + +func TestAccRancher2NodePoolDataSource(t *testing.T) { + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccCheckRancher2NodePoolDataSourceConfig, + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttr("data."+testAccRancher2NodePoolDataSourceType+".foo", "name", "foo"), + resource.TestCheckResourceAttr("data."+testAccRancher2NodePoolDataSourceType+".foo", "hostname_prefix", "foo-cluster-0"), + resource.TestCheckResourceAttr("data."+testAccRancher2NodePoolDataSourceType+".foo", "control_plane", "true"), + resource.TestCheckResourceAttr("data."+testAccRancher2NodePoolDataSourceType+".foo", "labels.cattle.io/creator", "norman"), + ), + }, + }, + }) +} diff --git a/rancher2/provider.go b/rancher2/provider.go index ac1a7d22a..701aaa111 100644 --- a/rancher2/provider.go +++ b/rancher2/provider.go @@ -98,8 +98,9 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ - "rancher2_project": dataSourceRancher2Project(), - "rancher2_setting": dataSourceRancher2Setting(), + "rancher2_node_pool": dataSourceRancher2NodePool(), + "rancher2_project": dataSourceRancher2Project(), + "rancher2_setting": dataSourceRancher2Setting(), }, ConfigureFunc: providerConfigure, diff --git a/rancher2/resource_rancher2_node_pool_test.go b/rancher2/resource_rancher2_node_pool_test.go index 2b0fc3a36..d19155b62 100644 --- a/rancher2/resource_rancher2_node_pool_test.go +++ b/rancher2/resource_rancher2_node_pool_test.go @@ -22,14 +22,23 @@ resource "rancher2_cluster" "foo" { } } } +` + testAccRancher2CloudCredential = ` +resource "rancher2_cloud_credential" "foo" { + name = "foo" + description= "Terraform cloudCredential acceptance test" + amazonec2_credential_config { + access_key = "XXXXXXXXXXXXXXXXXXXX" + secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + } +} ` testAccRancher2NodeTemplate = ` resource "rancher2_node_template" "foo" { name = "foo" description = "Terraform node pool acceptance test" + cloud_credential_id = "${rancher2_cloud_credential.foo.id}" amazonec2_config { - access_key = "XXXXXXXXXXXXXXXXXXXX" - secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" ami = "ami-XXXXXXXXXXXXXXX" region = "XX-west-1" security_group = ["XXXXXXXX"] @@ -48,7 +57,7 @@ var ( ) func init() { - testAccRancher2NodePoolConfig = testAccRancher2Cluster + testAccRancher2NodeTemplate + ` + testAccRancher2NodePoolConfig = testAccRancher2Cluster + testAccRancher2CloudCredential + testAccRancher2NodeTemplate + ` resource "rancher2_node_pool" "foo" { cluster_id = "${rancher2_cluster.foo.id}" name = "foo" @@ -61,7 +70,7 @@ resource "rancher2_node_pool" "foo" { } ` - testAccRancher2NodePoolUpdateConfig = testAccRancher2Cluster + testAccRancher2NodeTemplate + ` + testAccRancher2NodePoolUpdateConfig = testAccRancher2Cluster + testAccRancher2CloudCredential + testAccRancher2NodeTemplate + ` resource "rancher2_node_pool" "foo" { cluster_id = "${rancher2_cluster.foo.id}" name = "foo" @@ -74,7 +83,7 @@ resource "rancher2_node_pool" "foo" { } ` - testAccRancher2NodePoolRecreateConfig = testAccRancher2Cluster + testAccRancher2NodeTemplate + ` + testAccRancher2NodePoolRecreateConfig = testAccRancher2Cluster + testAccRancher2CloudCredential + testAccRancher2NodeTemplate + ` resource "rancher2_node_pool" "foo" { cluster_id = "${rancher2_cluster.foo.id}" name = "foo" From d7dcd77eeff44ff3ace0a3fe942d88ad5c43d2fe Mon Sep 17 00:00:00 2001 From: rawmind0 Date: Mon, 8 Jul 2019 19:08:41 +0200 Subject: [PATCH 2/4] Added node pool data sources. Doc files --- website/docs/d/nodePool.html.markdown | 38 +++++++++++++++++++++++++++ website/docs/r/nodePool.html.markdown | 12 +++++++-- website/rancher2.erb | 3 +++ 3 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 website/docs/d/nodePool.html.markdown diff --git a/website/docs/d/nodePool.html.markdown b/website/docs/d/nodePool.html.markdown new file mode 100644 index 000000000..891bd2692 --- /dev/null +++ b/website/docs/d/nodePool.html.markdown @@ -0,0 +1,38 @@ +--- +layout: "rancher2" +page_title: "Rancher2: rancher2_node_pool" +sidebar_current: "docs-rancher2-resource-node_pool" +description: |- + Get information on a Rancher v2 Node Pool resource. +--- + +# rancher2\_node\_pool + +Use this data source to retrieve information about a Rancher v2 Node Pool resource. + +## Example Usage + +```hcl +data "rancher2_node_pool" "foo" { + cluster_id = "${rancher2_cluster.foo-custom.id}" + name = "foo" +} +``` + +## Argument Reference + +* `cluster_id` - (Required) The rke cluster id to use Node Pool (string) +* `name` - (Required) The name of the Node Pool (string) +* `node_template_id` - (Optional/Computed) The Node Template ID to use for node creation (string) + + +## Attributes Reference + +* `id` - (Computed) The ID of the resource (string) +* `hostname_prefix` - (Computed) The prefix for created nodes of the Node Pool (string) +* `quantity` - (Computed) The number of nodes to create on Node Pool (int) +* `control_plane` - (Computed) RKE control plane role for created nodes (bool) +* `etcd` - (Computed) RKE etcd role for created nodes (bool) +* `worker` - (Computed) RKE role role for created nodes (bool) +* `annotations` - (Computed) Annotations for Node Pool object (map) +* `labels` - (Computed) Labels for Node Pool object (map) diff --git a/website/docs/r/nodePool.html.markdown b/website/docs/r/nodePool.html.markdown index 0854fce4d..663546ffd 100644 --- a/website/docs/r/nodePool.html.markdown +++ b/website/docs/r/nodePool.html.markdown @@ -24,13 +24,21 @@ resource "rancher2_cluster" "foo-custom" { } } } +# Create a new rancher2 Cloud Credential +resource "rancher2_cloud_credential" "foo" { + name = "foo" + description= "Terraform cloudCredential acceptance test" + amazonec2_credential_config { + access_key = "XXXXXXXXXXXXXXXXXXXX" + secret_key = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX" + } +} # Create a new rancher2 Node Template resource "rancher2_node_template" "foo" { name = "foo" description = "foo test" + cloud_credential_id = "${rancher2_cloud_credential.foo.id}" amazonec2_config { - access_key = "AWS_ACCESS_KEY" - secret_key = "" ami = "" region = "" security_group = [""] diff --git a/website/rancher2.erb b/website/rancher2.erb index 84baa29e7..bbe4e31b2 100644 --- a/website/rancher2.erb +++ b/website/rancher2.erb @@ -13,6 +13,9 @@ > Data Sources