From ec244fd2f71aead39a434b1c9693ff1c579c72dc Mon Sep 17 00:00:00 2001 From: Venkata Krishna Rohit Sakala Date: Mon, 28 Oct 2024 13:02:31 -0700 Subject: [PATCH] Remove codebase related to MCA, App & catalog --- docs/data-sources/app.md | 37 -- docs/data-sources/catalog.md | 36 -- docs/data-sources/multi_cluster_app.md | 36 -- rancher2/0_provider_upgrade_test.go | 28 -- rancher2/config.go | 135 ------ rancher2/data_source_rancher2_app.go | 119 ------ rancher2/data_source_rancher2_app_test.go | 42 -- rancher2/data_source_rancher2_catalog.go | 118 ------ rancher2/data_source_rancher2_catalog_test.go | 80 ---- .../data_source_rancher2_multi_cluster_app.go | 134 ------ ..._source_rancher2_multi_cluster_app_test.go | 31 -- rancher2/import_rancher2_app.go | 22 - rancher2/import_rancher2_catalog.go | 26 -- rancher2/import_rancher2_multi_cluster_app.go | 18 - rancher2/provider.go | 6 - rancher2/resource_rancher2_app.go | 328 --------------- rancher2/resource_rancher2_app_test.go | 234 ----------- rancher2/resource_rancher2_catalog.go | 217 ---------- rancher2/resource_rancher2_catalog_test.go | 393 ------------------ .../resource_rancher2_multi_cluster_app.go | 371 ----------------- ...esource_rancher2_multi_cluster_app_test.go | 222 ---------- rancher2/schema_app.go | 96 ----- rancher2/schema_catalog.go | 96 ----- rancher2/schema_multi_cluster_app.go | 106 ----- rancher2/structure_app.go | 164 -------- rancher2/structure_app_test.go | 280 ------------- rancher2/structure_catalog.go | 274 ------------ rancher2/structure_catalog_test.go | 137 ------ rancher2/structure_multi_cluster_app.go | 159 ------- rancher2/structure_multi_cluster_app_test.go | 200 --------- rancher2/util.go | 21 - website/rancher2.erb | 18 - 32 files changed, 4184 deletions(-) delete mode 100644 docs/data-sources/app.md delete mode 100644 docs/data-sources/catalog.md delete mode 100644 docs/data-sources/multi_cluster_app.md delete mode 100644 rancher2/data_source_rancher2_app.go delete mode 100644 rancher2/data_source_rancher2_app_test.go delete mode 100644 rancher2/data_source_rancher2_catalog.go delete mode 100644 rancher2/data_source_rancher2_catalog_test.go delete mode 100644 rancher2/data_source_rancher2_multi_cluster_app.go delete mode 100644 rancher2/data_source_rancher2_multi_cluster_app_test.go delete mode 100644 rancher2/import_rancher2_app.go delete mode 100644 rancher2/import_rancher2_catalog.go delete mode 100644 rancher2/import_rancher2_multi_cluster_app.go delete mode 100644 rancher2/resource_rancher2_app.go delete mode 100644 rancher2/resource_rancher2_app_test.go delete mode 100644 rancher2/resource_rancher2_catalog.go delete mode 100644 rancher2/resource_rancher2_catalog_test.go delete mode 100644 rancher2/resource_rancher2_multi_cluster_app.go delete mode 100644 rancher2/resource_rancher2_multi_cluster_app_test.go delete mode 100644 rancher2/schema_app.go delete mode 100644 rancher2/schema_catalog.go delete mode 100644 rancher2/schema_multi_cluster_app.go delete mode 100644 rancher2/structure_app.go delete mode 100644 rancher2/structure_app_test.go delete mode 100644 rancher2/structure_catalog.go delete mode 100644 rancher2/structure_catalog_test.go delete mode 100644 rancher2/structure_multi_cluster_app.go delete mode 100644 rancher2/structure_multi_cluster_app_test.go diff --git a/docs/data-sources/app.md b/docs/data-sources/app.md deleted file mode 100644 index 9264c64d..00000000 --- a/docs/data-sources/app.md +++ /dev/null @@ -1,37 +0,0 @@ ---- -page_title: "rancher2_app Data Source" ---- - -# rancher2\_app Data Source - -Use this data source to retrieve information about a Rancher v2 app. - -## Example Usage - -``` -data "rancher2_app" "rancher2" { - name = "foo" - project_id = "" - target_namespace = "" -} -``` - -## Argument Reference - -* `name` - (Required) The app name (string) -* `project_id` - (Required) The id of the project where the app is deployed (string) -* `target_namespace` - (Optional/Computed) The namespace name where the app is deployed (string) - -## Attributes Reference - -* `id` - (Computed) The ID of the resource (string) -* `catalog_name` - (Computed) Catalog name of the app (string) -* `answers` - (Computed) Answers for the app (map) -* `description` - (Computed) Description for the app (string) -* `external_id` - (Computed) The URL of the helm catalog app (string) -* `revision_id` - (Computed) Current revision id for the app (string) -* `template_name` - (Computed) Template name of the app (string) -* `template_version` - (Computed) Template version of the app (string) -* `values_yaml` - (Computed) values.yaml base64 encoded file content for the app (string) -* `annotations` - (Computed) Annotations for the catalog (map) -* `labels` - (Computed) Labels for the catalog (map) diff --git a/docs/data-sources/catalog.md b/docs/data-sources/catalog.md deleted file mode 100644 index a4b5b5e9..00000000 --- a/docs/data-sources/catalog.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -page_title: "rancher2_catalog Data Source" ---- - -# rancher2\_catalog Data Source - -Use this data source to retrieve information about a Rancher v2 catalog. - -## Example Usage - -``` -data "rancher2_catalog" "library" { - name = "catalog" -} -``` - -## Argument Reference - -* `name` - (Required) The catalog name. -* `scope` - (Optional) The scope of the catalog. `cluster`, `global`, and `project` are supported. Default `global` (string) - -## Attributes Reference - -* `id` - (Computed) The ID of the resource (string) -* `branch` - (Computed) The branch of the catalog repo to use (string) -* `cluster_id` - (Computed) The cluster id of the catalog (string) -* `description` - (Computed) A catalog description (string) -* `kind` - (Computed) The kind of the catalog. Just helm by the moment (string) -* `password` - (Computed/Sensitive) The password to access the catalog if needed (string) -* `project_id` - (Computed) The project id of the catalog (string) -* `username` - (Computed/Sensitive) The username to access the catalog if needed (string) -* `version` - (Computed) Helm version for the catalog (string) -* `url` - (Computed) The url of the catalog repo (string) -* `annotations` - (Computed) Annotations for the catalog (map) -* `labels` - (Computed) Labels for the catalog (map) - diff --git a/docs/data-sources/multi_cluster_app.md b/docs/data-sources/multi_cluster_app.md deleted file mode 100644 index 5f7e7812..00000000 --- a/docs/data-sources/multi_cluster_app.md +++ /dev/null @@ -1,36 +0,0 @@ ---- -page_title: "rancher2_multi_cluster_app Data Source" ---- - -# rancher2\_multi\_cluster\_app Data Source - -Use this data source to retrieve information about a Rancher v2 multi cluster app. - -## Example Usage - -``` -data "rancher2_multi_cluster_app" "foo" { - name = "foo" -} -``` - -## Argument Reference - -* `name` - (Required) The multi cluster app name (string) - -## Attributes Reference - -* `catalog_name` - (Computed) The multi cluster app catalog name (string) -* `id` - (Computed) The ID of the resource (string) -* `roles` - (Computed) The multi cluster app roles (list) -* `targets` - (Computed) The multi cluster app target projects (list) -* `template_name` - (Computed) The multi cluster app template name (string) -* `template_version` - (Computed) The multi cluster app template version (string) -* `template_version_id` - (Computed) The multi cluster app template version ID (string) -* `answers` - (Computed) The multi cluster app answers (list) -* `members` - (Computed) The multi cluster app members (list) -* `revision_history_limit` - (Computed) The multi cluster app revision history limit (int) -* `revision_id` - (Computed) Current revision id for the multi cluster app (string) -* `upgrade_strategy` - (Computed) The multi cluster app upgrade strategy (list) -* `annotations` - (Computed) Annotations for multi cluster app object (map) -* `labels` - (Computed) Labels for multi cluster app object (map) diff --git a/rancher2/0_provider_upgrade_test.go b/rancher2/0_provider_upgrade_test.go index c04d8e25..c34ea3fe 100644 --- a/rancher2/0_provider_upgrade_test.go +++ b/rancher2/0_provider_upgrade_test.go @@ -50,7 +50,6 @@ resource "rancher2_namespace" "testacc" { testAccCheckRancher2UpgradeVersion = []string{"v2.3.6", "v2.4.13", "v2.5.9", "v2.6.2"} testAccCheckRancher2RunningVersionIndex = 0 testAccCheckRancher2UpgradeCluster = os.Getenv("RANCHER_ACC_CLUSTER_NAME") - testAccCheckRancher2UpgradeCatalogV24 = testAccRancher2CatalogGlobal + testAccRancher2CatalogCluster + testAccRancher2CatalogProject testAccCheckRancher2UpgradeCertificateV24 = testAccRancher2Certificate + testAccRancher2CertificateNs testAccCheckRancher2BootstrapV23 = ` provider "rancher2" { @@ -99,7 +98,6 @@ provider "rancher2" { ` testAccCheckRancher2UpgradeConfigV23 = testAccCheckRancher2BootstrapV23 + ` -` + testAccRancher2App + ` ` + testAccCheckRancher2UpgradeCatalogV24 + ` ` + testAccCheckRancher2UpgradeCertificateV24 + ` ` + testAccRancher2CloudCredentialConfigAmazonec2 + ` @@ -114,7 +112,6 @@ provider "rancher2" { ` + testAccRancher2ClusterTemplateConfig + ` ` + testAccRancher2EtcdBackup + ` ` + testAccRancher2GlobalRoleBinding + ` -` + testAccRancher2MultiClusterApp + ` ` + testAccRancher2Namespace + ` ` + testAccRancher2NodeDriver + ` ` + testAccRancher2NodePool + ` @@ -136,7 +133,6 @@ provider "rancher2" { ` testAccCheckRancher2UpgradeConfigV24 = testAccCheckRancher2BootstrapV23 + ` -` + testAccRancher2App + ` ` + testAccCheckRancher2UpgradeCatalogV24 + ` ` + testAccCheckRancher2UpgradeCertificateV24 + ` ` + testAccRancher2CloudCredentialConfigAmazonec2 + ` @@ -151,7 +147,6 @@ provider "rancher2" { ` + testAccRancher2ClusterTemplateConfig + ` ` + testAccRancher2EtcdBackup + ` ` + testAccRancher2GlobalRoleBinding + ` -` + testAccRancher2MultiClusterApp + ` ` + testAccRancher2Namespace + ` ` + testAccRancher2NodeDriver + ` ` + testAccRancher2NodePool + ` @@ -173,7 +168,6 @@ provider "rancher2" { ` testAccCheckRancher2UpgradeConfigV25 = testAccCheckRancher2Bootstrap + ` -` + testAccRancher2App + ` ` + testAccRancher2AppV2 + ` ` + testAccCheckRancher2UpgradeCatalogV24 + ` ` + testAccRancher2CatalogV2 + ` @@ -192,7 +186,6 @@ provider "rancher2" { ` + testAccRancher2EtcdBackup + ` ` + testAccRancher2FeatureConfig + ` ` + testAccRancher2GlobalRoleBinding + ` -` + testAccRancher2MultiClusterApp + ` ` + testAccRancher2Namespace + ` ` + testAccRancher2NodeDriver + ` ` + testAccRancher2NodePool + ` @@ -216,7 +209,6 @@ provider "rancher2" { ` testAccCheckRancher2UpgradeConfigV26 = testAccCheckRancher2Bootstrap + ` -` + testAccRancher2App + ` ` + testAccRancher2AppV2 + ` ` + testAccCheckRancher2UpgradeCatalogV24 + ` ` + testAccRancher2CatalogV2 + ` @@ -237,7 +229,6 @@ provider "rancher2" { ` + testAccRancher2EtcdBackup + ` ` + testAccRancher2FeatureConfig + ` ` + testAccRancher2GlobalRoleBinding + ` -` + testAccRancher2MultiClusterApp + ` ` + testAccRancher2Namespace + ` ` + testAccRancher2NodeDriver + ` ` + testAccRancher2NodePool + ` @@ -283,10 +274,6 @@ func TestAccRancher2Upgrade(t *testing.T) { resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "password", testAccRancher2DefaultAdminPass), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "telemetry", "true"), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "current_password", testAccRancher2DefaultAdminPass), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), resource.TestCheckResourceAttr("rancher2_cluster.foo", "name", "foo"), testAccRancher2UpgradeRancher(), ), @@ -298,11 +285,6 @@ func TestAccRancher2Upgrade(t *testing.T) { resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "password", testAccRancher2DefaultAdminPass), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "telemetry", "true"), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "current_password", testAccRancher2DefaultAdminPass), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "version", "helm_v3"), resource.TestCheckResourceAttr("rancher2_cluster.foo", "name", "foo"), testAccRancher2UpgradeRancher(), ), @@ -314,11 +296,6 @@ func TestAccRancher2Upgrade(t *testing.T) { resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "password", testAccRancher2DefaultAdminPass), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "telemetry", "true"), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "current_password", testAccRancher2DefaultAdminPass), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "version", "helm_v3"), resource.TestCheckResourceAttr("rancher2_cluster.foo", "name", "foo"), testAccRancher2UpgradeRancher(), ), @@ -330,11 +307,6 @@ func TestAccRancher2Upgrade(t *testing.T) { resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "password", testAccRancher2DefaultAdminPass), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "telemetry", "true"), resource.TestCheckResourceAttr(testAccRancher2BootstrapType+".foo", "current_password", testAccRancher2DefaultAdminPass), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "version", "helm_v3"), resource.TestCheckResourceAttr("rancher2_cluster.foo", "name", "foo"), ), }, diff --git a/rancher2/config.go b/rancher2/config.go index cf815757..b93d6d2a 100644 --- a/rancher2/config.go +++ b/rancher2/config.go @@ -1281,141 +1281,6 @@ func (c *Config) GetSettingValue(name string) (string, error) { return setting.Value, nil } -func (c *Config) GetCatalogByName(name, scope string) (interface{}, error) { - if len(name) == 0 || len(scope) == 0 { - return nil, fmt.Errorf("[ERROR] Name nor scope can't be nil") - } - - client, err := c.ManagementClient() - if err != nil { - return nil, err - } - - filters := map[string]interface{}{"name": name} - listOpts := NewListOpts(filters) - - switch scope { - case catalogScopeCluster: - return client.ClusterCatalog.List(listOpts) - case catalogScopeGlobal: - return client.Catalog.List(listOpts) - case catalogScopeProject: - return client.ProjectCatalog.List(listOpts) - default: - return nil, fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - -func (c *Config) GetCatalog(id, scope string) (interface{}, error) { - if len(id) == 0 || len(scope) == 0 { - return nil, fmt.Errorf("[ERROR] Id nor scope can't be nil") - } - - client, err := c.ManagementClient() - if err != nil { - return nil, err - } - - switch scope { - case catalogScopeCluster: - return client.ClusterCatalog.ByID(id) - case catalogScopeGlobal: - return client.Catalog.ByID(id) - case catalogScopeProject: - return client.ProjectCatalog.ByID(id) - default: - return nil, fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - -func (c *Config) CreateCatalog(scope string, catalog interface{}) (interface{}, error) { - if catalog == nil || len(scope) == 0 { - return nil, fmt.Errorf("[ERROR] Catalog nor scope can't be nil") - } - - client, err := c.ManagementClient() - if err != nil { - return nil, err - } - - switch scope { - case catalogScopeCluster: - return client.ClusterCatalog.Create(catalog.(*managementClient.ClusterCatalog)) - case catalogScopeGlobal: - return client.Catalog.Create(catalog.(*managementClient.Catalog)) - case catalogScopeProject: - return client.ProjectCatalog.Create(catalog.(*managementClient.ProjectCatalog)) - default: - return nil, fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - -func (c *Config) UpdateCatalog(scope string, catalog interface{}, update map[string]interface{}) (interface{}, error) { - if catalog == nil || len(scope) == 0 { - return nil, fmt.Errorf("[ERROR] Catalog nor scope can't be nil") - } - - client, err := c.ManagementClient() - if err != nil { - return nil, err - } - - switch scope { - case catalogScopeCluster: - return client.ClusterCatalog.Update(catalog.(*managementClient.ClusterCatalog), update) - case catalogScopeGlobal: - return client.Catalog.Update(catalog.(*managementClient.Catalog), update) - case catalogScopeProject: - return client.ProjectCatalog.Update(catalog.(*managementClient.ProjectCatalog), update) - default: - return nil, fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - -func (c *Config) DeleteCatalog(scope string, catalog interface{}) error { - if catalog == nil || len(scope) == 0 { - return fmt.Errorf("[ERROR] Catalog nor scope can't be nil") - } - - client, err := c.ManagementClient() - if err != nil { - return err - } - - switch scope { - case catalogScopeCluster: - return client.ClusterCatalog.Delete(catalog.(*managementClient.ClusterCatalog)) - case catalogScopeGlobal: - return client.Catalog.Delete(catalog.(*managementClient.Catalog)) - case catalogScopeProject: - return client.ProjectCatalog.Delete(catalog.(*managementClient.ProjectCatalog)) - default: - return fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - -func (c *Config) RefreshCatalog(scope string, catalog interface{}) (*managementClient.CatalogRefresh, error) { - if catalog == nil || len(scope) == 0 { - return nil, fmt.Errorf("[ERROR] Catalog nor scope can't be nil") - } - - client, err := c.ManagementClient() - if err != nil { - return nil, err - } - - switch scope { - case catalogScopeCluster: - return client.ClusterCatalog.ActionRefresh(catalog.(*managementClient.ClusterCatalog)) - case catalogScopeGlobal: - return client.Catalog.ActionRefresh(catalog.(*managementClient.Catalog)) - case catalogScopeProject: - return client.ProjectCatalog.ActionRefresh(catalog.(*managementClient.ProjectCatalog)) - default: - return nil, fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - func getAuthConfigObject(kind string) (interface{}, error) { switch kind { case managementClient.ActiveDirectoryConfigType: diff --git a/rancher2/data_source_rancher2_app.go b/rancher2/data_source_rancher2_app.go deleted file mode 100644 index c85af9b4..00000000 --- a/rancher2/data_source_rancher2_app.go +++ /dev/null @@ -1,119 +0,0 @@ -package rancher2 - -import ( - "fmt" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" -) - -func dataSourceRancher2App() *schema.Resource { - return &schema.Resource{ - Read: dataSourceRancher2AppRead, - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - Description: "Name of the app", - }, - "project_id": { - Type: schema.TypeString, - Required: true, - Description: "Project ID to add app", - }, - "target_namespace": { - Type: schema.TypeString, - Optional: true, - Computed: true, - Description: "Namespace name to add app", - }, - "answers": { - Type: schema.TypeMap, - Computed: true, - Description: "Answers of the app", - }, - "catalog_name": { - Type: schema.TypeString, - Computed: true, - Description: "Catalog name of the app", - }, - "description": { - Type: schema.TypeString, - Computed: true, - }, - "external_id": { - Type: schema.TypeString, - Computed: true, - Description: "External ID of the app", - }, - "revision_id": { - Type: schema.TypeString, - Computed: true, - Description: "App revision id", - }, - "template_name": { - Type: schema.TypeString, - Computed: true, - Description: "Template name of the app", - }, - "template_version": { - Type: schema.TypeString, - Computed: true, - Description: "Template version of the app", - }, - "values_yaml": { - Type: schema.TypeString, - Computed: true, - Description: "values.yaml file content of the app", - }, - "annotations": { - Type: schema.TypeMap, - Optional: true, - Computed: true, - Description: "Annotations of the app", - }, - "labels": { - Type: schema.TypeMap, - Computed: true, - Description: "Labels of the app", - }, - }, - } -} - -func dataSourceRancher2AppRead(d *schema.ResourceData, meta interface{}) error { - projectID := d.Get("project_id").(string) - name := d.Get("name").(string) - targetNamespace := d.Get("target_namespace").(string) - - filters := map[string]interface{}{ - "projectId": projectID, - "name": name, - } - - if len(targetNamespace) > 0 { - filters["targetNamespace"] = targetNamespace - } - - listOpts := NewListOpts(filters) - - client, err := meta.(*Config).ProjectClient(projectID) - if err != nil { - return err - } - - apps, err := client.App.List(listOpts) - if err != nil { - return err - } - - count := len(apps.Data) - if count <= 0 { - return fmt.Errorf("[ERROR] app with name \"%s\" on project ID \"%s\" not found", name, projectID) - } - if count > 1 { - return fmt.Errorf("[ERROR] found %d app with name \"%s\" on project ID \"%s\"", count, name, projectID) - } - - return flattenApp(d, &apps.Data[0]) -} diff --git a/rancher2/data_source_rancher2_app_test.go b/rancher2/data_source_rancher2_app_test.go deleted file mode 100644 index 5fa38bd6..00000000 --- a/rancher2/data_source_rancher2_app_test.go +++ /dev/null @@ -1,42 +0,0 @@ -package rancher2 - -import ( - "fmt" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" -) - -func TestAccRancher2AppDataSource(t *testing.T) { - testAccCheckRancher2AppDataSourceConfig := testAccRancher2AppConfig + ` -data "` + testAccRancher2AppType + `" "foo" { - name = rancher2_app.foo.name - project_id = rancher2_app.foo.project_id -} -` - name := "data." + testAccRancher2AppType + ".foo" - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckRancher2AppDataSourceConfig, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(name, "name", "foo"), - resource.TestCheckResourceAttr(name, "description", "Terraform app acceptance test"), - resource.TestCheckResourceAttr(name, "target_namespace", "testacc"), - resource.TestCheckResourceAttr(name, "external_id", "catalog://?catalog=library&template=docker-registry&version=1.8.1"), - resource.TestCheckResourceAttr(name, "answers.ingress_host", "test.xip.io"), - ), - }, - }, - }) -} - -func testAccRancher2CheckClusterID() resource.TestCheckFunc { - return func(s *terraform.State) error { - return fmt.Errorf("testAccRancher2ClusterID %s", testAccRancher2ClusterID) - - } -} diff --git a/rancher2/data_source_rancher2_catalog.go b/rancher2/data_source_rancher2_catalog.go deleted file mode 100644 index 5e436843..00000000 --- a/rancher2/data_source_rancher2_catalog.go +++ /dev/null @@ -1,118 +0,0 @@ -package rancher2 - -import ( - "fmt" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" -) - -func dataSourceRancher2Catalog() *schema.Resource { - return &schema.Resource{ - Read: dataSourceRancher2CatalogRead, - - Schema: map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - }, - "scope": { - Type: schema.TypeString, - Optional: true, - Default: catalogScopeGlobal, - ValidateFunc: validation.StringInSlice(catalogScopes, true), - }, - "url": { - Type: schema.TypeString, - Computed: true, - }, - "cluster_id": { - Type: schema.TypeString, - Computed: true, - }, - "description": { - Type: schema.TypeString, - Computed: true, - }, - "kind": { - Type: schema.TypeString, - Computed: true, - }, - "branch": { - Type: schema.TypeString, - Computed: true, - }, - "password": { - Type: schema.TypeString, - Computed: true, - Sensitive: true, - }, - "project_id": { - Type: schema.TypeString, - Computed: true, - }, - "username": { - Type: schema.TypeString, - Computed: true, - Sensitive: true, - }, - "version": { - Type: schema.TypeString, - Computed: true, - }, - "annotations": { - Type: schema.TypeMap, - Computed: true, - }, - "labels": { - Type: schema.TypeMap, - Computed: true, - }, - }, - } -} - -func dataSourceRancher2CatalogRead(d *schema.ResourceData, meta interface{}) error { - name := d.Get("name").(string) - scope := d.Get("scope").(string) - - catalogs, err := meta.(*Config).GetCatalogByName(name, scope) - if err != nil { - return err - } - - switch scope { - case catalogScopeCluster: - err = dataSourceRancher2CatalogCheck(len(catalogs.(*managementClient.ClusterCatalogCollection).Data), scope, name) - if err != nil { - return err - } - return flattenCatalog(d, &catalogs.(*managementClient.ClusterCatalogCollection).Data[0]) - case catalogScopeGlobal: - err = dataSourceRancher2CatalogCheck(len(catalogs.(*managementClient.CatalogCollection).Data), scope, name) - if err != nil { - return err - } - return flattenCatalog(d, &catalogs.(*managementClient.CatalogCollection).Data[0]) - case catalogScopeProject: - err = dataSourceRancher2CatalogCheck(len(catalogs.(*managementClient.ProjectCatalogCollection).Data), scope, name) - if err != nil { - return err - } - return flattenCatalog(d, &catalogs.(*managementClient.ProjectCatalogCollection).Data[0]) - default: - return fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } - -} - -func dataSourceRancher2CatalogCheck(i int, scope, name string) error { - if i <= 0 { - return fmt.Errorf("[ERROR] %s catalog with name \"%s\" not found", scope, name) - } - if i > 1 { - return fmt.Errorf("[ERROR] found %d %s catalogs with name \"%s\"", i, scope, name) - } - return nil -} diff --git a/rancher2/data_source_rancher2_catalog_test.go b/rancher2/data_source_rancher2_catalog_test.go deleted file mode 100644 index 9cb98486..00000000 --- a/rancher2/data_source_rancher2_catalog_test.go +++ /dev/null @@ -1,80 +0,0 @@ -package rancher2 - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" -) - -func TestAccRancher2CatalogDataSource_Cluster(t *testing.T) { - testAccCheckRancher2CatalogClusterDataSourceConfig := testAccCheckRancher2ClusterSyncTestacc + testAccRancher2CatalogCluster + ` -data "` + testAccRancher2CatalogType + `" "library" { - name = rancher2_catalog.foo-cluster.name - scope = rancher2_catalog.foo-cluster.scope -} -` - name := "data." + testAccRancher2CatalogType + ".library" - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckRancher2CatalogClusterDataSourceConfig, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(name, "name", "foo-cluster"), - resource.TestCheckResourceAttr(name, "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(name, "scope", "cluster"), - resource.TestCheckResourceAttr(name, "version", "helm_v2"), - ), - }, - }, - }) -} - -func TestAccRancher2CatalogDataSource_Global(t *testing.T) { - testAccCheckRancher2CatalogGlobalDataSourceConfig := testAccRancher2CatalogGlobal + ` -data "` + testAccRancher2CatalogType + `" "library" { - name = "library" -} -` - name := "data." + testAccRancher2CatalogType + ".library" - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckRancher2CatalogGlobalDataSourceConfig, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(name, "name", "library"), - resource.TestCheckResourceAttr(name, "url", "https://git.rancher.io/charts"), - resource.TestCheckResourceAttr(name, "scope", "global"), - ), - }, - }, - }) -} - -func TestAccRancher2CatalogDataSource_Project(t *testing.T) { - testAccCheckRancher2CatalogProjectDataSourceConfig := testAccCheckRancher2ClusterSyncTestacc + testAccRancher2CatalogProject + ` -data "` + testAccRancher2CatalogType + `" "library" { - name = rancher2_catalog.foo-project.name - scope = rancher2_catalog.foo-project.scope -} -` - name := "data." + testAccRancher2CatalogType + ".library" - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckRancher2CatalogProjectDataSourceConfig, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(name, "name", "foo-project"), - resource.TestCheckResourceAttr(name, "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(name, "scope", "project"), - resource.TestCheckResourceAttr(name, "version", "helm_v2"), - ), - }, - }, - }) -} diff --git a/rancher2/data_source_rancher2_multi_cluster_app.go b/rancher2/data_source_rancher2_multi_cluster_app.go deleted file mode 100644 index be58d961..00000000 --- a/rancher2/data_source_rancher2_multi_cluster_app.go +++ /dev/null @@ -1,134 +0,0 @@ -package rancher2 - -import ( - "fmt" - - "github.com/hashicorp/terraform-plugin-sdk/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": { - 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": { - 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": { - 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": { - 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": { - Type: schema.TypeMap, - Computed: true, - }, - "labels": { - 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) - } - - templateVersion, err := client.TemplateVersion.ByID(multiClusterApps.Data[0].TemplateVersionID) - if err != nil { - return err - } - - return flattenMultiClusterApp(d, &multiClusterApps.Data[0], templateVersion.ExternalID) -} diff --git a/rancher2/data_source_rancher2_multi_cluster_app_test.go b/rancher2/data_source_rancher2_multi_cluster_app_test.go deleted file mode 100644 index 0025fd30..00000000 --- a/rancher2/data_source_rancher2_multi_cluster_app_test.go +++ /dev/null @@ -1,31 +0,0 @@ -package rancher2 - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" -) - -func TestAccRancher2MultiClusterAppDataSource(t *testing.T) { - testAccCheckRancher2MultiClusterAppDataSourceConfig := testAccRancher2MultiClusterAppConfig + ` -data "` + testAccRancher2MultiClusterAppType + `" "foo" { - name = rancher2_multi_cluster_app.foo.name -} -` - name := "data." + testAccRancher2MultiClusterAppType + ".foo" - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - Steps: []resource.TestStep{ - { - Config: testAccCheckRancher2MultiClusterAppDataSourceConfig, - Check: resource.ComposeTestCheckFunc( - resource.TestCheckResourceAttr(name, "name", "foo"), - resource.TestCheckResourceAttr(name, "template_version_id", "cattle-global-data:library-docker-registry-1.8.1"), - resource.TestCheckResourceAttr(name, "answers.0.values.ingress_host", "test.xip.io"), - resource.TestCheckResourceAttr(name, "roles.0", "project-member"), - ), - }, - }, - }) -} diff --git a/rancher2/import_rancher2_app.go b/rancher2/import_rancher2_app.go deleted file mode 100644 index 74a2e6f0..00000000 --- a/rancher2/import_rancher2_app.go +++ /dev/null @@ -1,22 +0,0 @@ -package rancher2 - -import ( - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" -) - -func resourceRancher2AppImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - projectID, appID, err := splitAppID(d.Id()) - if err != nil { - return []*schema.ResourceData{}, err - } - - d.SetId(appID) - d.Set("project_id", projectID) - - err = resourceRancher2AppRead(d, meta) - if err != nil { - return []*schema.ResourceData{}, err - } - - return []*schema.ResourceData{d}, nil -} diff --git a/rancher2/import_rancher2_catalog.go b/rancher2/import_rancher2_catalog.go deleted file mode 100644 index 48a91073..00000000 --- a/rancher2/import_rancher2_catalog.go +++ /dev/null @@ -1,26 +0,0 @@ -package rancher2 - -import ( - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" -) - -func resourceRancher2CatalogImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { - scope, id := splitID(d.Id()) - if len(scope) == 0 { - scope = catalogScopeGlobal - } - - catalog, err := meta.(*Config).GetCatalog(id, scope) - if err != nil { - return []*schema.ResourceData{}, err - } - - d.Set("scope", scope) - - err = flattenCatalog(d, catalog) - if err != nil { - return []*schema.ResourceData{}, err - } - - return []*schema.ResourceData{d}, nil -} diff --git a/rancher2/import_rancher2_multi_cluster_app.go b/rancher2/import_rancher2_multi_cluster_app.go deleted file mode 100644 index ad7c8937..00000000 --- a/rancher2/import_rancher2_multi_cluster_app.go +++ /dev/null @@ -1,18 +0,0 @@ -package rancher2 - -import ( - "github.com/hashicorp/terraform-plugin-sdk/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 -} diff --git a/rancher2/provider.go b/rancher2/provider.go index 4d06711b..1cbee1d7 100644 --- a/rancher2/provider.go +++ b/rancher2/provider.go @@ -107,7 +107,6 @@ func Provider() terraform.ResourceProvider { }, ResourcesMap: map[string]*schema.Resource{ - "rancher2_app": resourceRancher2App(), "rancher2_app_v2": resourceRancher2AppV2(), "rancher2_auth_config_activedirectory": resourceRancher2AuthConfigActiveDirectory(), "rancher2_auth_config_adfs": resourceRancher2AuthConfigADFS(), @@ -119,7 +118,6 @@ func Provider() terraform.ResourceProvider { "rancher2_auth_config_openldap": resourceRancher2AuthConfigOpenLdap(), "rancher2_auth_config_ping": resourceRancher2AuthConfigPing(), "rancher2_bootstrap": resourceRancher2Bootstrap(), - "rancher2_catalog": resourceRancher2Catalog(), "rancher2_catalog_v2": resourceRancher2CatalogV2(), "rancher2_certificate": resourceRancher2Certificate(), "rancher2_cloud_credential": resourceRancher2CloudCredential(), @@ -136,7 +134,6 @@ func Provider() terraform.ResourceProvider { "rancher2_global_role": resourceRancher2GlobalRole(), "rancher2_global_role_binding": resourceRancher2GlobalRoleBinding(), "rancher2_machine_config_v2": resourceRancher2MachineConfigV2(), - "rancher2_multi_cluster_app": resourceRancher2MultiClusterApp(), "rancher2_namespace": resourceRancher2Namespace(), "rancher2_node_driver": resourceRancher2NodeDriver(), "rancher2_node_pool": resourceRancher2NodePool(), @@ -155,8 +152,6 @@ func Provider() terraform.ResourceProvider { }, DataSourcesMap: map[string]*schema.Resource{ - "rancher2_app": dataSourceRancher2App(), - "rancher2_catalog": dataSourceRancher2Catalog(), "rancher2_catalog_v2": dataSourceRancher2CatalogV2(), "rancher2_certificate": dataSourceRancher2Certificate(), "rancher2_cloud_credential": dataSourceRancher2CloudCredential(), @@ -169,7 +164,6 @@ func Provider() terraform.ResourceProvider { "rancher2_etcd_backup": dataSourceRancher2EtcdBackup(), "rancher2_global_role": dataSourceRancher2GlobalRole(), "rancher2_global_role_binding": dataSourceRancher2GlobalRoleBinding(), - "rancher2_multi_cluster_app": dataSourceRancher2MultiClusterApp(), "rancher2_namespace": dataSourceRancher2Namespace(), "rancher2_node_driver": dataSourceRancher2NodeDriver(), "rancher2_node_pool": dataSourceRancher2NodePool(), diff --git a/rancher2/resource_rancher2_app.go b/rancher2/resource_rancher2_app.go deleted file mode 100644 index f841192e..00000000 --- a/rancher2/resource_rancher2_app.go +++ /dev/null @@ -1,328 +0,0 @@ -package rancher2 - -import ( - "fmt" - "log" - "strings" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - projectClient "github.com/rancher/rancher/pkg/client/generated/project/v3" -) - -func resourceRancher2App() *schema.Resource { - return &schema.Resource{ - Create: resourceRancher2AppCreate, - Read: resourceRancher2AppRead, - Update: resourceRancher2AppUpdate, - Delete: resourceRancher2AppDelete, - Importer: &schema.ResourceImporter{ - State: resourceRancher2AppImport, - }, - - Schema: appFields(), - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(10 * time.Minute), - Update: schema.DefaultTimeout(10 * time.Minute), - Delete: schema.DefaultTimeout(10 * time.Minute), - }, - } -} - -func resourceRancher2AppCreate(d *schema.ResourceData, meta interface{}) error { - projectID := d.Get("project_id").(string) - name := d.Get("name").(string) - - err := meta.(*Config).ProjectExist(projectID) - if err != nil { - return err - } - - err = resourceRancher2AppGetVersion(d, meta) - if err != nil { - return err - } - - app, err := expandApp(d) - if err != nil { - return err - } - - log.Printf("[INFO] Creating App %s on Project ID %s", name, projectID) - - client, err := meta.(*Config).ProjectClient(projectID) - if err != nil { - return err - } - - newApp, err := client.App.Create(app) - if err != nil { - return err - } - - if d.Get("wait").(bool) { - stateConf := &resource.StateChangeConf{ - Pending: []string{}, - Target: []string{"no"}, - Refresh: appTransitionRefreshFunc(client, newApp.ID), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - client.App.Delete(newApp) - return fmt.Errorf("[ERROR] waiting for app (%s) to finish transitioning: %s", newApp.ID, waitErr) - } - stateConf = &resource.StateChangeConf{ - Pending: []string{}, - Target: []string{"active"}, - Refresh: appStateRefreshFunc(client, newApp.ID), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr = stateConf.WaitForState() - if waitErr != nil { - client.App.Delete(newApp) - return fmt.Errorf("[ERROR] waiting for app (%s) to be active: %s", newApp.ID, waitErr) - } - } - d.SetId(newApp.ID) - - return resourceRancher2AppRead(d, meta) -} - -func resourceRancher2AppRead(d *schema.ResourceData, meta interface{}) error { - projectID := d.Get("project_id").(string) - id := d.Id() - - log.Printf("[INFO] Refreshing App ID %s", id) - - err := meta.(*Config).ProjectExist(projectID) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] Project ID %s not found.", projectID) - d.SetId("") - return nil - } - return err - } - - client, err := meta.(*Config).ProjectClient(projectID) - if err != nil { - return err - } - - app, err := client.App.ByID(id) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] App ID %s not found.", id) - d.SetId("") - return nil - } - return err - } - - return flattenApp(d, app) -} - -func resourceRancher2AppUpdate(d *schema.ResourceData, meta interface{}) error { - projectID := d.Get("project_id").(string) - id := d.Id() - - client, err := meta.(*Config).ProjectClient(projectID) - if err != nil { - return err - } - - app, err := client.App.ByID(id) - if err != nil { - return err - } - - if d.HasChange("description") || d.HasChange("annotations") || d.HasChange("labels") { - log.Printf("[INFO] Updating App ID %s", id) - - app.Description = d.Get("description").(string) - app.Annotations = toMapString(d.Get("annotations").(map[string]interface{})) - app.Labels = toMapString(d.Get("labels").(map[string]interface{})) - _, err := client.App.Replace(app) - if err != nil { - return err - } - } - - // Rollback or upgrade app - if d.HasChange("revision_id") { - revID := d.Get("revision_id").(string) - log.Printf("[INFO] Rollbacking App ID %s to %s", id, revID) - - rollback := &projectClient.RollbackRevision{ - ForceUpgrade: d.Get("force_upgrade").(bool), - RevisionID: revID, - } - err = client.App.ActionRollback(app, rollback) - if err != nil { - return err - } - } else if d.HasChange("answers") || d.HasChange("catalog_name") || d.HasChange("template_name") || d.HasChange("template_version") || d.HasChange("values_yaml") { - log.Printf("[INFO] Upgrading App ID %s", id) - - values, err := Base64Decode(d.Get("values_yaml").(string)) - if err != nil { - return err - } - - upgrade := &projectClient.AppUpgradeConfig{ - Answers: toMapString(d.Get("answers").(map[string]interface{})), - ExternalID: expandAppExternalID(d), - ForceUpgrade: d.Get("force_upgrade").(bool), - ValuesYaml: values, - } - - err = client.App.ActionUpgrade(app, upgrade) - if err != nil { - return err - } - } - - if d.Get("wait").(bool) { - stateConf := &resource.StateChangeConf{ - Pending: []string{"yes"}, - Target: []string{"no"}, - Refresh: appTransitionRefreshFunc(client, id), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf("[ERROR] waiting for app (%s) to finish transitioning: %s", id, waitErr) - } - stateConf = &resource.StateChangeConf{ - Pending: []string{}, - Target: []string{"active"}, - Refresh: appStateRefreshFunc(client, id), - Timeout: d.Timeout(schema.TimeoutUpdate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr = stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for app (%s) to be updated: %s", id, waitErr) - } - } - - return resourceRancher2AppRead(d, meta) -} - -func resourceRancher2AppDelete(d *schema.ResourceData, meta interface{}) error { - projectID := d.Get("project_id").(string) - id := d.Id() - - log.Printf("[INFO] Deleting App ID %s", id) - - client, err := meta.(*Config).ProjectClient(projectID) - if err != nil { - return err - } - - app, err := client.App.ByID(id) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] App ID %s not found.", d.Id()) - d.SetId("") - return nil - } - return err - } - - err = client.App.Delete(app) - if err != nil { - return fmt.Errorf("[ERROR] removing App: %s", err) - } - - stateConf := &resource.StateChangeConf{ - Pending: []string{"removing"}, - Target: []string{"removed"}, - Refresh: appStateRefreshFunc(client, id), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for App (%s) to be removed: %s", id, waitErr) - } - - d.SetId("") - return nil -} - -func resourceRancher2AppGetVersion(d *schema.ResourceData, meta interface{}) error { - catalogName := d.Get("catalog_name").(string) - appName := d.Get("template_name").(string) - appVersion := d.Get("template_version").(string) - - if len(appVersion) > 0 { - return nil - } - - if !strings.Contains(catalogName, ":") { - catalogName = "cattle-global-data:" + catalogName - } - - appID := catalogName + "-" + appName - - client, err := meta.(*Config).ManagementClient() - if err != nil { - return err - } - - template, err := client.Template.ByID(appID) - if err != nil { - return err - } - - appVersion, err = getLatestVersion(template.VersionLinks) - if err != nil { - return err - } - - d.Set("template_version", appVersion) - - return nil -} - -// appStateRefreshFunc returns a resource.StateRefreshFunc, used to watch a Rancher App. -func appStateRefreshFunc(client *projectClient.Client, appID string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - obj, err := client.App.ByID(appID) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - return obj, "removed", nil - } - return nil, "", err - } - return obj, obj.State, nil - } -} - -// appTransitionRefreshFunc returns a resource.StateRefreshFunc, used to watch a Rancher App. -func appTransitionRefreshFunc(client *projectClient.Client, appID string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - obj, err := client.App.ByID(appID) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - return obj, "no", nil - } - return nil, "", err - } - return obj, obj.Transitioning, nil - } -} diff --git a/rancher2/resource_rancher2_app_test.go b/rancher2/resource_rancher2_app_test.go deleted file mode 100644 index d85405bc..00000000 --- a/rancher2/resource_rancher2_app_test.go +++ /dev/null @@ -1,234 +0,0 @@ -package rancher2 - -import ( - "fmt" - "testing" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" - projectClient "github.com/rancher/rancher/pkg/client/generated/project/v3" -) - -const ( - testAccRancher2AppType = "rancher2_app" -) - -var ( - testAccRancher2App string - testAccRancher2AppUpdate string - testAccRancher2AppConfig string - testAccRancher2AppUpdateConfig string -) - -func init() { - testAccRancher2App = ` -resource "rancher2_app" "foo" { - catalog_name = "library" - name = "foo" - description = "Terraform app acceptance test" - project_id = rancher2_cluster_sync.testacc.default_project_id - template_name = "docker-registry" - template_version = "1.8.1" - target_namespace = rancher2_namespace.testacc.name - answers = { - "ingress_host" = "test.xip.io" - } - annotations = { - "testacc.terraform.io/test" = "true" - } - labels = { - "testacc.terraform.io/test" = "true" - } -} -` - testAccRancher2AppUpdate = ` -resource "rancher2_app" "foo" { - catalog_name = "library" - name = "foo" - description = "Terraform app acceptance test - updated" - project_id = rancher2_cluster_sync.testacc.default_project_id - template_name = "docker-registry" - template_version = "1.8.1" - target_namespace = rancher2_namespace.testacc.name - answers = { - "ingress_host" = "test2.xip.io" - } - annotations = { - "testacc.terraform.io/test" = "false" - } - labels = { - "testacc.terraform.io/test" = "false" - } -} -` - testAccRancher2AppConfig = testAccCheckRancher2ClusterSyncTestacc + testAccCheckRancher2NamespaceTestacc + testAccRancher2App - testAccRancher2AppUpdateConfig = testAccCheckRancher2ClusterSyncTestacc + testAccCheckRancher2NamespaceTestacc + testAccRancher2AppUpdate -} - -func TestAccRancher2App_basic(t *testing.T) { - var app *projectClient.App - - resource.Test(t, resource.TestCase{ - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2AppDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2AppConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2AppExists(testAccRancher2AppType+".foo", app), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "name", "foo"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "description", "Terraform app acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "target_namespace", "testacc"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "external_id", "catalog://?catalog=library&template=docker-registry&version=1.8.1"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "answers.ingress_host", "test.xip.io"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "annotations.testacc.terraform.io/test", "true"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "labels.testacc.terraform.io/test", "true"), - ), - }, - { - Config: testAccRancher2AppUpdateConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2AppExists(testAccRancher2AppType+".foo", app), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "name", "foo"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "description", "Terraform app acceptance test - updated"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "target_namespace", "testacc"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "external_id", "catalog://?catalog=library&template=docker-registry&version=1.8.1"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "answers.ingress_host", "test2.xip.io"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "annotations.testacc.terraform.io/test", "false"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "labels.testacc.terraform.io/test", "false"), - ), - }, - { - Config: testAccRancher2AppConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2AppExists(testAccRancher2AppType+".foo", app), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "name", "foo"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "description", "Terraform app acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "target_namespace", "testacc"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "external_id", "catalog://?catalog=library&template=docker-registry&version=1.8.1"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "answers.ingress_host", "test.xip.io"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "annotations.testacc.terraform.io/test", "true"), - resource.TestCheckResourceAttr(testAccRancher2AppType+".foo", "labels.testacc.terraform.io/test", "true"), - ), - }, - }, - }) -} - -func TestAccRancher2App_disappears(t *testing.T) { - var app *projectClient.App - - resource.Test(t, resource.TestCase{ - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2AppDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2AppConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2AppExists(testAccRancher2AppType+".foo", app), - testAccRancher2AppDisappears(app), - ), - ExpectNonEmptyPlan: true, - }, - }, - }) -} - -func testAccRancher2AppDisappears(app *projectClient.App) resource.TestCheckFunc { - return func(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != testAccRancher2AppType { - continue - } - - client, err := testAccProvider.Meta().(*Config).ProjectClient(rs.Primary.Attributes["project_id"]) - if err != nil { - return err - } - - app, err := client.App.ByID(rs.Primary.ID) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - - err = client.App.Delete(app) - if err != nil { - return fmt.Errorf("Error removing App: %s", err) - } - - stateConf := &resource.StateChangeConf{ - Pending: []string{"removing"}, - Target: []string{"removed"}, - Refresh: appStateRefreshFunc(client, rs.Primary.ID), - Timeout: 10 * time.Minute, - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for App (%s) to be removed: %s", app.ID, waitErr) - } - } - return nil - } -} - -func testAccCheckRancher2AppExists(n string, app *projectClient.App) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No App ID is set") - } - - client, err := testAccProvider.Meta().(*Config).ProjectClient(rs.Primary.Attributes["project_id"]) - if err != nil { - return err - } - - foundApp, err := client.App.ByID(rs.Primary.ID) - if err != nil { - if IsNotFound(err) { - return fmt.Errorf("App not found") - } - return err - } - - app = foundApp - - return nil - } -} - -func testAccCheckRancher2AppDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != testAccRancher2AppType { - continue - } - - client, err := testAccProvider.Meta().(*Config).ProjectClient(rs.Primary.Attributes["project_id"]) - if err != nil { - return err - } - - _, err = client.App.ByID(rs.Primary.ID) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - return fmt.Errorf("App still exists") - } - return nil -} diff --git a/rancher2/resource_rancher2_catalog.go b/rancher2/resource_rancher2_catalog.go deleted file mode 100644 index f7da0433..00000000 --- a/rancher2/resource_rancher2_catalog.go +++ /dev/null @@ -1,217 +0,0 @@ -package rancher2 - -import ( - "fmt" - "log" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" -) - -func resourceRancher2Catalog() *schema.Resource { - return &schema.Resource{ - Create: resourceRancher2CatalogCreate, - Read: resourceRancher2CatalogRead, - Update: resourceRancher2CatalogUpdate, - Delete: resourceRancher2CatalogDelete, - Importer: &schema.ResourceImporter{ - State: resourceRancher2CatalogImport, - }, - - Schema: catalogFields(), - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(10 * time.Minute), - Update: schema.DefaultTimeout(10 * time.Minute), - Delete: schema.DefaultTimeout(10 * time.Minute), - }, - } -} - -func resourceRancher2CatalogCreate(d *schema.ResourceData, meta interface{}) error { - scope := d.Get("scope").(string) - name := d.Get("name").(string) - catalog := expandCatalog(d) - - log.Printf("[INFO] Creating %s Catalog %s", scope, name) - - newCatalog, err := meta.(*Config).CreateCatalog(scope, catalog) - if err != nil { - return err - } - - id := "" - switch scope { - case catalogScopeCluster: - id = newCatalog.(*managementClient.ClusterCatalog).ID - case catalogScopeGlobal: - id = newCatalog.(*managementClient.Catalog).ID - case catalogScopeProject: - id = newCatalog.(*managementClient.ProjectCatalog).ID - } - - d.SetId(id) - - stateConf := &resource.StateChangeConf{ - Pending: []string{"refreshed"}, - Target: []string{"active"}, - Refresh: catalogStateRefreshFunc(meta, id, scope), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf("[ERROR] waiting for catalog (%s) to be created: %s", id, waitErr) - } - - return resourceRancher2CatalogRead(d, meta) -} - -func resourceRancher2CatalogRead(d *schema.ResourceData, meta interface{}) error { - scope := d.Get("scope").(string) - id := d.Id() - log.Printf("[INFO] Refreshing %s Catalog ID %s", scope, id) - - catalog, err := meta.(*Config).GetCatalog(id, scope) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] %s Catalog ID %s not found.", scope, id) - d.SetId("") - return nil - } - return err - } - - if d.Get("refresh").(bool) { - _, err := meta.(*Config).RefreshCatalog(scope, catalog) - if err != nil { - return err - } - stateConf := &resource.StateChangeConf{ - Pending: []string{"refreshed"}, - Target: []string{"active"}, - Refresh: catalogStateRefreshFunc(meta, id, scope), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for catalog (%s) to be refreshed: %s", id, waitErr) - } - } - - return flattenCatalog(d, catalog) -} - -func resourceRancher2CatalogUpdate(d *schema.ResourceData, meta interface{}) error { - scope := d.Get("scope").(string) - id := d.Id() - log.Printf("[INFO] Updating %s Catalog ID %s", scope, id) - - catalog, err := meta.(*Config).GetCatalog(id, scope) - if err != nil { - return err - } - - update := map[string]interface{}{ - "url": d.Get("url").(string), - "branch": d.Get("branch").(string), - "description": d.Get("description").(string), - "kind": d.Get("kind").(string), - "password": d.Get("password").(string), - "username": d.Get("username").(string), - "annotations": toMapString(d.Get("annotations").(map[string]interface{})), - "labels": toMapString(d.Get("labels").(map[string]interface{})), - } - - _, err = meta.(*Config).UpdateCatalog(scope, catalog, update) - if err != nil { - return err - } - - stateConf := &resource.StateChangeConf{ - Pending: []string{"refreshed"}, - Target: []string{"active"}, - Refresh: catalogStateRefreshFunc(meta, id, scope), - Timeout: d.Timeout(schema.TimeoutUpdate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for %s catalog (%s) to be updated: %s", scope, id, waitErr) - } - - return resourceRancher2CatalogRead(d, meta) -} - -func resourceRancher2CatalogDelete(d *schema.ResourceData, meta interface{}) error { - scope := d.Get("scope").(string) - id := d.Id() - log.Printf("[INFO] Deleting %s catalog ID %s", scope, id) - catalog, err := meta.(*Config).GetCatalog(id, scope) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] %s Catalog ID %s not found.", scope, id) - d.SetId("") - return nil - } - return err - } - - err = meta.(*Config).DeleteCatalog(scope, catalog) - if err != nil { - return fmt.Errorf("Error removing %s Catalog: %s", scope, err) - } - - log.Printf("[DEBUG] Waiting for %s catalog (%s) to be removed", scope, id) - - stateConf := &resource.StateChangeConf{ - Pending: []string{"active"}, - Target: []string{"removed"}, - Refresh: catalogStateRefreshFunc(meta, id, scope), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for %s catalog (%s) to be removed: %s", scope, id, waitErr) - } - - d.SetId("") - return nil -} - -// catalogStateRefreshFunc returns a resource.StateRefreshFunc, used to watch a Rancher Catalog. -func catalogStateRefreshFunc(meta interface{}, catalogID, scope string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - obj, err := meta.(*Config).GetCatalog(catalogID, scope) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - return obj, "removed", nil - } - return nil, "", err - } - - var state string - - switch scope { - case catalogScopeCluster: - state = obj.(*managementClient.ClusterCatalog).State - case catalogScopeGlobal: - state = obj.(*managementClient.Catalog).State - case catalogScopeProject: - state = obj.(*managementClient.ProjectCatalog).State - } - - return obj, state, nil - } -} diff --git a/rancher2/resource_rancher2_catalog_test.go b/rancher2/resource_rancher2_catalog_test.go deleted file mode 100644 index 0646e29e..00000000 --- a/rancher2/resource_rancher2_catalog_test.go +++ /dev/null @@ -1,393 +0,0 @@ -package rancher2 - -import ( - "fmt" - "testing" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" -) - -const ( - testAccRancher2CatalogType = "rancher2_catalog" - testAccRancher2CatalogGlobal = ` -resource "` + testAccRancher2CatalogType + `" "foo-global" { - name = "foo-global" - url = "http://foo.com:8080" - description= "Terraform catalog acceptance test" - version = "helm_v3" - annotations = { - "testacc.terraform.io/test" = "true" - } - labels = { - "testacc.terraform.io/test" = "true" - } -} -` - testAccRancher2CatalogGlobalUpdate = ` -resource "` + testAccRancher2CatalogType + `" "foo-global" { - name = "foo-global" - url = "http://foo.updated.com:8080" - description= "Terraform catalog acceptance test - updated" - version = "helm_v3" - annotations = { - "testacc.terraform.io/test" = "false" - } - labels = { - "testacc.terraform.io/test" = "false" - } -} - ` -) - -var ( - testAccRancher2CatalogCluster string - testAccRancher2CatalogClusterUpdate string - testAccRancher2CatalogClusterConfig string - testAccRancher2CatalogClusterUpdateConfig string - testAccRancher2CatalogProject string - testAccRancher2CatalogProjectUpdate string - testAccRancher2CatalogProjectConfig string - testAccRancher2CatalogProjectUpdateConfig string -) - -func init() { - testAccRancher2CatalogCluster = ` -resource "` + testAccRancher2CatalogType + `" "foo-cluster" { - name = "foo-cluster" - url = "http://foo.com:8080" - description= "Terraform catalog acceptance test" - cluster_id = rancher2_cluster_sync.testacc.cluster_id - scope = "cluster" - version = "helm_v2" -} -` - testAccRancher2CatalogClusterUpdate = ` -resource "` + testAccRancher2CatalogType + `" "foo-cluster" { - name = "foo-cluster" - url = "http://foo.updated.com:8080" - description= "Terraform catalog acceptance test - updated" - cluster_id = rancher2_cluster_sync.testacc.cluster_id - scope = "cluster" - version = "helm_v2" -} - ` - testAccRancher2CatalogClusterConfig = testAccCheckRancher2ClusterSyncTestacc + testAccRancher2CatalogCluster - testAccRancher2CatalogClusterUpdateConfig = testAccCheckRancher2ClusterSyncTestacc + testAccRancher2CatalogClusterUpdate - testAccRancher2CatalogProject = ` -resource "` + testAccRancher2CatalogType + `" "foo-project" { - name = "foo-project" - url = "http://foo.com:8080" - description= "Terraform catalog acceptance test" - project_id = rancher2_cluster_sync.testacc.default_project_id - scope = "project" - version = "helm_v2" -} -` - testAccRancher2CatalogProjectUpdate = ` -resource "` + testAccRancher2CatalogType + `" "foo-project" { - name = "foo-project" - url = "http://foo.updated.com:8080" - description= "Terraform catalog acceptance test - updated" - project_id = rancher2_cluster_sync.testacc.default_project_id - scope = "project" - version = "helm_v2" -} -` - testAccRancher2CatalogProjectConfig = testAccCheckRancher2ClusterSyncTestacc + testAccRancher2CatalogProject - testAccRancher2CatalogProjectUpdateConfig = testAccCheckRancher2ClusterSyncTestacc + testAccRancher2CatalogProjectUpdate -} - -func TestAccRancher2Catalog_basic_Global(t *testing.T) { - var catalog interface{} - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2CatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2CatalogGlobal, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-global", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "version", "helm_v3"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "annotations.testacc.terraform.io/test", "true"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "labels.testacc.terraform.io/test", "true"), - ), - }, - { - Config: testAccRancher2CatalogGlobalUpdate, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-global", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test - updated"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.updated.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "version", "helm_v3"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "annotations.testacc.terraform.io/test", "false"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "labels.testacc.terraform.io/test", "false"), - ), - }, - { - Config: testAccRancher2CatalogGlobal, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-global", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "name", "foo-global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "scope", "global"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "version", "helm_v3"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "annotations.testacc.terraform.io/test", "true"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-global", "labels.testacc.terraform.io/test", "true"), - ), - }, - }, - }) -} - -func TestAccRancher2Catalog_disappears_Global(t *testing.T) { - var catalog interface{} - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2CatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2CatalogGlobal, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-global", catalog), - testAccRancher2CatalogDisappears(catalog), - ), - ExpectNonEmptyPlan: true, - }, - }, - }) -} - -func TestAccRancher2Catalog_basic_Cluster(t *testing.T) { - var catalog interface{} - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2CatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2CatalogClusterConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-cluster", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "name", "foo-cluster"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "scope", "cluster"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "cluster_id", testAccRancher2ClusterID), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "version", "helm_v2"), - ), - }, - { - Config: testAccRancher2CatalogClusterUpdateConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-cluster", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "name", "foo-cluster"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "description", "Terraform catalog acceptance test - updated"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "url", "http://foo.updated.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "scope", "cluster"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "cluster_id", testAccRancher2ClusterID), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "version", "helm_v2"), - ), - }, - { - Config: testAccRancher2CatalogClusterConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-cluster", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "name", "foo-cluster"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "scope", "cluster"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "cluster_id", testAccRancher2ClusterID), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-cluster", "version", "helm_v2"), - ), - }, - }, - }) -} - -func TestAccRancher2Catalog_disappears_Cluster(t *testing.T) { - var catalog interface{} - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2CatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2CatalogClusterConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-cluster", catalog), - testAccRancher2CatalogDisappears(catalog), - ), - ExpectNonEmptyPlan: true, - }, - }, - }) -} - -func TestAccRancher2Catalog_basic_Project(t *testing.T) { - var catalog interface{} - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2CatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2CatalogProjectConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-project", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "name", "foo-project"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "scope", "project"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "version", "helm_v2"), - ), - }, - { - Config: testAccRancher2CatalogProjectUpdateConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-project", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "name", "foo-project"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "description", "Terraform catalog acceptance test - updated"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "url", "http://foo.updated.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "scope", "project"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "version", "helm_v2"), - ), - }, - { - Config: testAccRancher2CatalogProjectConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-project", catalog), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "name", "foo-project"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "description", "Terraform catalog acceptance test"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "url", "http://foo.com:8080"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "scope", "project"), - resource.TestCheckResourceAttr(testAccRancher2CatalogType+".foo-project", "version", "helm_v2"), - ), - }, - }, - }) -} - -func TestAccRancher2Catalog_disappears_Project(t *testing.T) { - var catalog interface{} - - resource.Test(t, resource.TestCase{ - PreCheck: func() { testAccPreCheck(t) }, - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2CatalogDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2CatalogProjectConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2CatalogExists(testAccRancher2CatalogType+".foo-project", catalog), - testAccRancher2CatalogDisappears(catalog), - ), - ExpectNonEmptyPlan: true, - }, - }, - }) -} - -func testAccRancher2CatalogDisappears(cat interface{}) resource.TestCheckFunc { - return func(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != testAccRancher2CatalogType { - continue - } - - scope := rs.Primary.Attributes["scope"] - id := rs.Primary.ID - cat, err := testAccProvider.Meta().(*Config).GetCatalog(id, scope) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - - err = testAccProvider.Meta().(*Config).DeleteCatalog(scope, cat) - if err != nil { - return fmt.Errorf("Error removing %s Catalog: %s", scope, err) - } - - stateConf := &resource.StateChangeConf{ - Pending: []string{"active"}, - Target: []string{"removed"}, - Refresh: catalogStateRefreshFunc(testAccProvider.Meta(), id, scope), - Timeout: 10 * time.Minute, - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for %s catalog (%s) to be removed: %s", scope, id, waitErr) - } - } - return nil - - } -} - -func testAccCheckRancher2CatalogExists(n string, cat interface{}) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No catalog ID is set") - } - - scope := rs.Primary.Attributes["scope"] - id := rs.Primary.ID - foundReg, err := testAccProvider.Meta().(*Config).GetCatalog(id, scope) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - - cat = foundReg - - return nil - } -} - -func testAccCheckRancher2CatalogDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != testAccRancher2CatalogType { - continue - } - scope := rs.Primary.Attributes["scope"] - id := rs.Primary.ID - _, err := testAccProvider.Meta().(*Config).GetCatalog(id, scope) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - return fmt.Errorf("Catalog still exists") - } - return nil -} diff --git a/rancher2/resource_rancher2_multi_cluster_app.go b/rancher2/resource_rancher2_multi_cluster_app.go deleted file mode 100644 index 283ff5f3..00000000 --- a/rancher2/resource_rancher2_multi_cluster_app.go +++ /dev/null @@ -1,371 +0,0 @@ -package rancher2 - -import ( - "fmt" - "log" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" -) - -func resourceRancher2MultiClusterApp() *schema.Resource { - return &schema.Resource{ - Create: resourceRancher2MultiClusterAppCreate, - Read: resourceRancher2MultiClusterAppRead, - Update: resourceRancher2MultiClusterAppUpdate, - Delete: resourceRancher2MultiClusterAppDelete, - Importer: &schema.ResourceImporter{ - State: resourceRancher2MultiClusterAppImport, - }, - - Schema: multiClusterAppFields(), - Timeouts: &schema.ResourceTimeout{ - Create: schema.DefaultTimeout(10 * time.Minute), - Update: schema.DefaultTimeout(10 * time.Minute), - Delete: schema.DefaultTimeout(10 * time.Minute), - }, - } -} - -func resourceRancher2MultiClusterAppCreate(d *schema.ResourceData, meta interface{}) error { - name := d.Get("name").(string) - - err := resourceRancher2AppGetVersion(d, meta) - if err != nil { - return err - } - - multiClusterApp, err := expandMultiClusterApp(d) - if err != nil { - return err - } - - log.Printf("[INFO] Creating multi cluster app %s", name) - - client, err := meta.(*Config).ManagementClient() - if err != nil { - return err - } - - newMultiClusterApp, err := client.MultiClusterApp.Create(multiClusterApp) - if err != nil { - return err - } - - d.SetId(newMultiClusterApp.ID) - - if d.Get("wait").(bool) { - stateConf := &resource.StateChangeConf{ - Pending: []string{}, - Target: []string{"active"}, - Refresh: multiClusterAppStateRefreshFunc(client, newMultiClusterApp.ID), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf("[ERROR] waiting for multi cluster app (%s) to be created: %s", newMultiClusterApp.ID, waitErr) - } - } - - return resourceRancher2MultiClusterAppRead(d, meta) -} - -func resourceRancher2MultiClusterAppRead(d *schema.ResourceData, meta interface{}) error { - id := d.Id() - - log.Printf("[INFO] Refreshing multi cluster app ID %s", id) - - client, err := meta.(*Config).ManagementClient() - if err != nil { - return err - } - - multiClusterApp, err := client.MultiClusterApp.ByID(id) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] multi cluster app ID %s not found.", id) - d.SetId("") - return nil - } - return err - } - - templateVersion, err := client.TemplateVersion.ByID(multiClusterApp.TemplateVersionID) - if err != nil { - return err - } - - return flattenMultiClusterApp(d, multiClusterApp, templateVersion.ExternalID) -} - -func resourceRancher2MultiClusterAppUpdate(d *schema.ResourceData, meta interface{}) error { - id := d.Id() - - client, err := meta.(*Config).ManagementClient() - if err != nil { - return err - } - - multiClusterApp, err := client.MultiClusterApp.ByID(id) - if err != nil { - return err - } - - updateApp := true - - // Rollback or modify targets - if d.HasChange("revision_id") { - updateApp = false - revID := d.Get("revision_id").(string) - log.Printf("[INFO] Rollbacking multi cluster app ID %s to %s", id, revID) - - rollback := &managementClient.MultiClusterAppRollbackInput{ - RevisionID: revID, - } - err = client.MultiClusterApp.ActionRollback(multiClusterApp, rollback) - if err != nil { - return err - } - } else if d.HasChange("targets") { - updateApp = false - - removeTarget := multiClusterAppTargetToRemove(d, multiClusterApp) - addTarget := multiClusterAppTargetToAdd(d, multiClusterApp) - - if len(removeTarget.Projects) > 0 { - log.Printf("[INFO] Removing targets on multi cluster app ID %s", id) - err = client.MultiClusterApp.ActionRemoveProjects(multiClusterApp, removeTarget) - if err != nil { - return err - } - if d.HasChange("answers") { - // answer for removed target has to be deleted manually - updateApp = true - } - } - - if len(addTarget.Projects) > 0 { - log.Printf("[INFO] Adding targets on multi cluster app ID %s", id) - err = client.MultiClusterApp.ActionAddProjects(multiClusterApp, addTarget) - if err != nil { - return err - } - } - } - - // Update app if needed - if updateApp { - log.Printf("[INFO] Updating multi cluster app ID %s", id) - - update := map[string]interface{}{ - "answers": expandAnswers(d.Get("answers").([]interface{})), - "members": expandMembers(d.Get("members").([]interface{})), - "revisionHistoryLimit": d.Get("revision_history_limit").(int), - "roles": toArrayString(d.Get("roles").([]interface{})), - "templateVersionId": expandMultiClusterAppTemplateVersionID(d), - "upgradeStrategy": expandUpgradeStrategy(d.Get("upgrade_strategy").([]interface{})), - "annotations": toMapString(d.Get("annotations").(map[string]interface{})), - "labels": toMapString(d.Get("labels").(map[string]interface{})), - } - _, err := client.MultiClusterApp.Update(multiClusterApp, update) - if err != nil { - return err - } - } - - if d.Get("wait").(bool) { - stateConf := &resource.StateChangeConf{ - Pending: []string{}, - Target: []string{"active"}, - Refresh: multiClusterAppStateRefreshFunc(client, id), - Timeout: d.Timeout(schema.TimeoutCreate), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf("[ERROR] waiting for multi cluster app (%s) to be created: %s", id, waitErr) - } - } - - return resourceRancher2MultiClusterAppRead(d, meta) -} - -func resourceRancher2MultiClusterAppDelete(d *schema.ResourceData, meta interface{}) error { - id := d.Id() - - log.Printf("[INFO] Deleting multi cluster app ID %s", id) - - client, err := meta.(*Config).ManagementClient() - if err != nil { - return err - } - - multiClusterApp, err := client.MultiClusterApp.ByID(id) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - log.Printf("[INFO] multi cluster app ID %s not found.", d.Id()) - d.SetId("") - return nil - } - return err - } - - err = client.MultiClusterApp.Delete(multiClusterApp) - if err != nil { - return fmt.Errorf("[ERROR] removing multi cluster app: %s", err) - } - - stateConf := &resource.StateChangeConf{ - Pending: []string{"removing"}, - Target: []string{"removed"}, - Refresh: multiClusterAppStateRefreshFunc(client, id), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for multi cluster app (%s) to be removed: %s", id, waitErr) - } - d.SetId("") - - for i := range multiClusterApp.Targets { - client, err := meta.(*Config).ProjectClient(multiClusterApp.Targets[i].ProjectID) - if err != nil { - continue - } - mappID := splitProjectIDPart(multiClusterApp.Targets[i].ProjectID) + ":" + multiClusterApp.Targets[i].AppID - stateConf = &resource.StateChangeConf{ - Pending: []string{"removing"}, - Target: []string{"removed"}, - Refresh: appStateRefreshFunc(client, mappID), - Timeout: d.Timeout(schema.TimeoutDelete), - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - stateConf.WaitForState() - } - time.Sleep(5 * time.Second) - - return nil -} - -func resourceRancher2MultiClusterAppGetVersion(d *schema.ResourceData, meta interface{}) error { - catalogName := d.Get("catalog_name").(string) - appName := d.Get("template_name").(string) - appVersion := d.Get("template_version").(string) - - if len(appVersion) > 0 { - return nil - } - - catalogName = MultiClusterAppTemplatePrefix + catalogName - - appID := catalogName + "-" + appName - - client, err := meta.(*Config).ManagementClient() - if err != nil { - return err - } - - template, err := client.Template.ByID(appID) - if err != nil { - return err - } - - appVersion, err = getLatestVersion(template.VersionLinks) - if err != nil { - return err - } - - d.Set("template_version", appVersion) - - return nil -} - -// multiClusterAppStateRefreshFunc returns a resource.StateRefreshFunc, used to watch a Rancher MultiClusterApp. -func multiClusterAppStateRefreshFunc(client *managementClient.Client, appID string) resource.StateRefreshFunc { - return func() (interface{}, string, error) { - obj, err := client.MultiClusterApp.ByID(appID) - if err != nil { - if IsNotFound(err) || IsForbidden(err) { - return obj, "removed", nil - } - return nil, "", err - } - - return obj, obj.State, nil - } -} - -func multiClusterAppTargetToRemove(d *schema.ResourceData, mca *managementClient.MultiClusterApp) *managementClient.UpdateMultiClusterAppTargetsInput { - newTargets := expandTargets(d.Get("targets").([]interface{})) - - removeTarget := &managementClient.UpdateMultiClusterAppTargetsInput{} - for _, t := range mca.Targets { - found := false - for _, newT := range newTargets { - if t == newT { - found = true - break - } - } - if !found { - var a *managementClient.Answer - if d.HasChange("answers") { - for _, answer := range mca.Answers { - if t.ProjectID == answer.ProjectID { - a = &answer - break - } - } - } - removeTarget.Projects = append(removeTarget.Projects, t.ProjectID) - if a != nil { - removeTarget.Answers = append(removeTarget.Answers, *a) - } - } - } - - return removeTarget -} - -func multiClusterAppTargetToAdd(d *schema.ResourceData, mca *managementClient.MultiClusterApp) *managementClient.UpdateMultiClusterAppTargetsInput { - newTargets := expandTargets(d.Get("targets").([]interface{})) - - addTarget := &managementClient.UpdateMultiClusterAppTargetsInput{} - for _, newT := range newTargets { - found := false - for _, t := range mca.Targets { - if t == newT { - found = true - break - } - } - if !found { - var a *managementClient.Answer - if d.HasChange("answers") { - newAnswers := expandAnswers(d.Get("answers").([]interface{})) - for _, answer := range newAnswers { - if newT.ProjectID == answer.ProjectID { - a = &answer - break - } - } - } - addTarget.Projects = append(addTarget.Projects, newT.ProjectID) - if a != nil { - addTarget.Answers = append(addTarget.Answers, *a) - } - } - } - - return addTarget -} diff --git a/rancher2/resource_rancher2_multi_cluster_app_test.go b/rancher2/resource_rancher2_multi_cluster_app_test.go deleted file mode 100644 index 240b1b10..00000000 --- a/rancher2/resource_rancher2_multi_cluster_app_test.go +++ /dev/null @@ -1,222 +0,0 @@ -package rancher2 - -import ( - "fmt" - "testing" - "time" - - "github.com/hashicorp/terraform-plugin-sdk/helper/resource" - "github.com/hashicorp/terraform-plugin-sdk/terraform" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" -) - -const ( - testAccRancher2MultiClusterAppType = "rancher2_multi_cluster_app" -) - -var ( - testAccRancher2MultiClusterApp string - testAccRancher2MultiClusterAppUpdate string - testAccRancher2MultiClusterAppConfig string - testAccRancher2MultiClusterAppUpdateConfig string -) - -func init() { - testAccRancher2MultiClusterApp = ` -resource "` + testAccRancher2MultiClusterAppType + `" "foo" { - catalog_name = "library" - name = "foo" - targets { - project_id = rancher2_cluster_sync.testacc.default_project_id - } - template_name = "docker-registry" - template_version = "1.8.1" - answers { - values = { - "ingress_host" = "test.xip.io" - } - } - roles = ["project-member"] -} -` - testAccRancher2MultiClusterAppUpdate = ` -resource "` + testAccRancher2MultiClusterAppType + `" "foo" { - catalog_name = "library" - name = "foo" - targets { - project_id = rancher2_cluster_sync.testacc.default_project_id - } - template_name = "docker-registry" - template_version = "1.8.1" - answers { - values = { - "ingress_host" = "test2.xip.io" - } - } - roles = ["cluster-admin"] -} -` - testAccRancher2MultiClusterAppConfig = testAccCheckRancher2ClusterSyncTestacc + testAccRancher2MultiClusterApp - testAccRancher2MultiClusterAppUpdateConfig = testAccCheckRancher2ClusterSyncTestacc + testAccRancher2MultiClusterAppUpdate -} - -func TestAccRancher2MultiClusterApp_basic(t *testing.T) { - var app *managementClient.MultiClusterApp - - resource.Test(t, resource.TestCase{ - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2MultiClusterAppDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2MultiClusterAppConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2MultiClusterAppExists(testAccRancher2MultiClusterAppType+".foo", app), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "name", "foo"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "template_version_id", "cattle-global-data:library-docker-registry-1.8.1"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "answers.0.values.ingress_host", "test.xip.io"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "roles.0", "project-member"), - ), - }, - { - Config: testAccRancher2MultiClusterAppUpdateConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2MultiClusterAppExists(testAccRancher2MultiClusterAppType+".foo", app), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "name", "foo"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "template_version_id", "cattle-global-data:library-docker-registry-1.8.1"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "answers.0.values.ingress_host", "test2.xip.io"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "roles.0", "cluster-admin"), - ), - }, - { - Config: testAccRancher2MultiClusterAppConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2MultiClusterAppExists(testAccRancher2MultiClusterAppType+".foo", app), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "name", "foo"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "template_version_id", "cattle-global-data:library-docker-registry-1.8.1"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "answers.0.values.ingress_host", "test.xip.io"), - resource.TestCheckResourceAttr(testAccRancher2MultiClusterAppType+".foo", "roles.0", "project-member"), - ), - }, - }, - }) -} - -func TestAccRancher2MultiClusterApp_disappears(t *testing.T) { - var app *managementClient.MultiClusterApp - - time.Sleep(5 * time.Second) - - resource.Test(t, resource.TestCase{ - Providers: testAccProviders, - CheckDestroy: testAccCheckRancher2MultiClusterAppDestroy, - Steps: []resource.TestStep{ - { - Config: testAccRancher2MultiClusterAppConfig, - Check: resource.ComposeTestCheckFunc( - testAccCheckRancher2MultiClusterAppExists(testAccRancher2MultiClusterAppType+".foo", app), - testAccRancher2MultiClusterAppDisappears(app), - ), - ExpectNonEmptyPlan: true, - }, - }, - }) -} - -func testAccRancher2MultiClusterAppDisappears(mca *managementClient.MultiClusterApp) resource.TestCheckFunc { - return func(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != testAccRancher2MultiClusterAppType { - continue - } - - client, err := testAccProvider.Meta().(*Config).ManagementClient() - if err != nil { - return err - } - - mca, err := client.MultiClusterApp.ByID(rs.Primary.ID) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - - err = client.MultiClusterApp.Delete(mca) - if err != nil { - return fmt.Errorf("Error removing multi cluster app: %s", err) - } - - stateConf := &resource.StateChangeConf{ - Pending: []string{"removing"}, - Target: []string{"removed"}, - Refresh: multiClusterAppStateRefreshFunc(client, rs.Primary.ID), - Timeout: 10 * time.Minute, - Delay: 1 * time.Second, - MinTimeout: 3 * time.Second, - } - - _, waitErr := stateConf.WaitForState() - if waitErr != nil { - return fmt.Errorf( - "[ERROR] waiting for multi cluster app (%s) to be removed: %s", rs.Primary.ID, waitErr) - } - time.Sleep(5 * time.Second) - } - return nil - } -} - -func testAccCheckRancher2MultiClusterAppExists(n string, mca *managementClient.MultiClusterApp) resource.TestCheckFunc { - return func(s *terraform.State) error { - rs, ok := s.RootModule().Resources[n] - - if !ok { - return fmt.Errorf("Not found: %s", n) - } - - if rs.Primary.ID == "" { - return fmt.Errorf("No multi cluster app ID is set") - } - - client, err := testAccProvider.Meta().(*Config).ManagementClient() - if err != nil { - return err - } - - foundMultiClusterApp, err := client.MultiClusterApp.ByID(rs.Primary.ID) - if err != nil { - if IsNotFound(err) { - return fmt.Errorf("Multi cluster app not found") - } - return err - } - - mca = foundMultiClusterApp - - return nil - } -} - -func testAccCheckRancher2MultiClusterAppDestroy(s *terraform.State) error { - for _, rs := range s.RootModule().Resources { - if rs.Type != testAccRancher2MultiClusterAppType { - continue - } - - client, err := testAccProvider.Meta().(*Config).ManagementClient() - if err != nil { - return err - } - - _, err = client.MultiClusterApp.ByID(rs.Primary.ID) - if err != nil { - if IsNotFound(err) { - return nil - } - return err - } - return fmt.Errorf("Multi cluster app still exists") - } - return nil -} diff --git a/rancher2/schema_app.go b/rancher2/schema_app.go deleted file mode 100644 index 9a9b8140..00000000 --- a/rancher2/schema_app.go +++ /dev/null @@ -1,96 +0,0 @@ -package rancher2 - -import ( - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" -) - -//Schemas - -func appFields() map[string]*schema.Schema { - s := map[string]*schema.Schema{ - "catalog_name": { - Type: schema.TypeString, - Required: true, - Description: "Catalog name of the app", - }, - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "Name of the app", - }, - "project_id": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "Project ID to add app", - }, - "target_namespace": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "Namespace name to add app", - }, - "template_name": { - Type: schema.TypeString, - Required: true, - Description: "Template name of the app", - }, - "answers": { - Type: schema.TypeMap, - Optional: true, - Description: "Answers of the app", - }, - "description": { - Type: schema.TypeString, - Optional: true, - Computed: true, - }, - "external_id": { - Type: schema.TypeString, - Computed: true, - Description: "External ID of the app", - }, - "force_upgrade": { - Type: schema.TypeBool, - Optional: true, - Default: false, - Description: "Force app upgrade", - }, - "revision_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, - Description: "App revision id", - }, - "template_version": { - Type: schema.TypeString, - Optional: true, - Computed: true, - Description: "Template version of the app", - }, - "values_yaml": { - Type: schema.TypeString, - Optional: true, - Description: "values.yaml base64 encoded file content of the app", - ValidateFunc: validation.StringIsBase64, - StateFunc: func(val interface{}) string { - s, _ := Base64Decode(val.(string)) - return Base64Encode(TrimSpace(s)) - }, - }, - "wait": { - Type: schema.TypeBool, - Optional: true, - Default: true, - Description: "Wait until app is deployed and active", - }, - } - - for k, v := range commonAnnotationLabelFields() { - s[k] = v - } - - return s -} diff --git a/rancher2/schema_catalog.go b/rancher2/schema_catalog.go deleted file mode 100644 index 9b7a9a28..00000000 --- a/rancher2/schema_catalog.go +++ /dev/null @@ -1,96 +0,0 @@ -package rancher2 - -import ( - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - "github.com/hashicorp/terraform-plugin-sdk/helper/validation" -) - -const ( - catalogKindHelm = "helm" - catalogScopeCluster = "cluster" - catalogScopeGlobal = "global" - catalogScopeProject = "project" - catalogHelmV2 = "helm_v2" - catalogHelmV3 = "helm_v3" -) - -var ( - catalogKinds = []string{catalogKindHelm} - catalogScopes = []string{catalogScopeCluster, catalogScopeGlobal, catalogScopeProject} - catalogHelmVersions = []string{catalogHelmV2, catalogHelmV3} -) - -// Shemas - -func catalogFields() map[string]*schema.Schema { - s := map[string]*schema.Schema{ - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - }, - "url": { - Type: schema.TypeString, - Required: true, - }, - "branch": { - Type: schema.TypeString, - Optional: true, - Default: "master", - }, - "cluster_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "description": { - Type: schema.TypeString, - Optional: true, - }, - "kind": { - Type: schema.TypeString, - Optional: true, - Default: catalogKindHelm, - ValidateFunc: validation.StringInSlice(catalogKinds, true), - }, - "password": { - Type: schema.TypeString, - Optional: true, - Sensitive: true, - }, - "project_id": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - }, - "refresh": { - Type: schema.TypeBool, - Optional: true, - Default: false, - }, - "scope": { - Type: schema.TypeString, - Optional: true, - Default: catalogScopeGlobal, - ValidateFunc: validation.StringInSlice(catalogScopes, true), - }, - "username": { - Type: schema.TypeString, - Optional: true, - Sensitive: true, - }, - "version": { - Type: schema.TypeString, - Optional: true, - ForceNew: true, - Computed: true, - ValidateFunc: validation.StringInSlice(catalogHelmVersions, true), - }, - } - - for k, v := range commonAnnotationLabelFields() { - s[k] = v - } - - return s -} diff --git a/rancher2/schema_multi_cluster_app.go b/rancher2/schema_multi_cluster_app.go deleted file mode 100644 index 7e8454fb..00000000 --- a/rancher2/schema_multi_cluster_app.go +++ /dev/null @@ -1,106 +0,0 @@ -package rancher2 - -import ( - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" -) - -//Schemas - -func multiClusterAppFields() map[string]*schema.Schema { - s := map[string]*schema.Schema{ - "catalog_name": { - Type: schema.TypeString, - Required: true, - Description: "Multi cluster app catalog name", - }, - "name": { - Type: schema.TypeString, - Required: true, - ForceNew: true, - Description: "Multi cluster app name", - }, - "roles": { - Type: schema.TypeList, - Required: true, - Description: "Multi cluster app roles", - Elem: &schema.Schema{ - Type: schema.TypeString, - }, - }, - "targets": { - Type: schema.TypeList, - Required: true, - Description: "Multi cluster app targets", - Elem: &schema.Resource{ - Schema: targetFields(), - }, - }, - "template_name": { - Type: schema.TypeString, - Required: true, - Description: "Multi cluster app template name", - }, - "answers": { - Type: schema.TypeList, - Optional: true, - Computed: true, - Description: "Multi cluster app answers", - Elem: &schema.Resource{ - Schema: answerFields(), - }, - }, - "members": { - Type: schema.TypeList, - Optional: true, - Description: "Multi cluster app members", - Elem: &schema.Resource{ - Schema: memberFields(), - }, - }, - "revision_history_limit": { - Type: schema.TypeInt, - Optional: true, - Default: 10, - Description: "Multi cluster app revision history limit", - }, - "revision_id": { - Type: schema.TypeString, - Optional: true, - Computed: true, - Description: "Multi cluster app revision name", - }, - "template_version": { - Type: schema.TypeString, - Optional: true, - Computed: true, - Description: "Multi cluster app template version", - }, - "template_version_id": { - Type: schema.TypeString, - Computed: true, - Description: "Multi cluster app template version ID", - }, - "upgrade_strategy": { - Type: schema.TypeList, - Optional: true, - Computed: true, - MaxItems: 1, - Description: "Multi cluster app upgrade strategy", - Elem: &schema.Resource{ - Schema: upgradeStrategyFields(), - }, - }, - "wait": { - Type: schema.TypeBool, - Optional: true, - Default: true, - Description: "Wait until multi cluster app is active", - }, - } - - for k, v := range commonAnnotationLabelFields() { - s[k] = v - } - - return s -} diff --git a/rancher2/structure_app.go b/rancher2/structure_app.go deleted file mode 100644 index 699c0f65..00000000 --- a/rancher2/structure_app.go +++ /dev/null @@ -1,164 +0,0 @@ -package rancher2 - -import ( - "fmt" - "strings" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - projectClient "github.com/rancher/rancher/pkg/client/generated/project/v3" -) - -const ( - AppTemplateExternalIDPrefix = "catalog://?" - AppCatalogClusterLocalPrefix = "local" - AppCatalogClusterPrefix = "c-" - AppCatalogProjectPrefix = "p-" -) - -// Flatteners - -func flattenAppExternalID(d *schema.ResourceData, in string) { - //Global catalog url: catalog://?catalog=demo&template=test&version=1.23.0 - //Cluster catalog url: catalog://?catalog=c-XXXXX/test&type=clusterCatalog&template=test&version=1.23.0 - //Project catalog url: catalog://?catalog=p-XXXXX/test&type=projectCatalog&template=test&version=1.23.0 - - str := strings.TrimPrefix(in, AppTemplateExternalIDPrefix) - values := strings.Split(str, "&") - out := make(map[string]string, len(values)) - for _, v := range values { - pair := strings.Split(v, "=") - if len(pair) != 2 { - continue - } - if pair[0] == "catalog" && (strings.HasPrefix(pair[1], AppCatalogClusterLocalPrefix) || strings.HasPrefix(pair[1], AppCatalogClusterPrefix) || strings.HasPrefix(pair[1], AppCatalogProjectPrefix)) { - pair[1] = strings.Replace(pair[1], "/", ":", -1) - } - out[pair[0]] = pair[1] - } - - d.Set("external_id", in) - d.Set("catalog_name", out["catalog"]) - d.Set("template_name", out["template"]) - d.Set("template_version", out["version"]) -} - -func flattenApp(d *schema.ResourceData, in *projectClient.App) error { - if in == nil { - return nil - } - - d.SetId(in.ID) - - flattenAppExternalID(d, in.ExternalID) - - d.Set("name", in.Name) - d.Set("project_id", in.ProjectID) - d.Set("target_namespace", in.TargetNamespace) - - if len(in.Answers) > 0 { - err := d.Set("answers", toMapInterface(in.Answers)) - if err != nil { - return err - } - } - - if len(in.Description) > 0 { - d.Set("description", in.Description) - } - - if len(in.AppRevisionID) > 0 { - d.Set("revision_id", in.AppRevisionID) - } - - if len(in.ValuesYaml) > 0 { - d.Set("values_yaml", Base64Encode(in.ValuesYaml)) - } - - err := d.Set("annotations", toMapInterface(in.Annotations)) - if err != nil { - return err - } - - err = d.Set("labels", toMapInterface(in.Labels)) - if err != nil { - return err - } - - return nil - -} - -// Expanders - -func expandAppExternalID(in *schema.ResourceData) string { - //Global catalog url: catalog://?catalog=test&template=test&version=1.23.0 - //Cluster catalog url: catalog://?catalog=c-XXXXX/test&type=clusterCatalog&template=test&version=1.23.0 - //Project catalog url: catalog://?catalog=p-XXXXX/test&type=projectCatalog&template=test&version=1.23.0 - - catalogName := in.Get("catalog_name").(string) - appName := in.Get("template_name").(string) - appVersion := in.Get("template_version").(string) - - if strings.HasPrefix(catalogName, AppCatalogClusterLocalPrefix) || strings.HasPrefix(catalogName, AppCatalogClusterPrefix) { - catalogName = strings.Replace(catalogName, ":", "/", -1) - catalogName = catalogName + "&type=clusterCatalog" - } - if strings.HasPrefix(catalogName, AppCatalogProjectPrefix) { - catalogName = strings.Replace(catalogName, ":", "/", -1) - catalogName = catalogName + "&type=projectCatalog" - } - - catalogPart := "catalog=" + catalogName - appNamePart := "&template=" + appName - appVersionPart := "&version=" + appVersion - - return AppTemplateExternalIDPrefix + catalogPart + appNamePart + appVersionPart -} - -func expandApp(in *schema.ResourceData) (*projectClient.App, error) { - obj := &projectClient.App{} - if in == nil { - return nil, nil - } - - if v := in.Id(); len(v) > 0 { - obj.ID = v - } - - obj.ExternalID = expandAppExternalID(in) - obj.Name = in.Get("name").(string) - obj.ProjectID = in.Get("project_id").(string) - obj.TargetNamespace = in.Get("target_namespace").(string) - - if v, ok := in.Get("answers").(map[string]interface{}); ok && len(v) > 0 { - obj.Answers = toMapString(v) - } - - if v, ok := in.Get("description").(string); ok && len(v) > 0 { - obj.Description = v - } - - if v, ok := in.Get("revision_id").(string); ok && len(v) > 0 { - obj.AppRevisionID = v - } - - if v, ok := in.Get("values_yaml").(string); ok && len(v) > 0 { - values, err := Base64Decode(v) - if err != nil { - return nil, fmt.Errorf("expanding app: values_yaml is not base64 encoded: %s", v) - } - obj.ValuesYaml = values - } - - if v, ok := in.Get("annotations").(map[string]interface{}); ok && len(v) > 0 { - obj.Annotations = toMapString(v) - } - - if v, ok := in.Get("labels").(map[string]interface{}); ok && len(v) > 0 { - obj.Labels = toMapString(v) - } - - obj.Timeout = int64(in.Timeout(schema.TimeoutCreate).Seconds()) - - return obj, nil -} diff --git a/rancher2/structure_app_test.go b/rancher2/structure_app_test.go deleted file mode 100644 index bd74e35a..00000000 --- a/rancher2/structure_app_test.go +++ /dev/null @@ -1,280 +0,0 @@ -package rancher2 - -import ( - "reflect" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - projectClient "github.com/rancher/rancher/pkg/client/generated/project/v3" - "github.com/stretchr/testify/assert" -) - -var ( - testAppConfGlobal *projectClient.App - testAppInterfaceGlobal map[string]interface{} - testAppConfClusterLocal *projectClient.App - testAppInterfaceClusterLocal map[string]interface{} - testAppConfCluster *projectClient.App - testAppInterfaceCluster map[string]interface{} - testAppConfProject *projectClient.App - testAppInterfaceProject map[string]interface{} -) - -func init() { - testAppConfGlobal = &projectClient.App{ - ExternalID: "catalog://?catalog=test&template=test&version=1.23.0", - Name: "name", - ProjectID: "project:test", - TargetNamespace: "target_namespace", - Answers: map[string]string{ - "answers1": "one", - "answers2": "two", - }, - Description: "description", - AppRevisionID: "revision_id", - ValuesYaml: "values_yaml", - Annotations: map[string]string{ - "node_one": "one", - "node_two": "two", - }, - Labels: map[string]string{ - "option1": "value1", - "option2": "value2", - }, - Timeout: 1200, - } - testAppInterfaceGlobal = map[string]interface{}{ - "catalog_name": "test", - //"external_id": "catalog://?catalog=test&template=test&version=1.23.0", - "name": "name", - "project_id": "project:test", - "target_namespace": "target_namespace", - "template_name": "test", - "answers": map[string]interface{}{ - "answers1": "one", - "answers2": "two", - }, - "description": "description", - "revision_id": "revision_id", - "template_version": "1.23.0", - "values_yaml": Base64Encode("values_yaml"), - "annotations": map[string]interface{}{ - "node_one": "one", - "node_two": "two", - }, - "labels": map[string]interface{}{ - "option1": "value1", - "option2": "value2", - }, - } - testAppConfClusterLocal = &projectClient.App{ - ExternalID: "catalog://?catalog=local/test&type=clusterCatalog&template=test&version=1.23.0", - Name: "name", - ProjectID: "project:test", - TargetNamespace: "target_namespace", - Answers: map[string]string{ - "answers1": "one", - "answers2": "two", - }, - Description: "description", - AppRevisionID: "revision_id", - ValuesYaml: "values_yaml", - Annotations: map[string]string{ - "node_one": "one", - "node_two": "two", - }, - Labels: map[string]string{ - "option1": "value1", - "option2": "value2", - }, - Timeout: 1200, - } - testAppInterfaceClusterLocal = map[string]interface{}{ - "catalog_name": "local:test", - "name": "name", - "project_id": "project:test", - "target_namespace": "target_namespace", - "template_name": "test", - "answers": map[string]interface{}{ - "answers1": "one", - "answers2": "two", - }, - "description": "description", - "revision_id": "revision_id", - "template_version": "1.23.0", - "values_yaml": Base64Encode("values_yaml"), - "annotations": map[string]interface{}{ - "node_one": "one", - "node_two": "two", - }, - "labels": map[string]interface{}{ - "option1": "value1", - "option2": "value2", - }, - } - testAppConfCluster = &projectClient.App{ - ExternalID: "catalog://?catalog=c-XXXXX/test&type=clusterCatalog&template=test&version=1.23.0", - Name: "name", - ProjectID: "project:test", - TargetNamespace: "target_namespace", - Answers: map[string]string{ - "answers1": "one", - "answers2": "two", - }, - Description: "description", - AppRevisionID: "revision_id", - ValuesYaml: "values_yaml", - Annotations: map[string]string{ - "node_one": "one", - "node_two": "two", - }, - Labels: map[string]string{ - "option1": "value1", - "option2": "value2", - }, - Timeout: 1200, - } - testAppInterfaceCluster = map[string]interface{}{ - "catalog_name": "c-XXXXX:test", - "name": "name", - "project_id": "project:test", - "target_namespace": "target_namespace", - "template_name": "test", - "answers": map[string]interface{}{ - "answers1": "one", - "answers2": "two", - }, - "description": "description", - "revision_id": "revision_id", - "template_version": "1.23.0", - "values_yaml": Base64Encode("values_yaml"), - "annotations": map[string]interface{}{ - "node_one": "one", - "node_two": "two", - }, - "labels": map[string]interface{}{ - "option1": "value1", - "option2": "value2", - }, - } - testAppConfProject = &projectClient.App{ - ExternalID: "catalog://?catalog=p-XXXXX/test&type=projectCatalog&template=test&version=1.23.0", - Name: "name", - ProjectID: "project:test", - TargetNamespace: "target_namespace", - Answers: map[string]string{ - "answers1": "one", - "answers2": "two", - }, - Description: "description", - AppRevisionID: "revision_id", - ValuesYaml: "values_yaml", - Annotations: map[string]string{ - "node_one": "one", - "node_two": "two", - }, - Labels: map[string]string{ - "option1": "value1", - "option2": "value2", - }, - Timeout: 1200, - } - testAppInterfaceProject = map[string]interface{}{ - "catalog_name": "p-XXXXX:test", - "name": "name", - "project_id": "project:test", - "target_namespace": "target_namespace", - "template_name": "test", - "answers": map[string]interface{}{ - "answers1": "one", - "answers2": "two", - }, - "description": "description", - "revision_id": "revision_id", - "template_version": "1.23.0", - "values_yaml": Base64Encode("values_yaml"), - "annotations": map[string]interface{}{ - "node_one": "one", - "node_two": "two", - }, - "labels": map[string]interface{}{ - "option1": "value1", - "option2": "value2", - }, - } -} - -func TestFlattenApp(t *testing.T) { - - cases := []struct { - Input *projectClient.App - ExpectedOutput map[string]interface{} - }{ - { - testAppConfGlobal, - testAppInterfaceGlobal, - }, - { - testAppConfClusterLocal, - testAppInterfaceClusterLocal, - }, - { - testAppConfCluster, - testAppInterfaceCluster, - }, - { - testAppConfProject, - testAppInterfaceProject, - }, - } - - for _, tc := range cases { - output := schema.TestResourceDataRaw(t, appFields(), tc.ExpectedOutput) - err := flattenApp(output, tc.Input) - if err != nil { - assert.FailNow(t, "[ERROR] on flattener: %#v", err) - } - expectedOutput := map[string]interface{}{} - for k := range tc.ExpectedOutput { - expectedOutput[k] = output.Get(k) - } - if !reflect.DeepEqual(expectedOutput, tc.ExpectedOutput) { - assert.FailNow(t, "Unexpected output from flattener.\nExpected: %#v\nGiven: %#v", - expectedOutput, tc.ExpectedOutput) - } - } -} - -func TestExpandApp(t *testing.T) { - - cases := []struct { - Input map[string]interface{} - ExpectedOutput interface{} - }{ - { - testAppInterfaceGlobal, - testAppConfGlobal, - }, - { - testAppInterfaceClusterLocal, - testAppConfClusterLocal, - }, - { - testAppInterfaceCluster, - testAppConfCluster, - }, - { - testAppInterfaceProject, - testAppConfProject, - }, - } - - for _, tc := range cases { - inputResourceData := schema.TestResourceDataRaw(t, appFields(), tc.Input) - output, err := expandApp(inputResourceData) - if err != nil { - assert.FailNow(t, "[ERROR] on flattener: %#v", err) - } - assert.Equal(t, tc.ExpectedOutput, output, "Unexpected output from expander.") - } -} diff --git a/rancher2/structure_catalog.go b/rancher2/structure_catalog.go deleted file mode 100644 index 57f40cdf..00000000 --- a/rancher2/structure_catalog.go +++ /dev/null @@ -1,274 +0,0 @@ -package rancher2 - -import ( - "fmt" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" -) - -// Flatteners - -func flattenClusterCatalog(d *schema.ResourceData, in *managementClient.ClusterCatalog) error { - if in == nil { - return nil - } - - d.SetId(in.ID) - d.Set("name", in.Name) - d.Set("url", in.URL) - d.Set("description", in.Description) - d.Set("kind", in.Kind) - d.Set("branch", in.Branch) - - if len(in.Password) > 0 { - d.Set("password", in.Password) - } - - d.Set("username", in.Username) - d.Set("cluster_id", in.ClusterID) - - if len(in.HelmVersion) > 0 { - d.Set("version", in.HelmVersion) - } - - err := d.Set("annotations", toMapInterface(in.Annotations)) - if err != nil { - return err - } - - err = d.Set("labels", toMapInterface(in.Labels)) - if err != nil { - return err - } - - return nil -} - -func flattenGlobalCatalog(d *schema.ResourceData, in *managementClient.Catalog) error { - if in == nil { - return nil - } - - d.SetId(in.ID) - d.Set("name", in.Name) - d.Set("url", in.URL) - d.Set("description", in.Description) - d.Set("kind", in.Kind) - d.Set("branch", in.Branch) - - if len(in.Password) > 0 { - d.Set("password", in.Password) - } - - d.Set("username", in.Username) - - if len(in.HelmVersion) > 0 { - d.Set("version", in.HelmVersion) - } - - err := d.Set("annotations", toMapInterface(in.Annotations)) - if err != nil { - return err - } - - err = d.Set("labels", toMapInterface(in.Labels)) - if err != nil { - return err - } - - return nil -} - -func flattenProjectCatalog(d *schema.ResourceData, in *managementClient.ProjectCatalog) error { - if in == nil { - return nil - } - - d.SetId(in.ID) - d.Set("name", in.Name) - d.Set("url", in.URL) - d.Set("description", in.Description) - d.Set("kind", in.Kind) - d.Set("branch", in.Branch) - - if len(in.Password) > 0 { - d.Set("password", in.Password) - } - - d.Set("username", in.Username) - d.Set("project_id", in.ProjectID) - - if len(in.HelmVersion) > 0 { - d.Set("version", in.HelmVersion) - } - - err := d.Set("annotations", toMapInterface(in.Annotations)) - if err != nil { - return err - } - - err = d.Set("labels", toMapInterface(in.Labels)) - if err != nil { - return err - } - - return nil -} - -func flattenCatalog(d *schema.ResourceData, in interface{}) error { - if in == nil { - return nil - } - - scope := d.Get("scope").(string) - switch scope { - case catalogScopeCluster: - return flattenClusterCatalog(d, in.(*managementClient.ClusterCatalog)) - case catalogScopeGlobal: - return flattenGlobalCatalog(d, in.(*managementClient.Catalog)) - case catalogScopeProject: - return flattenProjectCatalog(d, in.(*managementClient.ProjectCatalog)) - default: - return fmt.Errorf("[ERROR] Unsupported scope on catalog: %s", scope) - } -} - -// Expanders - -func expandClusterCatalog(in *schema.ResourceData) *managementClient.ClusterCatalog { - obj := &managementClient.ClusterCatalog{} - - if in == nil { - return nil - } - - if v := in.Id(); len(v) > 0 { - obj.ID = v - } - - obj.Name = in.Get("name").(string) - obj.URL = in.Get("url").(string) - obj.Description = in.Get("description").(string) - obj.Kind = in.Get("kind").(string) - obj.Branch = in.Get("branch").(string) - obj.ClusterID = in.Get("cluster_id").(string) - - if v, ok := in.Get("password").(string); ok && len(v) > 0 { - obj.Password = v - } - if v, ok := in.Get("username").(string); ok && len(v) > 0 { - obj.Username = v - } - - if v, ok := in.Get("version").(string); ok && len(v) > 0 { - obj.HelmVersion = v - } - - if v, ok := in.Get("annotations").(map[string]interface{}); ok && len(v) > 0 { - obj.Annotations = toMapString(v) - } - - if v, ok := in.Get("labels").(map[string]interface{}); ok && len(v) > 0 { - obj.Labels = toMapString(v) - } - - return obj -} - -func expandGlobalCatalog(in *schema.ResourceData) *managementClient.Catalog { - obj := &managementClient.Catalog{} - - if in == nil { - return nil - } - - if v := in.Id(); len(v) > 0 { - obj.ID = v - } - - obj.Name = in.Get("name").(string) - obj.URL = in.Get("url").(string) - obj.Description = in.Get("description").(string) - obj.Kind = in.Get("kind").(string) - obj.Branch = in.Get("branch").(string) - - if v, ok := in.Get("password").(string); ok && len(v) > 0 { - obj.Password = v - } - if v, ok := in.Get("username").(string); ok && len(v) > 0 { - obj.Username = v - } - - if v, ok := in.Get("version").(string); ok && len(v) > 0 { - obj.HelmVersion = v - } - - if v, ok := in.Get("annotations").(map[string]interface{}); ok && len(v) > 0 { - obj.Annotations = toMapString(v) - } - - if v, ok := in.Get("labels").(map[string]interface{}); ok && len(v) > 0 { - obj.Labels = toMapString(v) - } - - return obj -} - -func expandProjectCatalog(in *schema.ResourceData) *managementClient.ProjectCatalog { - obj := &managementClient.ProjectCatalog{} - - if in == nil { - return nil - } - - if v := in.Id(); len(v) > 0 { - obj.ID = v - } - - obj.Name = in.Get("name").(string) - obj.URL = in.Get("url").(string) - obj.Description = in.Get("description").(string) - obj.Kind = in.Get("kind").(string) - obj.Branch = in.Get("branch").(string) - obj.ProjectID = in.Get("project_id").(string) - - if v, ok := in.Get("password").(string); ok && len(v) > 0 { - obj.Password = v - } - if v, ok := in.Get("username").(string); ok && len(v) > 0 { - obj.Username = v - } - - if v, ok := in.Get("version").(string); ok && len(v) > 0 { - obj.HelmVersion = v - } - - if v, ok := in.Get("annotations").(map[string]interface{}); ok && len(v) > 0 { - obj.Annotations = toMapString(v) - } - - if v, ok := in.Get("labels").(map[string]interface{}); ok && len(v) > 0 { - obj.Labels = toMapString(v) - } - - return obj -} - -func expandCatalog(in *schema.ResourceData) interface{} { - if in == nil { - return nil - } - - scope := in.Get("scope").(string) - switch scope { - case catalogScopeCluster: - return expandClusterCatalog(in) - case catalogScopeGlobal: - return expandGlobalCatalog(in) - case catalogScopeProject: - return expandProjectCatalog(in) - default: - return nil - } -} diff --git a/rancher2/structure_catalog_test.go b/rancher2/structure_catalog_test.go deleted file mode 100644 index 07dfd8fe..00000000 --- a/rancher2/structure_catalog_test.go +++ /dev/null @@ -1,137 +0,0 @@ -package rancher2 - -import ( - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" - "github.com/stretchr/testify/assert" -) - -var ( - testCatalogGlobalConf *managementClient.Catalog - testCatalogGlobalInterface map[string]interface{} - testCatalogClusterConf *managementClient.ClusterCatalog - testCatalogClusterInterface map[string]interface{} - testCatalogProjectConf *managementClient.ProjectCatalog - testCatalogProjectInterface map[string]interface{} -) - -func init() { - testCatalogGlobalConf = &managementClient.Catalog{ - Name: "catalog-test", - URL: "url", - Description: "description", - Kind: "kind", - Branch: "branch", - HelmVersion: "helm_v3", - } - testCatalogGlobalInterface = map[string]interface{}{ - "name": "catalog-test", - "url": "url", - "description": "description", - "kind": "kind", - "branch": "branch", - "scope": "global", - "version": "helm_v3", - } - testCatalogClusterConf = &managementClient.ClusterCatalog{ - Name: "catalog-test", - URL: "url", - Description: "description", - Kind: "kind", - Branch: "branch", - ClusterID: "cluster_id", - HelmVersion: "helm_v3", - } - testCatalogClusterInterface = map[string]interface{}{ - "name": "catalog-test", - "url": "url", - "description": "description", - "kind": "kind", - "branch": "branch", - "scope": "cluster", - "cluster_id": "cluster_id", - "version": "helm_v3", - } - testCatalogProjectConf = &managementClient.ProjectCatalog{ - Name: "catalog-test", - URL: "url", - Description: "description", - Kind: "kind", - Branch: "branch", - ProjectID: "project_id", - HelmVersion: "helm_v3", - } - testCatalogProjectInterface = map[string]interface{}{ - "name": "catalog-test", - "url": "url", - "description": "description", - "kind": "kind", - "branch": "branch", - "scope": "project", - "project_id": "project_id", - "version": "helm_v3", - } -} - -func TestFlattenCatalog(t *testing.T) { - - cases := []struct { - Input interface{} - ExpectedOutput map[string]interface{} - }{ - { - testCatalogGlobalConf, - testCatalogGlobalInterface, - }, - { - testCatalogClusterConf, - testCatalogClusterInterface, - }, - { - testCatalogProjectConf, - testCatalogProjectInterface, - }, - } - - for _, tc := range cases { - output := schema.TestResourceDataRaw(t, catalogFields(), tc.ExpectedOutput) - err := flattenCatalog(output, tc.Input) - if err != nil { - assert.FailNow(t, "[ERROR] on flattener: %#v", err) - } - expectedOutput := map[string]interface{}{} - for k := range tc.ExpectedOutput { - expectedOutput[k] = output.Get(k) - } - assert.Equal(t, tc.ExpectedOutput, expectedOutput, "Unexpected output from flattener.") - } -} - -func TestExpandCatalog(t *testing.T) { - - cases := []struct { - Input map[string]interface{} - ExpectedOutput interface{} - }{ - { - testCatalogGlobalInterface, - testCatalogGlobalConf, - }, - { - testCatalogClusterInterface, - testCatalogClusterConf, - }, - { - testCatalogProjectInterface, - testCatalogProjectConf, - }, - } - - for _, tc := range cases { - inputResourceData := schema.TestResourceDataRaw(t, catalogFields(), tc.Input) - output := expandCatalog(inputResourceData) - assert.Equal(t, tc.ExpectedOutput, output, "Unexpected output from expander.") - } -} diff --git a/rancher2/structure_multi_cluster_app.go b/rancher2/structure_multi_cluster_app.go deleted file mode 100644 index d29c062a..00000000 --- a/rancher2/structure_multi_cluster_app.go +++ /dev/null @@ -1,159 +0,0 @@ -package rancher2 - -import ( - "fmt" - "strings" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" -) - -const ( - MultiClusterAppTemplatePrefix = "cattle-global-data:" -) - -// Flatteners - -func flattenMultiClusterAppTemplateVersionID(d *schema.ResourceData, externalID string) string { - //Global catalog url: catalog://?catalog=demo&template=test&version=1.23.0 - - str := strings.TrimPrefix(externalID, AppTemplateExternalIDPrefix) - values := strings.Split(str, "&") - out := make(map[string]string, len(values)) - for _, v := range values { - pair := strings.Split(v, "=") - if len(pair) != 2 { - continue - } - out[pair[0]] = pair[1] - } - - d.Set("catalog_name", out["catalog"]) - d.Set("template_name", out["template"]) - d.Set("template_version", out["version"]) - - //Template version ID: cattle-global-data:test-test-1.23.0 - return MultiClusterAppTemplatePrefix + out["catalog"] + "-" + out["template"] + "-" + out["version"] -} - -func flattenMultiClusterApp(d *schema.ResourceData, in *managementClient.MultiClusterApp, externalID string) error { - if in == nil { - return fmt.Errorf("[ERROR] flattening multi cluster app: Input setting is nil") - } - - d.SetId(in.ID) - d.Set("name", in.Name) - - err := d.Set("roles", toArrayInterface(in.Roles)) - if err != nil { - return err - } - - err = d.Set("targets", flattenTargets(in.Targets)) - if err != nil { - return err - } - - d.Set("template_version_id", flattenMultiClusterAppTemplateVersionID(d, externalID)) - - err = d.Set("answers", flattenAnswers(in.Answers)) - if err != nil { - return err - } - - err = d.Set("members", flattenMembers(in.Members)) - if err != nil { - return err - } - - d.Set("revision_history_limit", int(in.RevisionHistoryLimit)) - - if in.Status != nil { - d.Set("revision_id", in.Status.RevisionID) - } - - err = d.Set("upgrade_strategy", flattenUpgradeStrategy(in.UpgradeStrategy)) - if err != nil { - return err - } - - err = d.Set("annotations", toMapInterface(in.Annotations)) - if err != nil { - return err - } - - err = d.Set("labels", toMapInterface(in.Labels)) - if err != nil { - return err - } - - return nil -} - -// Expanders - -func expandMultiClusterAppTemplateVersionID(in *schema.ResourceData) string { - //Template version ID: cattle-global-data:test-test-1.23.0 - - catalogName := in.Get("catalog_name").(string) - appName := in.Get("template_name").(string) - appVersion := in.Get("template_version").(string) - - return MultiClusterAppTemplatePrefix + catalogName + "-" + appName + "-" + appVersion -} - -func expandMultiClusterApp(in *schema.ResourceData) (*managementClient.MultiClusterApp, error) { - obj := &managementClient.MultiClusterApp{} - if in == nil { - return nil, fmt.Errorf("[ERROR] expanding multi cluster app: Input ResourceData is nil") - } - - if v := in.Id(); len(v) > 0 { - obj.ID = v - } - - obj.Name = in.Get("name").(string) - - if v, ok := in.Get("roles").([]interface{}); ok && len(v) > 0 { - obj.Roles = toArrayString(v) - } - - if v, ok := in.Get("targets").([]interface{}); ok && len(v) > 0 { - obj.Targets = expandTargets(v) - } - - obj.TemplateVersionID = expandMultiClusterAppTemplateVersionID(in) - - if v, ok := in.Get("answers").([]interface{}); ok && len(v) > 0 { - obj.Answers = expandAnswers(v) - } - - if v, ok := in.Get("members").([]interface{}); ok && len(v) > 0 { - obj.Members = expandMembers(v) - } - - if v, ok := in.Get("revision_history_limit").(int); ok && v > 0 { - obj.RevisionHistoryLimit = int64(v) - } - - if v, ok := in.Get("revision_id").(string); ok && len(v) > 0 { - if obj.Status == nil { - obj.Status = &managementClient.MultiClusterAppStatus{} - } - obj.Status.RevisionID = v - } - - if v, ok := in.Get("upgrade_strategy").([]interface{}); ok && len(v) > 0 { - obj.UpgradeStrategy = expandUpgradeStrategy(v) - } - - if v, ok := in.Get("annotations").(map[string]interface{}); ok && len(v) > 0 { - obj.Annotations = toMapString(v) - } - - if v, ok := in.Get("labels").(map[string]interface{}); ok && len(v) > 0 { - obj.Labels = toMapString(v) - } - - return obj, nil -} diff --git a/rancher2/structure_multi_cluster_app_test.go b/rancher2/structure_multi_cluster_app_test.go deleted file mode 100644 index f1c7acfd..00000000 --- a/rancher2/structure_multi_cluster_app_test.go +++ /dev/null @@ -1,200 +0,0 @@ -package rancher2 - -import ( - "reflect" - "testing" - - "github.com/hashicorp/terraform-plugin-sdk/helper/schema" - managementClient "github.com/rancher/rancher/pkg/client/generated/management/v3" - "github.com/stretchr/testify/assert" -) - -var ( - testMultiClusterAppTargetsConf []managementClient.Target - testMultiClusterAppTargetsInterface []interface{} - testMultiClusterAppAnswersConf []managementClient.Answer - testMultiClusterAppAnswersInterface []interface{} - testMultiClusterAppMembersConf []managementClient.Member - testMultiClusterAppMembersInterface []interface{} - testMultiClusterAppStatusConf *managementClient.MultiClusterAppStatus - testMultiClusterAppRollingUpdateConf *managementClient.RollingUpdate - testMultiClusterAppRollingUpdateInterface []interface{} - testMultiClusterAppUpgradeStrategyConf *managementClient.UpgradeStrategy - testMultiClusterAppUpgradeStrategyInterface []interface{} - testMultiClusterAppExternalID string - testMultiClusterAppConf *managementClient.MultiClusterApp - testMultiClusterAppInterface map[string]interface{} -) - -func init() { - testMultiClusterAppTargetsConf = []managementClient.Target{ - { - ProjectID: "project_id", - AppID: "app_id", - Healthstate: "health_state", - State: "state", - }, - } - testMultiClusterAppTargetsInterface = []interface{}{ - map[string]interface{}{ - "project_id": "project_id", - "app_id": "app_id", - "health_state": "health_state", - "state": "state", - }, - } - testMultiClusterAppAnswersConf = []managementClient.Answer{ - { - ClusterID: "cluster_id", - ProjectID: "project_id", - Values: map[string]string{ - "value1": "one", - "value2": "two", - }, - }, - } - testMultiClusterAppAnswersInterface = []interface{}{ - map[string]interface{}{ - "cluster_id": "cluster_id", - "project_id": "project_id", - "values": map[string]interface{}{ - "value1": "one", - "value2": "two", - }, - }, - } - testMultiClusterAppMembersConf = []managementClient.Member{ - { - AccessType: "access_type", - GroupPrincipalID: "group_principal_id", - UserPrincipalID: "user_principal_id", - }, - } - testMultiClusterAppMembersInterface = []interface{}{ - map[string]interface{}{ - "access_type": "access_type", - "group_principal_id": "group_principal_id", - "user_principal_id": "user_principal_id", - }, - } - testMultiClusterAppStatusConf = &managementClient.MultiClusterAppStatus{ - RevisionID: "revision_id", - } - testMultiClusterAppRollingUpdateConf = &managementClient.RollingUpdate{ - BatchSize: 10, - Interval: 10, - } - testMultiClusterAppRollingUpdateInterface = []interface{}{ - map[string]interface{}{ - "batch_size": 10, - "interval": 10, - }, - } - testMultiClusterAppUpgradeStrategyConf = &managementClient.UpgradeStrategy{ - RollingUpdate: testMultiClusterAppRollingUpdateConf, - } - testMultiClusterAppUpgradeStrategyInterface = []interface{}{ - map[string]interface{}{ - "rolling_update": testMultiClusterAppRollingUpdateInterface, - }, - } - testMultiClusterAppExternalID = "catalog://?catalog=test&template=test-demo&version=1.23.0" - testMultiClusterAppConf = &managementClient.MultiClusterApp{ - Name: "foo", - Targets: testMultiClusterAppTargetsConf, - TemplateVersionID: "cattle-global-data:test-test-demo-1.23.0", - Answers: testMultiClusterAppAnswersConf, - Members: testMultiClusterAppMembersConf, - RevisionHistoryLimit: 10, - Status: testMultiClusterAppStatusConf, - Roles: []string{ - "role1", - "role2", - }, - UpgradeStrategy: testMultiClusterAppUpgradeStrategyConf, - Annotations: map[string]string{ - "node_one": "one", - "node_two": "two", - }, - Labels: map[string]string{ - "option1": "value1", - "option2": "value2", - }, - } - testMultiClusterAppInterface = map[string]interface{}{ - "catalog_name": "test", - "name": "foo", - "targets": testMultiClusterAppTargetsInterface, - "template_name": "test-demo", - "answers": testMultiClusterAppAnswersInterface, - "members": testMultiClusterAppMembersInterface, - "revision_history_limit": 10, - "revision_id": "revision_id", - "roles": []interface{}{ - "role1", - "role2", - }, - "template_version": "1.23.0", - "upgrade_strategy": testMultiClusterAppUpgradeStrategyInterface, - "annotations": map[string]interface{}{ - "node_one": "one", - "node_two": "two", - }, - "labels": map[string]interface{}{ - "option1": "value1", - "option2": "value2", - }, - } -} - -func TestFlattenMultiClusterApp(t *testing.T) { - - cases := []struct { - Input *managementClient.MultiClusterApp - ExpectedOutput map[string]interface{} - }{ - { - testMultiClusterAppConf, - testMultiClusterAppInterface, - }, - } - - for _, tc := range cases { - output := schema.TestResourceDataRaw(t, multiClusterAppFields(), map[string]interface{}{}) - err := flattenMultiClusterApp(output, tc.Input, testMultiClusterAppExternalID) - if err != nil { - assert.FailNow(t, "[ERROR] on flattener: %#v", err) - } - expectedOutput := map[string]interface{}{} - for k := range tc.ExpectedOutput { - expectedOutput[k] = output.Get(k) - } - if !reflect.DeepEqual(expectedOutput, tc.ExpectedOutput) { - assert.FailNow(t, "Unexpected output from flattener.\nExpected: %#v\nGiven: %#v", tc.ExpectedOutput, expectedOutput) - } - } -} - -func TestExpandMultiClusterApp(t *testing.T) { - - cases := []struct { - Input map[string]interface{} - ExpectedOutput *managementClient.MultiClusterApp - }{ - { - testMultiClusterAppInterface, - testMultiClusterAppConf, - }, - } - - for _, tc := range cases { - inputResourceData := schema.TestResourceDataRaw(t, multiClusterAppFields(), tc.Input) - output, err := expandMultiClusterApp(inputResourceData) - if err != nil { - assert.FailNow(t, "[ERROR] on expander: %#v", err) - } - if !reflect.DeepEqual(output, tc.ExpectedOutput) { - assert.FailNow(t, "Unexpected output from expander.\nExpected: %#v\nGiven: %#v", tc.ExpectedOutput, output) - } - } -} diff --git a/rancher2/util.go b/rancher2/util.go index 8a206a2f..1106ce9a 100644 --- a/rancher2/util.go +++ b/rancher2/util.go @@ -459,27 +459,6 @@ func splitAppID(id string) (projectID, appID string, err error) { return fields[0] + separator + fields[1], fields[1] + separator + fields[2], nil } -func updateVersionExternalID(externalID, version string) string { - //Global catalog url: catalog://?catalog=demo&template=test&version=1.23.0 - //Cluster catalog url: catalog://?catalog=c-XXXXX/test&type=clusterCatalog&template=test&version=1.23.0 - //Project catalog url: catalog://?catalog=p-XXXXX/test&type=projectCatalog&template=test&version=1.23.0 - - str := strings.TrimPrefix(externalID, AppTemplateExternalIDPrefix) - values := strings.Split(str, "&") - out := AppTemplateExternalIDPrefix - for _, v := range values { - if strings.Contains(v, "version=") { - pair := strings.Split(v, "=") - if pair[0] == "version" { - pair[1] = version - } - v = pair[0] + "=" + pair[1] - } - out = out + "&" + v - } - return out -} - func toArrayString(in []interface{}) []string { out := make([]string, len(in)) for i, v := range in { diff --git a/website/rancher2.erb b/website/rancher2.erb index 7eccc6d0..3f1f8221 100644 --- a/website/rancher2.erb +++ b/website/rancher2.erb @@ -13,12 +13,6 @@ > Data Sources