This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_source_yandex_vpc_route_table.go
118 lines (103 loc) · 2.69 KB
/
data_source_yandex_vpc_route_table.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
package yandex
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/vpc/v1"
"github.com/yandex-cloud/go-sdk/sdkresolvers"
)
func dataSourceYandexVPCRouteTable() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexVPCRouteTableRead,
Schema: map[string]*schema.Schema{
"route_table_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
},
"network_id": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"static_route": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"destination_prefix": {
Type: schema.TypeString,
Computed: true,
},
"next_hop_address": {
Type: schema.TypeString,
Computed: true,
},
},
},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceYandexVPCRouteTableRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := config.Context()
err := checkOneOf(d, "route_table_id", "name")
if err != nil {
return err
}
routeTableID := d.Get("route_table_id").(string)
routeTableName, routeTableNameOk := d.GetOk("name")
if routeTableNameOk {
routeTableID, err = resolveObjectID(ctx, config, routeTableName.(string), sdkresolvers.RouteTableResolver)
if err != nil {
return fmt.Errorf("failed to resolve data source route table by name: %v", err)
}
}
routeTable, err := config.sdk.VPC().RouteTable().Get(ctx, &vpc.GetRouteTableRequest{
RouteTableId: routeTableID,
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("route table with ID %q", routeTableID))
}
createdAt, err := getTimestamp(routeTable.CreatedAt)
if err != nil {
return err
}
d.Set("route_table_id", routeTable.Id)
d.Set("name", routeTable.Name)
d.Set("description", routeTable.Description)
d.Set("folder_id", routeTable.FolderId)
d.Set("created_at", createdAt)
d.Set("network_id", routeTable.NetworkId)
if err := d.Set("labels", routeTable.Labels); err != nil {
return err
}
staticRoutes := flattenStaticRoutes(routeTable)
if err := d.Set("static_route", staticRoutes); err != nil {
return err
}
d.SetId(routeTable.Id)
return nil
}