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
BrooksCunningham marked this conversation as resolved.
Show resolved Hide resolved
brotli_compression = true
domain_inspector = false
image_optimizer = false
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.

Loading