diff --git a/docs/data-sources/cce_charts.md b/docs/data-sources/cce_charts.md new file mode 100644 index 0000000000..40e7b5052e --- /dev/null +++ b/docs/data-sources/cce_charts.md @@ -0,0 +1,62 @@ +--- +subcategory: "Cloud Container Engine (CCE)" +layout: "huaweicloud" +page_title: "HuaweiCloud: huaweicloud_cce_charts" +description: |- + Use this data source to get the list of CCE charts within HuaweiCloud. +--- + +# huaweicloud_cce_charts + +Use this data source to get the list of CCE charts within HuaweiCloud. + +## Example Usage + +```hcl +data "huaweicloud_cce_charts" "test" {} +``` + +## Argument Reference + +The following arguments are supported: + +* `region` - (Optional, String) Specifies the region in which to obtain the CCE cluster certificate. If omitted, the + provider-level region will be used. + +## Attribute Reference + +In addition to all arguments above, the following attributes are exported: + +* `id` - The data source ID in UUID format. + +* `charts` - The list of charts. + The [charts](#CCE_charts) structure is documented below. + + +The `charts` block supports: + +* `id` - The chart ID. + +* `name` - The chart name. + +* `values` - The values of the chart. + +* `translate` - The traslate source of the chart. + +* `instruction` - The instruction of the chart. + +* `version` - The chart version. + +* `description` - The description of the chart. + +* `source` - The source of the chart. + +* `icon_url` - The icon URL. + +* `public` - Whether the chart is public. + +* `chart_url` - The chart URL. + +* `created_at` - The create time. + +* `updated_at` - The update time. diff --git a/huaweicloud/provider.go b/huaweicloud/provider.go index 841d9e2554..eb5231646f 100644 --- a/huaweicloud/provider.go +++ b/huaweicloud/provider.go @@ -522,7 +522,9 @@ func Provider() *schema.Provider { "huaweicloud_cce_node": cce.DataSourceNode(), "huaweicloud_cce_nodes": cce.DataSourceNodes(), "huaweicloud_cce_node_pool": cce.DataSourceCCENodePoolV3(), - "huaweicloud_cci_namespaces": cci.DataSourceCciNamespaces(), + "huaweicloud_cce_charts": cce.DataSourceCCECharts(), + + "huaweicloud_cci_namespaces": cci.DataSourceCciNamespaces(), "huaweicloud_ccm_certificates": ccm.DataSourceCertificates(), "huaweicloud_ccm_certificate_export": ccm.DataSourceCertificateExport(), diff --git a/huaweicloud/services/acceptance/cce/data_source_huaweicloud_cce_charts_test.go b/huaweicloud/services/acceptance/cce/data_source_huaweicloud_cce_charts_test.go new file mode 100644 index 0000000000..1c5010e13e --- /dev/null +++ b/huaweicloud/services/acceptance/cce/data_source_huaweicloud_cce_charts_test.go @@ -0,0 +1,37 @@ +package cce + +import ( + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/services/acceptance" +) + +func TestAccChartsDataSource_basic(t *testing.T) { + datasourceName := "data.huaweicloud_cce_charts.test" + dc := acceptance.InitDataSourceCheck(datasourceName) + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.TestAccPreCheck(t) }, + ProviderFactories: acceptance.TestAccProviderFactories, + CheckDestroy: dc.CheckResourceDestroy(), + Steps: []resource.TestStep{ + { + Config: testCharts_basic, + Check: resource.ComposeTestCheckFunc( + dc.CheckResourceExists(), + resource.TestCheckOutput("is_results_not_empty", "true"), + ), + }, + }, + }) +} + +const testCharts_basic = ` +data "huaweicloud_cce_charts" "test" {} + +output "is_results_not_empty" { + value = length(data.huaweicloud_cce_charts.test.charts) > 0 +} +` diff --git a/huaweicloud/services/cce/data_source_huaweicloud_cce_charts.go b/huaweicloud/services/cce/data_source_huaweicloud_cce_charts.go new file mode 100644 index 0000000000..6171bd02f8 --- /dev/null +++ b/huaweicloud/services/cce/data_source_huaweicloud_cce_charts.go @@ -0,0 +1,161 @@ +package cce + +import ( + "context" + + "github.com/hashicorp/go-multierror" + "github.com/hashicorp/go-uuid" + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + + "github.com/chnsz/golangsdk" + + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/config" + "github.com/huaweicloud/terraform-provider-huaweicloud/huaweicloud/utils" +) + +// @API CCE GET /v2/charts +func DataSourceCCECharts() *schema.Resource { + return &schema.Resource{ + ReadContext: dataSourceCCEChartsRead, + + Schema: map[string]*schema.Schema{ + "region": { + Type: schema.TypeString, + Optional: true, + Computed: true, + }, + "charts": { + Type: schema.TypeList, + Computed: true, + Elem: &schema.Resource{ + Schema: map[string]*schema.Schema{ + "id": { + Type: schema.TypeString, + Computed: true, + }, + "name": { + Type: schema.TypeString, + Computed: true, + }, + "values": { + Type: schema.TypeString, + Computed: true, + }, + "translate": { + Type: schema.TypeString, + Computed: true, + }, + "instruction": { + Type: schema.TypeString, + Computed: true, + }, + "version": { + Type: schema.TypeString, + Computed: true, + }, + "description": { + Type: schema.TypeString, + Computed: true, + }, + "source": { + Type: schema.TypeString, + Computed: true, + }, + "icon_url": { + Type: schema.TypeString, + Computed: true, + }, + "public": { + Type: schema.TypeBool, + Computed: true, + }, + "chart_url": { + Type: schema.TypeString, + Computed: true, + }, + "created_at": { + Type: schema.TypeString, + Computed: true, + }, + "updated_at": { + Type: schema.TypeString, + Computed: true, + }, + }, + }, + }, + }, + } +} + +func dataSourceCCEChartsRead(_ context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { + cfg := meta.(*config.Config) + region := cfg.GetRegion(d) + + var mErr *multierror.Error + + var ( + getChartsHttpUrl = "v2/charts" + getChartsProduct = "cce" + ) + getChartsClient, err := cfg.NewServiceClient(getChartsProduct, region) + if err != nil { + return diag.Errorf("error creating CCE client: %s", err) + } + + getChartsHttpPath := getChartsClient.Endpoint + getChartsHttpUrl + + getChartsOpt := golangsdk.RequestOpts{ + KeepResponseBody: true, + } + + getChartsResp, err := getChartsClient.Request("GET", getChartsHttpPath, &getChartsOpt) + if err != nil { + return diag.Errorf("error retrieving CCE charts: %s", err) + } + + getChartsRespBody, err := utils.FlattenResponse(getChartsResp) + if err != nil { + return diag.FromErr(err) + } + + dataSourceId, err := uuid.GenerateUUID() + if err != nil { + return diag.Errorf("unable to generate ID: %s", err) + } + d.SetId(dataSourceId) + + mErr = multierror.Append( + mErr, + d.Set("charts", flattenCharts(getChartsRespBody)), + ) + + return diag.FromErr(mErr.ErrorOrNil()) +} + +func flattenCharts(resp interface{}) []map[string]interface{} { + charts := resp.([]interface{}) + if len(charts) == 0 { + return nil + } + res := make([]map[string]interface{}, len(charts)) + for i, chart := range charts { + res[i] = map[string]interface{}{ + "id": utils.PathSearch("id", chart, nil), + "name": utils.PathSearch("name", chart, nil), + "values": utils.PathSearch("values", chart, nil), + "translate": utils.PathSearch("translate", chart, nil), + "instruction": utils.PathSearch("instruction", chart, nil), + "version": utils.PathSearch("version", chart, nil), + "description": utils.PathSearch("description", chart, nil), + "source": utils.PathSearch("source", chart, nil), + "icon_url": utils.PathSearch("icon_url", chart, nil), + "public": utils.PathSearch("public", chart, nil), + "chart_url": utils.PathSearch("chart_url", chart, nil), + "created_at": utils.PathSearch("create_at", chart, nil), + "updated_at": utils.PathSearch("update_at", chart, nil), + } + } + return res +}