|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of kubernetes cluster addons. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_kubernetes_charts" "name" {} |
| 8 | +``` |
| 9 | +*/ |
| 10 | +package tencentcloud |
| 11 | + |
| 12 | +import ( |
| 13 | + "context" |
| 14 | + "encoding/json" |
| 15 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 16 | + tke "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tke/v20180525" |
| 17 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 18 | +) |
| 19 | + |
| 20 | +func dataSourceTencentCloudKubernetesCharts() *schema.Resource { |
| 21 | + return &schema.Resource{ |
| 22 | + Read: dataSourceTencentCloudKubernetesChartsRead, |
| 23 | + Schema: map[string]*schema.Schema{ |
| 24 | + "kind": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Description: "Kind of app chart. Available values: `log`, `scheduler`, `network`, `storage`, `monitor`, `dns`, `image`, `other`, `invisible`.", |
| 28 | + }, |
| 29 | + "arch": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Optional: true, |
| 32 | + Description: "Operation system app supported. Available values: `arm32`, `arm64`, `amd64`.", |
| 33 | + }, |
| 34 | + "cluster_type": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "Cluster type. Available values: `tke`, `eks`.", |
| 38 | + }, |
| 39 | + "result_output_file": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Optional: true, |
| 42 | + Description: "Used to save results.", |
| 43 | + }, |
| 44 | + "chart_list": { |
| 45 | + Type: schema.TypeList, |
| 46 | + Computed: true, |
| 47 | + Description: "App chart list.", |
| 48 | + Elem: &schema.Resource{ |
| 49 | + Schema: map[string]*schema.Schema{ |
| 50 | + "name": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Computed: true, |
| 53 | + Description: "Name of chart.", |
| 54 | + }, |
| 55 | + "label": { |
| 56 | + Type: schema.TypeMap, |
| 57 | + Computed: true, |
| 58 | + Description: "Label of chart.", |
| 59 | + }, |
| 60 | + "latest_version": { |
| 61 | + Type: schema.TypeString, |
| 62 | + Computed: true, |
| 63 | + Description: "Chart latest version.", |
| 64 | + }, |
| 65 | + }, |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func dataSourceTencentCloudKubernetesChartsRead(d *schema.ResourceData, meta interface{}) error { |
| 73 | + defer logElapsed("data_source.tencentcloud_kubernetes_charts.read")() |
| 74 | + |
| 75 | + logId := getLogId(contextNil) |
| 76 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 77 | + client := meta.(*TencentCloudClient).apiV3Conn |
| 78 | + service := TkeService{client: client} |
| 79 | + |
| 80 | + var ( |
| 81 | + kind = d.Get("kind").(string) |
| 82 | + arch = d.Get("arch").(string) |
| 83 | + clusterType = d.Get("cluster_type").(string) |
| 84 | + ) |
| 85 | + |
| 86 | + request := tke.NewGetTkeAppChartListRequest() |
| 87 | + if kind != "" { |
| 88 | + request.Kind = &kind |
| 89 | + } |
| 90 | + |
| 91 | + if arch != "" { |
| 92 | + request.Arch = &arch |
| 93 | + } |
| 94 | + |
| 95 | + if clusterType != "" { |
| 96 | + request.ClusterType = &clusterType |
| 97 | + } |
| 98 | + |
| 99 | + response, err := service.GetTkeAppChartList(ctx, request) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + chartList := make([]interface{}, 0) |
| 105 | + |
| 106 | + for i := range response { |
| 107 | + item := response[i] |
| 108 | + chart := map[string]interface{}{ |
| 109 | + "name": item.Name, |
| 110 | + "latest_version": item.LatestVersion, |
| 111 | + } |
| 112 | + |
| 113 | + label := make(map[string]interface{}) |
| 114 | + |
| 115 | + if err := json.Unmarshal([]byte(*item.Label), &label); err != nil { |
| 116 | + return err |
| 117 | + } |
| 118 | + |
| 119 | + chart["label"] = label |
| 120 | + |
| 121 | + chartList = append(chartList, chart) |
| 122 | + } |
| 123 | + |
| 124 | + err = d.Set("chart_list", chartList) |
| 125 | + |
| 126 | + if err != nil { |
| 127 | + return err |
| 128 | + } |
| 129 | + |
| 130 | + output, ok := d.GetOk("result_output_file") |
| 131 | + if ok && output.(string) != "" { |
| 132 | + err := writeToFile(output.(string), chartList) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + ids := []string{kind, arch, clusterType} |
| 139 | + d.SetId("app_chart_" + helper.DataResourceIdsHash(ids)) |
| 140 | + |
| 141 | + return nil |
| 142 | +} |
0 commit comments