|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of cat node |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_cat_node" "node"{ |
| 8 | + node_type = 1 |
| 9 | + location = 2 |
| 10 | + is_ipv6 = false |
| 11 | +} |
| 12 | +``` |
| 13 | +*/ |
| 14 | +package tencentcloud |
| 15 | + |
| 16 | +import ( |
| 17 | + "context" |
| 18 | + "log" |
| 19 | + |
| 20 | + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" |
| 21 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 22 | + cat "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/cat/v20180409" |
| 23 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 24 | +) |
| 25 | + |
| 26 | +func dataSourceTencentCloudCatNode() *schema.Resource { |
| 27 | + return &schema.Resource{ |
| 28 | + Read: dataSourceTencentCloudCatNodeRead, |
| 29 | + Schema: map[string]*schema.Schema{ |
| 30 | + "node_type": { |
| 31 | + Type: schema.TypeInt, |
| 32 | + Optional: true, |
| 33 | + Description: "Node type 1:IDC,2:LastMile,3:Mobile.", |
| 34 | + }, |
| 35 | + |
| 36 | + "location": { |
| 37 | + Type: schema.TypeInt, |
| 38 | + Optional: true, |
| 39 | + Description: "Node area:1=Chinese Mainland,2=Hong Kong, Macao and Taiwan,3=Overseas.", |
| 40 | + }, |
| 41 | + |
| 42 | + "is_ipv6": { |
| 43 | + Type: schema.TypeBool, |
| 44 | + Optional: true, |
| 45 | + Description: "is IPv6.", |
| 46 | + }, |
| 47 | + |
| 48 | + "node_name": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Description: "Node name.", |
| 52 | + }, |
| 53 | + |
| 54 | + "pay_mode": { |
| 55 | + Type: schema.TypeInt, |
| 56 | + Optional: true, |
| 57 | + Description: "Payment mode:1=Trial version,2=Paid version.", |
| 58 | + }, |
| 59 | + |
| 60 | + "node_define": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Computed: true, |
| 63 | + Description: "Probe node list.", |
| 64 | + Elem: &schema.Resource{ |
| 65 | + Schema: map[string]*schema.Schema{ |
| 66 | + "name": { |
| 67 | + Type: schema.TypeString, |
| 68 | + Computed: true, |
| 69 | + Description: "Node name.", |
| 70 | + }, |
| 71 | + "code": { |
| 72 | + Type: schema.TypeString, |
| 73 | + Computed: true, |
| 74 | + Description: "Node ID.", |
| 75 | + }, |
| 76 | + "type": { |
| 77 | + Type: schema.TypeInt, |
| 78 | + Computed: true, |
| 79 | + Description: "Node Type;1 = IDC,2 = LastMile,3 = Mobile.", |
| 80 | + }, |
| 81 | + "net_service": { |
| 82 | + Type: schema.TypeString, |
| 83 | + Computed: true, |
| 84 | + Description: "Network service provider.", |
| 85 | + }, |
| 86 | + "district": { |
| 87 | + Type: schema.TypeString, |
| 88 | + Computed: true, |
| 89 | + Description: "District.", |
| 90 | + }, |
| 91 | + "city": { |
| 92 | + Type: schema.TypeString, |
| 93 | + Computed: true, |
| 94 | + Description: "City.", |
| 95 | + }, |
| 96 | + "ip_type": { |
| 97 | + Type: schema.TypeInt, |
| 98 | + Computed: true, |
| 99 | + Description: "IP type:1 = IPv4,2 = IPv6.", |
| 100 | + }, |
| 101 | + "location": { |
| 102 | + Type: schema.TypeInt, |
| 103 | + Computed: true, |
| 104 | + Description: "Node area:1=Chinese Mainland,2=Hong Kong, Macao and Taiwan,3=Overseas.", |
| 105 | + }, |
| 106 | + "code_type": { |
| 107 | + Type: schema.TypeString, |
| 108 | + Computed: true, |
| 109 | + Description: "If the node type is base, it is an availability dial test point; if it is blank, it is an advanced dial test point.", |
| 110 | + }, |
| 111 | + "node_define_status": { |
| 112 | + Type: schema.TypeInt, |
| 113 | + Computed: true, |
| 114 | + Description: "Node status: 1=running, 2=offline.", |
| 115 | + }, |
| 116 | + }, |
| 117 | + }, |
| 118 | + }, |
| 119 | + |
| 120 | + "result_output_file": { |
| 121 | + Type: schema.TypeString, |
| 122 | + Optional: true, |
| 123 | + Description: "Used to save results.", |
| 124 | + }, |
| 125 | + }, |
| 126 | + } |
| 127 | +} |
| 128 | + |
| 129 | +func dataSourceTencentCloudCatNodeRead(d *schema.ResourceData, meta interface{}) error { |
| 130 | + defer logElapsed("data_source.tencentcloud_cat_node.read")() |
| 131 | + defer inconsistentCheck(d, meta)() |
| 132 | + |
| 133 | + logId := getLogId(contextNil) |
| 134 | + ctx := context.WithValue(context.TODO(), logIdKey, logId) |
| 135 | + |
| 136 | + paramMap := make(map[string]interface{}) |
| 137 | + if v, _ := d.GetOk("node_type"); v != nil { |
| 138 | + paramMap["node_type"] = helper.IntInt64(v.(int)) |
| 139 | + } |
| 140 | + |
| 141 | + if v, _ := d.GetOk("location"); v != nil { |
| 142 | + paramMap["location"] = helper.IntInt64(v.(int)) |
| 143 | + } |
| 144 | + |
| 145 | + if v, _ := d.GetOk("is_ipv6"); v != nil { |
| 146 | + paramMap["is_ipv6"] = helper.Bool(v.(bool)) |
| 147 | + } |
| 148 | + |
| 149 | + if v, ok := d.GetOk("node_name"); ok { |
| 150 | + paramMap["node_name"] = helper.String(v.(string)) |
| 151 | + } |
| 152 | + |
| 153 | + if v, ok := d.GetOk("pay_mode"); ok { |
| 154 | + paramMap["pay_mode"] = helper.IntInt64(v.(int)) |
| 155 | + } |
| 156 | + |
| 157 | + catService := CatService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 158 | + |
| 159 | + var nodeSets []*cat.NodeDefine |
| 160 | + err := resource.Retry(readRetryTimeout, func() *resource.RetryError { |
| 161 | + results, e := catService.DescribeCatNodeByFilter(ctx, paramMap) |
| 162 | + if e != nil { |
| 163 | + return retryError(e) |
| 164 | + } |
| 165 | + nodeSets = results |
| 166 | + return nil |
| 167 | + }) |
| 168 | + if err != nil { |
| 169 | + log.Printf("[CRITAL]%s read Cat nodeSet failed, reason:%+v", logId, err) |
| 170 | + return err |
| 171 | + } |
| 172 | + |
| 173 | + ids := make([]string, 0, len(nodeSets)) |
| 174 | + nodeSetList := make([]map[string]interface{}, 0, len(nodeSets)) |
| 175 | + if nodeSets != nil { |
| 176 | + for _, nodeSet := range nodeSets { |
| 177 | + nodeSetMap := map[string]interface{}{} |
| 178 | + if nodeSet.Name != nil { |
| 179 | + nodeSetMap["name"] = nodeSet.Name |
| 180 | + } |
| 181 | + if nodeSet.Code != nil { |
| 182 | + nodeSetMap["code"] = nodeSet.Code |
| 183 | + } |
| 184 | + if nodeSet.Type != nil { |
| 185 | + nodeSetMap["type"] = nodeSet.Type |
| 186 | + } |
| 187 | + if nodeSet.NetService != nil { |
| 188 | + nodeSetMap["net_service"] = nodeSet.NetService |
| 189 | + } |
| 190 | + if nodeSet.District != nil { |
| 191 | + nodeSetMap["district"] = nodeSet.District |
| 192 | + } |
| 193 | + if nodeSet.City != nil { |
| 194 | + nodeSetMap["city"] = nodeSet.City |
| 195 | + } |
| 196 | + if nodeSet.IPType != nil { |
| 197 | + nodeSetMap["ip_type"] = nodeSet.IPType |
| 198 | + } |
| 199 | + if nodeSet.Location != nil { |
| 200 | + nodeSetMap["location"] = nodeSet.Location |
| 201 | + } |
| 202 | + if nodeSet.CodeType != nil { |
| 203 | + nodeSetMap["code_type"] = nodeSet.CodeType |
| 204 | + } |
| 205 | + if nodeSet.NodeDefineStatus != nil { |
| 206 | + nodeSetMap["node_define_status"] = nodeSet.NodeDefineStatus |
| 207 | + } |
| 208 | + ids = append(ids, *nodeSet.Name) |
| 209 | + nodeSetList = append(nodeSetList, nodeSetMap) |
| 210 | + } |
| 211 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 212 | + err = d.Set("node_define", nodeSetList) |
| 213 | + if err != nil { |
| 214 | + return err |
| 215 | + } |
| 216 | + } |
| 217 | + |
| 218 | + output, ok := d.GetOk("result_output_file") |
| 219 | + if ok && output.(string) != "" { |
| 220 | + if e := writeToFile(output.(string), nodeSetList); e != nil { |
| 221 | + return e |
| 222 | + } |
| 223 | + } |
| 224 | + |
| 225 | + return nil |
| 226 | +} |
0 commit comments