This repository has been archived by the owner on Aug 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_source_opentelekomcloud_vpc_route_v2.go
96 lines (82 loc) · 2.17 KB
/
data_source_opentelekomcloud_vpc_route_v2.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
package opentelekomcloud
import (
"fmt"
"github.com/huaweicloud/golangsdk/openstack/networking/v2/routes"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
)
func dataSourceVPCRouteV2() *schema.Resource {
return &schema.Resource{
Read: dataSourceVpcRouteV2Read,
Schema: map[string]*schema.Schema{
"region": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
},
"nexthop": {
Type: schema.TypeString,
Optional: true,
},
"destination": {
Type: schema.TypeString,
Optional: true,
},
"tenant_id": {
Type: schema.TypeString,
Optional: true,
},
"vpc_id": {
Type: schema.TypeString,
Optional: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
},
},
}
}
func dataSourceVpcRouteV2Read(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
vpcRouteClient, err := config.hwNetworkV2Client(GetRegion(d, config))
if err != nil {
return err
}
listOpts := routes.ListOpts{
Type: d.Get("type").(string),
Destination: d.Get("destination").(string),
VPC_ID: d.Get("vpc_id").(string),
Tenant_Id: d.Get("tenant_id").(string),
RouteID: d.Id(),
}
pages, err := routes.List(vpcRouteClient, listOpts).AllPages()
refinedRoutes, err := routes.ExtractRoutes(pages)
if err != nil {
return fmt.Errorf("Unable to retrieve vpc routes: %s", err)
}
if len(refinedRoutes) < 1 {
return fmt.Errorf("Your query returned no results. " +
"Please change your search criteria and try again.")
}
if len(refinedRoutes) > 1 {
return fmt.Errorf("Your query returned more than one result." +
" Please try a more specific search criteria")
}
Route := refinedRoutes[0]
log.Printf("[INFO] Retrieved Vpc Route using given filter %s: %+v", Route.RouteID, Route)
d.SetId(Route.RouteID)
d.Set("type", Route.Type)
d.Set("nexthop", Route.NextHop)
d.Set("destination", Route.Destination)
d.Set("tenant_id", Route.Tenant_Id)
d.Set("vpc_id", Route.VPC_ID)
d.Set("id", Route.RouteID)
d.Set("region", GetRegion(d, config))
return nil
}