Skip to content

Commit 378791b

Browse files
Added 2 optional attributes to account and subaccount resources: (#388)
* enable_http2_for_new_sites - enable HTTP/2 support for traffic between end-users (visitors) and Imperva for newly created SSL sites. * enable_http2_to_origin_for_new_sites - enable HTTP/2 support for traffic between Imperva and origin server for newly created SSL sites. Co-authored-by: adi.shlomo <adi.shlomo@imperva.com>
1 parent 03ff8fa commit 378791b

File tree

8 files changed

+264
-35
lines changed

8 files changed

+264
-35
lines changed

incapsula/client_account.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,12 @@ type AccountStatusResponse struct {
5656
Email string `json:"email"`
5757
EmailVerified bool `json:"email_verified"`
5858
} `json:"logins"`
59-
SupportLevel string `json:"support_level"`
60-
SupportAllTLSVersions bool `json:"supprt_all_tls_versions"`
61-
WildcardSANForNewSites string `json:"wildcard_san_for_new_sites"`
62-
NakedDomainSANForNewWWWSites bool `json:"naked_domain_san_for_new_www_sites"`
59+
SupportLevel string `json:"support_level"`
60+
SupportAllTLSVersions bool `json:"supprt_all_tls_versions"`
61+
WildcardSANForNewSites string `json:"wildcard_san_for_new_sites"`
62+
NakedDomainSANForNewWWWSites bool `json:"naked_domain_san_for_new_www_sites"`
63+
EnableHttp2ForNewSites bool `json:"enable_http2_for_new_sites"`
64+
EnableHttp2ToOriginForNewSites bool `json:"enable_http2_to_origin_for_new_sites"`
6365
} `json:"account"`
6466
ParentID int `json:"parent_id"`
6567
Email string `json:"email"`

incapsula/http2_updater.go

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package incapsula
2+
3+
import (
4+
"fmt"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
6+
"log"
7+
)
8+
9+
func updateHttp2Properties(client *Client, d *schema.ResourceData) error {
10+
enableHttp2ForNewSiteChanged := d.HasChange("enable_http2_for_new_sites") && d.Get("enable_http2_for_new_sites") != ""
11+
enableHttp2ToOriginForNewSitesChanged := d.HasChange("enable_http2_to_origin_for_new_sites") && d.Get("enable_http2_to_origin_for_new_sites") != ""
12+
13+
log.Printf("[INFO] adi enable_http2_for_new_sites %v %v %v %v\n", d.HasChange("enable_http2_for_new_sites"), d.Get("enable_http2_for_new_sites"),
14+
d.HasChange("enable_http2_to_origin_for_new_sites"), d.Get("enable_http2_to_origin_for_new_sites"))
15+
16+
if !enableHttp2ForNewSiteChanged && !enableHttp2ToOriginForNewSitesChanged {
17+
return nil
18+
}
19+
20+
if d.Get("enable_http2_for_new_sites").(string) == "false" && d.Get("enable_http2_to_origin_for_new_sites").(string) == "true" {
21+
log.Printf("[ERROR] Could not update Incapsula account param enable_http2_for_new_sites with value (%s) and enable_http2_to_origin_for_new_sites with value (%s) for account_id: %s",
22+
d.Get("enable_http2_for_new_sites"), d.Get("enable_http2_to_origin_for_new_sites"), d.Id())
23+
return fmt.Errorf("[ERROR] invalid values for enable_http2_for_new_sites and enable_http2_to_origin_for_new_sites")
24+
}
25+
26+
updateParamsList := getParamsToUpdateInOrder(enableHttp2ForNewSiteChanged, enableHttp2ToOriginForNewSitesChanged, d)
27+
28+
return updateParams(client, d, updateParamsList)
29+
}
30+
31+
func getParamsToUpdateInOrder(enableHttp2ForNewSiteChanged bool, enableHttp2ToOriginForNewSitesChanged bool, d *schema.ResourceData) []string {
32+
33+
updateParamsList := make([]string, 0)
34+
if enableHttp2ForNewSiteChanged && !enableHttp2ToOriginForNewSitesChanged {
35+
updateParamsList = append(updateParamsList, "enable_http2_for_new_sites")
36+
} else if !enableHttp2ForNewSiteChanged && enableHttp2ToOriginForNewSitesChanged {
37+
updateParamsList = append(updateParamsList, "enable_http2_to_origin_for_new_sites")
38+
} else if d.Get("enable_http2_to_origin_for_new_sites").(string) == "true" { // if the origin is true, then the client must be set first
39+
updateParamsList = append(updateParamsList, "enable_http2_for_new_sites", "enable_http2_to_origin_for_new_sites")
40+
} else {
41+
updateParamsList = append(updateParamsList, "enable_http2_to_origin_for_new_sites", "enable_http2_for_new_sites")
42+
}
43+
return updateParamsList
44+
}
45+
46+
func updateParams(client *Client, d *schema.ResourceData, updateParams []string) error {
47+
for i := 0; i < len(updateParams); i++ {
48+
param := updateParams[i]
49+
if d.HasChange(param) && d.Get(param) != "" {
50+
log.Printf("[INFO] Updating Incapsula account param (%s) with value (%s) for account_id: %s\n", param, d.Get(param), d.Id())
51+
_, err := client.UpdateAccount(d.Id(), param, d.Get(param).(string))
52+
if err != nil {
53+
log.Printf("[ERROR] Could not update Incapsula account param (%s) with value (%t) for account_id: %s %s\n", param, d.Get(param).(bool), d.Id(), err)
54+
return err
55+
}
56+
}
57+
}
58+
return nil
59+
}

