Skip to content

Commit 216e22d

Browse files
authored
Support caid for account role editing (#610)
1 parent 2dfbe08 commit 216e22d

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

incapsula/client_account_role.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,8 @@ func (c *Client) GetAccountRole(roleId int) (*RoleDetailsDTO, error) {
121121
}
122122

123123
// UpdateAccountRole - Update the Account Role for a given role ID
124-
func (c *Client) UpdateAccountRole(roleId int, requestDTO RoleDetailsBasicDTO) (*RoleDetailsDTO, error) {
125-
log.Printf("[INFO] Updating Incapsula account role (Id: %d)\n", roleId)
124+
func (c *Client) UpdateAccountRole(roleId int, accountId int, requestDTO RoleDetailsBasicDTO) (*RoleDetailsDTO, error) {
125+
log.Printf("[INFO] Updating Incapsula account role (Id: %d, Account Id: %d)\n", roleId, accountId)
126126

127127
log.Printf("[INFO] requestDTO: %+v\n", requestDTO)
128128

@@ -132,7 +132,8 @@ func (c *Client) UpdateAccountRole(roleId int, requestDTO RoleDetailsBasicDTO) (
132132
reqURL := fmt.Sprintf("%s/%s/%d", c.config.BaseURLAPI, endpointRoleUpdate, roleId)
133133
log.Printf("[INFO] reqURL: %v\n", reqURL)
134134

135-
resp, err := c.DoJsonRequestWithHeaders(http.MethodPost, reqURL, roleJSON, UpdateAccountRole)
135+
params := GetRequestParamsWithCaid(accountId)
136+
resp, err := c.DoJsonAndQueryParamsRequestWithHeaders(http.MethodPost, reqURL, roleJSON, params, UpdateAccountRole)
136137
if err != nil {
137138
return nil, fmt.Errorf("Error updating account role with Id %d: %s", roleId, err)
138139
}

incapsula/client_account_role_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,9 @@ func TestClientUpdateRoleBadConnection(t *testing.T) {
143143
config := &Config{APIID: "foo", APIKey: "bar", BaseURL: "badness.incapsula.com"}
144144
client := &Client{config: config, httpClient: &http.Client{Timeout: time.Millisecond * 1}}
145145
roleID := 123
146+
accountID := 456
146147
requestDTO := RoleDetailsBasicDTO{}
147-
updateRoleResponse, err := client.UpdateAccountRole(roleID, requestDTO)
148+
updateRoleResponse, err := client.UpdateAccountRole(roleID, accountID, requestDTO)
148149
if err == nil {
149150
t.Errorf("Should have received an error")
150151
}
@@ -158,9 +159,11 @@ func TestClientUpdateRoleBadConnection(t *testing.T) {
158159

159160
func TestClientUpdateRoleBadJSON(t *testing.T) {
160161
roleID := 123
162+
accountID := 456
161163
server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, req *http.Request) {
162-
if req.URL.String() != fmt.Sprintf("/%s/%d", endpointRoleUpdate, roleID) {
163-
t.Errorf("Should have have hit /%s/%d endpoint. Got: %s", endpointAccountUpdate, roleID, req.URL.String())
164+
expectedURL := fmt.Sprintf("/%s/%d?caid=%d", endpointRoleUpdate, roleID, accountID)
165+
if req.URL.String() != expectedURL {
166+
t.Errorf("Should have hit %s endpoint. Got: %s", expectedURL, req.URL.String())
164167
}
165168
rw.Write([]byte(`{`))
166169
}))
@@ -169,7 +172,7 @@ func TestClientUpdateRoleBadJSON(t *testing.T) {
169172
config := &Config{APIID: "foo", APIKey: "bar", BaseURLAPI: server.URL}
170173
client := &Client{config: config, httpClient: &http.Client{}}
171174
requestDTO := RoleDetailsBasicDTO{}
172-
updateRoleResponse, err := client.UpdateAccountRole(roleID, requestDTO)
175+
updateRoleResponse, err := client.UpdateAccountRole(roleID, accountID, requestDTO)
173176
if err == nil {
174177
t.Errorf("Should have received an error")
175178
}

incapsula/resource_account_role.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,11 +142,12 @@ func resourceAccountRoleRead(d *schema.ResourceData, m interface{}) error {
142142
func resourceAccountRoleUpdate(d *schema.ResourceData, m interface{}) error {
143143
client := m.(*Client)
144144
roleID, _ := strconv.Atoi(d.Id())
145+
accountId := d.Get("account_id").(int)
145146

146-
log.Printf("[INFO] Updating Incapsula account role with id: %d\n", roleID)
147+
log.Printf("[INFO] Updating Incapsula account role with id: %d (account id: %d)\n", roleID, accountId)
147148

148149
requestDTO := populateRoleDetailsDTO(d)
149-
responseDTO, err := client.UpdateAccountRole(roleID, requestDTO)
150+
responseDTO, err := client.UpdateAccountRole(roleID, accountId, requestDTO)
150151

151152
if err != nil {
152153
log.Printf("[ERROR] Could not create Incapsula account role: %s\n", err)

0 commit comments

Comments
 (0)