|
| 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 | +} |
0 commit comments