Skip to content

Commit

Permalink
Merge pull request #32 from wallarm/release-v1.1.2
Browse files Browse the repository at this point in the history
Release v1.1.2
  • Loading branch information
wallarm-akalashnikov authored Jul 21, 2023
2 parents fa933a5 + d4ed93d commit 303f103
Show file tree
Hide file tree
Showing 5 changed files with 110 additions and 4 deletions.
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ require (
github.com/hashicorp/go-cleanhttp v0.5.1
github.com/hashicorp/terraform-plugin-sdk v1.16.0
github.com/pkg/errors v0.9.1
github.com/wallarm/wallarm-go v0.0.25
github.com/wallarm/wallarm-go v0.0.26
golang.org/x/net v0.0.0-20210726213435-c6fcb2dbf985 // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
golang.org/x/tools v0.1.5 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,8 @@ github.com/wallarm/wallarm-go v0.0.24 h1:TbGn0Re1/TC0d94qUJntkJnQiCjPVPZ0p3wlzVv
github.com/wallarm/wallarm-go v0.0.24/go.mod h1:KQxO+EBaGpIgOqBoByKW4KNMEJFgkxR64FSiA4U/52I=
github.com/wallarm/wallarm-go v0.0.25 h1:kE5IlElUC9ynLNF3+Adnu2jZ6rdHhiJ1MbxGoN7Dra8=
github.com/wallarm/wallarm-go v0.0.25/go.mod h1:KQxO+EBaGpIgOqBoByKW4KNMEJFgkxR64FSiA4U/52I=
github.com/wallarm/wallarm-go v0.0.26 h1:tiLMBfDMcR60Zy6PENJOXQfOQu7x0fQJFEXtdjsSs+0=
github.com/wallarm/wallarm-go v0.0.26/go.mod h1:KQxO+EBaGpIgOqBoByKW4KNMEJFgkxR64FSiA4U/52I=
github.com/xanzy/ssh-agent v0.2.1 h1:TCbipTQL2JiiCprBWx9frJ2eJlCYT00NmctrHxVAr70=
github.com/xanzy/ssh-agent v0.2.1/go.mod h1:mLlQY/MoOhWBj+gOGMQkOeiEvkx+8pJSI+0Bx9h2kr4=
github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
Expand Down
11 changes: 10 additions & 1 deletion wallarm/resource_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,16 @@ func resourceWallarmNodeDelete(d *schema.ResourceData, m interface{}) error {
client := m.(wallarm.API)
nodeID := d.Get("node_id").(int)
if err := client.NodeDelete(nodeID); err != nil {
return err
isNotFoundErr, err2 := isNotFoundError(err)
if err2 != nil {
return err2
}

if isNotFoundErr {
fmt.Print("Resource has already been deleted")
} else {
return err
}
}
return nil
}
Expand Down
76 changes: 74 additions & 2 deletions wallarm/resource_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,27 @@ package wallarm

import (
"fmt"
"strconv"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
wallarm "github.com/wallarm/wallarm-go"
)

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

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckWallarmDestroy(name),
Steps: []resource.TestStep{
{
Config: testWallarmNodeConfig(rnd, "tf-test-"+rnd),
Check: resource.ComposeTestCheckFunc(
testAccCheckWallarmNodeExists(name),
resource.TestCheckResourceAttr(name, "hostname", "tf-test-"+rnd),
),
},
Expand All @@ -31,3 +36,70 @@ resource "wallarm_node" "%[1]s" {
hostname = "%[2]s"
}`, resourceID, hostname)
}

func testAccCheckWallarmDestroy(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(wallarm.API)

for _, resource := range s.RootModule().Resources {
if resource.Type != name {
continue
}

clientID, err := strconv.Atoi(resource.Primary.Attributes["client_id"])
if err != nil {
return err
}

hostname := resource.Primary.Attributes["hostname"]

// TODO: Add filter by hostname in wallarm-go and use here and resource
nodes, err := client.NodeRead(clientID, "all")
if err != nil {
return err
}

for _, node := range nodes.Body {
if node.Hostname == hostname {
return fmt.Errorf("Resource still exists: %s", name)
}
}

return nil
}

return nil
}
}

func testAccCheckWallarmNodeExists(name string) resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(wallarm.API)

resource, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Not found: %s", name)
}

clientID, err := strconv.Atoi(resource.Primary.Attributes["client_id"])
if err != nil {
return err
}

hostname := resource.Primary.Attributes["hostname"]

// TODO: Add filter by hostname in wallarm-go and use here and resource
nodes, err := client.NodeRead(clientID, "all")
if err != nil {
return err
}

for _, node := range nodes.Body {
if node.Hostname == hostname {
return nil
}
}

return fmt.Errorf("WallarmNode not found: %s", name)
}
}
23 changes: 23 additions & 0 deletions wallarm/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"encoding/json"
"fmt"
"math/rand"
"regexp"
"sort"
"strconv"
"strings"
Expand Down Expand Up @@ -558,7 +559,9 @@ func existsAction(d *schema.ResourceData, m interface{}, hintType string) (strin
}

for _, body := range respRules.Body {

var apiActions []wallarm.ActionDetails = nil

for _, condition := range body.Conditions {
apiAct := condition.(map[string]interface{})
result, err := json.Marshal(apiAct)
Expand All @@ -584,6 +587,16 @@ func existsHint(d *schema.ResourceData, m interface{}, actionID int, hintType st
client := m.(wallarm.API)
clientID := retrieveClientID(d, client)

var points wallarm.TwoDimensionalSlice

if ps, ok := d.GetOk("point"); ok {
var err error
points, err = expandPointsToTwoDimensionalArray(ps.([]interface{}))
if err != nil {
return "", false, err
}
}

hint := &wallarm.HintRead{
Limit: 1000,
Offset: 0,
Expand All @@ -593,6 +606,7 @@ func existsHint(d *schema.ResourceData, m interface{}, actionID int, hintType st
Clientid: []int{clientID},
ActionID: []int{actionID},
Type: []string{hintType},
Point: points,
},
}
actionHints, err := client.HintRead(hint)
Expand All @@ -616,3 +630,12 @@ func ImportAsExistsError(resourceName, id string) error {
"- to be managed via Terraform this resource needs to be imported into the State. "+
"Please see the resource documentation for %q for more information.", id, resourceName)
}

func isNotFoundError(err error) (bool, error) {
matched, matchErr := regexp.MatchString("HTTP Status: 404", err.Error())
if matchErr != nil {
return false, matchErr
}

return matched, nil
}

0 comments on commit 303f103

Please sign in to comment.