Skip to content

Commit

Permalink
Added http2 flag to application delivery resource (#359)
Browse files Browse the repository at this point in the history
* added http2 flag to application delivery resosurce to force keeping current server value when it's null

* added null validation to application delivery resource to keeping current server value when it's null.

---------

Co-authored-by: jamil.otman <jamil.otman@imperva.com>
  • Loading branch information
JameelOthman and jamilOthman authored Oct 11, 2023
1 parent ab5f12b commit 55f6eab
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 12 deletions.
4 changes: 2 additions & 2 deletions incapsula/client_application_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type Network struct {
TcpPrePooling bool `json:"tcp_pre_pooling"`
OriginConnectionReuse bool `json:"origin_connection_reuse"`
SupportNonSniClients bool `json:"support_non_sni_clients"`
EnableHttp2 bool `json:"enable_http2"`
Http2ToOrigin bool `json:"http2_to_origin"`
EnableHttp2 *bool `json:"enable_http2"`
Http2ToOrigin *bool `json:"http2_to_origin"`
Port Port `json:"port"`
SslPort SslPort `json:"ssl_port"`
}
Expand Down
43 changes: 37 additions & 6 deletions incapsula/resource_application_delivery.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,17 @@ func resourceApplicationDelivery() *schema.Resource {
Type: schema.TypeBool,
Description: "Allows supporting browsers to take advantage of the performance enhancements provided by HTTP/2 for your website. Non-supporting browsers can connect via HTTP/1.0 or HTTP/1.1.",
Optional: true,
Default: false,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.GetRawConfig().GetAttr("enable_http2").IsNull()
},
},
"http2_to_origin": {
Type: schema.TypeBool,
Description: "Enables HTTP/2 for the connection between Imperva and your origin server. (HTTP/2 must also be supported by the origin server.)",
Optional: true,
Default: false,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.GetRawConfig().GetAttr("http2_to_origin").IsNull()
},
},
"port_to": {
Type: schema.TypeInt,
Expand Down Expand Up @@ -314,8 +318,15 @@ func resourceApplicationDeliveryRead(d *schema.ResourceData, m interface{}) erro
d.Set("tcp_pre_pooling", applicationDelivery.Network.TcpPrePooling)
d.Set("origin_connection_reuse", applicationDelivery.Network.OriginConnectionReuse)
d.Set("support_non_sni_clients", applicationDelivery.Network.SupportNonSniClients)
d.Set("enable_http2", applicationDelivery.Network.EnableHttp2)
d.Set("http2_to_origin", applicationDelivery.Network.Http2ToOrigin)

if applicationDelivery.Network.EnableHttp2 != nil {
d.Set("enable_http2", applicationDelivery.Network.EnableHttp2)
}

if applicationDelivery.Network.Http2ToOrigin != nil {
d.Set("http2_to_origin", applicationDelivery.Network.Http2ToOrigin)
}

d.Set("port_to", getPortTo(applicationDelivery.Network.Port.To, defaultPortTo))
d.Set("ssl_port_to", getPortTo(applicationDelivery.Network.SslPort.To, defaultSslPortTo))

Expand All @@ -338,6 +349,26 @@ func resourceApplicationDeliveryRead(d *schema.ResourceData, m interface{}) erro
func resourceApplicationDeliveryUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
siteID := d.Get("site_id").(int)
http2 := new(bool)
http2ToOrigin := new(bool)

rawConfig := d.GetRawConfig().GetAttr("enable_http2")

if rawConfig.IsNull() {
http2 = nil
http2ToOrigin = nil
} else {
if !d.Get("enable_http2").(bool) && d.Get("http2_to_origin").(bool) {
log.Printf("[ERROR] error in Application Delivery resource - " +
"HTTP/2 to Origin support requires that HTTP/2 will be enabled for your website")

return fmt.Errorf("error in Application Delivery resource - " +
"HTTP/2 to Origin support requires that HTTP/2 will be enabled for your website")
} else {
*http2 = d.Get("enable_http2").(bool)
*http2ToOrigin = d.Get("http2_to_origin").(bool)
}
}

compression := Compression{
FileCompression: d.Get("file_compression").(bool),
Expand All @@ -358,8 +389,8 @@ func resourceApplicationDeliveryUpdate(d *schema.ResourceData, m interface{}) er
TcpPrePooling: d.Get("tcp_pre_pooling").(bool),
OriginConnectionReuse: d.Get("origin_connection_reuse").(bool),
SupportNonSniClients: d.Get("support_non_sni_clients").(bool),
EnableHttp2: d.Get("enable_http2").(bool),
Http2ToOrigin: d.Get("http2_to_origin").(bool),
EnableHttp2: http2,
Http2ToOrigin: http2ToOrigin,
Port: Port{To: strconv.Itoa(d.Get("port_to").(int))},
SslPort: SslPort{To: strconv.Itoa(d.Get("ssl_port_to").(int))},
}
Expand Down
61 changes: 57 additions & 4 deletions incapsula/resource_application_delivery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,19 +12,49 @@ import (
const applicationDeliveryResourceName = "incapsula_application_delivery"
const applicationDeliveryResource = applicationDeliveryResourceName + "." + applicationDeliveryName
const applicationDeliveryName = "testacc-terraform-application_delivery"

const customErrorPageBasic = "<!DOCTYPE html>\n<html lang=‘en’>\n <head>\n <meta charset=‘UTF-8’>\n <meta name=‘viewport’ content=‘width=device-width, initial-scale=1.0’>\n <meta http-equiv=‘X-UA-Compatible’ content=‘ie=edge’>\n <link href=‘https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;700&display=swap’ rel=‘stylesheet’>\n <title>[Error Title]</title>\n </head>\n <body>\n <div class=‘container’>\n <div class=‘container-inner’>\n <div class=‘header’>\n <div class=‘error-description’>\n $TITLE$\n </div>\n </div>\n <div class=‘main’>\n <div class=‘troubleshooting’>\n <div class=‘content’>\n $BODY$\n </div>\n\t <h1>custom edited error</h1>\n </div>\n </div>\n </div>\n </div>\n </body>\n</html>"
const customErrorPageInput = "<<-EOT\n" + customErrorPageBasic + "\nEOT"

func TestAccIncapsulaApplicationDelivery_basic(t *testing.T) {
var domainName = GenerateTestDomain(t)
log.Printf("======================== BEGIN TEST ========================")
log.Printf("[DEBUG] Running test resource_application_delivery_test.TestAccIncapsulaApplicationDelivery_basic")
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckApplicationDeliveryBasic(t),
Config: testAccCheckApplicationDeliveryBasic(domainName),
Check: resource.ComposeTestCheckFunc(
testCheckApplicationDeliveryExists(applicationDeliveryResource),
resource.TestCheckResourceAttr(applicationDeliveryResource, "file_compression", "true"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "compression_type", "GZIP"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "minify_css", "true"), //value wasn't set by tf resurce. checking default value from server
resource.TestCheckResourceAttr(applicationDeliveryResource, "minify_js", "true"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "minify_static_html", "false"),

resource.TestCheckResourceAttr(applicationDeliveryResource, "aggressive_compression", "true"),

resource.TestCheckResourceAttr(applicationDeliveryResource, "compress_jpeg", "true"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "compress_png", "true"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "aggressive_compression", "true"),

resource.TestCheckResourceAttr(applicationDeliveryResource, "enable_http2", "false"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "http2_to_origin", "false"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "origin_connection_reuse", "false"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "port_to", "225"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "ssl_port_to", "443"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "support_non_sni_clients", "true"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "tcp_pre_pooling", "false"),

resource.TestCheckResourceAttr(applicationDeliveryResource, "redirect_http_to_https", "false"),
resource.TestCheckResourceAttr(applicationDeliveryResource, "redirect_naked_to_full", "false"),

resource.TestCheckResourceAttr(applicationDeliveryResource, "error_access_denied", customErrorPageBasic),
),
},
{
Config: testAccCheckApplicationDeliveryIgnoreHttp2(domainName),
Check: resource.ComposeTestCheckFunc(
testCheckApplicationDeliveryExists(applicationDeliveryResource),
resource.TestCheckResourceAttr(applicationDeliveryResource, "file_compression", "true"),
Expand Down Expand Up @@ -100,8 +130,8 @@ func testACCStateApplicationDeliveryID(s *terraform.State) (string, error) {
return "", fmt.Errorf("Error finding site_id argument in Application Delivery resource test")
}

func testAccCheckApplicationDeliveryBasic(t *testing.T) string {
return testAccCheckIncapsulaSiteConfigBasic(GenerateTestDomain(t)) + fmt.Sprintf(`
func testAccCheckApplicationDeliveryBasic(domainName string) string {
return testAccCheckIncapsulaSiteConfigBasic(domainName) + fmt.Sprintf(`
resource "%s" "%s" {
site_id = incapsula_site.testacc-terraform-site.id
depends_on = ["%s"]
Expand All @@ -124,3 +154,26 @@ resource "%s" "%s" {
applicationDeliveryResourceName, applicationDeliveryName, siteResourceName, customErrorPageInput,
)
}

func testAccCheckApplicationDeliveryIgnoreHttp2(domainName string) string {
return testAccCheckIncapsulaSiteConfigBasic(domainName) + fmt.Sprintf(`
resource "%s" "%s" {
site_id = incapsula_site.testacc-terraform-site.id
depends_on = ["%s"]
file_compression = true
compression_type = "GZIP"
compress_jpeg = true
minify_static_html = false
aggressive_compression = true
progressive_image_rendering = false
support_non_sni_clients = true
origin_connection_reuse = false
port_to = 225
tcp_pre_pooling = false
redirect_naked_to_full = false
redirect_http_to_https = false
error_access_denied = %s
}`,
applicationDeliveryResourceName, applicationDeliveryName, siteResourceName, customErrorPageInput,
)
}

0 comments on commit 55f6eab

Please sign in to comment.