|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of tat agent |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_tat_agent" "agent" { |
| 8 | + # instance_ids = ["ins-f9jr4bd2"] |
| 9 | + filters { |
| 10 | + name = "environment" |
| 11 | + values = ["Linux"] |
| 12 | + } |
| 13 | +} |
| 14 | +``` |
| 15 | +*/ |
| 16 | +package tencentcloud |
| 17 | + |
| 18 | +import ( |
| 19 | + "context" |
| 20 | + |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 23 | + tat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/tat/v20201028" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | +) |
| 26 | + |
| 27 | +func dataSourceTencentCloudTatAgent() *schema.Resource { |
| 28 | + return &schema.Resource{ |
| 29 | + Read: dataSourceTencentCloudTatAgentRead, |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "instance_ids": { |
| 32 | + Optional: true, |
| 33 | + Type: schema.TypeSet, |
| 34 | + Elem: &schema.Schema{ |
| 35 | + Type: schema.TypeString, |
| 36 | + }, |
| 37 | + Description: "List of instance IDs for the query.", |
| 38 | + }, |
| 39 | + |
| 40 | + "filters": { |
| 41 | + Optional: true, |
| 42 | + Type: schema.TypeList, |
| 43 | + Description: "Filter conditions. agent-status - String - Required: No - (Filter condition) Filter by agent status. Valid values: Online, Offline. environment - String - Required: No - (Filter condition) Filter by the agent environment. Valid value: Linux. instance-id - String - Required: No - (Filter condition) Filter by the instance ID. Up to 10 Filters allowed in one request. For each filter, five Filter.Values can be specified. InstanceIds and Filters cannot be specified at the same time.", |
| 44 | + Elem: &schema.Resource{ |
| 45 | + Schema: map[string]*schema.Schema{ |
| 46 | + "name": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Required: true, |
| 49 | + Description: "Field to be filtered.", |
| 50 | + }, |
| 51 | + "values": { |
| 52 | + Type: schema.TypeSet, |
| 53 | + Elem: &schema.Schema{ |
| 54 | + Type: schema.TypeString, |
| 55 | + }, |
| 56 | + Required: true, |
| 57 | + Description: "Filter values of the field.", |
| 58 | + }, |
| 59 | + }, |
| 60 | + }, |
| 61 | + }, |
| 62 | + |
| 63 | + "automation_agent_set": { |
| 64 | + Computed: true, |
| 65 | + Type: schema.TypeList, |
| 66 | + Description: "List of agent message.", |
| 67 | + Elem: &schema.Resource{ |
| 68 | + Schema: map[string]*schema.Schema{ |
| 69 | + "instance_id": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "InstanceId.", |
| 73 | + }, |
| 74 | + "version": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "Agent version.", |
| 78 | + }, |
| 79 | + "last_heartbeat_time": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: "Time of last heartbeat.", |
| 83 | + }, |
| 84 | + "agent_status": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Computed: true, |
| 87 | + Description: "Agent status.Ranges:<li> Online:Online<li> Offline:Offline.", |
| 88 | + }, |
| 89 | + "environment": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "Environment for Agent.Ranges:<li> Linux:Linux instance<li> Windows:Windows instance.", |
| 93 | + }, |
| 94 | + "support_features": { |
| 95 | + Type: schema.TypeSet, |
| 96 | + Elem: &schema.Schema{ |
| 97 | + Type: schema.TypeString, |
| 98 | + }, |
| 99 | + Computed: true, |
| 100 | + Description: "List of feature Agent support.", |
| 101 | + }, |
| 102 | + }, |
| 103 | + }, |
| 104 | + }, |
| 105 | + |
| 106 | + "result_output_file": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Optional: true, |
| 109 | + Description: "Used to save results.", |
| 110 | + }, |
| 111 | + }, |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func dataSourceTencentCloudTatAgentRead(d *schema.ResourceData, meta interface{}) error { |
| 116 | + defer logElapsed("data_source.tencentcloud_tat_agent.read")() |
| 117 | + defer inconsistentCheck(d, meta)() |
| 118 | + |
| 119 | + logId := getLogId(contextNil) |
| 120 | + |
| 121 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 122 | + |
| 123 | + paramMap := make(map[string]interface{}) |
| 124 | + if v, ok := d.GetOk("instance_ids"); ok { |
| 125 | + instanceIdsSet := v.(*schema.Set).List() |
| 126 | + paramMap["InstanceIds"] = helper.InterfacesStringsPoint(instanceIdsSet) |
| 127 | + } |
| 128 | + |
| 129 | + if v, ok := d.GetOk("filters"); ok { |
| 130 | + filtersSet := v.([]interface{}) |
| 131 | + tmpSet := make([]*tat.Filter, 0, len(filtersSet)) |
| 132 | + |
| 133 | + for _, item := range filtersSet { |
| 134 | + filter := tat.Filter{} |
| 135 | + filterMap := item.(map[string]interface{}) |
| 136 | + |
| 137 | + if v, ok := filterMap["name"]; ok { |
| 138 | + filter.Name = helper.String(v.(string)) |
| 139 | + } |
| 140 | + if v, ok := filterMap["values"]; ok { |
| 141 | + valuesSet := v.(*schema.Set).List() |
| 142 | + filter.Values = helper.InterfacesStringsPoint(valuesSet) |
| 143 | + } |
| 144 | + tmpSet = append(tmpSet, &filter) |
| 145 | + } |
| 146 | + paramMap["filters"] = tmpSet |
| 147 | + } |
| 148 | + |
| 149 | + service := TatService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 150 | + |
| 151 | + var automationAgentSet []*tat.AutomationAgentInfo |
| 152 | + |
| 153 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 154 | + result, e := service.DescribeTatAgentByFilter(ctx, paramMap) |
| 155 | + if e != nil { |
| 156 | + return retryError(e) |
| 157 | + } |
| 158 | + automationAgentSet = result |
| 159 | + return nil |
| 160 | + }) |
| 161 | + if err != nil { |
| 162 | + return err |
| 163 | + } |
| 164 | + |
| 165 | + ids := make([]string, 0, len(automationAgentSet)) |
| 166 | + tmpList := make([]map[string]interface{}, 0, len(automationAgentSet)) |
| 167 | + |
| 168 | + if automationAgentSet != nil { |
| 169 | + for _, automationAgentInfo := range automationAgentSet { |
| 170 | + automationAgentInfoMap := map[string]interface{}{} |
| 171 | + |
| 172 | + if automationAgentInfo.InstanceId != nil { |
| 173 | + automationAgentInfoMap["instance_id"] = automationAgentInfo.InstanceId |
| 174 | + } |
| 175 | + |
| 176 | + if automationAgentInfo.Version != nil { |
| 177 | + automationAgentInfoMap["version"] = automationAgentInfo.Version |
| 178 | + } |
| 179 | + |
| 180 | + if automationAgentInfo.LastHeartbeatTime != nil { |
| 181 | + automationAgentInfoMap["last_heartbeat_time"] = automationAgentInfo.LastHeartbeatTime |
| 182 | + } |
| 183 | + |
| 184 | + if automationAgentInfo.AgentStatus != nil { |
| 185 | + automationAgentInfoMap["agent_status"] = automationAgentInfo.AgentStatus |
| 186 | + } |
| 187 | + |
| 188 | + if automationAgentInfo.Environment != nil { |
| 189 | + automationAgentInfoMap["environment"] = automationAgentInfo.Environment |
| 190 | + } |
| 191 | + |
| 192 | + if automationAgentInfo.SupportFeatures != nil { |
| 193 | + automationAgentInfoMap["support_features"] = automationAgentInfo.SupportFeatures |
| 194 | + } |
| 195 | + |
| 196 | + ids = append(ids, *automationAgentInfo.InstanceId) |
| 197 | + tmpList = append(tmpList, automationAgentInfoMap) |
| 198 | + } |
| 199 | + |
| 200 | + _ = d.Set("automation_agent_set", tmpList) |
| 201 | + } |
| 202 | + |
| 203 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 204 | + output, ok := d.GetOk("result_output_file") |
| 205 | + if ok && output.(string) != "" { |
| 206 | + if e := writeToFile(output.(string), tmpList); e != nil { |
| 207 | + return e |
| 208 | + } |
| 209 | + } |
| 210 | + return nil |
| 211 | +} |
0 commit comments