Skip to content

Commit 090ce47

Browse files
authored
adding sources, destinations and snis to routes (#60)
1 parent 6e1c64c commit 090ce47

File tree

10 files changed

+484
-22
lines changed

10 files changed

+484
-22
lines changed

README.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,31 @@ resource "kong_route" "route" {
9494
```
9595
The route resource maps directly onto the json for the route endpoint in Kong. For more information on the parameters [see the Kong Route create documentation](https://getkong.org/docs/1.0.x/admin-api/#route-object).
9696

97+
To create a tcp/tls route you set `sources` and `destinations` by repeating the corresponding element (`source` or `destination`) for each
98+
source or destination you want, for example:
99+
100+
```hcl
101+
102+
resource "kong_route" "route" {
103+
protocols = [ "tcp" ]
104+
strip_path = true
105+
preserve_host = false
106+
source = {
107+
ip = "192.168.1.1"
108+
port = 80
109+
}
110+
source = {
111+
ip = "192.168.1.2"
112+
}
113+
destination = {
114+
ip = "172.10.1.1"
115+
port = 81
116+
}
117+
snis = ["foo.com"]
118+
service_id = "${kong_service.service.id}"
119+
}
120+
```
121+
97122
To import a route:
98123
```
99124
terraform import kong_route.<route_identifier> <route_id>

kong/resource_kong_route.go

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,46 @@ func resourceKongRoute() *schema.Resource {
4848
ForceNew: false,
4949
Default: true,
5050
},
51+
"source": &schema.Schema{
52+
Type: schema.TypeSet,
53+
Optional: true,
54+
ForceNew: false,
55+
Elem: &schema.Resource{
56+
Schema: map[string]*schema.Schema{
57+
"ip": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
},
61+
"port": {
62+
Type: schema.TypeInt,
63+
Optional: true,
64+
},
65+
},
66+
},
67+
},
68+
"destination": &schema.Schema{
69+
Type: schema.TypeSet,
70+
Optional: true,
71+
ForceNew: false,
72+
Elem: &schema.Resource{
73+
Schema: map[string]*schema.Schema{
74+
"ip": {
75+
Type: schema.TypeString,
76+
Optional: true,
77+
},
78+
"port": {
79+
Type: schema.TypeInt,
80+
Optional: true,
81+
},
82+
},
83+
},
84+
},
85+
"snis": &schema.Schema{
86+
Type: schema.TypeList,
87+
Optional: true,
88+
ForceNew: false,
89+
Elem: &schema.Schema{Type: schema.TypeString},
90+
},
5191
"preserve_host": &schema.Schema{
5292
Type: schema.TypeBool,
5393
Optional: true,
@@ -106,34 +146,46 @@ func resourceKongRouteRead(d *schema.ResourceData, meta interface{}) error {
106146
if route == nil {
107147
d.SetId("")
108148
} else {
109-
if &route.Protocols != nil {
149+
if route.Protocols != nil {
110150
d.Set("protocols", gokong.StringValueSlice(route.Protocols))
111151
}
112152

113-
if &route.Methods != nil {
153+
if route.Methods != nil {
114154
d.Set("methods", gokong.StringValueSlice(route.Methods))
115155
}
116156

117-
if &route.Hosts != nil {
157+
if route.Hosts != nil {
118158
d.Set("hosts", gokong.StringValueSlice(route.Hosts))
119159
}
120160

121-
if &route.Paths != nil {
161+
if route.Paths != nil {
122162
d.Set("paths", gokong.StringValueSlice(route.Paths))
123163
}
124164

125-
if &route.StripPath != nil {
165+
if route.StripPath != nil {
126166
d.Set("strip_path", route.StripPath)
127167
}
128168

129-
if &route.PreserveHost != nil {
169+
if route.Sources != nil {
170+
d.Set("source", route.Sources)
171+
}
172+
173+
if route.Destinations != nil {
174+
d.Set("destination", route.Sources)
175+
}
176+
177+
if route.PreserveHost != nil {
130178
d.Set("preserve_host", route.PreserveHost)
131179
}
132180

133181
if route.RegexPriority != nil {
134182
d.Set("regex_priority", route.RegexPriority)
135183
}
136184

185+
if route.Snis != nil {
186+
d.Set("snis", gokong.StringValueSlice(route.Snis))
187+
}
188+
137189
if route.Service != nil {
138190
d.Set("service_id", route.Service)
139191
}
@@ -155,15 +207,17 @@ func resourceKongRouteDelete(d *schema.ResourceData, meta interface{}) error {
155207
}
156208

157209
func createKongRouteRequestFromResourceData(d *schema.ResourceData) *gokong.RouteRequest {
158-
resource := readIdPtrFromResource(d, "service_id")
159210
return &gokong.RouteRequest{
160211
Protocols: readStringArrayPtrFromResource(d, "protocols"),
161212
Methods: readStringArrayPtrFromResource(d, "methods"),
162213
Hosts: readStringArrayPtrFromResource(d, "hosts"),
163214
Paths: readStringArrayPtrFromResource(d, "paths"),
164215
StripPath: readBoolPtrFromResource(d, "strip_path"),
216+
Sources: readIpPortArrayFromResource(d, "source"),
217+
Destinations: readIpPortArrayFromResource(d, "destination"),
165218
PreserveHost: readBoolPtrFromResource(d, "preserve_host"),
166219
RegexPriority: readIntPtrFromResource(d, "regex_priority"),
167-
Service: resource,
220+
Snis: readStringArrayPtrFromResource(d, "snis"),
221+
Service: readIdPtrFromResource(d, "service_id"),
168222
}
169223
}

kong/resource_kong_route_test.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,39 @@ func TestAccKongRoute(t *testing.T) {
4646
})
4747
}
4848

49+
func TestAccKongRouteWithSourcesAndDestinations(t *testing.T) {
50+
resource.Test(t, resource.TestCase{
51+
Providers: testAccProviders,
52+
CheckDestroy: testAccCheckKongRouteDestroy,
53+
Steps: []resource.TestStep{
54+
{
55+
Config: testCreateRouteWithSourcesAndDestinationsConfig,
56+
Check: resource.ComposeTestCheckFunc(
57+
testAccCheckKongRouteExists("kong_route.route"),
58+
resource.TestCheckResourceAttr("kong_route.route", "protocols.0", "tcp"),
59+
resource.TestCheckResourceAttr("kong_route.route", "strip_path", "true"),
60+
resource.TestCheckResourceAttr("kong_route.route", "preserve_host", "false"),
61+
resource.TestCheckResourceAttr("kong_route.route", "source.#", "2"),
62+
resource.TestCheckResourceAttr("kong_route.route", "destination.#", "1"),
63+
resource.TestCheckResourceAttr("kong_route.route", "snis.0", "foo.com"),
64+
),
65+
},
66+
{
67+
Config: testUpdateRouteWithSourcesAndDestinationsConfig,
68+
Check: resource.ComposeTestCheckFunc(
69+
testAccCheckKongRouteExists("kong_route.route"),
70+
resource.TestCheckResourceAttr("kong_route.route", "protocols.0", "tcp"),
71+
resource.TestCheckResourceAttr("kong_route.route", "strip_path", "true"),
72+
resource.TestCheckResourceAttr("kong_route.route", "preserve_host", "false"),
73+
resource.TestCheckResourceAttr("kong_route.route", "source.#", "1"),
74+
resource.TestCheckResourceAttr("kong_route.route", "destination.#", "2"),
75+
resource.TestCheckResourceAttr("kong_route.route", "snis.0", "bar.com"),
76+
),
77+
},
78+
},
79+
})
80+
}
81+
4982
func TestAccKongRouteImport(t *testing.T) {
5083

5184
resource.Test(t, resource.TestCase{
@@ -151,6 +184,61 @@ resource "kong_route" "route" {
151184
service_id = "${kong_service.service.id}"
152185
}
153186
`
187+
188+
const testCreateRouteWithSourcesAndDestinationsConfig = `
189+
resource "kong_service" "service" {
190+
name = "test"
191+
protocol = "http"
192+
host = "test.org"
193+
}
194+
195+
resource "kong_route" "route" {
196+
protocols = [ "tcp" ]
197+
strip_path = true
198+
preserve_host = false
199+
source = {
200+
ip = "192.168.1.1"
201+
port = 80
202+
}
203+
source = {
204+
ip = "192.168.1.2"
205+
}
206+
destination = {
207+
ip = "172.10.1.1"
208+
port = 81
209+
}
210+
snis = ["foo.com"]
211+
service_id = "${kong_service.service.id}"
212+
}
213+
`
214+
215+
const testUpdateRouteWithSourcesAndDestinationsConfig = `
216+
resource "kong_service" "service" {
217+
name = "test"
218+
protocol = "http"
219+
host = "test.org"
220+
}
221+
222+
resource "kong_route" "route" {
223+
protocols = [ "tcp" ]
224+
strip_path = true
225+
preserve_host = false
226+
source = {
227+
ip = "192.168.1.1"
228+
port = 80
229+
}
230+
destination = {
231+
ip = "172.10.1.1"
232+
port = 81
233+
}
234+
destination = {
235+
ip = "172.10.1.2"
236+
port = 82
237+
}
238+
snis = ["bar.com"]
239+
service_id = "${kong_service.service.id}"
240+
}
241+
`
154242
const testImportRouteConfig = `
155243
resource "kong_service" "service" {
156244
name = "test"

kong/resource_reader.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,28 @@ func readStringArrayPtrFromResource(d *schema.ResourceData, key string) []*strin
2121
return nil
2222
}
2323

24+
func readIpPortArrayFromResource(d *schema.ResourceData, key string) []*gokong.IpPort {
25+
if attr, ok := d.GetOk(key); ok {
26+
set := attr.(*schema.Set)
27+
results := make([]*gokong.IpPort, 0)
28+
for _, item := range set.List() {
29+
m := item.(map[string]interface{})
30+
ipPort := &gokong.IpPort{}
31+
if port, ok := m["port"].(int); ok && port != 0 {
32+
ipPort.Port = &port
33+
}
34+
if ip, ok := m["ip"].(string); ok && ip != "" {
35+
ipPort.Ip = &ip
36+
}
37+
results = append(results, ipPort)
38+
}
39+
40+
return results
41+
}
42+
43+
return nil
44+
}
45+
2446
func readStringFromResource(d *schema.ResourceData, key string) string {
2547
if value, ok := d.GetOk(key); ok {
2648
return value.(string)

vendor/github.com/kevholditch/gokong/README.md

Lines changed: 53 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kevholditch/gokong/client.go

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vendor/github.com/kevholditch/gokong/convert.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)