incapsula/resource_account.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,18 @@ func resourceAccount() *schema.Resource {
110110
Optional: true,
111111
Computed: true,
112112
},
113-
113+
"enable_http2_for_new_sites": {
114+
Description: "Enable HTTP/2 for traffic between end-users (visitors) and Imperva for newly created SSL sites. Options are `true` and `false`. Defaults to `true`",
115+
Type: schema.TypeString,
116+
Default: "true",
117+
Optional: true,
118+
},
119+
"enable_http2_to_origin_for_new_sites": {
120+
Description: "Enable HTTP/2 support for traffic between Imperva and your origin server for newly created SSL sites. This option can only be 'true' once 'enable_http2_for_new_sites' is enabled for newly created sites. Options are `true` and `false`. Defaults to `false`",
121+
Type: schema.TypeString,
122+
Default: "false",
123+
Optional: true,
124+
},
114125
// Computed Attributes
115126
"support_level": {
116127
Description: "Account support level",
@@ -201,15 +212,16 @@ func resourceAccountRead(d *schema.ResourceData, m interface{}) error {
201212
d.Set("plan_id", accountStatusResponse.Account.PlanID)
202213
d.Set("plan_name", accountStatusResponse.Account.PlanName)
203214
d.Set("trial_end_date", accountStatusResponse.Account.TrialEndDate)
204-
d.Set("account_id", accountStatusResponse.Account.AccountID)
205215
d.Set("ref_id", accountStatusResponse.Account.RefID)
206216
d.Set("user_name", accountStatusResponse.Account.UserName)
207217
d.Set("account_name", accountStatusResponse.Account.AccountName)
208218
d.Set("support_level", accountStatusResponse.Account.SupportLevel)
209-
d.Set("support_all_tls_versions", accountStatusResponse.Account.SupportAllTLSVersions)
219+
d.Set("support_all_tls_versions", strconv.FormatBool(accountStatusResponse.Account.SupportAllTLSVersions))
210220
d.Set("wildcard_san_for_new_sites", accountStatusResponse.Account.WildcardSANForNewSites)
211221
d.Set("naked_domain_san_for_new_www_sites", strconv.FormatBool(accountStatusResponse.Account.NakedDomainSANForNewWWWSites))
212222
d.Set("consent_required", accountStatusResponse.ConsentRequired)
223+
d.Set("enable_http2_for_new_sites", strconv.FormatBool(accountStatusResponse.Account.EnableHttp2ForNewSites))
224+
d.Set("enable_http2_to_origin_for_new_sites", strconv.FormatBool(accountStatusResponse.Account.EnableHttp2ToOriginForNewSites))
213225

214226
// Get the performance settings for the site
215227
defaultAccountDataStorageRegion, err := client.GetAccountDataStorageRegion(d.Id())
@@ -307,7 +319,7 @@ func updateAdditionalAccountProperties(client *Client, d *schema.ResourceData) e
307319
}
308320
}
309321

310-
return nil
322+
return updateHttp2Properties(client, d)
311323
}
312324

