Skip to content

Commit ae8f4ab

Browse files
authored
V384 (#198)
* remove unnecessary code and fix tests * bump version to 3.8.4
1 parent 7571d28 commit ae8f4ab

File tree

10 files changed

+22
-81
lines changed

10 files changed

+22
-81
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11

2+
## 3.8.4 (Aug 17, 2022)
3+
4+
BUG FIXES:
5+
6+
* incapsula_subaccount: fix 'read' method to use another API to the backend ([#197](https://github.com/imperva/terraform-provider-incapsula/pull/197))
7+
28
## 3.8.3 (Aug 11, 2022)
39

410
BUG FIXES:

GNUmakefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ NAMESPACE=terraform-providers
66
PKG_NAME=incapsula
77
BINARY=terraform-provider-${PKG_NAME}
88
# Whenever bumping provider version, please update the version in incapsula/client.go (line 27) as well.
9-
VERSION=3.8.3
9+
VERSION=3.8.4
1010

1111

1212
OS_ARCH=darwin_amd64

incapsula/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type Client struct {
2626
func NewClient(config *Config) *Client {
2727
client := &http.Client{}
2828

29-
return &Client{config: config, httpClient: client, providerVersion: "3.8.3"}
29+
return &Client{config: config, httpClient: client, providerVersion: "3.8.4"}
3030
}
3131

3232
// Verify checks the API credentials

incapsula/client_account.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,13 +136,13 @@ func (c *Client) AddAccount(email, refID, userName, planID, accountName, logLeve
136136
}
137137

138138
// AccountStatus gets the Incapsula managed account's status
139-
func (c *Client) AccountStatus(accountID int) (*AccountStatusResponse, error) {
139+
func (c *Client) AccountStatus(accountID int, operation string) (*AccountStatusResponse, error) {
140140
log.Printf("[INFO] Getting Incapsula account status for account id: %d\n", accountID)
141141

142142
// Post form to Incapsula
143143
values := url.Values{"account_id": {strconv.Itoa(accountID)}}
144144
reqURL := fmt.Sprintf("%s/%s", c.config.BaseURL, endpointAccountStatus)
145-
resp, err := c.PostFormWithHeaders(reqURL, values, ReadAccount)
145+
resp, err := c.PostFormWithHeaders(reqURL, values, operation)
146146
if err != nil {
147147
return nil, fmt.Errorf("Error getting account status for account id %d: %s", accountID, err)
148148
}

incapsula/client_account_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func TestClientAccountStatusBadConnection(t *testing.T) {
112112
config := &Config{APIID: "foo", APIKey: "bar", BaseURL: "badness.incapsula.com"}
113113
client := &Client{config: config, httpClient: &http.Client{Timeout: time.Millisecond * 1}}
114114
accountID := 123
115-
accountStatusResponse, err := client.AccountStatus(accountID)
115+
accountStatusResponse, err := client.AccountStatus(accountID, ReadAccount)
116116
if err == nil {
117117
t.Errorf("Should have received an error")
118118
}
@@ -136,7 +136,7 @@ func TestClientAccountStatusBadJSON(t *testing.T) {
136136
config := &Config{APIID: "foo", APIKey: "bar", BaseURL: server.URL}
137137
client := &Client{config: config, httpClient: &http.Client{}}
138138
accountID := 123
139-
accountStatusResponse, err := client.AccountStatus(accountID)
139+
accountStatusResponse, err := client.AccountStatus(accountID, ReadAccount)
140140
if err == nil {
141141
t.Errorf("Should have received an error")
142142
}
@@ -160,7 +160,7 @@ func TestClientAccountStatusInvalidAccount(t *testing.T) {
160160
config := &Config{APIID: "foo", APIKey: "bar", BaseURL: server.URL}
161161
client := &Client{config: config, httpClient: &http.Client{}}
162162
accountID := 123
163-
accountStatusResponse, err := client.AccountStatus(accountID)
163+
accountStatusResponse, err := client.AccountStatus(accountID, ReadAccount)
164164
if err == nil {
165165
t.Errorf("Should have received an error")
166166
}
@@ -184,7 +184,7 @@ func TestClientAccountStatusValidAccount(t *testing.T) {
184184
config := &Config{APIID: "foo", APIKey: "bar", BaseURL: server.URL}
185185
client := &Client{config: config, httpClient: &http.Client{}}
186186
accountID := 123
187-
accountStatusResponse, err := client.AccountStatus(accountID)
187+
accountStatusResponse, err := client.AccountStatus(accountID, ReadAccount)
188188
if err != nil {
189189
t.Errorf("Should not have received an error")
190190
}

incapsula/client_subaccount.go

Lines changed: 0 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import (
1111

1212
// Endpoints (unexported consts)
1313
const endpointSubAccountAdd = "subaccounts/add"
14-
const endpointSubAccountList = "accounts/listSubAccounts"
1514
const endpointSubAccountDelete = "subaccounts/delete"
1615
const PAGE_SIZE = 50
1716

@@ -102,70 +101,6 @@ func (c *Client) AddSubAccount(subAccountPayload *SubAccountPayload) (*SubAccoun
102101
return &subAccountAddResponse, nil
103102
}
104103

105-
// GetSubAccount gets the Incapsula list of SubAccounts
106-
func (c *Client) GetSubAccount(parentAccountID int, subAccountID int) (*SubAccount, error) {
107-
108-
log.Printf("[INFO] Reading Incapsula subaccounts for id: %d", subAccountID)
109-
110-
var count = 0
111-
var shouldFetch = true
112-
// Pagination (default page size 50)
113-
for shouldFetch {
114-
log.Printf("[DEBUG] looking for subaccount %d, fetching for page: %d", subAccountID, count)
115-
var subAccounts, error = c.sendListSubAccountsRequest(parentAccountID, count)
116-
if error != nil {
117-
return nil, error
118-
}
119-
for _, subAccount := range subAccounts {
120-
if subAccount.SubAccountID == subAccountID {
121-
log.Printf("[INFO] found subaccount : %v\n", subAccount)
122-
return &subAccount, nil
123-
}
124-
}
125-
shouldFetch = len(subAccounts) == PAGE_SIZE
126-
count += 1
127-
}
128-
log.Printf("[DEBUG] didn't find subaccount %d returning nil", subAccountID)
129-
return nil, nil
130-
}
131-
132-
func (c *Client) sendListSubAccountsRequest(accountId int, pageNum int) ([]SubAccount, error) {
133-
values := map[string][]string{}
134-
135-
if accountId != 0 {
136-
values["account_id"] = make([]string, 1)
137-
values["account_id"][0] = fmt.Sprint(accountId)
138-
}
139-
values["page_num"] = make([]string, 1)
140-
values["page_num"][0] = fmt.Sprint(pageNum)
141-
values["page_size"] = make([]string, 1)
142-
values["page_size"][0] = fmt.Sprint(PAGE_SIZE)
143-
144-
log.Printf("[INFO] Pagination loop, page : %d)\n", pageNum)
145-
146-
// Post form to Incapsula
147-
resp, err := c.PostFormWithHeaders(fmt.Sprintf("%s/%s", c.config.BaseURL, endpointSubAccountList), values, ReadSubAccount)
148-
if err != nil {
149-
return nil, fmt.Errorf("Error getting subaccounts for account %d: %s", accountId, err)
150-
}
151-
152-
// Read the body
153-
defer resp.Body.Close()
154-
responseBody, err := ioutil.ReadAll(resp.Body)
155-
156-
// Dump JSON
157-
log.Printf("[DEBUG] Incapsula subaccounts JSON response: %s\n", string(responseBody))
158-
159-
// Parse the JSON
160-
var subAccountListResponse SubAccountListResponse
161-
err = json.Unmarshal([]byte(responseBody), &subAccountListResponse)
162-
if err != nil {
163-
return nil, fmt.Errorf("Error parsing subaccounts list JSON response for accountid: %d %s\nresponse: %s", accountId, err, string(responseBody))
164-
}
165-
166-
return subAccountListResponse.SubAccounts, nil
167-
}
168-
169104
// DeleteSubAccount deletes a SubAcccount currently managed by Incapsula
170105
func (c *Client) DeleteSubAccount(subAccountID int) error {
171106
// Specifically shaded this struct, no need to share across funcs or export

incapsula/resource_account.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ func resourceAccountRead(d *schema.ResourceData, m interface{}) error {
172172

173173
log.Printf("[INFO] Reading Incapsula account for Account ID: %d\n", accountID)
174174

175-
accountStatusResponse, err := client.AccountStatus(accountID)
175+
accountStatusResponse, err := client.AccountStatus(accountID, ReadAccount)
176176

177177
// Account object may have been deleted
178178
if accountStatusResponse != nil && accountStatusResponse.Res.(float64) == 9403 {

incapsula/resource_account_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func testCheckIncapsulaAccountDestroy(state *terraform.State) error {
8989
return fmt.Errorf("Account ID conversion error for %s: %s", accountIDStr, err)
9090
}
9191

92-
_, err = client.AccountStatus(accountID)
92+
_, err = client.AccountStatus(accountID, ReadAccount)
9393

9494
if err == nil {
9595
return fmt.Errorf("Incapsula account id: %d still exists", accountID)
@@ -117,7 +117,7 @@ func testCheckIncapsulaAccountExists(name string) resource.TestCheckFunc {
117117
}
118118

119119
client := testAccProvider.Meta().(*Client)
120-
accountStatusResponse, err := client.AccountStatus(accountID)
120+
accountStatusResponse, err := client.AccountStatus(accountID, ReadAccount)
121121
if accountStatusResponse == nil {
122122
return fmt.Errorf("Incapsula account id: %d does not exist", accountID)
123123
}

incapsula/resource_subaccount.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ func resourceSubAccountRead(d *schema.ResourceData, m interface{}) error {
9898

9999
log.Printf("[INFO] Reading Incapsula account for Account ID: %d\n", accountID)
100100

101-
accountStatusResponse, err := client.AccountStatus(accountID)
101+
accountStatusResponse, err := client.AccountStatus(accountID, ReadSubAccount)
102102

103103
// Account object may have been deleted
104104
if accountStatusResponse != nil && accountStatusResponse.Res.(float64) == 9403 {

incapsula/resource_subaccount_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,14 @@ func testAccIncapsulaSubAccountDestroy(state *terraform.State) error {
5959
subAccountIDStr := res.Primary.ID
6060
subAccountID, _ := strconv.Atoi(subAccountIDStr)
6161

62-
subAccount, err := client.GetSubAccount(0, subAccountID)
62+
subAccount, err := client.AccountStatus(subAccountID, ReadSubAccount)
6363
if err != nil {
6464
return err
6565
}
6666

6767
found := false
6868

69-
if subAccount != nil && subAccount.SubAccountID == subAccountID {
69+
if subAccount != nil && subAccount.AccountID == subAccountID {
7070
log.Printf("[INFO] subaccount : %v\n", subAccount)
7171
found = true
7272
}
@@ -116,14 +116,14 @@ func testCheckIncapsulaSubAccountExists() resource.TestCheckFunc {
116116

117117
client := testAccProvider.Meta().(*Client)
118118
log.Printf("[INFO] **** subAccountID: %d", subAccountID)
119-
subAccount, err := client.GetSubAccount(0, subAccountID)
119+
subAccount, err := client.AccountStatus(subAccountID, ReadSubAccount)
120120
if err != nil {
121121
return err
122122
}
123123

124124
found := false
125125

126-
if subAccount != nil && subAccount.SubAccountID == subAccountID {
126+
if subAccount != nil && subAccount.AccountID == subAccountID {
127127
log.Printf("[INFO] subaccount : %v\n", subAccount)
128128
found = true
129129
}

0 commit comments

Comments
 (0)