|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tdmq environment_attributes |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tdmq_environment_attributes" "environment_attributes" { |
| 8 | + environment_id = "keep-ns" |
| 9 | + cluster_id = "pulsar-9n95ax58b9vn" |
| 10 | +} |
| 11 | +``` |
| 12 | +*/ |
| 13 | +package tencentcloud |
| 14 | + |
| 15 | +import ( |
| 16 | + "context" |
| 17 | + |
| 18 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 19 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 20 | + tdmq "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tdmq/v20200217" |
| 21 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 22 | +) |
| 23 | + |
| 24 | +func dataSourceTencentCloudTdmqEnvironmentAttributes() *schema.Resource { |
| 25 | + return &schema.Resource{ |
| 26 | + Read: dataSourceTencentCloudTdmqEnvironmentAttributesRead, |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "environment_id": { |
| 29 | + Required: true, |
| 30 | + Type: schema.TypeString, |
| 31 | + Description: "Environment (namespace) name.", |
| 32 | + }, |
| 33 | + "cluster_id": { |
| 34 | + Optional: true, |
| 35 | + Type: schema.TypeString, |
| 36 | + Description: "ID of the Pulsar cluster.", |
| 37 | + }, |
| 38 | + // computed |
| 39 | + "msg_ttl": { |
| 40 | + Computed: true, |
| 41 | + Type: schema.TypeInt, |
| 42 | + Description: "Expiration time of unconsumed messages, unit second, maximum 1296000 (15 days).", |
| 43 | + }, |
| 44 | + "rate_in_byte": { |
| 45 | + Computed: true, |
| 46 | + Type: schema.TypeInt, |
| 47 | + Description: "Consumption rate limit, unit byte/second, 0 unlimited rate.", |
| 48 | + }, |
| 49 | + "rate_in_size": { |
| 50 | + Computed: true, |
| 51 | + Type: schema.TypeInt, |
| 52 | + Description: "Consumption rate limit, unit number/second, 0 is unlimited.", |
| 53 | + }, |
| 54 | + "retention_hours": { |
| 55 | + Computed: true, |
| 56 | + Type: schema.TypeInt, |
| 57 | + Description: "Consumed message storage policy, unit hour, 0 will be deleted immediately after consumption.", |
| 58 | + }, |
| 59 | + "retention_size": { |
| 60 | + Computed: true, |
| 61 | + Type: schema.TypeInt, |
| 62 | + Description: "Consumed message storage strategy, unit G, 0 Delete immediately after consumption.", |
| 63 | + }, |
| 64 | + "replicas": { |
| 65 | + Computed: true, |
| 66 | + Type: schema.TypeInt, |
| 67 | + Description: "Duplicate number.", |
| 68 | + }, |
| 69 | + "remark": { |
| 70 | + Computed: true, |
| 71 | + Type: schema.TypeString, |
| 72 | + Description: "Remark.", |
| 73 | + }, |
| 74 | + "result_output_file": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Optional: true, |
| 77 | + Description: "Used to save results.", |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +func dataSourceTencentCloudTdmqEnvironmentAttributesRead(d *schema.ResourceData, meta interface{}) error { |
| 84 | + defer logElapsed("data_source.tencentcloud_tdmq_environment_attributes.read")() |
| 85 | + defer inconsistentCheck(d, meta)() |
| 86 | + |
| 87 | + var ( |
| 88 | + logId = getLogId(contextNil) |
| 89 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 90 | + service = TdmqService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 91 | + tdmqEnv *tdmq.DescribeEnvironmentAttributesResponseParams |
| 92 | + environmentId string |
| 93 | + clusterId string |
| 94 | + ) |
| 95 | + |
| 96 | + paramMap := make(map[string]interface{}) |
| 97 | + if v, ok := d.GetOk("environment_id"); ok { |
| 98 | + paramMap["EnvironmentId"] = helper.String(v.(string)) |
| 99 | + environmentId = v.(string) |
| 100 | + } |
| 101 | + |
| 102 | + if v, ok := d.GetOk("cluster_id"); ok { |
| 103 | + paramMap["ClusterId"] = helper.String(v.(string)) |
| 104 | + clusterId = v.(string) |
| 105 | + } |
| 106 | + |
| 107 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 108 | + result, e := service.DescribeTdmqEnvironmentAttributesByFilter(ctx, paramMap) |
| 109 | + if e != nil { |
| 110 | + return retryError(e) |
| 111 | + } |
| 112 | + |
| 113 | + tdmqEnv = result |
| 114 | + return nil |
| 115 | + }) |
| 116 | + |
| 117 | + if err != nil { |
| 118 | + return err |
| 119 | + } |
| 120 | + |
| 121 | + ids := make([]string, 0) |
| 122 | + if tdmqEnv.EnvironmentId != nil { |
| 123 | + _ = d.Set("environment_id", tdmqEnv.EnvironmentId) |
| 124 | + } |
| 125 | + |
| 126 | + if tdmqEnv.MsgTTL != nil { |
| 127 | + _ = d.Set("msg_ttl", tdmqEnv.MsgTTL) |
| 128 | + } |
| 129 | + |
| 130 | + if tdmqEnv.RateInByte != nil { |
| 131 | + _ = d.Set("rate_in_byte", tdmqEnv.RateInByte) |
| 132 | + } |
| 133 | + |
| 134 | + if tdmqEnv.RateInSize != nil { |
| 135 | + _ = d.Set("rate_in_size", tdmqEnv.RateInSize) |
| 136 | + } |
| 137 | + |
| 138 | + if tdmqEnv.RetentionHours != nil { |
| 139 | + _ = d.Set("retention_hours", tdmqEnv.RetentionHours) |
| 140 | + } |
| 141 | + |
| 142 | + if tdmqEnv.RetentionSize != nil { |
| 143 | + _ = d.Set("retention_size", tdmqEnv.RetentionSize) |
| 144 | + } |
| 145 | + |
| 146 | + if tdmqEnv.Replicas != nil { |
| 147 | + _ = d.Set("replicas", tdmqEnv.Replicas) |
| 148 | + } |
| 149 | + |
| 150 | + if tdmqEnv.Remark != nil { |
| 151 | + _ = d.Set("remark", tdmqEnv.Remark) |
| 152 | + } |
| 153 | + |
| 154 | + ids = append(ids, environmentId) |
| 155 | + ids = append(ids, clusterId) |
| 156 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 157 | + output, ok := d.GetOk("result_output_file") |
| 158 | + if ok && output.(string) != "" { |
| 159 | + if e := writeToFile(output.(string), d); e != nil { |
| 160 | + return e |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + return nil |
| 165 | +} |
0 commit comments