313325
func replaceAccountNameParamName(param string) string {

incapsula/resource_account_test.go

Lines changed: 97 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,33 @@ package incapsula
22

33
import (
44
"fmt"
5+
"math/rand"
56
"os"
67
"strconv"
78
"strings"
89
"testing"
10+
"time"
911

1012
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
1113
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
1214
)
1315

14-
const testEmail = "example@example.com"
16+
const testEmail = "example@imperva.com"
1517
const accountResourceName = "incapsula_account.test-terraform-account"
1618

17-
func GenerateTestEmail(t *testing.T) string {
18-
if v := os.Getenv("INCAPSULA_API_ID"); v == "" {
19-
t.Fatal("INCAPSULA_API_ID must be set for acceptance tests")
20-
}
21-
return "id" + os.Getenv("INCAPSULA_API_ID") + "." + testEmail
22-
}
23-
24-
func SkipIfAccountTypeIsResellerEndUser(t *testing.T) resource.ErrorCheckFunc {
25-
return func(err error) error {
26-
if err == nil {
27-
return nil
28-
}
29-
if strings.Contains(err.Error(), "Operation not allowed") {
30-
t.Skipf("skipping test since account type is RESELLER_END_USER. Error: %s", err.Error())
31-
}
32-
33-
return err
34-
}
35-
}
36-
3719
func TestIncapsulaAccount_Basic(t *testing.T) {
20+
email := GenerateTestEmail(t)
3821
resource.Test(t, resource.TestCase{
3922
ErrorCheck: SkipIfAccountTypeIsResellerEndUser(t),
4023
PreCheck: func() { testAccPreCheck(t) },
4124
Providers: testAccProviders,
4225
CheckDestroy: testCheckIncapsulaAccountDestroy,
4326
Steps: []resource.TestStep{
4427
{
45-
Config: testCheckIncapsulaAccountConfigBasic(GenerateTestEmail(t)),
28+
Config: testCheckIncapsulaAccountConfigBasic(email),
4629
Check: resource.ComposeTestCheckFunc(
4730
testCheckIncapsulaAccountExists(accountResourceName),
48-
resource.TestCheckResourceAttr(accountResourceName, "email", GenerateTestEmail(t)),
31+
resource.TestCheckResourceAttr(accountResourceName, "email", email),
4932
),
5033
},
5134
},
@@ -66,6 +49,40 @@ func TestIncapsulaAccount_ImportBasic(t *testing.T) {
6649
ResourceName: "incapsula_account.test-terraform-account",
6750
ImportState: true,
6851
ImportStateVerify: true,
52+
ImportStateIdFunc: testACCStateAccountID,
53+
},
54+
},
55+
})
56+
}
57+
58+
func TestIncapsulaAccount_Http2Defaults(t *testing.T) {
59+
testIncapsulaAccountHttp2Client(t, true, false)
60+
}
61+
62+
func TestIncapsulaAccount_Http2ClientAndOriginEnabled(t *testing.T) {
63+
testIncapsulaAccountHttp2Client(t, true, true)
64+
}
65+
66+
func TestIncapsulaAccount_Http2ClientAndOriginDisabled(t *testing.T) {
67+
testIncapsulaAccountHttp2Client(t, false, false)
68+
}
69+
70+
func testIncapsulaAccountHttp2Client(t *testing.T, enableHttp2ForNewSites bool, enableHttp2ToOriginForNewSites bool) {
71+
email := GenerateTestEmail(t)
72+
resource.Test(t, resource.TestCase{
73+
ErrorCheck: SkipIfAccountTypeIsResellerEndUser(t),
74+
PreCheck: func() { testAccPreCheck(t) },
75+
Providers: testAccProviders,
76+
CheckDestroy: testCheckIncapsulaAccountDestroy,
77+
Steps: []resource.TestStep{
78+
{
79+
Config: testHttp2AccountConfig(email, enableHttp2ForNewSites, enableHttp2ToOriginForNewSites),
80+
Check: resource.ComposeTestCheckFunc(
81+
testCheckIncapsulaAccountExists(accountResourceName),
82+
resource.TestCheckResourceAttr(accountResourceName, "email", email),
83+
resource.TestCheckResourceAttr(accountResourceName, "enable_http2_for_new_sites", strconv.FormatBool(enableHttp2ForNewSites)),
84+
resource.TestCheckResourceAttr(accountResourceName, "enable_http2_to_origin_for_new_sites", strconv.FormatBool(enableHttp2ToOriginForNewSites)),
85+
),
6986
},
7087
},
7188
})
@@ -127,10 +144,67 @@ func testCheckIncapsulaAccountExists(name string) resource.TestCheckFunc {
127144
}
128145

129146
func testCheckIncapsulaAccountConfigBasic(email string) string {
147+
return fmt.Sprintf(`
148+
resource "incapsula_account" "test-terraform-account" {
149+
email = "%s"
150+
account_name = "testTerraform"
151+
plan_id = "entTrial"
152+
support_all_tls_versions = "false"
153+
naked_domain_san_for_new_www_sites = "true"
154+
}`,
155+
email,
156+
)
157+
}
158+
159+
func testHttp2AccountConfig(email string, enableHttp2ForNewSites bool, enableHttp2ToOriginForNewSites bool) string {
130160
return fmt.Sprintf(`
131161
resource "incapsula_account" "test-terraform-account" {
132162
email = "%s"
163+
enable_http2_for_new_sites = "%t"
164+
enable_http2_to_origin_for_new_sites = "%t"
165+
account_name = "testTerraform"
166+
plan_id = "entTrial"
167+
support_all_tls_versions = "false"
133168
}`,
134169
email,
170+
enableHttp2ForNewSites,
171+
enableHttp2ToOriginForNewSites,
135172
)
136173
}
174+
175+
func SkipIfAccountTypeIsResellerEndUser(t *testing.T) resource.ErrorCheckFunc {
176+
return func(err error) error {
177+
if err == nil {
178+
return nil
179+
}
180+
if strings.Contains(err.Error(), "Operation not allowed") {
181+
t.Skipf("skipping test since account type is RESELLER_END_USER. Error: %s", err.Error())
182+
}
183+
184+
return err
185+
}
186+
}
187+
188+
func GenerateTestEmail(t *testing.T) string {
189+
if v := os.Getenv("INCAPSULA_API_ID"); v == "" {
190+
t.Fatal("INCAPSULA_API_ID must be set for acceptance tests")
191+
}
192+
193+
s3 := rand.NewSource(time.Now().UnixNano())
194+
r3 := rand.New(s3)
195+
generatedDomain = "id" + os.Getenv("INCAPSULA_API_ID") + strconv.Itoa(r3.Intn(1000)) + testEmail
196+
197+
return generatedDomain
198+
}
199+
200+
func testACCStateAccountID(s *terraform.State) (string, error) {
201+
for _, rs := range s.RootModule().Resources {
202+
if rs.Type != "incapsula_account" {
203+
continue
204+
}
205+
accountID := rs.Primary.ID
206+
207+
return accountID, nil
208+
}
209+
return "", fmt.Errorf("Error finding an Account\"")
210+
}

incapsula/resource_subaccount.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,18 @@ func resourceSubAccount() *schema.Resource {
5959
Optional: true,
6060
ValidateFunc: validation.StringInSlice([]string{"APAC", "EU", "US", "AU"}, false),
6161
},
62+
"enable_http2_for_new_sites": {
63+
Description: "Enable HTTP/2 for traffic between end-users (visitors) and Imperva for newly created SSL sites. Options are `true` and `false`. Defaults to `true`",
64+
Type: schema.TypeString,
65+
Default: "true",
66+
Optional: true,
67+
},
68+
"enable_http2_to_origin_for_new_sites": {
69+
Description: "Enable HTTP/2 support for traffic between Imperva and your origin server for newly created SSL sites. This option can only be 'true' once 'enable_http2_for_new_sites' is enabled for newly created sites. Options are `true` and `false`. Defaults to `false`",
70+
Type: schema.TypeString,
71+
Default: "false",
72+
Optional: true,
73+
},
6274
},
6375
}
6476
}
@@ -96,6 +108,11 @@ func resourceSubAccountCreate(d *schema.ResourceData, m interface{}) error {
96108
return err
97109
}
98110

