Skip to content

Commit

Permalink
Merge pull request rancher#53 from rawmind0/clusterdriver_ds
Browse files Browse the repository at this point in the history
Cluster and node driver data sources
  • Loading branch information
rawmind0 authored Jul 12, 2019
2 parents f3d0eed + 332cac7 commit 40199a8
Show file tree
Hide file tree
Showing 9 changed files with 355 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ FEATURES:
* **New Data Source:** `rancher2_catalog`
* **New Data Source:** `rancher2_cloud_credential`
* **New Data Source:** `rancher2_cluster`
* **New Data Source:** `rancher2_cluster_driver`
* **New Data Source:** `rancher2_cluster_role_template_binding`
* **New Data Source:** `rancher2_etcd_backup`
* **New Data Source:** `rancher2_global_role_binding`
* **New Data Source:** `rancher2_namespace`
* **New Data Source:** `rancher2_node_driver`
* **New Data Source:** `rancher2_node_pool`
* **New Data Source:** `rancher2_project_role_template_binding`
* **New Data Source:** `rancher2_user`
Expand Down
93 changes: 93 additions & 0 deletions rancher2/data_source_rancher2_cluster_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package rancher2

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceRancher2ClusterDriver() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2ClusterDriverRead,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"active": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"builtin": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"actual_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"checksum": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ui_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"whitelist_domains": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"annotations": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
},
}
}

func dataSourceRancher2ClusterDriverRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}

name := d.Get("name").(string)
url := d.Get("url").(string)

filters := map[string]interface{}{
"name": name,
}
if len(url) > 0 {
filters["url"] = url
}
listOpts := NewListOpts(filters)

clusterDrivers, err := client.KontainerDriver.List(listOpts)
if err != nil {
return err
}

count := len(clusterDrivers.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] cluster driver with name \"%s\" not found", name)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d cluster driver with name \"%s\"", count, name)
}

return flattenClusterDriver(d, &clusterDrivers.Data[0])
}
40 changes: 40 additions & 0 deletions rancher2/data_source_rancher2_cluster_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rancher2

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

const (
testAccRancher2ClusterDriverDataSourceType = "rancher2_cluster_driver"
)

var (
testAccCheckRancher2ClusterDriverDataSourceConfig string
)

func init() {
testAccCheckRancher2ClusterDriverDataSourceConfig = `
data "` + testAccRancher2ClusterDriverDataSourceType + `" "foo" {
name = "amazonElasticContainerService"
}
`
}

func TestAccRancher2ClusterDriverDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckRancher2ClusterDriverDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data."+testAccRancher2ClusterDriverDataSourceType+".foo", "name", "amazonElasticContainerService"),
resource.TestCheckResourceAttr("data."+testAccRancher2ClusterDriverDataSourceType+".foo", "id", "amazonelasticcontainerservice"),
resource.TestCheckResourceAttr("data."+testAccRancher2ClusterDriverDataSourceType+".foo", "labels.cattle.io/creator", "norman"),
),
},
},
})
}
97 changes: 97 additions & 0 deletions rancher2/data_source_rancher2_node_driver.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
package rancher2

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
)

func dataSourceRancher2NodeDriver() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2NodeDriverRead,

Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"url": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"active": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"builtin": &schema.Schema{
Type: schema.TypeBool,
Computed: true,
},
"checksum": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"description": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"external_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"ui_url": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"whitelist_domains": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"annotations": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
},
}
}

func dataSourceRancher2NodeDriverRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}

name := d.Get("name").(string)
url := d.Get("url").(string)

filters := map[string]interface{}{
"name": name,
}
if len(url) > 0 {
filters["url"] = url
}
listOpts := NewListOpts(filters)

nodeDrivers, err := client.NodeDriver.List(listOpts)
if err != nil {
return err
}

count := len(nodeDrivers.Data)
if count <= 0 {
return fmt.Errorf("[ERROR] node driver with name \"%s\" not found", name)
}
if count > 1 {
return fmt.Errorf("[ERROR] found %d node driver with name \"%s\"", count, name)
}

return flattenNodeDriver(d, &nodeDrivers.Data[0])
}
40 changes: 40 additions & 0 deletions rancher2/data_source_rancher2_node_driver_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package rancher2

import (
"testing"

"github.com/hashicorp/terraform/helper/resource"
)

const (
testAccRancher2NodeDriverDataSourceType = "rancher2_node_driver"
)

var (
testAccCheckRancher2NodeDriverDataSourceConfig string
)

func init() {
testAccCheckRancher2NodeDriverDataSourceConfig = `
data "` + testAccRancher2NodeDriverDataSourceType + `" "foo" {
name = "amazonec2"
}
`
}

