|
| 1 | +/* |
| 2 | +Use this data source to query detailed information of VPN gateways. |
| 3 | +
|
| 4 | +Example Usage |
| 5 | +
|
| 6 | +```hcl |
| 7 | +data "tencentcloud_vpn_gateways" "foo" { |
| 8 | + vpn_gateway_id = "main" |
| 9 | + destination_cidr_block = "vpngw-8ccsnclt" |
| 10 | + instance_type = "1.1.1.1" |
| 11 | + instance_id = "ap-guangzhou-3" |
| 12 | + tags = { |
| 13 | + test = "tf" |
| 14 | + } |
| 15 | +}a |
| 16 | +``` |
| 17 | +*/ |
| 18 | +package tencentcloud |
| 19 | + |
| 20 | +import ( |
| 21 | + "context" |
| 22 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 23 | + vpc "github.com/tencentcloud/tencentcloud-sdk-go/tencentcloud/vpc/v20170312" |
| 24 | + "github.com/tencentcloudstack/terraform-provider-tencentcloud/tencentcloud/internal/helper" |
| 25 | + "log" |
| 26 | +) |
| 27 | + |
| 28 | +func dataSourceTencentCloudVpnGatewayRoutes() *schema.Resource { |
| 29 | + return &schema.Resource{ |
| 30 | + Read: dataSourceTencentCloudVpnGatewayRoutesRead, |
| 31 | + |
| 32 | + Schema: map[string]*schema.Schema{ |
| 33 | + "vpn_gateway_id": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + Description: "VPN gateway ID.", |
| 37 | + }, |
| 38 | + "destination_cidr": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Optional: true, |
| 41 | + Description: "Destination IDC IP range.", |
| 42 | + }, |
| 43 | + "instance_type": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + Description: "Next hop type (type of the associated instance). Valid values: VPNCONN (VPN tunnel) and CCN (CCN instance).", |
| 47 | + }, |
| 48 | + "instance_id": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Description: "Instance ID of the next hop.", |
| 52 | + }, |
| 53 | + "result_output_file": { |
| 54 | + Type: schema.TypeString, |
| 55 | + Optional: true, |
| 56 | + Description: "Used to save results.", |
| 57 | + }, |
| 58 | + |
| 59 | + // Computed values |
| 60 | + "vpn_gateway_route_list": { |
| 61 | + Type: schema.TypeList, |
| 62 | + Computed: true, |
| 63 | + Description: "Information list of the vpn gateway routes.", |
| 64 | + Elem: &schema.Resource{ |
| 65 | + Schema: VpnGatewayRoutePara(), |
| 66 | + }, |
| 67 | + }, |
| 68 | + }, |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +func dataSourceTencentCloudVpnGatewayRoutesRead(d *schema.ResourceData, meta interface{}) error { |
| 73 | + defer logElapsed("data_source.tencentcloud_vpn_gateway_routes.read")() |
| 74 | + |
| 75 | + var ( |
| 76 | + logId = getLogId(contextNil) |
| 77 | + ctx = context.WithValue(context.TODO(), logIdKey, logId) |
| 78 | + vpnGatewayId = d.Get("vpn_gateway_id").(string) |
| 79 | + vpcService = VpcService{client: meta.(*TencentCloudClient).apiV3Conn} |
| 80 | + ) |
| 81 | + |
| 82 | + params := make(map[string]string) |
| 83 | + if v, ok := d.GetOk("destination_cidr"); ok { |
| 84 | + params["DestinationCidr"] = v.(string) |
| 85 | + } |
| 86 | + if v, ok := d.GetOk("instance_id"); ok { |
| 87 | + params["InstanceId"] = v.(string) |
| 88 | + } |
| 89 | + if v, ok := d.GetOk("instance_type"); ok { |
| 90 | + params["InstanceType"] = v.(string) |
| 91 | + } |
| 92 | + |
| 93 | + filters := make([]*vpc.Filter, 0, len(params)) |
| 94 | + for k, v := range params { |
| 95 | + filter := &vpc.Filter{ |
| 96 | + Name: helper.String(k), |
| 97 | + Values: []*string{helper.String(v)}, |
| 98 | + } |
| 99 | + filters = append(filters, filter) |
| 100 | + } |
| 101 | + err, result := vpcService.DescribeVpnGatewayRoutes(ctx, vpnGatewayId, filters) |
| 102 | + if err != nil { |
| 103 | + log.Printf("[CRITAL]%s read VPN gateway routes failed, reason:%s\n ", logId, err.Error()) |
| 104 | + return err |
| 105 | + } |
| 106 | + ids := make([]string, 0, len(result)) |
| 107 | + routeList := make([]map[string]interface{}, 0, len(result)) |
| 108 | + for _, route := range result { |
| 109 | + routeList = append(routeList, ConverterVpnGatewayRouteToMap(vpnGatewayId, route)) |
| 110 | + ids = append(ids, *route.RouteId) |
| 111 | + } |
| 112 | + d.SetId(helper.DataResourceIdsHash(ids)) |
| 113 | + if e := d.Set("vpn_gateway_route_list", routeList); e != nil { |
| 114 | + log.Printf("[CRITAL]%s provider set vpn gateway route list fail, reason:%s\n ", logId, e.Error()) |
| 115 | + return e |
| 116 | + } |
| 117 | + |
| 118 | + output, ok := d.GetOk("result_output_file") |
| 119 | + if ok && output.(string) != "" { |
| 120 | + if e := writeToFile(output.(string), routeList); e != nil { |
| 121 | + return e |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + return nil |
| 126 | +} |
0 commit comments