111+
err = updateHttp2Properties(client, d)
112+
if err != nil {
113+
return err
114+
}
115+
99116
// There may be a timing/race condition here
100117
// Set an arbitrary period to sleep
101118
time.Sleep(3 * time.Second)
@@ -138,6 +155,8 @@ func resourceSubAccountRead(d *schema.ResourceData, m interface{}) error {
138155
return err
139156
}
140157
d.Set("data_storage_region", defaultAccountDataStorageRegion.Region)
158+
d.Set("enable_http2_for_new_sites", strconv.FormatBool(accountStatusResponse.Account.EnableHttp2ForNewSites))
159+
d.Set("enable_http2_to_origin_for_new_sites", strconv.FormatBool(accountStatusResponse.Account.EnableHttp2ToOriginForNewSites))
141160

142161
log.Printf("[INFO] Finished reading Incapsula account for account ud: %d\n", accountID)
143162

@@ -187,6 +206,11 @@ func resourceSubAccountUpdate(d *schema.ResourceData, m interface{}) error {
187206
return err
188207
}
189208

209+
err = updateHttp2Properties(client, d)
210+
if err != nil {
211+
return err
212+
}
213+
190214
// Set the rest of the state from the resource read
191215
return resourceAccountRead(d, m)
192216
}

0 commit comments

Comments
 (0)