Skip to content

Commit c6e5097

Browse files
adding bot-defence profile
1 parent a2da335 commit c6e5097

File tree

3 files changed

+185
-0
lines changed

3 files changed

+185
-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_defence": resourceBigipLtmProfileBotDefence(),
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 resourceBigipLtmProfileBotDefence() *schema.Resource {
19+
return &schema.Resource{
20+
CreateContext: resourceBigipLtmProfileBotDefenceCreate,
21+
ReadContext: resourceBigipLtmProfileBotDefenceRead,
22+
UpdateContext: resourceBigipLtmProfileBotDefenceUpdate,
23+
DeleteContext: resourceBigipLtmProfileBotDefenceDelete,
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 Defence 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 Defence 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 Defence. 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 resourceBigipLtmProfileBotDefenceCreate(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 Defence Profile:%+v ", name)
74+
pss := &bigip.BotDefenseProfile{
75+
Name: name,
76+
}
77+
config := getProfileBotDefenceConfig(d, pss)
78+
log.Printf("[DEBUG] Bot Defence 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 resourceBigipLtmProfileBotDefenceRead(ctx, d, meta)
85+
}
86+
87+
func resourceBigipLtmProfileBotDefenceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
client := meta.(*bigip.BigIP)
89+
log.Printf("[INFO] Reading Bot Defence Profile:%+v ", client)
90+
name := d.Id()
91+
log.Printf("[INFO] Reading Bot Defence Profile:%+v ", name)
92+
botProfile, err := client.GetBotDefenseProfile(name)
93+
if err != nil {
94+
return diag.FromErr(err)
95+
}
96+
log.Printf("[DEBUG] Bot Defence 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 resourceBigipLtmProfileBotDefenceUpdate(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 Defence Profile:%+v ", name)
109+
pss := &bigip.BotDefenseProfile{
110+
Name: name,
111+
}
112+
config := getProfileBotDefenceConfig(d, pss)
113+
114+
err := client.ModifyBotDefenseProfile(name, config)
115+
if err != nil {
116+
return diag.FromErr(err)
117+
}
118+
return resourceBigipLtmProfileBotDefenceRead(ctx, d, meta)
119+
}
120+
121+
func resourceBigipLtmProfileBotDefenceDelete(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 Defence 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 getProfileBotDefenceConfig(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][getProfileBotDefenceConfig] config:%+v ", config)
142+
return config
143+
}

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)