Skip to content

Commit 303f103

Browse files
Merge pull request #32 from wallarm/release-v1.1.2
Release v1.1.2
2 parents fa933a5 + d4ed93d commit 303f103

5 files changed

Lines changed: 110 additions & 4 deletions

File tree

go.mod

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ require (
77
github.com/hashicorp/go-cleanhttp v0.5.1
88
github.com/hashicorp/terraform-plugin-sdk v1.16.0
99
github.com/pkg/errors v0.9.1
10-
github.com/wallarm/wallarm-go v0.0.25
10+
github.com/wallarm/wallarm-go v0.0.26
1111
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
1212
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
1313
golang.org/x/tools v0.1.5 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,8 @@ github.com/wallarm/wallarm-go v0.0.24 h1:TbGn0Re1/TC0d94qUJntkJnQiCjPVPZ0p3wlzVv
310310
github.com/wallarm/wallarm-go v0.0.24/go.mod h1:KQxO+EBaGpIgOqBoByKW4KNMEJFgkxR64FSiA4U/52I=
311311
github.com/wallarm/wallarm-go v0.0.25 h1:kE5IlElUC9ynLNF3+Adnu2jZ6rdHhiJ1MbxGoN7Dra8=
312312
github.com/wallarm/wallarm-go v0.0.25/go.mod h1:KQxO+EBaGpIgOqBoByKW4KNMEJFgkxR64FSiA4U/52I=
313+
github.com/wallarm/wallarm-go v0.0.26 h1:tiLMBfDMcR60Zy6PENJOXQfOQu7x0fQJFEXtdjsSs+0=
314+
github.com/wallarm/wallarm-go v0.0.26/go.mod h1:KQxO+EBaGpIgOqBoByKW4KNMEJFgkxR64FSiA4U/52I=
313315
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
314316
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
315317
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=

wallarm/resource_node.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,16 @@ func resourceWallarmNodeDelete(d *schema.ResourceData, m interface{}) error {
141141
client := m.(wallarm.API)
142142
nodeID := d.Get("node_id").(int)
143143
if err := client.NodeDelete(nodeID); err != nil {
144-
return err
144+
isNotFoundErr, err2 := isNotFoundError(err)
145+
if err2 != nil {
146+
return err2
147+
}
148+
149+
if isNotFoundErr {
150+
fmt.Print("Resource has already been deleted")
151+
} else {
152+
return err
153+
}
145154
}
146155
return nil
147156
}

wallarm/resource_node_test.go

Lines changed: 74 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,27 @@ package wallarm
22

33
import (
44
"fmt"
5+
"strconv"
56
"testing"
67

78
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
10+
wallarm "github.com/wallarm/wallarm-go"
811
)
912

1013
func TestAccWallarmNode(t *testing.T) {
1114
rnd := generateRandomResourceName(10)
1215
name := "wallarm_node." + rnd
1316

1417
resource.Test(t, resource.TestCase{
15-
PreCheck: func() { testAccPreCheck(t) },
16-
Providers: testAccProviders,
18+
PreCheck: func() { testAccPreCheck(t) },
19+
Providers: testAccProviders,
20+
CheckDestroy: testAccCheckWallarmDestroy(name),
1721
Steps: []resource.TestStep{
1822
{
1923
Config: testWallarmNodeConfig(rnd, "tf-test-"+rnd),
2024
Check: resource.ComposeTestCheckFunc(
25+
testAccCheckWallarmNodeExists(name),
2126
resource.TestCheckResourceAttr(name, "hostname", "tf-test-"+rnd),
2227
),
2328
},
@@ -31,3 +36,70 @@ resource "wallarm_node" "%[1]s" {
3136
hostname = "%[2]s"
3237
}`, resourceID, hostname)
3338
}
39+
40+
func testAccCheckWallarmDestroy(name string) resource.TestCheckFunc {
41+
return func(s *terraform.State) error {
42+
client := testAccProvider.Meta().(wallarm.API)
43+
44+
for _, resource := range s.RootModule().Resources {
45+
if resource.Type != name {
46+
continue
47+
}
48+
49+
clientID, err := strconv.Atoi(resource.Primary.Attributes["client_id"])
50+
if err != nil {
51+
return err
52+
}
53+
54+
hostname := resource.Primary.Attributes["hostname"]
55+
56+
// TODO: Add filter by hostname in wallarm-go and use here and resource
57+
nodes, err := client.NodeRead(clientID, "all")
58+
if err != nil {
59+
return err
60+
}
61+
62+
for _, node := range nodes.Body {
63+
if node.Hostname == hostname {
64+
return fmt.Errorf("Resource still exists: %s", name)
65+
}
66+
}
67+
68+
return nil
69+
}
70+
71+
return nil
72+
}
73+
}
74+
75+
func testAccCheckWallarmNodeExists(name string) resource.TestCheckFunc {
76+
return func(s *terraform.State) error {
77+
client := testAccProvider.Meta().(wallarm.API)
78+
79+
resource, ok := s.RootModule().Resources[name]
80+
if !ok {
81+
return fmt.Errorf("Not found: %s", name)
82+
}
83+
84+
clientID, err := strconv.Atoi(resource.Primary.Attributes["client_id"])
85+
if err != nil {
86+
return err
87+
}
88+
89+
hostname := resource.Primary.Attributes["hostname"]
90+
91+
// TODO: Add filter by hostname in wallarm-go and use here and resource
92+
nodes, err := client.NodeRead(clientID, "all")
93+
if err != nil {
94+
return err
95+
}
96+
97+
for _, node := range nodes.Body {
98+
if node.Hostname == hostname {
99+
return nil
100+
}
101+
}
102+
103+
return fmt.Errorf("WallarmNode not found: %s", name)
104+
}
105+
}

wallarm/utils.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"encoding/json"
66
"fmt"
77
"math/rand"
8+
"regexp"
89
"sort"
910
"strconv"
1011
"strings"
@@ -558,7 +559,9 @@ func existsAction(d *schema.ResourceData, m interface{}, hintType string) (strin
558559
}
559560

560561
for _, body := range respRules.Body {
562+
561563
var apiActions []wallarm.ActionDetails = nil
564+
562565
for _, condition := range body.Conditions {
563566
apiAct := condition.(map[string]interface{})
564567
result, err := json.Marshal(apiAct)
@@ -584,6 +587,16 @@ func existsHint(d *schema.ResourceData, m interface{}, actionID int, hintType st
584587
client := m.(wallarm.API)
585588
clientID := retrieveClientID(d, client)
586589

590+
var points wallarm.TwoDimensionalSlice
591+
592+
if ps, ok := d.GetOk("point"); ok {
593+
var err error
594+
points, err = expandPointsToTwoDimensionalArray(ps.([]interface{}))
595+
if err != nil {
596+
return "", false, err
597+
}
598+
}
599+
587600
hint := &wallarm.HintRead{
588601
Limit: 1000,
589602
Offset: 0,
@@ -593,6 +606,7 @@ func existsHint(d *schema.ResourceData, m interface{}, actionID int, hintType st
593606
Clientid: []int{clientID},
594607
ActionID: []int{actionID},
595608
Type: []string{hintType},
609+
Point: points,
596610
},
597611
}
598612
actionHints, err := client.HintRead(hint)
@@ -616,3 +630,12 @@ func ImportAsExistsError(resourceName, id string) error {
616630
"- to be managed via Terraform this resource needs to be imported into the State. "+
617631
"Please see the resource documentation for %q for more information.", id, resourceName)
618632
}
633+
634+
func isNotFoundError(err error) (bool, error) {
635+
matched, matchErr := regexp.MatchString("HTTP Status: 404", err.Error())
636+
if matchErr != nil {
637+
return false, matchErr
638+
}
639+
640+
return matched, nil
641+
}

0 commit comments

Comments
 (0)