Skip to content

Commit

Permalink
Merge pull request rancher#54 from rawmind0/nodepool_ds
Browse files Browse the repository at this point in the history
Node pool data source
  • Loading branch information
rawmind0 authored Jul 12, 2019
2 parents d33be33 + da63df3 commit 085cadc
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ FEATURES:

* **New Data Source:** `rancher2_catalog`
* **New Data Source:** `rancher2_cloud_credential`
* **New Data Source:** `rancher2_node_pool`

ENHANCEMENTS:

Expand Down
92 changes: 92 additions & 0 deletions rancher2/data_source_rancher2_node_pool.go
Original file line number Diff line number Diff line change
@@ -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])
}
86 changes: 86 additions & 0 deletions rancher2/data_source_rancher2_node_pool_test.go
Original file line number Diff line number Diff line change
@@ -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"),
),
},
},
})
}
1 change: 1 addition & 0 deletions rancher2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ func Provider() terraform.ResourceProvider {
DataSourcesMap: map[string]*schema.Resource{
"rancher2_catalog": dataSourceRancher2Catalog(),
"rancher2_cloud_credential": dataSourceRancher2CloudCredential(),
"rancher2_node_pool": dataSourceRancher2NodePool(),
"rancher2_project": dataSourceRancher2Project(),
"rancher2_setting": dataSourceRancher2Setting(),
},
Expand Down
19 changes: 14 additions & 5 deletions rancher2/resource_rancher2_node_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -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"
Expand Down
38 changes: 38 additions & 0 deletions website/docs/d/nodePool.html.markdown
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 10 additions & 2 deletions website/docs/r/nodePool.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<AWS_SECRET_KEY>"
ami = "<AMI_ID>"
region = "<REGION>"
security_group = ["<AWS_SECURITY_GROUP>"]
Expand Down
3 changes: 3 additions & 0 deletions website/rancher2.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
<li<%= sidebar_current("docs-rancher2-datasource-cloud_credential") %>>
<a href="/docs/providers/rancher2/d/cloudCredential.html">rancher2_cloud_credential</a>
</li>
<li<%= sidebar_current("docs-rancher2-datasource-node_pool") %>>
<a href="/docs/providers/rancher2/d/nodePool.html">rancher2_node_pool</a>
</li>
<li<%= sidebar_current("docs-rancher2-datasource-project") %>>
<a href="/docs/providers/rancher2/d/project.html">rancher2_project</a>
</li>
Expand Down

0 comments on commit 085cadc

Please sign in to comment.