|
| 1 | +package dlc |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + dlcv20210125 "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/dlc/v20210125" |
| 9 | + |
| 10 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 11 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 12 | +) |
| 13 | + |
| 14 | +func DataSourceTencentCloudDlcDataEngineNetwork() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Read: dataSourceTencentCloudDlcDataEngineNetworkRead, |
| 17 | + Schema: map[string]*schema.Schema{ |
| 18 | + "sort_by": { |
| 19 | + Type: schema.TypeString, |
| 20 | + Optional: true, |
| 21 | + Description: "Sort Field.", |
| 22 | + }, |
| 23 | + |
| 24 | + "sorting": { |
| 25 | + Type: schema.TypeString, |
| 26 | + Optional: true, |
| 27 | + Description: "Sort order, asc or desc.", |
| 28 | + }, |
| 29 | + |
| 30 | + "filters": { |
| 31 | + Type: schema.TypeList, |
| 32 | + Optional: true, |
| 33 | + Description: "Filter conditions are optional, engine-network-id--engine network ID, engine-network-state--engine network status.", |
| 34 | + Elem: &schema.Resource{ |
| 35 | + Schema: map[string]*schema.Schema{ |
| 36 | + "name": { |
| 37 | + Type: schema.TypeString, |
| 38 | + Required: true, |
| 39 | + Description: "Attribute name, if there are multiple filters, the relationship between filters is a logical OR relationship.", |
| 40 | + }, |
| 41 | + "values": { |
| 42 | + Type: schema.TypeList, |
| 43 | + Required: true, |
| 44 | + Description: "Attribute value, if there are multiple values, the relationship between values is a logical OR relationship.", |
| 45 | + Elem: &schema.Schema{ |
| 46 | + Type: schema.TypeString, |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + }, |
| 52 | + |
| 53 | + "engine_networks_infos": { |
| 54 | + Type: schema.TypeList, |
| 55 | + Computed: true, |
| 56 | + Description: "Engine network information list.", |
| 57 | + Elem: &schema.Resource{ |
| 58 | + Schema: map[string]*schema.Schema{ |
| 59 | + "engine_network_name": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Optional: true, |
| 62 | + Description: "Engine network name.", |
| 63 | + }, |
| 64 | + "engine_network_state": { |
| 65 | + Type: schema.TypeInt, |
| 66 | + Required: true, |
| 67 | + Description: "Engine network status, 0--initialized, 2--available, -1--deleted.", |
| 68 | + }, |
| 69 | + "engine_network_cidr": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Optional: true, |
| 72 | + Description: "Engine network CIDR.", |
| 73 | + }, |
| 74 | + "engine_network_id": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Optional: true, |
| 77 | + Description: "Engine network ID.", |
| 78 | + }, |
| 79 | + "create_time": { |
| 80 | + Type: schema.TypeInt, |
| 81 | + Optional: true, |
| 82 | + Description: "Creation time.", |
| 83 | + }, |
| 84 | + "update_time": { |
| 85 | + Type: schema.TypeInt, |
| 86 | + Optional: true, |
| 87 | + Description: "Update time.", |
| 88 | + }, |
| 89 | + "private_link_number": { |
| 90 | + Type: schema.TypeInt, |
| 91 | + Optional: true, |
| 92 | + Description: "Number of private links.", |
| 93 | + }, |
| 94 | + "engine_number": { |
| 95 | + Type: schema.TypeInt, |
| 96 | + Optional: true, |
| 97 | + Description: "Number of engines.", |
| 98 | + }, |
| 99 | + "gate_way_info": { |
| 100 | + Type: schema.TypeList, |
| 101 | + Optional: true, |
| 102 | + Description: "Gateway information list.", |
| 103 | + Elem: &schema.Resource{ |
| 104 | + Schema: map[string]*schema.Schema{ |
| 105 | + "gateway_id": { |
| 106 | + Type: schema.TypeString, |
| 107 | + Optional: true, |
| 108 | + Description: "Gateway ID.", |
| 109 | + }, |
| 110 | + "gateway_name": { |
| 111 | + Type: schema.TypeString, |
| 112 | + Optional: true, |
| 113 | + Description: "Gateway name.", |
| 114 | + }, |
| 115 | + "size": { |
| 116 | + Type: schema.TypeInt, |
| 117 | + Optional: true, |
| 118 | + Description: "Gateway size.", |
| 119 | + }, |
| 120 | + "state": { |
| 121 | + Type: schema.TypeInt, |
| 122 | + Optional: true, |
| 123 | + Description: "Gateway status: -1--Failed, -2--Deleted, 0--Init,1--Pause, 2--running, 3--ToBeDeleted, 4--Deleting, 5--Pausing, 6--Resuming, 7--Isolating, 8--Isolated, 9--Renewing, 10--Modifying, 11--Modified, 12--Restoring, 13--Restored, 14--ToBeRestored.", |
| 124 | + }, |
| 125 | + "pay_mode": { |
| 126 | + Type: schema.TypeInt, |
| 127 | + Optional: true, |
| 128 | + Description: "Pay mode.", |
| 129 | + }, |
| 130 | + "mode": { |
| 131 | + Type: schema.TypeInt, |
| 132 | + Optional: true, |
| 133 | + Description: "Gateway mode.", |
| 134 | + }, |
| 135 | + }, |
| 136 | + }, |
| 137 | + }, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + |
| 142 | + "result_output_file": { |
| 143 | + Type: schema.TypeString, |
| 144 | + Optional: true, |
| 145 | + Description: "Used to save results.", |
| 146 | + }, |
| 147 | + }, |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +func dataSourceTencentCloudDlcDataEngineNetworkRead(d *schema.ResourceData, meta interface{}) error { |
| 152 | + defer tccommon.LogElapsed("data_source.tencentcloud_dlc_data_engine_network.read")() |
| 153 | + defer tccommon.InconsistentCheck(d, meta)() |
| 154 | + |
| 155 | + var ( |
| 156 | + logId = tccommon.GetLogId(nil) |
| 157 | + ctx = tccommon.NewResourceLifeCycleHandleFuncContext(context.Background(), logId, d, meta) |
| 158 | + service = DlcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 159 | + ) |
| 160 | + |
| 161 | + paramMap := make(map[string]interface{}) |
| 162 | + if v, ok := d.GetOk("sort_by"); ok { |
| 163 | + paramMap["SortBy"] = helper.String(v.(string)) |
| 164 | + } |
| 165 | + |
| 166 | + if v, ok := d.GetOk("sorting"); ok { |
| 167 | + paramMap["Sorting"] = helper.String(v.(string)) |
| 168 | + } |
| 169 | + |
| 170 | + if v, ok := d.GetOk("filters"); ok { |
| 171 | + filtersSet := v.([]interface{}) |
| 172 | + tmpSet := make([]*dlcv20210125.Filter, 0, len(filtersSet)) |
| 173 | + for _, item := range filtersSet { |
| 174 | + filtersMap := item.(map[string]interface{}) |
| 175 | + filter := dlcv20210125.Filter{} |
| 176 | + if v, ok := filtersMap["name"].(string); ok && v != "" { |
| 177 | + filter.Name = helper.String(v) |
| 178 | + } |
| 179 | + |
| 180 | + if v, ok := filtersMap["values"]; ok { |
| 181 | + valuesSet := v.([]interface{}) |
| 182 | + for i := range valuesSet { |
| 183 | + values := valuesSet[i].(string) |
| 184 | + filter.Values = append(filter.Values, helper.String(values)) |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + tmpSet = append(tmpSet, &filter) |
| 189 | + } |
| 190 | + |
| 191 | + paramMap["Filters"] = tmpSet |
| 192 | + } |
| 193 | + |
| 194 | + var respData []*dlcv20210125.EngineNetworkInfo |
| 195 | + reqErr := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 196 | + result, e := service.DescribeDlcDataEngineNetworkByFilter(ctx, paramMap) |
| 197 | + if e != nil { |
| 198 | + return tccommon.RetryError(e) |
| 199 | + } |
| 200 | + |
| 201 | + respData = result |
| 202 | + return nil |
| 203 | + }) |
| 204 | + |
| 205 | + if reqErr != nil { |
| 206 | + return reqErr |
| 207 | + } |
| 208 | + |
| 209 | + engineNetworkInfosList := make([]map[string]interface{}, 0, len(respData)) |
| 210 | + if respData != nil { |
| 211 | + for _, engineNetworkInfos := range respData { |
| 212 | + engineNetworkInfosMap := map[string]interface{}{} |
| 213 | + if engineNetworkInfos.EngineNetworkName != nil { |
| 214 | + engineNetworkInfosMap["engine_network_name"] = engineNetworkInfos.EngineNetworkName |
| 215 | + } |
| 216 | + |
| 217 | + if engineNetworkInfos.EngineNetworkState != nil { |
| 218 | + engineNetworkInfosMap["engine_network_state"] = engineNetworkInfos.EngineNetworkState |
| 219 | + } |
| 220 | + |
| 221 | + if engineNetworkInfos.EngineNetworkCidr != nil { |
| 222 | + engineNetworkInfosMap["engine_network_cidr"] = engineNetworkInfos.EngineNetworkCidr |
| 223 | + } |
| 224 | + |
| 225 | + if engineNetworkInfos.EngineNetworkId != nil { |
| 226 | + engineNetworkInfosMap["engine_network_id"] = engineNetworkInfos.EngineNetworkId |
| 227 | + } |
| 228 | + |
| 229 | + if engineNetworkInfos.CreateTime != nil { |
| 230 | + engineNetworkInfosMap["create_time"] = engineNetworkInfos.CreateTime |
| 231 | + } |
| 232 | + |
| 233 | + if engineNetworkInfos.UpdateTime != nil { |
| 234 | + engineNetworkInfosMap["update_time"] = engineNetworkInfos.UpdateTime |
| 235 | + } |
| 236 | + |
| 237 | + if engineNetworkInfos.PrivateLinkNumber != nil { |
| 238 | + engineNetworkInfosMap["private_link_number"] = engineNetworkInfos.PrivateLinkNumber |
| 239 | + } |
| 240 | + |
| 241 | + if engineNetworkInfos.EngineNumber != nil { |
| 242 | + engineNetworkInfosMap["engine_number"] = engineNetworkInfos.EngineNumber |
| 243 | + } |
| 244 | + |
| 245 | + gateWayInfoList := make([]map[string]interface{}, 0, len(engineNetworkInfos.GateWayInfo)) |
| 246 | + if engineNetworkInfos.GateWayInfo != nil { |
| 247 | + for _, gateWayInfo := range engineNetworkInfos.GateWayInfo { |
| 248 | + gateWayInfoMap := map[string]interface{}{} |
| 249 | + |
| 250 | + if gateWayInfo.GatewayId != nil { |
| 251 | + gateWayInfoMap["gateway_id"] = gateWayInfo.GatewayId |
| 252 | + } |
| 253 | + |
| 254 | + if gateWayInfo.GatewayName != nil { |
| 255 | + gateWayInfoMap["gateway_name"] = gateWayInfo.GatewayName |
| 256 | + } |
| 257 | + |
| 258 | + if gateWayInfo.Size != nil { |
| 259 | + gateWayInfoMap["size"] = gateWayInfo.Size |
| 260 | + } |
| 261 | + |
| 262 | + if gateWayInfo.State != nil { |
| 263 | + gateWayInfoMap["state"] = gateWayInfo.State |
| 264 | + } |
| 265 | + |
| 266 | + if gateWayInfo.PayMode != nil { |
| 267 | + gateWayInfoMap["pay_mode"] = gateWayInfo.PayMode |
| 268 | + } |
| 269 | + |
| 270 | + if gateWayInfo.Mode != nil { |
| 271 | + gateWayInfoMap["mode"] = gateWayInfo.Mode |
| 272 | + } |
| 273 | + |
| 274 | + gateWayInfoList = append(gateWayInfoList, gateWayInfoMap) |
| 275 | + } |
| 276 | + |
| 277 | + engineNetworkInfosMap["gate_way_info"] = gateWayInfoList |
| 278 | + } |
| 279 | + |
| 280 | + engineNetworkInfosList = append(engineNetworkInfosList, engineNetworkInfosMap) |
| 281 | + } |
| 282 | + |
| 283 | + _ = d.Set("engine_network_infos", engineNetworkInfosList) |
| 284 | + } |
| 285 | + |
| 286 | + d.SetId(helper.BuildToken()) |
| 287 | + output, ok := d.GetOk("result_output_file") |
| 288 | + if ok && output.(string) != "" { |
| 289 | + if e := tccommon.WriteToFile(output.(string), engineNetworkInfosList); e != nil { |
| 290 | + return e |
| 291 | + } |
| 292 | + } |
| 293 | + |
| 294 | + return nil |
| 295 | +} |
0 commit comments