Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(product_enablement) - Add support for Fastly Bot Management. #880

Merged
1 change: 1 addition & 0 deletions docs/resources/service_vcl.md
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,7 @@ Optional:

Optional:

- `bot_management` (Boolean) Enable Bot Management support
- `brotli_compression` (Boolean) Enable Brotli Compression support
- `domain_inspector` (Boolean) Enable Domain Inspector support
- `image_optimizer` (Boolean) Enable Image Optimizer support (all backends must have a `shield` attribute)
Expand Down
57 changes: 56 additions & 1 deletion fastly/block_fastly_service_product_enablement.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ func (h *ProductEnablementServiceAttributeHandler) GetSchema() *schema.Schema {
}

if h.GetServiceMetadata().serviceType == ServiceTypeVCL {
blockAttributes["bot_management"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Description: "Enable Bot Management support",
}
blockAttributes["brotli_compression"] = &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -111,6 +116,16 @@ func (h *ProductEnablementServiceAttributeHandler) Create(_ context.Context, d *
}

if h.GetServiceMetadata().serviceType == ServiceTypeVCL {
if resource["bot_management"].(bool) {
log.Println("[DEBUG] bot_management set")
_, err := conn.EnableProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBotManagement,
ServiceID: serviceID,
})
if err != nil {
return fmt.Errorf("failed to enable bot_management: %w", err)
}
}
if resource["brotli_compression"].(bool) {
log.Println("[DEBUG] brotli_compression set")
_, err := conn.EnableProduct(&gofastly.ProductEnablementInput{
Expand Down Expand Up @@ -209,6 +224,12 @@ func (h *ProductEnablementServiceAttributeHandler) Read(_ context.Context, d *sc
}

if h.GetServiceMetadata().serviceType == ServiceTypeVCL {
if _, err := conn.GetProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBotManagement,
ServiceID: d.Id(),
}); err == nil {
result["bot_management"] = true
}
if _, err := conn.GetProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBrotliCompression,
ServiceID: d.Id(),
Expand Down Expand Up @@ -299,6 +320,29 @@ func (h *ProductEnablementServiceAttributeHandler) Update(_ context.Context, d *
}

if h.GetServiceMetadata().serviceType == ServiceTypeVCL {
if v, ok := modified["bot_management"]; ok {
if v.(bool) {
log.Println("[DEBUG] bot_management will be enabled")
_, err := conn.EnableProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBotManagement,
ServiceID: serviceID,
})
if err != nil {
return fmt.Errorf("failed to enable bot_management: %w", err)
}
} else {
log.Println("[DEBUG] bot_management will be disabled")
err := conn.DisableProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBotManagement,
ServiceID: serviceID,
})
if err != nil {
if e := h.checkAPIError(err); e != nil {
return e
}
}
}
}
if v, ok := modified["brotli_compression"]; ok {
if v.(bool) {
log.Println("[DEBUG] brotli_compression will be enabled")
Expand Down Expand Up @@ -469,8 +513,19 @@ func (h *ProductEnablementServiceAttributeHandler) Delete(_ context.Context, d *
}

if h.GetServiceMetadata().serviceType == ServiceTypeVCL {
log.Println("[DEBUG] disable brotli_compression")
log.Println("[DEBUG] disable bot_management")
err := conn.DisableProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBotManagement,
ServiceID: d.Id(),
})
if err != nil {
if e := h.checkAPIError(err); e != nil {
return e
}
}

log.Println("[DEBUG] disable brotli_compression")
err = conn.DisableProduct(&gofastly.ProductEnablementInput{
ProductID: gofastly.ProductBrotliCompression,
ServiceID: d.Id(),
})
Expand Down
1 change: 1 addition & 0 deletions fastly/block_fastly_service_product_enablement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func TestAccFastlyServiceVCLProductEnablement_basic(t *testing.T) {
}

product_enablement {
bot_management = false
brotli_compression = true
domain_inspector = false
image_optimizer = false
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/bflad/tfproviderlint v0.30.0
github.com/fastly/go-fastly/v9 v9.8.0
github.com/fastly/go-fastly/v9 v9.9.0
github.com/google/go-cmp v0.6.0
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/terraform-plugin-docs v0.19.4
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dnaeon/go-vcr v1.2.0 h1:zHCHvJYTMh1N7xnV7zf1m1GPBF9Ad0Jk/whtQ1663qI=
github.com/emirpasic/gods v1.18.1 h1:FXtiHYKDGKCW2KzwZKx0iC0PQmdlorYgdFG9jPXJ1Bc=
github.com/fastly/go-fastly/v9 v9.8.0 h1:15dtV3fmLlS/8wbdU3tBsW3Tb0Tj/gQrdS4v5mhtDDE=
github.com/fastly/go-fastly/v9 v9.8.0/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fastly/go-fastly/v9 v9.9.0 h1:VDCyORoWi608l/LBp+tY+qic3M5/5ZFw+rtAfV6VR9E=
github.com/fastly/go-fastly/v9 v9.9.0/go.mod h1:5w2jgJBZqQEebOwM/rRg7wutAcpDTziiMYWb/6qdM7U=
github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYFFOfk=
github.com/fatih/color v1.16.0 h1:zmkK9Ngbjj+K0yRhTVONQh1p/HknKYSlNT+vZCzyokM=
github.com/fatih/color v1.16.0/go.mod h1:fL2Sau1YI5c0pdGEVCbKQbLXB6edEj1ZgiY4NijnWvE=
Expand Down
1 change: 1 addition & 0 deletions tests/interface/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ resource "fastly_service_vcl" "interface-test-project" {
}

product_enablement {
bot_management = false
brotli_compression = true
domain_inspector = false
image_optimizer = false
Expand Down

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

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ github.com/cloudflare/circl/sign/ed448
# github.com/davecgh/go-spew v1.1.1
## explicit
github.com/davecgh/go-spew/spew
# github.com/fastly/go-fastly/v9 v9.8.0
# github.com/fastly/go-fastly/v9 v9.9.0
## explicit; go 1.20
github.com/fastly/go-fastly/v9/fastly
# github.com/fatih/color v1.16.0
Expand Down
Loading