forked from rancher/norman
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request rancher#54 from rawmind0/nodepool_ds
Node pool data source
- Loading branch information
Showing
8 changed files
with
245 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters