Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix nil pointers when time.Duration could be nil #2504

Merged
merged 3 commits into from
Jun 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .changelog/2504.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tunnel_config: fix nil pointers for time.Durations
```
16 changes: 12 additions & 4 deletions internal/sdkv2provider/resource_cloudflare_tunnel_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,12 +119,8 @@ func buildTunnelOriginRequest(originRequest map[string]interface{}) (originConfi

func parseOriginRequest(originRequest cloudflare.OriginRequestConfig) (returnValue []map[string]interface{}) {
returnValue = append(returnValue, map[string]interface{}{
"connect_timeout": originRequest.ConnectTimeout.String(),
"tls_timeout": originRequest.TLSTimeout.String(),
"tcp_keep_alive": originRequest.TCPKeepAlive.String(),
"no_happy_eyeballs": originRequest.NoHappyEyeballs,
"keep_alive_connections": originRequest.KeepAliveConnections,
"keep_alive_timeout": originRequest.KeepAliveTimeout.String(),
"http_host_header": originRequest.HTTPHostHeader,
"origin_server_name": originRequest.OriginServerName,
"ca_pool": originRequest.CAPool,
Expand All @@ -136,6 +132,18 @@ func parseOriginRequest(originRequest cloudflare.OriginRequestConfig) (returnVal
"proxy_type": originRequest.ProxyType,
"http2_origin": originRequest.Http2Origin,
})
if originRequest.ConnectTimeout != nil {
returnValue[0]["connect_timeout"] = originRequest.ConnectTimeout.String()
}
if originRequest.TLSTimeout != nil {
returnValue[0]["tls_timeout"] = originRequest.TLSTimeout.String()
}
if originRequest.TCPKeepAlive != nil {
returnValue[0]["tcp_keep_alive"] = originRequest.TCPKeepAlive.String()
}
if originRequest.KeepAliveTimeout != nil {
returnValue[0]["keep_alive_timeout"] = originRequest.KeepAliveTimeout.String()
}
var accessConfig []map[string]interface{}
if originRequest.Access != nil {
accessConfig = append(accessConfig, map[string]interface{}{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,37 @@ func testTunnelConfigShort(resourceID, accountID, tunnelSecret string) string {
`, resourceID, accountID, tunnelSecret)
}

func testTunnelConfigNilPointer(resourceID, accountID, tunnelSecret string) string {
return fmt.Sprintf(`

resource "cloudflare_tunnel" "%[1]s" {
account_id = "%[2]s"
name = "%[1]s"
secret = "%[3]s"
}

resource "cloudflare_tunnel_config" "%[1]s" {
account_id = "%[2]s"
tunnel_id = cloudflare_tunnel.%[1]s.id

config {
warp_routing {
enabled = true
}
origin_request {
no_tls_verify = true
}
ingress_rule {
hostname = "foo"
service = "https://10.0.0.1:8006"
}
ingress_rule {
service = "http_status:501"
}
}
}`, resourceID, accountID, tunnelSecret)
}

func TestAccCloudflareTunnelConfig_Full(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_tunnel_config." + rnd
Expand Down Expand Up @@ -196,3 +227,37 @@ func TestAccCloudflareTunnelConfig_Short(t *testing.T) {
},
})
}

func TestAccCloudflareTunnelConfig_NilPointer(t *testing.T) {
rnd := generateRandomResourceName()
name := "cloudflare_tunnel_config." + rnd
zoneID := os.Getenv("CLOUDFLARE_ACCOUNT_ID")
tunnelSecret := acctest.RandStringFromCharSet(32, acctest.CharSetAlpha)

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
testAccPreCheckAccount(t)
},
ProviderFactories: providerFactories,
Steps: []resource.TestStep{
{
Config: testTunnelConfigNilPointer(rnd, zoneID, tunnelSecret),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(name, "config.0.warp_routing.0.enabled", "true"),
resource.TestCheckResourceAttr(name, "config.0.origin_request.0.no_tls_verify", "true"),
resource.TestCheckResourceAttr(name, "config.0.ingress_rule.#", "2"),
resource.TestCheckResourceAttr(name, "config.0.ingress_rule.0.hostname", "foo"),
resource.TestCheckResourceAttr(name, "config.0.ingress_rule.0.service", "https://10.0.0.1:8006"),
resource.TestCheckResourceAttr(name, "config.0.ingress_rule.1.service", "http_status:501"),
),
},
{
ResourceName: name,
ImportStateIdPrefix: fmt.Sprintf("%s/", zoneID),
ImportState: true,
ImportStateVerify: true,
},
},
})
}