Skip to content

Commit

Permalink
Changes for Per-App Delete Added
Browse files Browse the repository at this point in the history
  • Loading branch information
ramaniprateek committed Jun 11, 2024
1 parent 8e4d7ce commit f971baf
Show file tree
Hide file tree
Showing 7 changed files with 260 additions and 9 deletions.
31 changes: 22 additions & 9 deletions bigip/resource_bigip_as3.go
Original file line number Diff line number Diff line change
Expand Up @@ -452,20 +452,33 @@ func resourceBigipAs3Delete(ctx context.Context, d *schema.ResourceData, meta in
tList, _, _ = client.GetTenantList(d.Get("as3_json").(string))
}

if d.Id() != "" && tList != "" {
if d.Id() != "" && tList != "" && d.Get("tenant_filter") == "" {
name = tList
} else {
name = d.Id()
}
log.Printf("[INFO] Deleting As3 config for tenants:%+v", name)
err, failedTenants := client.DeleteAs3Bigip(name)
if err != nil {
log.Printf("[ERROR] Unable to DeleteContext: %v :", err)
return diag.FromErr(err)
}
if failedTenants != "" {
_ = d.Set("tenant_list", name)
return resourceBigipAs3Read(ctx, d, meta)
if d.Get("per_app_mode").(bool) {
applicationList := d.Get("application_list").(string)
log.Printf("[INFO] Deleting As3 config for Applications:%+v", applicationList)
for _, appName := range strings.Split(applicationList, ",") {
log.Printf("[INFO] Deleting AS3 for Application : %s", appName)
err := client.DeletePerApplicationAs3Bigip(name, appName)

Check failure on line 466 in bigip/resource_bigip_as3.go

View workflow job for this annotation

GitHub Actions / golint

client.DeletePerApplicationAs3Bigip undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method DeletePerApplicationAs3Bigip)) (typecheck)

Check failure on line 466 in bigip/resource_bigip_as3.go

View workflow job for this annotation

GitHub Actions / golint

client.DeletePerApplicationAs3Bigip undefined (type *"github.com/f5devcentral/go-bigip".BigIP has no field or method DeletePerApplicationAs3Bigip) (typecheck)
if err != nil {
log.Printf("[ERROR] Unable to DeleteContext: %v :", err)
return diag.FromErr(err)
}
}
} else {
err, failedTenants := client.DeleteAs3Bigip(name)
if err != nil {
log.Printf("[ERROR] Unable to DeleteContext: %v :", err)
return diag.FromErr(err)
}
if failedTenants != "" {
_ = d.Set("tenant_list", name)
return resourceBigipAs3Read(ctx, d, meta)
}
}
d.SetId("")
return nil
Expand Down
126 changes: 126 additions & 0 deletions bigip/resource_bigip_as3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"net/http"
"os"
"regexp"
"strings"
"testing"

bigip "github.com/f5devcentral/go-bigip"
Expand Down Expand Up @@ -98,6 +99,28 @@ resource "bigip_as3" "perapp2" {
ignore_metadata = true
}
`
var TestAs3PerAppResource = `
resource "bigip_as3" "as3-example" {
tenant_name = "dmz"
as3_json = "${file("` + dir + `/../examples/as3/perApplication_example.json")}"
}
`
var TestAs3PerAppResource1 = `
resource "bigip_as3" "as3-example1" {
tenant_name = "dmz"
as3_json = "${file("` + dir + `/../examples/as3/as3_per_app_example1.json")}"
}
`
var TestAs3PerAppResource2 = `
resource "bigip_as3" "as3-example1" {
tenant_name = "dmz"
as3_json = "${file("` + dir + `/../examples/as3/as3_per_app_example1.json")}"
}
resource "bigip_as3" "as3-example2" {
tenant_name = "dmz"
as3_json = "${file("` + dir + `/../examples/as3/as3_per_app_example2.json")}"
}
`

func TestAccBigipAs3_create_SingleTenant(t *testing.T) {
resource.Test(t, resource.TestCase{
Expand Down Expand Up @@ -434,6 +457,46 @@ func testCheckAs3Exists(name string, exists bool) resource.TestCheckFunc {
}
}

func testCheckAS3AppExists(tenantName, appNames string, exists bool) resource.TestCheckFunc {
return func(s *terraform.State) error {
clientBigip := testAccProvider.Meta().(*bigip.BigIP)
tr := &http.Transport{
TLSClientConfig: &tls.Config{InsecureSkipVerify: true}}
client := &http.Client{Transport: tr}

for _, appName := range strings.Split(appNames, ",") {
url := clientBigip.Host + "/mgmt/shared/appsvcs/declare/" + tenantName + "/applications/" + appName
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return fmt.Errorf("[ERROR] Error while creating http request with AS3 json: %v", err)
}
req.SetBasicAuth(clientBigip.User, clientBigip.Password)
req.Header.Set("Accept", "application/json")
req.Header.Set("Content-Type", "application/json")

resp, err := client.Do(req)
if err != nil {
return err
}

defer func() {
if err := resp.Body.Close(); err != nil {
log.Printf("[DEBUG] Could not close the request to %s", url)
}
}()
var body bytes.Buffer
_, err = io.Copy(&body, resp.Body)
// body, err := ioutil.ReadAll(resp.Body)
bodyString := body.String()
if (resp.Status == "204 No Content" || err != nil || resp.StatusCode == 404) && exists {
return fmt.Errorf("[ERROR] Error while checking as3resource present in bigip :%s %v", bodyString, err)
}
}

return nil
}
}

func TestAccBigipAs3_badJSON(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
Expand Down Expand Up @@ -464,3 +527,66 @@ func testCheckAs3Destroy(s *terraform.State) error {
}
return nil
}

func TestAccBigipPer_AppAs3_SingleTenant(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAcctPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testCheckAs3Destroy,
Steps: []resource.TestStep{
{
Config: TestAs3PerAppResource,
Check: resource.ComposeTestCheckFunc(
testCheckAs3Exists("dmz", true),
testCheckAS3AppExists("dmz", "Application1,Application2", true),
),
},
},
})
}

func TestAccBigipPer_AppAs3_update_addApplication(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAcctPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testCheckAs3Destroy,
Steps: []resource.TestStep{
{
Config: TestAs3PerAppResource1,

Check: resource.ComposeTestCheckFunc(
testCheckAs3Exists("dmz", true),
testCheckAS3AppExists("dmz", "path_app1", true),
),
},
{
Config: TestAs3PerAppResource2,
Check: resource.ComposeTestCheckFunc(
testCheckAs3Exists("dmz", true),
testCheckAS3AppExists("dmz", "path_app1,path_app2", true),
),
},
},
})
}

// Per-App mode is disabled
func TestAccBigipPer_AppAs3_update_invalidJson(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() {
testAcctPreCheck(t)
},
Providers: testAccProviders,
CheckDestroy: testCheckAs3Destroy,
Steps: []resource.TestStep{
{
Config: TestAs3PerAppResource1,
ExpectError: regexp.MustCompile("Invalid request value"),
},
},
})
}
25 changes: 25 additions & 0 deletions examples/as3/as3_per_app_example1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"schemaVersion": "3.50.0",
"path_app1": {
"class": "Application",
"vs_name_app1": {
"class": "Service_HTTP",
"virtualAddresses": [
"192.1.1.24"
],
"pool": "pool"
},
"pool": {
"class": "Pool",
"members": [
{
"servicePort": 80,
"serverAddresses": [
"192.20.1.10",
"192.30.1.20"
]
}
]
}
}
}
25 changes: 25 additions & 0 deletions examples/as3/as3_per_app_example2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"schemaVersion": "3.50.0",
"path_app2": {
"class": "Application",
"vs_name_app2": {
"class": "Service_HTTP",
"virtualAddresses": [
"192.1.1.234"
],
"pool": "pool"
},
"pool": {
"class": "Pool",
"members": [
{
"servicePort": 80,
"serverAddresses": [
"12.20.1.10",
"12.30.1.20"
]
}
]
}
}
}
6 changes: 6 additions & 0 deletions examples/as3/bigip_as3.tf
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,10 @@ resource "bigip_as3" "as3-example1" {
depends_on = ["null_resource.install_as3"]
}

// Per-Application Deployment example
resource "bigip_as3" "as3-example1" {
as3_json = file("perApplication_example.json")
tenant_name = "Test"
}


47 changes: 47 additions & 0 deletions examples/as3/perApplication_example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"schemaVersion": "3.50.0",
"Application1": {
"class": "Application",
"service": {
"class": "Service_HTTP",
"virtualAddresses": [
"192.0.2.1"
],
"pool": "pool"
},
"pool": {
"class": "Pool",
"members": [
{
"servicePort": 80,
"serverAddresses": [
"192.0.2.10",
"192.0.2.20"
]
}
]
}
},
"Application2": {
"class": "Application",
"service": {
"class": "Service_HTTP",
"virtualAddresses": [
"192.0.3.2"
],
"pool": "pool"
},
"pool": {
"class": "Pool",
"members": [
{
"servicePort": 80,
"serverAddresses": [
"192.0.3.30",
"192.0.3.40"
]
}
]
}
}
}
9 changes: 9 additions & 0 deletions vendor/github.com/f5devcentral/go-bigip/as3bigip.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f971baf

Please sign in to comment.