Skip to content

Commit

Permalink
fix(products): Improve API usability.
Browse files Browse the repository at this point in the history
* Eliminate 'fastly/products' package, moving its types into 'internal/productcore'.

* Add 'EnableOutput' type aliases in each product, so that users of
  the API don't need to know whether that product has its own
  EnableOutput or not.

* Add 'ProductName' constant in each product, so that users of the API
  will have access to the canonical name of the product along with its
  product_id.
  • Loading branch information
kpfleming committed Dec 17, 2024
1 parent 824cab9 commit 8f6ebe6
Show file tree
Hide file tree
Showing 22 changed files with 182 additions and 154 deletions.
38 changes: 0 additions & 38 deletions fastly/products/api_output.go

This file was deleted.

16 changes: 10 additions & 6 deletions fastly/products/bot_management/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package bot_management

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const ProductID = "bot_management"
const (
ProductID = "bot_management"
ProductName = "Bot Management"
)

type EnableOutput = productcore.EnableOutput

// Get gets the status of the Bot Management product on the service.
func Get(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Get[*products.EnableOutput](&productcore.GetInput{
func Get(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Get[*EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the Bot Management product on the service.
func Enable(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Put[*products.EnableOutput](&productcore.PutInput[*productcore.NullInput]{
func Enable(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Put[*EnableOutput](&productcore.PutInput[*productcore.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
Expand Down
9 changes: 4 additions & 5 deletions fastly/products/bot_management/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/fastly/products/bot_management"
"github.com/fastly/go-fastly/v9/internal/productcore"
"github.com/fastly/go-fastly/v9/internal/test_utils"
Expand All @@ -19,19 +18,19 @@ var functionalTests = []*test_utils.FunctionalTest{
ServiceID: serviceID,
IgnoreFailure: true,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*bot_management.EnableOutput]{
Phase: "before enablement",
OpFn: bot_management.Get,
ProductID: bot_management.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
productcore.NewEnableTest(&productcore.EnableTestInput[*products.EnableOutput, *productcore.NullInput]{
productcore.NewEnableTest(&productcore.EnableTestInput[*bot_management.EnableOutput, *productcore.NullInput]{
OpNoInputFn: bot_management.Enable,
ProductID: bot_management.ProductID,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*bot_management.EnableOutput]{
Phase: "after enablement",
OpFn: bot_management.Get,
ProductID: bot_management.ProductID,
Expand All @@ -41,7 +40,7 @@ var functionalTests = []*test_utils.FunctionalTest{
OpFn: bot_management.Disable,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*bot_management.EnableOutput]{
Phase: "after disablement",
OpFn: bot_management.Get,
ProductID: bot_management.ProductID,
Expand Down
16 changes: 10 additions & 6 deletions fastly/products/brotli_compression/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package brotli_compression

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const ProductID = "brotli_compression"
const (
ProductID = "brotli_compression"
ProductName = "Brotli Compression"
)

type EnableOutput = productcore.EnableOutput

// Get gets the status of the Brotli Compression product on the service.
func Get(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Get[*products.EnableOutput](&productcore.GetInput{
func Get(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Get[*EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the Brotli Compression product on the service.
func Enable(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Put[*products.EnableOutput](&productcore.PutInput[*productcore.NullInput]{
func Enable(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Put[*EnableOutput](&productcore.PutInput[*productcore.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
Expand Down
9 changes: 4 additions & 5 deletions fastly/products/brotli_compression/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/fastly/products/brotli_compression"
"github.com/fastly/go-fastly/v9/internal/productcore"
"github.com/fastly/go-fastly/v9/internal/test_utils"
Expand All @@ -19,19 +18,19 @@ var functionalTests = []*test_utils.FunctionalTest{
ServiceID: serviceID,
IgnoreFailure: true,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*brotli_compression.EnableOutput]{
Phase: "before enablement",
OpFn: brotli_compression.Get,
ProductID: brotli_compression.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
productcore.NewEnableTest(&productcore.EnableTestInput[*products.EnableOutput, *productcore.NullInput]{
productcore.NewEnableTest(&productcore.EnableTestInput[*brotli_compression.EnableOutput, *productcore.NullInput]{
OpNoInputFn: brotli_compression.Enable,
ProductID: brotli_compression.ProductID,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*brotli_compression.EnableOutput]{
Phase: "after enablement",
OpFn: brotli_compression.Get,
ProductID: brotli_compression.ProductID,
Expand All @@ -41,7 +40,7 @@ var functionalTests = []*test_utils.FunctionalTest{
OpFn: brotli_compression.Disable,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*brotli_compression.EnableOutput]{
Phase: "after disablement",
OpFn: brotli_compression.Get,
ProductID: brotli_compression.ProductID,
Expand Down
20 changes: 12 additions & 8 deletions fastly/products/ddos_protection/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ package ddos_protection

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const ProductID = "ddos_protection"
const (
ProductID = "ddos_protection"
ProductName = "DDoS Protection"
)

type EnableOutput = productcore.EnableOutput

// ErrMissingMode is the error returned by the UpdateConfiguration
// function when it is passed a ConfigureInput struct with a mode
Expand All @@ -18,26 +22,26 @@ type ConfigureInput struct {
}

type ConfigureOutput struct {
products.ConfigureOutput `mapstructure:",squash"`
Configuration *configureOutputNested `mapstructure:"configuration"`
productcore.ConfigureOutput `mapstructure:",squash"`
Configuration *configureOutputNested `mapstructure:"configuration"`
}

type configureOutputNested struct {
Mode *string `mapstructure:"mode"`
}

// Get gets the status of the DDoS Protection product on the service.
func Get(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Get[*products.EnableOutput](&productcore.GetInput{
func Get(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Get[*EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the DDoS Protection product on the service.
func Enable(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Put[*products.EnableOutput](&productcore.PutInput[*productcore.NullInput]{
func Enable(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Put[*EnableOutput](&productcore.PutInput[*productcore.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
Expand Down
9 changes: 4 additions & 5 deletions fastly/products/ddos_protection/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/fastly/products/ddos_protection"
"github.com/fastly/go-fastly/v9/internal/productcore"
"github.com/fastly/go-fastly/v9/internal/test_utils"
Expand All @@ -29,19 +28,19 @@ var functionalTests = []*test_utils.FunctionalTest{
ServiceID: serviceID,
IgnoreFailure: true,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*ddos_protection.EnableOutput]{
Phase: "before enablement",
OpFn: ddos_protection.Get,
ProductID: ddos_protection.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
productcore.NewEnableTest(&productcore.EnableTestInput[*products.EnableOutput, *productcore.NullInput]{
productcore.NewEnableTest(&productcore.EnableTestInput[*ddos_protection.EnableOutput, *productcore.NullInput]{
OpNoInputFn: ddos_protection.Enable,
ProductID: ddos_protection.ProductID,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*ddos_protection.EnableOutput]{
Phase: "after enablement",
OpFn: ddos_protection.Get,
ProductID: ddos_protection.ProductID,
Expand Down Expand Up @@ -81,7 +80,7 @@ var functionalTests = []*test_utils.FunctionalTest{
OpFn: ddos_protection.Disable,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*ddos_protection.EnableOutput]{
Phase: "after disablement",
OpFn: ddos_protection.Get,
ProductID: ddos_protection.ProductID,
Expand Down
16 changes: 10 additions & 6 deletions fastly/products/domain_inspector/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package domain_inspector

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const ProductID = "domain_inspector"
const (
ProductID = "domain_inspector"
ProductName = "Domain Inspector"
)

type EnableOutput = productcore.EnableOutput

// Get gets the status of the Domain Inspector product on the service.
func Get(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Get[*products.EnableOutput](&productcore.GetInput{
func Get(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Get[*EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the Domain Inspector product on the service.
func Enable(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Put[*products.EnableOutput](&productcore.PutInput[*productcore.NullInput]{
func Enable(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Put[*EnableOutput](&productcore.PutInput[*productcore.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
Expand Down
9 changes: 4 additions & 5 deletions fastly/products/domain_inspector/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"testing"

"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/fastly/products/domain_inspector"
"github.com/fastly/go-fastly/v9/internal/productcore"
"github.com/fastly/go-fastly/v9/internal/test_utils"
Expand All @@ -19,19 +18,19 @@ var functionalTests = []*test_utils.FunctionalTest{
ServiceID: serviceID,
IgnoreFailure: true,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*domain_inspector.EnableOutput]{
Phase: "before enablement",
OpFn: domain_inspector.Get,
ProductID: domain_inspector.ProductID,
ServiceID: serviceID,
ExpectFailure: true,
}),
productcore.NewEnableTest(&productcore.EnableTestInput[*products.EnableOutput, *productcore.NullInput]{
productcore.NewEnableTest(&productcore.EnableTestInput[*domain_inspector.EnableOutput, *productcore.NullInput]{
OpNoInputFn: domain_inspector.Enable,
ProductID: domain_inspector.ProductID,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*domain_inspector.EnableOutput]{
Phase: "after enablement",
OpFn: domain_inspector.Get,
ProductID: domain_inspector.ProductID,
Expand All @@ -41,7 +40,7 @@ var functionalTests = []*test_utils.FunctionalTest{
OpFn: domain_inspector.Disable,
ServiceID: serviceID,
}),
productcore.NewGetTest(&productcore.GetTestInput[*products.EnableOutput]{
productcore.NewGetTest(&productcore.GetTestInput[*domain_inspector.EnableOutput]{
Phase: "after disablement",
OpFn: domain_inspector.Get,
ProductID: domain_inspector.ProductID,
Expand Down
16 changes: 10 additions & 6 deletions fastly/products/fanout/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,28 @@ package fanout

import (
"github.com/fastly/go-fastly/v9/fastly"
"github.com/fastly/go-fastly/v9/fastly/products"
"github.com/fastly/go-fastly/v9/internal/productcore"
)

const ProductID = "fanout"
const (
ProductID = "fanout"
ProductName = "Fanout"
)

type EnableOutput = productcore.EnableOutput

// Get gets the status of the Fanout product on the service.
func Get(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Get[*products.EnableOutput](&productcore.GetInput{
func Get(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Get[*EnableOutput](&productcore.GetInput{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
})
}

// Enable enables the Fanout product on the service.
func Enable(c *fastly.Client, serviceID string) (*products.EnableOutput, error) {
return productcore.Put[*products.EnableOutput](&productcore.PutInput[*productcore.NullInput]{
func Enable(c *fastly.Client, serviceID string) (*EnableOutput, error) {
return productcore.Put[*EnableOutput](&productcore.PutInput[*productcore.NullInput]{
Client: c,
ProductID: ProductID,
ServiceID: serviceID,
Expand Down
Loading

0 comments on commit 8f6ebe6

Please sign in to comment.