Skip to content

Commit 3ee2111

Browse files
Merge pull request #926 from F5Networks/devel_jira1416
adding bot-defence profile
2 parents a2da335 + 98f8a8a commit 3ee2111

File tree

4 files changed

+268
-0
lines changed

4 files changed

+268
-0
lines changed

bigip/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,7 @@ func Provider() *schema.Provider {
190190
"bigip_ltm_cipher_group": resourceBigipLtmCipherGroup(),
191191
"bigip_partition": resourceBigipPartition(),
192192
"bigip_ltm_request_log_profile": resourceBigipLtmProfileRequestLog(),
193+
"bigip_ltm_profile_bot_defense": resourceBigipLtmProfileBotDefense(),
193194
},
194195
}
195196
p.ConfigureContextFunc = func(ctx context.Context, d *schema.ResourceData) (interface{}, diag.Diagnostics) {
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
/*
2+
Copyright 2024 F5 Networks Inc.
3+
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4+
If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
5+
*/
6+
package bigip
7+
8+
import (
9+
"context"
10+
"log"
11+
12+
bigip "github.com/f5devcentral/go-bigip"
13+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
14+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
15+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
16+
)
17+
18+
func resourceBigipLtmProfileBotDefense() *schema.Resource {
19+
return &schema.Resource{
20+
CreateContext: resourceBigipLtmProfileBotDefenseCreate,
21+
ReadContext: resourceBigipLtmProfileBotDefenseRead,
22+
UpdateContext: resourceBigipLtmProfileBotDefenseUpdate,
23+
DeleteContext: resourceBigipLtmProfileBotDefenseDelete,
24+
Importer: &schema.ResourceImporter{
25+
StateContext: schema.ImportStatePassthroughContext,
26+
},
27+
Schema: map[string]*schema.Schema{
28+
"name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
ForceNew: true,
32+
Description: "Name of the Bot Defense profile",
33+
ValidateFunc: validateF5NameWithDirectory,
34+
},
35+
"defaults_from": {
36+
Type: schema.TypeString,
37+
Optional: true,
38+
Default: "/Common/bot-defense",
39+
Description: "Specifies the profile from which this profile inherits settings. The default is the system-supplied `request-log` profile",
40+
ValidateFunc: validateF5Name,
41+
},
42+
"description": {
43+
Type: schema.TypeString,
44+
Optional: true,
45+
Computed: true,
46+
Description: "User defined description for Bot Defense profile",
47+
},
48+
"template": {
49+
Type: schema.TypeString,
50+
Optional: true,
51+
Computed: true,
52+
ValidateFunc: validation.StringInSlice([]string{
53+
"relaxed",
54+
"enabled"}, false),
55+
Description: "Enables or disables Bot Defense. The default is `disabled`",
56+
},
57+
"enforcement_mode": {
58+
Type: schema.TypeString,
59+
Optional: true,
60+
Computed: true,
61+
ValidateFunc: validation.StringInSlice([]string{
62+
"transparent",
63+
"blocking"}, false),
64+
Description: "Specifies the protocol to be used for high-speed logging of requests. The default is `mds-udp`",
65+
},
66+
},
67+
}
68+
}
69+
70+
func resourceBigipLtmProfileBotDefenseCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
71+
client := meta.(*bigip.BigIP)
72+
name := d.Get("name").(string)
73+
log.Printf("[INFO] Creating Bot Defense Profile:%+v ", name)
74+
pss := &bigip.BotDefenseProfile{
75+
Name: name,
76+
}
77+
config := getProfileBotDefenseConfig(d, pss)
78+
log.Printf("[DEBUG] Bot Defense Profile config :%+v ", config)
79+
err := client.AddBotDefenseProfile(config)
80+
if err != nil {
81+
return diag.FromErr(err)
82+
}
83+
d.SetId(name)
84+
return resourceBigipLtmProfileBotDefenseRead(ctx, d, meta)
85+
}
86+
87+
func resourceBigipLtmProfileBotDefenseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
client := meta.(*bigip.BigIP)
89+
log.Printf("[INFO] Reading Bot Defense Profile:%+v ", client)
90+
name := d.Id()
91+
log.Printf("[INFO] Reading Bot Defense Profile:%+v ", name)
92+
botProfile, err := client.GetBotDefenseProfile(name)
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
log.Printf("[DEBUG] Bot Defense Profile config :%+v ", botProfile)
97+
d.Set("name", botProfile.FullPath)
98+
d.Set("defaults_from", botProfile.DefaultsFrom)
99+
d.Set("description", botProfile.Description)
100+
d.Set("template", botProfile.Template)
101+
d.Set("enforcement_mode", botProfile.EnforcementMode)
102+
return nil
103+
}
104+
105+
func resourceBigipLtmProfileBotDefenseUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
106+
client := meta.(*bigip.BigIP)
107+
name := d.Id()
108+
log.Printf("[INFO] Updating Bot Defense Profile:%+v ", name)
109+
pss := &bigip.BotDefenseProfile{
110+
Name: name,
111+
}
112+
config := getProfileBotDefenseConfig(d, pss)
113+
114+
err := client.ModifyBotDefenseProfile(name, config)
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
return resourceBigipLtmProfileBotDefenseRead(ctx, d, meta)
119+
}
120+
121+
func resourceBigipLtmProfileBotDefenseDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
122+
client := meta.(*bigip.BigIP)
123+
124+
name := d.Id()
125+
log.Println("[INFO] Deleting Bot Defense Profile " + name)
126+
err := client.DeleteBotDefenseProfile(name)
127+
if err != nil {
128+
return diag.FromErr(err)
129+
}
130+
131+
d.SetId("")
132+
return nil
133+
}
134+
135+
func getProfileBotDefenseConfig(d *schema.ResourceData, config *bigip.BotDefenseProfile) *bigip.BotDefenseProfile {
136+
config.Name = d.Get("name").(string)
137+
config.DefaultsFrom = d.Get("defaults_from").(string)
138+
config.Description = d.Get("description").(string)
139+
config.Template = d.Get("template").(string)
140+
config.EnforcementMode = d.Get("enforcement_mode").(string)
141+
log.Printf("[INFO][getProfileBotDefenseConfig] config:%+v ", config)
142+
return config
143+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
package bigip
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
bigip "github.com/f5devcentral/go-bigip"
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
)
11+
12+
var resBotDefenseName = "bigip_ltm_profile_bot_defense"
13+
14+
func TestAccBigipLtmProfileBotDefenseTC1(t *testing.T) {
15+
t.Parallel()
16+
var instName = "test-bot-defense-tc1"
17+
var TestBotDefenseName = fmt.Sprintf("/%s/%s", TestPartition, instName)
18+
resFullName := fmt.Sprintf("%s.%s", resBotDefenseName, instName)
19+
resource.Test(t, resource.TestCase{
20+
PreCheck: func() {
21+
testAcctPreCheck(t)
22+
},
23+
Providers: testAccProviders,
24+
CheckDestroy: testCheckBotDefensesDestroyed,
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testaccbigipltmprofileBotDefenseDefaultConfig(TestPartition, TestBotDefenseName, instName),
28+
Check: resource.ComposeTestCheckFunc(
29+
testCheckBotDefenseExists(TestBotDefenseName),
30+
resource.TestCheckResourceAttr(resFullName, "name", TestBotDefenseName),
31+
resource.TestCheckResourceAttr(resFullName, "defaults_from", "/Common/bot-defense"),
32+
),
33+
Destroy: false,
34+
},
35+
},
36+
})
37+
}
38+
39+
func testCheckBotDefenseExists(name string) resource.TestCheckFunc {
40+
return func(s *terraform.State) error {
41+
client := testAccProvider.Meta().(*bigip.BigIP)
42+
p, err := client.GetBotDefenseProfile(name)
43+
if err != nil {
44+
return err
45+
}
46+
if p == nil {
47+
return fmt.Errorf("BotDefense %s was not created ", name)
48+
}
49+
50+
return nil
51+
}
52+
}
53+
54+
func testCheckBotDefensesDestroyed(s *terraform.State) error {
55+
client := testAccProvider.Meta().(*bigip.BigIP)
56+
57+
for _, rs := range s.RootModule().Resources {
58+
if rs.Type != "bigip_ltm_profile_bot_defence" {
59+
continue
60+
}
61+
62+
name := rs.Primary.ID
63+
BotDefense, err := client.GetBotDefenseProfile(name)
64+
if err != nil {
65+
return err
66+
}
67+
if BotDefense != nil {
68+
return fmt.Errorf("BotDefense %s not destroyed. ", name)
69+
}
70+
}
71+
return nil
72+
}
73+
74+
func testaccbigipltmprofileBotDefenseDefaultConfig(partition, profileName, resourceName string) string {
75+
return fmt.Sprintf(`
76+
resource "bigip_ltm_profile_bot_defence" "%[3]s" {
77+
name = "%[2]s"
78+
defaults_from = "/%[1]s/bot-defense"
79+
description = "test-bot"
80+
template = "relaxed"
81+
}
82+
`, partition, profileName, resourceName)
83+
}

vendor/github.com/f5devcentral/go-bigip/ltm.go

Lines changed: 41 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)