Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add data_source huaweicloud_cce_addon #1071

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions docs/data-sources/cce_addon.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
---
subcategory: "Cloud Container Engine (CCE)"
---

# huaweicloud\_cce\_addon

To get the specified addon in a cluster.

## Example Usage

```hcl
variable "cluster_id" { }
variable "tempalte_name" { }

data "huaweicloud_cce_addon" "addon" {
cluster_id = var.cluster_id
name = var.tempalte_name
}
```
## Argument Reference

The following arguments are supported:

* `region` - (Optional, String) The region in which to create the cce addon resource.
If omitted, the provider-level region will be used.
* `cluster_id` - (Required, String) Specifies the ID of the cluster.
* `addon_id` - (Optional, String) Specifies the ID of the addon.
* `template_name` - (Optional, String) Specifies the name of the addon template.
* `version` - (Optional, String) Specifies the version of the addon.
* `status` - (Optional, String) Specifies the addon status.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - ID of the addon instance.
* `description` - Description of addon instance.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.14
require (
github.com/hashicorp/errwrap v1.0.0
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/huaweicloud/golangsdk v0.0.0-20210417091247-f78a81171753
github.com/huaweicloud/golangsdk v0.0.0-20210420021131-cc06e265cc39
github.com/jen20/awspolicyequivalence v1.1.0
github.com/smartystreets/goconvey v0.0.0-20190222223459-a17d461953aa // indirect
github.com/stretchr/testify v1.4.0
Expand Down
8 changes: 2 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,8 @@ github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/huaweicloud/golangsdk v0.0.0-20210408110332-c61d9b336da5 h1:qsDthJ6oyB3Jexl1NTK/d8LePQL+zM7dIVyeeSpSmBs=
github.com/huaweicloud/golangsdk v0.0.0-20210408110332-c61d9b336da5/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210412080213-f1dcb144bd25 h1:nal36cQv+dJUErG3UCFqdQFWdYjAZw9XqJP4Xwnoxf0=
github.com/huaweicloud/golangsdk v0.0.0-20210412080213-f1dcb144bd25/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210417091247-f78a81171753 h1:ogihIkqOr4NqphAjbdgZb7di7HzVU9lD9ZcQEOnLrJI=
github.com/huaweicloud/golangsdk v0.0.0-20210417091247-f78a81171753/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/huaweicloud/golangsdk v0.0.0-20210420021131-cc06e265cc39 h1:QFIWCqLimeHn9WO/nVlNSWwu8vm2UqbbIp5I2UQoFMk=
github.com/huaweicloud/golangsdk v0.0.0-20210420021131-cc06e265cc39/go.mod h1:fcOI5u+0f62JtJd7zkCch/Z57BNC6bhqb32TKuiF4r0=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/imdario/mergo v0.3.9 h1:UauaLniWCFHWd+Jp9oCEkTBj8VO/9DKg3PV3VCNMDIg=
github.com/imdario/mergo v0.3.9/go.mod h1:2EnlNZ0deacrJVfApfmtdGgDfMuh/nq6Ok1EcJh5FfA=
Expand Down
90 changes: 90 additions & 0 deletions huaweicloud/data_source_huaweicloud_cce_addon_v3.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
package huaweicloud

import (
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/huaweicloud/golangsdk/openstack/cce/v3/addons"
"github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config"
)

func DataSourceCCEAddonV3() *schema.Resource {
return &schema.Resource{
Read: dataSourceCceAddonsV3Read,

Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"cluster_id": {
Type: schema.TypeString,
Required: true,
},
"addon_id": {
Type: schema.TypeString,
Optional: true,
},
"template_name": {
Type: schema.TypeString,
Optional: true,
},
"version": {
Type: schema.TypeString,
Optional: true,
},
"status": {
Type: schema.TypeString,
Optional: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceCceAddonsV3Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*config.Config)
cceClient, err := config.CceAddonV3Client(GetRegion(d, config))
if err != nil {
return fmt.Errorf("unable to create HuaweiCloud CCE client : %s", err)
}

listOpts := addons.ListOpts{
Uid: d.Get("addon_id").(string),
AddonTemplateName: d.Get("template_name").(string),
Version: d.Get("version").(string),
Status: d.Get("status").(string),
}

refinedAddons, err := addons.List(cceClient, d.Get("cluster_id").(string), listOpts)

if err != nil {
return fmt.Errorf("unable to retrieve Addons: %s", err)
}

if len(refinedAddons) < 1 {
return fmt.Errorf("your query returned no results, please change your search criteria and try again")
}

if len(refinedAddons) > 1 {
return fmt.Errorf("your query returned more than one result, please try a more specific search criteria")
}

Addon := refinedAddons[0]

log.Printf("[DEBUG] Retrieved Nodes using given filter %s: %+v", Addon.Metadata.Id, Addon)
d.SetId(Addon.Metadata.Id)
d.Set("addon_id", Addon.Metadata.Id)
d.Set("template_name", Addon.Spec.AddonTemplateName)
d.Set("version", Addon.Spec.Version)
d.Set("status", Addon.Status.Status)
d.Set("region", GetRegion(d, config))
d.Set("description", Addon.Spec.Description)

return nil
}
55 changes: 55 additions & 0 deletions huaweicloud/data_source_huaweicloud_cce_addon_v3_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package huaweicloud

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccCCEAddonV3DataSource_basic(t *testing.T) {
rName := fmt.Sprintf("tf-acc-test-%s", acctest.RandString(5))
resourceName := "data.huaweicloud_cce_addon.test"

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCCEAddonV3DataSource_basic(rName),
Check: resource.ComposeTestCheckFunc(
testAccCheckCCEAddonV3DataSourceID(resourceName),
resource.TestCheckResourceAttr(resourceName, "template_name", "metrics-server"),
),
},
},
})
}

func testAccCheckCCEAddonV3DataSourceID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find Addons data source: %s ", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("Addon data source ID not set ")
}

return nil
}
}

func testAccCCEAddonV3DataSource_basic(rName string) string {
return fmt.Sprintf(`
%s

data "huaweicloud_cce_addon" "test" {
cluster_id = huaweicloud_cce_cluster.test.id
template_name = huaweicloud_cce_addon.test.template_name
}
`, testAccCCEAddonV3_basic(rName))
}
1 change: 1 addition & 0 deletions huaweicloud/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,6 +260,7 @@ func Provider() terraform.ResourceProvider {
"huaweicloud_cce_cluster": DataSourceCCEClusterV3(),
"huaweicloud_cce_node": DataSourceCCENodeV3(),
"huaweicloud_cce_node_pool": DataSourceCCENodePoolV3(),
"huaweicloud_cce_addon": DataSourceCCEAddonV3(),
"huaweicloud_cdm_flavors": dataSourceCdmFlavorV1(),
"huaweicloud_compute_flavors": DataSourceEcsFlavors(),
"huaweicloud_compute_instance": DataSourceComputeInstance(),
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading