Skip to content

Commit 2ef843a

Browse files
committed
implemented all missing fields
1 parent 324b20b commit 2ef843a

File tree

3 files changed

+101
-1
lines changed

3 files changed

+101
-1
lines changed

docs/resources/upstream.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,20 @@
33
## Example Usage
44

55
```hcl
6+
resource "kong_certificate" "certificate" {
7+
certificate = <<EOF
8+
-----BEGIN CERTIFICATE-----
9+
......
10+
-----END CERTIFICATE-----
11+
EOF
12+
private_key = <<EOF
13+
-----BEGIN PRIVATE KEY-----
14+
.....
15+
-----END PRIVATE KEY-----
16+
EOF
17+
snis = ["foo.com"]
18+
}
19+
620
resource "kong_upstream" "upstream" {
721
name = "sample_upstream"
822
slots = 10
@@ -12,6 +26,10 @@ resource "kong_upstream" "upstream" {
1226
hash_fallback_header = "FallbackHeaderName"
1327
hash_on_cookie = "CookieName"
1428
hash_on_cookie_path = "/path"
29+
host_header = "x-host"
30+
tags = ["a", "b"]
31+
client_certificate_id = kong_certificate.certificate.id
32+
1533
healthchecks {
1634
active {
1735
type = "https"

kong/resource_kong_upstream.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,22 @@ func resourceKongUpstream() *schema.Resource {
4949
ForceNew: false,
5050
Default: nil,
5151
},
52+
"host_header": {
53+
Type: schema.TypeString,
54+
Optional: true,
55+
ForceNew: false,
56+
},
57+
"tags": {
58+
Type: schema.TypeList,
59+
Optional: true,
60+
ForceNew: false,
61+
Elem: &schema.Schema{Type: schema.TypeString},
62+
},
63+
"client_certificate_id": {
64+
Type: schema.TypeString,
65+
Optional: true,
66+
ForceNew: false,
67+
},
5268
"hash_fallback_header": {
5369
Type: schema.TypeString,
5470
Optional: true,
@@ -331,6 +347,22 @@ func resourceKongUpstreamRead(ctx context.Context, d *schema.ResourceData, meta
331347
if err := d.Set("healthchecks", flattenHealthCheck(upstream.Healthchecks)); err != nil {
332348
return diag.FromErr(err)
333349
}
350+
err = d.Set("tags", upstream.Tags)
351+
if err != nil {
352+
return diag.FromErr(err)
353+
}
354+
if upstream.HostHeader != nil {
355+
err = d.Set("host_header", upstream.HostHeader)
356+
if err != nil {
357+
return diag.FromErr(err)
358+
}
359+
}
360+
if upstream.ClientCertificate != nil {
361+
err = d.Set("client_certificate_id", upstream.ClientCertificate.ID)
362+
if err != nil {
363+
return diag.FromErr(err)
364+
}
365+
}
334366
}
335367

336368
return diags
@@ -363,6 +395,15 @@ func createKongUpstreamRequestFromResourceData(d *schema.ResourceData) *kong.Ups
363395
upstreamRequest.HashFallbackHeader = readStringPtrFromResource(d, "hash_fallback_header")
364396
upstreamRequest.HashOnCookie = readStringPtrFromResource(d, "hash_on_cookie")
365397
upstreamRequest.HashOnCookiePath = readStringPtrFromResource(d, "hash_on_cookie_path")
398+
upstreamRequest.HostHeader = readStringPtrFromResource(d, "host_header")
399+
upstreamRequest.Tags = readStringArrayPtrFromResource(d, "tags")
400+
401+
clientCertificateID := readIdPtrFromResource(d, "client_certificate_id")
402+
if clientCertificateID != nil {
403+
upstreamRequest.ClientCertificate = &kong.Certificate{
404+
ID: clientCertificateID,
405+
}
406+
}
366407

367408
if healthChecksArray := readArrayFromResource(d, "healthchecks"); healthChecksArray != nil && len(healthChecksArray) > 0 {
368409
healthChecksMap := healthChecksArray[0].(map[string]interface{})

kong/resource_kong_upstream_test.go

Lines changed: 42 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ func TestAccKongUpstream(t *testing.T) {
8282
),
8383
},
8484
{
85-
Config: testUpdateUpstreamConfig,
85+
Config: fmt.Sprintf(testUpdateUpstreamConfig, testCert1, testKey1),
8686
Check: resource.ComposeTestCheckFunc(
8787
testAccCheckKongUpstreamExists("kong_upstream.upstream"),
8888
resource.TestCheckResourceAttr("kong_upstream.upstream", "name", "MyUpstream"),
@@ -94,6 +94,33 @@ func TestAccKongUpstream(t *testing.T) {
9494
resource.TestCheckResourceAttr("kong_upstream.upstream", "hash_on_cookie", "CookieName"),
9595
resource.TestCheckResourceAttr("kong_upstream.upstream", "hash_on_cookie_path", "/path"),
9696

97+
resource.TestCheckResourceAttr("kong_upstream.upstream", "host_header", "x-host"),
98+
resource.TestCheckResourceAttr("kong_upstream.upstream", "tags.#", "2"),
99+
resource.TestCheckResourceAttr("kong_upstream.upstream", "tags.0", "a"),
100+
resource.TestCheckResourceAttr("kong_upstream.upstream", "tags.1", "b"),
101+
func(s *terraform.State) error {
102+
module := s.RootModule()
103+
cert, ok := module.Resources["kong_certificate.certificate"]
104+
if !ok {
105+
return fmt.Errorf("could not find certificate resource")
106+
}
107+
108+
service, ok := module.Resources["kong_upstream.upstream"]
109+
if !ok {
110+
return fmt.Errorf("could not find upstream resource")
111+
}
112+
113+
v, ok := service.Primary.Attributes["client_certificate_id"]
114+
if !ok {
115+
return fmt.Errorf("could not find client_certificate_id property")
116+
}
117+
118+
if v != cert.Primary.ID {
119+
return fmt.Errorf("client_certificate_id does not match certificate id")
120+
}
121+
return nil
122+
},
123+
97124
resource.TestCheckResourceAttr("kong_upstream.upstream", "healthchecks.0.active.0.type", "https"),
98125
resource.TestCheckResourceAttr("kong_upstream.upstream", "healthchecks.0.active.0.timeout", "10"),
99126
resource.TestCheckResourceAttr("kong_upstream.upstream", "healthchecks.0.active.0.concurrency", "20"),
@@ -1048,6 +1075,16 @@ resource "kong_upstream" "upstream" {
10481075
}
10491076
`
10501077
const testUpdateUpstreamConfig = `
1078+
resource "kong_certificate" "certificate" {
1079+
certificate = <<EOF
1080+
%s
1081+
EOF
1082+
private_key = <<EOF
1083+
%s
1084+
EOF
1085+
snis = ["foo.com"]
1086+
}
1087+
10511088
resource "kong_upstream" "upstream" {
10521089
name = "MyUpstream"
10531090
slots = 20
@@ -1057,6 +1094,10 @@ resource "kong_upstream" "upstream" {
10571094
hash_fallback_header = "FallbackHeaderName"
10581095
hash_on_cookie = "CookieName"
10591096
hash_on_cookie_path = "/path"
1097+
host_header = "x-host"
1098+
tags = ["a", "b"]
1099+
client_certificate_id = kong_certificate.certificate.id
1100+
10601101
healthchecks {
10611102
active {
10621103
type = "https"

0 commit comments

Comments
 (0)