|
| 1 | +package ccn |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 7 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 8 | + tccommon "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/common" |
| 9 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 10 | +) |
| 11 | + |
| 12 | +func DataSourceTencentCloudCcnRoutes() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Read: dataSourceTencentCloudCcnRoutesRead, |
| 15 | + |
| 16 | + Schema: map[string]*schema.Schema{ |
| 17 | + "ccn_id": { |
| 18 | + Type: schema.TypeString, |
| 19 | + Required: true, |
| 20 | + Description: "ID of the CCN to be queried.", |
| 21 | + }, |
| 22 | + "filters": { |
| 23 | + Optional: true, |
| 24 | + Type: schema.TypeList, |
| 25 | + Description: "Filter conditions.", |
| 26 | + Elem: &schema.Resource{ |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "Field to be filtered. Support `route-id`, `cidr-block`, `instance-type`, `instance-region`, `instance-id`, `route-table-id`.", |
| 32 | + }, |
| 33 | + "values": { |
| 34 | + Type: schema.TypeSet, |
| 35 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 36 | + Required: true, |
| 37 | + Description: "Filter value of the field.", |
| 38 | + }, |
| 39 | + }, |
| 40 | + }, |
| 41 | + }, |
| 42 | + // Computed |
| 43 | + "route_list": { |
| 44 | + Computed: true, |
| 45 | + Type: schema.TypeList, |
| 46 | + Description: "CCN route list.", |
| 47 | + Elem: &schema.Resource{ |
| 48 | + Schema: map[string]*schema.Schema{ |
| 49 | + "route_id": { |
| 50 | + Type: schema.TypeString, |
| 51 | + Computed: true, |
| 52 | + Description: "route ID.", |
| 53 | + }, |
| 54 | + "destination_cidr_block": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "Destination.", |
| 58 | + }, |
| 59 | + "instance_type": { |
| 60 | + Type: schema.TypeString, |
| 61 | + Computed: true, |
| 62 | + Description: "Next hop type (associated instance type), all types: VPC, DIRECTCONNECT.", |
| 63 | + }, |
| 64 | + "instance_id": { |
| 65 | + Type: schema.TypeString, |
| 66 | + Computed: true, |
| 67 | + Description: "Next jump (associated instance ID).", |
| 68 | + }, |
| 69 | + "instance_name": { |
| 70 | + Type: schema.TypeString, |
| 71 | + Computed: true, |
| 72 | + Description: "Next jump (associated instance name).", |
| 73 | + }, |
| 74 | + "instance_region": { |
| 75 | + Type: schema.TypeString, |
| 76 | + Computed: true, |
| 77 | + Description: "Next jump (associated instance region).", |
| 78 | + }, |
| 79 | + "update_time": { |
| 80 | + Type: schema.TypeString, |
| 81 | + Computed: true, |
| 82 | + Description: "update time.", |
| 83 | + }, |
| 84 | + "enabled": { |
| 85 | + Type: schema.TypeBool, |
| 86 | + Computed: true, |
| 87 | + Description: "Is routing enabled.", |
| 88 | + }, |
| 89 | + "instance_uin": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Computed: true, |
| 92 | + Description: "The UIN (root account) to which the associated instance belongs.", |
| 93 | + }, |
| 94 | + "extra_state": { |
| 95 | + Type: schema.TypeString, |
| 96 | + Computed: true, |
| 97 | + Description: "Extension status of routing.", |
| 98 | + }, |
| 99 | + "is_bgp": { |
| 100 | + Type: schema.TypeBool, |
| 101 | + Computed: true, |
| 102 | + Description: "Is it dynamic routing.", |
| 103 | + }, |
| 104 | + "route_priority": { |
| 105 | + Type: schema.TypeInt, |
| 106 | + Computed: true, |
| 107 | + Description: "Routing priority.", |
| 108 | + }, |
| 109 | + "instance_extra_name": { |
| 110 | + Type: schema.TypeString, |
| 111 | + Computed: true, |
| 112 | + Description: "Next hop extension name (associated instance extension name).", |
| 113 | + }, |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + "result_output_file": { |
| 118 | + Type: schema.TypeString, |
| 119 | + Optional: true, |
| 120 | + Description: "Used to save results.", |
| 121 | + }, |
| 122 | + }, |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +func dataSourceTencentCloudCcnRoutesRead(d *schema.ResourceData, meta interface{}) error { |
| 127 | + defer tccommon.LogElapsed("data_source.tencentcloud_ccn_routes.read")() |
| 128 | + |
| 129 | + var ( |
| 130 | + logId = tccommon.GetLogId(tccommon.ContextNil) |
| 131 | + ctx = context.WithValue(context.TODO(), tccommon.LogIdKey, logId) |
| 132 | + service = VpcService{client: meta.(tccommon.ProviderMeta).GetAPIV3Conn()} |
| 133 | + ) |
| 134 | + |
| 135 | + paramMap := make(map[string]interface{}) |
| 136 | + if v, ok := d.GetOk("ccn_id"); ok { |
| 137 | + paramMap["CcnId"] = helper.String(v.(string)) |
| 138 | + } |
| 139 | + |
| 140 | + if v, ok := d.GetOk("filters"); ok { |
| 141 | + filtersSet := v.([]interface{}) |
| 142 | + tmpSet := make([]*vpc.Filter, 0, len(filtersSet)) |
| 143 | + for _, item := range filtersSet { |
| 144 | + filter := vpc.Filter{} |
| 145 | + filterMap := item.(map[string]interface{}) |
| 146 | + if v, ok := filterMap["name"]; ok { |
| 147 | + filter.Name = helper.String(v.(string)) |
| 148 | + } |
| 149 | + |
| 150 | + if v, ok := filterMap["values"]; ok { |
| 151 | + valuesSet := v.(*schema.Set).List() |
| 152 | + filter.Values = helper.InterfacesStringsPoint(valuesSet) |
| 153 | + } |
| 154 | + |
| 155 | + tmpSet = append(tmpSet, &filter) |
| 156 | + } |
| 157 | + |
| 158 | + paramMap["Filters"] = tmpSet |
| 159 | + } |
| 160 | + |
| 161 | + var routeSet []*vpc.CcnRoute |
| 162 | + err := resource.Retry(tccommon.ReadRetryTimeout, func() *resource.RetryError { |
| 163 | + result, e := service.DescribeVpcDescribeCcnRoutesByFilter(ctx, paramMap) |
| 164 | + if e != nil { |
| 165 | + return tccommon.RetryError(e) |
| 166 | + } |
| 167 | + |
| 168 | + routeSet = result |
| 169 | + return nil |
| 170 | + }) |
| 171 | + |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + |
| 176 | + ids := make([]string, 0, len(routeSet)) |
| 177 | + tmpList := make([]map[string]interface{}, 0, len(routeSet)) |
| 178 | + |
| 179 | + if routeSet != nil { |
| 180 | + for _, route := range routeSet { |
| 181 | + tmpMap := make(map[string]interface{}) |
| 182 | + if route.RouteId != nil { |
| 183 | + tmpMap["route_id"] = route.RouteId |
| 184 | + } |
| 185 | + |
| 186 | + if route.DestinationCidrBlock != nil { |
| 187 | + tmpMap["destination_cidr_block"] = route.DestinationCidrBlock |
| 188 | + } |
| 189 | + |
| 190 | + if route.InstanceType != nil { |
| 191 | + tmpMap["instance_type"] = route.InstanceType |
| 192 | + } |
| 193 | + |
| 194 | + if route.InstanceId != nil { |
| 195 | + tmpMap["instance_id"] = route.InstanceId |
| 196 | + } |
| 197 | + |
| 198 | + if route.InstanceName != nil { |
| 199 | + tmpMap["instance_name"] = route.InstanceName |
| 200 | + } |
| 201 | + |
| 202 | + if route.InstanceRegion != nil { |
| 203 | + tmpMap["instance_region"] = route.InstanceRegion |
| 204 | + } |
| 205 | + |
| 206 | + if route.UpdateTime != nil { |
| 207 | + tmpMap["update_time"] = route.UpdateTime |
| 208 | + } |
| 209 | + |
| 210 | + if route.Enabled != nil { |
| 211 | + tmpMap["enabled"] = route.Enabled |
| 212 | + } |
| 213 | + |
| 214 | + if route.InstanceUin != nil { |
| 215 | + tmpMap["instance_uin"] = route.InstanceUin |
| 216 | + } |
| 217 | + |
| 218 | + if route.ExtraState != nil { |
| 219 | + tmpMap["extra_state"] = route.ExtraState |
| 220 | + } |
| 221 | + |
| 222 | + if route.IsBgp != nil { |
| 223 | + tmpMap["is_bgp"] = route.IsBgp |
| 224 | + } |
| 225 | + |
| 226 | + if route.RoutePriority != nil { |
| 227 | + tmpMap["route_priority"] = route.RoutePriority |
| 228 | + } |
| 229 | + |
| 230 | + if route.InstanceExtraName != nil { |
| 231 | + tmpMap["instance_extra_name"] = route.InstanceExtraName |
| 232 | + } |
| 233 | + |
| 234 | + ids = append(ids, *route.RouteId) |
| 235 | + tmpList = append(tmpList, tmpMap) |
| 236 | + } |
| 237 | + |
| 238 | + _ = d.Set("route_list", tmpList) |
| 239 | + } |
| 240 | + |
| 241 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 242 | + output, ok := d.GetOk("result_output_file") |
| 243 | + if ok && output.(string) != "" { |
| 244 | + if e := tccommon.WriteToFile(output.(string), tmpList); e != nil { |
| 245 | + return e |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + return nil |
| 250 | +} |
0 commit comments