|
| 1 | +/* |
| 2 | +Provides an available EMR for the user. |
| 3 | +
|
| 4 | +The EMR data source fetch proper EMR from user's EMR pool. |
| 5 | +
|
| 6 | +Example Usage |
| 7 | +
|
| 8 | +```hcl |
| 9 | +data "tencentcloud_emr" "my_emr" { |
| 10 | + filter { |
| 11 | + name = "address-status" |
| 12 | + values = ["UNBIND"] |
| 13 | + } |
| 14 | +} |
| 15 | +``` |
| 16 | +*/ |
| 17 | +package tencentcloud |
| 18 | + |
| 19 | +import ( |
| 20 | + "context" |
| 21 | + "log" |
| 22 | + |
| 23 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 24 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 25 | + emr "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/emr/v20190103" |
| 26 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 27 | +) |
| 28 | + |
| 29 | +func dataSourceTencentCloudEmr() *schema.Resource { |
| 30 | + return &schema.Resource{ |
| 31 | + Read: dataSourceTencentCloudEmrRead, |
| 32 | + |
| 33 | + Schema: map[string]*schema.Schema{ |
| 34 | + "display_strategy": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + Description: "Display strategy(e.g.:clusterList, monitorManage).", |
| 38 | + }, |
| 39 | + "prefix_instance_ids": { |
| 40 | + Type: schema.TypeList, |
| 41 | + Optional: true, |
| 42 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 43 | + Description: "fetch all instances with same prefix(e.g.:emr-xxxxxx).", |
| 44 | + }, |
| 45 | + "project_id": { |
| 46 | + Type: schema.TypeInt, |
| 47 | + Optional: true, |
| 48 | + Description: "Fetch all instances which owner same project. Default 0 meaning use default project id.", |
| 49 | + }, |
| 50 | + "result_output_file": { |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Description: "Used to save results.", |
| 54 | + }, |
| 55 | + "clusters": { |
| 56 | + Type: schema.TypeList, |
| 57 | + Computed: true, |
| 58 | + Description: "A list of clusters will be exported and its every element contains the following attributes:", |
| 59 | + Elem: &schema.Resource{ |
| 60 | + Schema: map[string]*schema.Schema{ |
| 61 | + "id": { |
| 62 | + Type: schema.TypeInt, |
| 63 | + Computed: true, |
| 64 | + Description: "Id of instance.", |
| 65 | + }, |
| 66 | + "cluster_id": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Cluster id of instance.", |
| 70 | + }, |
| 71 | + "ftitle": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Title of instance.", |
| 75 | + }, |
| 76 | + "cluster_name": { |
| 77 | + Type: schema.TypeString, |
| 78 | + Computed: true, |
| 79 | + Description: "Cluster name of instance.", |
| 80 | + }, |
| 81 | + "region_id": { |
| 82 | + Type: schema.TypeInt, |
| 83 | + Computed: true, |
| 84 | + Description: "Region id of instance.", |
| 85 | + }, |
| 86 | + "zone_id": { |
| 87 | + Type: schema.TypeInt, |
| 88 | + Computed: true, |
| 89 | + Description: "Zone id of instance.", |
| 90 | + }, |
| 91 | + "zone": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "Zone of instance.", |
| 95 | + }, |
| 96 | + "master_ip": { |
| 97 | + Type: schema.TypeString, |
| 98 | + Computed: true, |
| 99 | + Description: "Master ip of instance.", |
| 100 | + }, |
| 101 | + "project_id": { |
| 102 | + Type: schema.TypeInt, |
| 103 | + Computed: true, |
| 104 | + Description: "Project id of instance.", |
| 105 | + }, |
| 106 | + "charge_type": { |
| 107 | + Type: schema.TypeInt, |
| 108 | + Computed: true, |
| 109 | + Description: "Charge type of instance.", |
| 110 | + }, |
| 111 | + "status": { |
| 112 | + Type: schema.TypeInt, |
| 113 | + Computed: true, |
| 114 | + Description: "Status of instance.", |
| 115 | + }, |
| 116 | + "add_time": { |
| 117 | + Type: schema.TypeString, |
| 118 | + Computed: true, |
| 119 | + Description: "Add time of instance.", |
| 120 | + }, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + }, |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +func dataSourceTencentCloudEmrRead(d *schema.ResourceData, meta interface{}) error { |
| 129 | + defer logElapsed("data_source.tencentcloud_emr.read")() |
| 130 | + |
| 131 | + logId := getLogId(contextNil) |
| 132 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 133 | + |
| 134 | + emrServer := EMRService{ |
| 135 | + client: meta.(*TencentCloudClient).apiV3Conn, |
| 136 | + } |
| 137 | + |
| 138 | + filters := map[string]interface{}{} |
| 139 | + if v, ok := d.GetOk("display_strategy"); ok { |
| 140 | + filters["display_strategy"] = v.(string) |
| 141 | + } |
| 142 | + if v, ok := d.GetOk("prefix_instance_ids"); ok { |
| 143 | + filters["prefix_instance_ids"] = v.(string) |
| 144 | + } |
| 145 | + if v, ok := d.GetOk("project_id"); ok { |
| 146 | + filters["project_id"] = v.(int64) |
| 147 | + } |
| 148 | + var clusters []*emr.ClusterInstancesInfo |
| 149 | + var errRet error |
| 150 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 151 | + clusters, errRet = emrServer.DescribeInstances(ctx, filters) |
| 152 | + if errRet != nil { |
| 153 | + return retryError(errRet, InternalError) |
| 154 | + } |
| 155 | + return nil |
| 156 | + }) |
| 157 | + if err != nil { |
| 158 | + return err |
| 159 | + } |
| 160 | + |
| 161 | + emr_instances := make([]map[string]interface{}, 0, len(clusters)) |
| 162 | + ids := make([]string, 0, len(clusters)) |
| 163 | + for _, cluster := range clusters { |
| 164 | + mapping := map[string]interface{}{ |
| 165 | + "cluster_id": cluster.ClusterId, |
| 166 | + "cluster_name": cluster.ClusterName, |
| 167 | + "ftitle": cluster.Ftitle, |
| 168 | + "status": cluster.Status, |
| 169 | + "region_id": cluster.RegionId, |
| 170 | + "zone_id": cluster.ZoneId, |
| 171 | + "zone": cluster.Zone, |
| 172 | + "charge_type": cluster.ChargeType, |
| 173 | + "master_ip": cluster.MasterIp, |
| 174 | + "add_time": cluster.AddTime, |
| 175 | + } |
| 176 | + emr_instances = append(emr_instances, mapping) |
| 177 | + ids = append(ids, (string)(*cluster.Id)) |
| 178 | + } |
| 179 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 180 | + err = d.Set("clusters", emr_instances) |
| 181 | + if err != nil { |
| 182 | + log.Printf("[CRITAL]%s provider set cluster list fail, reason:%s\n ", logId, err.Error()) |
| 183 | + return err |
| 184 | + } |
| 185 | + output, ok := d.GetOk("result_output_file") |
| 186 | + if ok && output.(string) != "" { |
| 187 | + if err := writeToFile(output.(string), emr_instances); err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + } |
| 191 | + return nil |
| 192 | +} |
0 commit comments