Skip to content

Commit

Permalink
Merge pull request rancher#128 from rawmind0/multiapp
Browse files Browse the repository at this point in the history
Added rancher2_multi_cluster_app datasource and resource
  • Loading branch information
rawmind0 authored Sep 6, 2019
2 parents 15d715d + 5743d0d commit 4dec1a2
Show file tree
Hide file tree
Showing 26 changed files with 2,249 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ FEATURES:

* **New Data Source:** `rancher2_app`
* **New Data Source:** `rancher2_certificate`
* **New Data Source:** `rancher2_multi_cluster_app`
* **New Data Source:** `rancher2_node_template`
* **New Data Source:** `rancher2_secret`
* **New Resource:** `rancher2_certificate`
* **New Resource:** `rancher2_app`
* **New Resource:** `rancher2_multi_cluster_app`
* **New Resource:** `rancher2_secret`

ENHANCEMENTS:
Expand Down
129 changes: 129 additions & 0 deletions rancher2/data_source_rancher2_multi_cluster_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package rancher2

import (
"fmt"

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

func dataSourceRancher2MultiClusterApp() *schema.Resource {
return &schema.Resource{
Read: dataSourceRancher2MultiClusterAppRead,

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
Description: "Multi cluster app name",
},
"targets": {
Type: schema.TypeList,
Computed: true,
Description: "Multi cluster app targets",
Elem: &schema.Resource{
Schema: targetFields(),
},
},
"catalog_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Multi cluster app catalog name",
},
"answers": {
Type: schema.TypeList,
Computed: true,
Description: "Multi cluster app answers",
Elem: &schema.Resource{
Schema: answerFields(),
},
},
"members": {
Type: schema.TypeList,
Computed: true,
Description: "Multi cluster app members",
Elem: &schema.Resource{
Schema: memberFields(),
},
},
"revision_history_limit": {
Type: schema.TypeInt,
Computed: true,
Description: "Multi cluster app revision history limit",
},
"revision_id": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Multi cluster app revision id",
},
"roles": {
Type: schema.TypeList,
Computed: true,
Description: "Multi cluster app members",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"template_version": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Multi cluster app template version",
},
"template_version_id": {
Type: schema.TypeString,
Computed: true,
Description: "Multi cluster app template version ID",
},
"template_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
Description: "Multi cluster app template name",
},
"upgrade_strategy": {
Type: schema.TypeList,
Computed: true,
Description: "Multi cluster app upgrade strategy",
Elem: &schema.Resource{
Schema: upgradeStrategyFields(),
},
},
"annotations": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
"labels": &schema.Schema{
Type: schema.TypeMap,
Computed: true,
},
},
}
}

func dataSourceRancher2MultiClusterAppRead(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)

filters := map[string]interface{}{
"name": name,
}

listOpts := NewListOpts(filters)

client, err := meta.(*Config).ManagementClient()
if err != nil {
return err
}

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

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

return flattenMultiClusterApp(d, &multiClusterApps.Data[0])
}
73 changes: 73 additions & 0 deletions rancher2/data_source_rancher2_multi_cluster_app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package rancher2

import (
"testing"

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

const (
testAccRancher2MultiClusterAppDataSourceType = "rancher2_multi_cluster_app"
)

var (
testAccCheckRancher2MultiClusterAppDataSourceConfig string
)

func init() {
testAccCheckRancher2MultiClusterAppDataSourceConfig = `
resource "rancher2_project" "foo" {
name = "foo"
cluster_id = "` + testAccRancher2ClusterID + `"
description = "Terraform project acceptance test"
resource_quota {
project_limit {
limits_cpu = "2000m"
limits_memory = "2000Mi"
requests_storage = "2Gi"
}
namespace_default_limit {
limits_cpu = "500m"
limits_memory = "500Mi"
requests_storage = "1Gi"
}
}
}
resource "rancher2_multi_cluster_app" "foo" {
catalog_name = "library"
name = "foo"
targets {
project_id = "${rancher2_project.foo.id}"
}
template_name = "docker-registry"
template_version = "1.6.1"
answers {
values = {
"ingress_host" = "test.xip.io"
}
}
roles = ["project-member"]
}
data "` + testAccRancher2MultiClusterAppDataSourceType + `" "foo" {
name = "${rancher2_multi_cluster_app.foo.name}"
}
`
}

func TestAccRancher2MultiClusterAppDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckRancher2MultiClusterAppDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data."+testAccRancher2MultiClusterAppDataSourceType+".foo", "name", "foo"),
resource.TestCheckResourceAttr("data."+testAccRancher2MultiClusterAppDataSourceType+".foo", "template_version_id", "cattle-global-data:library-docker-registry-1.6.1"),
resource.TestCheckResourceAttr("data."+testAccRancher2MultiClusterAppDataSourceType+".foo", "answers.0.values.ingress_host", "test.xip.io"),
resource.TestCheckResourceAttr("data."+testAccRancher2MultiClusterAppDataSourceType+".foo", "roles.0", "project-member"),
),
},
},
})
}
18 changes: 18 additions & 0 deletions rancher2/import_rancher2_multi_cluster_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package rancher2

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

func resourceRancher2MultiClusterAppImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
resourceID := "cattle-global-data:" + d.Id()

d.SetId(resourceID)

err := resourceRancher2MultiClusterAppRead(d, meta)
if err != nil {
return []*schema.ResourceData{}, err
}

return []*schema.ResourceData{d}, nil
}
2 changes: 2 additions & 0 deletions rancher2/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ func Provider() terraform.ResourceProvider {
"rancher2_cluster_role_template_binding": resourceRancher2ClusterRoleTemplateBinding(),
"rancher2_etcd_backup": resourceRancher2EtcdBackup(),
"rancher2_global_role_binding": resourceRancher2GlobalRoleBinding(),
"rancher2_multi_cluster_app": resourceRancher2MultiClusterApp(),
"rancher2_namespace": resourceRancher2Namespace(),
"rancher2_node_driver": resourceRancher2NodeDriver(),
"rancher2_node_pool": resourceRancher2NodePool(),
Expand All @@ -118,6 +119,7 @@ func Provider() terraform.ResourceProvider {
"rancher2_cluster_role_template_binding": dataSourceRancher2ClusterRoleTemplateBinding(),
"rancher2_etcd_backup": dataSourceRancher2EtcdBackup(),
"rancher2_global_role_binding": dataSourceRancher2GlobalRoleBinding(),
"rancher2_multi_cluster_app": dataSourceRancher2MultiClusterApp(),
"rancher2_namespace": dataSourceRancher2Namespace(),
"rancher2_node_driver": dataSourceRancher2NodeDriver(),
"rancher2_node_pool": dataSourceRancher2NodePool(),
Expand Down
Loading

0 comments on commit 4dec1a2

Please sign in to comment.