func TestAccRancher2NodeDriverDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckRancher2NodeDriverDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data."+testAccRancher2NodeDriverDataSourceType+".foo", "name", "amazonec2"),
resource.TestCheckResourceAttr("data."+testAccRancher2NodeDriverDataSourceType+".foo", "id", "amazonec2"),
resource.TestCheckResourceAttr("data."+testAccRancher2NodeDriverDataSourceType+".foo", "labels.cattle.io/creator", "norman"),
),
},
},
})
}
2 changes: 2 additions & 0 deletions rancher2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ func Provider() terraform.ResourceProvider {
"rancher2_catalog": dataSourceRancher2Catalog(),
"rancher2_cloud_credential": dataSourceRancher2CloudCredential(),
"rancher2_cluster": dataSourceRancher2Cluster(),
"rancher2_cluster_driver": dataSourceRancher2ClusterDriver(),
"rancher2_cluster_role_template_binding": dataSourceRancher2ClusterRoleTemplateBinding(),
"rancher2_etcd_backup": dataSourceRancher2EtcdBackup(),
"rancher2_global_role_binding": dataSourceRancher2GlobalRoleBinding(),
"rancher2_namespace": dataSourceRancher2Namespace(),
"rancher2_node_driver": dataSourceRancher2NodeDriver(),
"rancher2_node_pool": dataSourceRancher2NodePool(),
"rancher2_project": dataSourceRancher2Project(),
"rancher2_project_role_template_binding": dataSourceRancher2ProjectRoleTemplateBinding(),
Expand Down
37 changes: 37 additions & 0 deletions website/docs/d/clusterDriver.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
layout: "rancher2"
page_title: "Rancher2: rancher2_cluster_driver"
sidebar_current: "docs-rancher2-resource-cluster_driver"
description: |-
Get information on a Rancher v2 Cluster Driver resource.
---

# rancher2\_cluster\_driver

Use this data source to retrieve information about a Rancher v2 Cluster Driver resource.

## Example Usage

```hcl
data "rancher2_cluster_driver" "foo" {
name = "foo"
}
```

## Argument Reference

* `name` - (Required) Name of the cluster driver (string)
* `url` - (Optional/Computed) The URL to download the machine driver binary for 64-bit Linux (string)

## Attributes Reference

* `id` - (Computed) The ID of the resource (string)
* `active` - (Computed) Specify if the cluster driver state (bool)
* `builtin` - (Computed) Specify wheter the cluster driver is an internal cluster driver or not (bool)
* `actual_url` - (Computed) Actual url of the cluster driver (string)
* `checksum` - (Computed) Verify that the downloaded driver matches the expected checksum (string)
* `ui_url` - (Computed) The URL to load for customized Add Clusters screen for this driver (string)
* `whitelist_domains` - (Computed) Domains to whitelist for the ui (list)
* `annotations` - (Computed) Annotations of the resource (map)
* `labels` - (Computed) Labels of the resource (map)

38 changes: 38 additions & 0 deletions website/docs/d/nodeDriver.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
---
layout: "rancher2"
page_title: "Rancher2: rancher2_node_driver"
sidebar_current: "docs-rancher2-resource-node_driver"
description: |-
Get information on a Rancher v2 Node Driver resource.
---

# rancher2\_node\_driver

Use this data source to retrieve information about a Rancher v2 Node Driver resource.

## Example Usage

```hcl
data "rancher2_node_driver" "foo" {
name = "foo"
}
```

## Argument Reference

* `name` - (Required) Name of the node driver (string)
* `url` - (Optional/Computed) The URL to download the machine driver binary for 64-bit Linux (string)

## Attributes Reference

* `id` - (Computed) The ID of the resource (string)
* `active` - (Computed) Specify if the node driver state (bool)
* `builtin` - (Computed) Specify wheter the node driver is an internal cluster driver or not (bool)
* `checksum` - (Computed) Verify that the downloaded driver matches the expected checksum (string)
* `description` - (Computed) Description of the node driver (string)
* `external_id` - (Computed) External ID (string)
* `ui_url` - (Computed) The URL to load for customized Add Node screen for this driver (string)
* `whitelist_domains` - (Computed) Domains to whitelist for the ui (list)
* `annotations` - (Computed) Annotations of the resource (map)
* `labels` - (Computed) Labels of the resource (map)

6 changes: 6 additions & 0 deletions website/rancher2.erb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
<li<%= sidebar_current("docs-rancher2-datasource-cluster") %>>
<a href="/docs/providers/rancher2/d/cluster.html">rancher2_cluster</a>
</li>
<li<%= sidebar_current("docs-rancher2-datasource-cluster_driver") %>>
<a href="/docs/providers/rancher2/d/clusterDriver.html">rancher2_cluster_driver</a>
</li>
<li<%= sidebar_current("docs-rancher2-datasource-cluster_role_template_binding") %>>
<a href="/docs/providers/rancher2/d/clusterRole.html">rancher2_cluster_role_template_binding</a>
</li>
Expand All @@ -34,6 +37,9 @@
<li<%= sidebar_current("docs-rancher2-datasource-namespace") %>>
<a href="/docs/providers/rancher2/d/namespace.html">rancher2_namespace</a>
</li>
<li<%= sidebar_current("docs-rancher2-datasource-node_driver") %>>
<a href="/docs/providers/rancher2/d/nodeDriver.html">rancher2_node_driver</a>
</li>
<li<%= sidebar_current("docs-rancher2-datasource-node_pool") %>>
<a href="/docs/providers/rancher2/d/nodePool.html">rancher2_node_pool</a>
</li>
Expand Down

0 comments on commit 40199a8

Please sign in to comment.