Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,11 @@ lint.fix:
testapi:
@go run ./main.go -exportfile ./test/test-space-export.json ./test/testapi

.PHONY: testapi-region
## run api tests with EU region
testapi-region:
@go run ./main.go -exportfile ./test/test-space-export.json -region eu ./test/testapi

.PHONY: test
## Run tests
test: testapi
Expand Down
31 changes: 24 additions & 7 deletions config/config_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,33 @@
package config

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestConfigFromYAML(t *testing.T) {
config, err := LoadConfigFromYAML("./sampleconfig.yaml")
require.NoError(t, err)
require.Equal(t, "abc123", config.SpaceID)
require.Equal(t, "dev", config.Environment)
require.Equal(t, "v1.0.19", config.RequireVersion)
require.Len(t, config.ContentTypes, 2)
func TestLoadConfigFromYAML(t *testing.T) {
t.Run("full config with region", func(t *testing.T) {
cfg, err := LoadConfigFromYAML("./sampleconfig.yaml")
require.NoError(t, err)
require.Equal(t, "abc123", cfg.SpaceID)
require.Equal(t, "dev", cfg.Environment)
require.Equal(t, "v1.0.19", cfg.RequireVersion)
require.Len(t, cfg.ContentTypes, 2)
assert.Equal(t, "eu", cfg.Region)
})
t.Run("region defaults to empty string when absent", func(t *testing.T) {
f, err := os.CreateTemp(t.TempDir(), "gocontentful-test-*.yaml")
require.NoError(t, err)
_, err = f.WriteString("spaceId: abc123\n")
require.NoError(t, err)
require.NoError(t, f.Close())

cfg, err := LoadConfigFromYAML(f.Name())
require.NoError(t, err)
require.Equal(t, "abc123", cfg.SpaceID)
assert.Equal(t, "", cfg.Region)
})
}
1 change: 1 addition & 0 deletions config/sampleconfig.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
region: eu
spaceId: "abc123"
environment: "dev"
exportFile: "foo.json"
Expand Down
1 change: 1 addition & 0 deletions config/vo.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ type Config struct {
ContentTypes []string `json:"contentTypes,omitempty" yaml:"contentTypes,omitempty"`
PathTargetPackage string `json:"pathTargetPackage,omitempty" yaml:"pathTargetPackage,omitempty"`
RequireVersion string `json:"requireVersion,omitempty" yaml:"requireVersion,omitempty"`
Region string `json:"region,omitempty" yaml:"region,omitempty"`
}
13 changes: 9 additions & 4 deletions erm/space.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type spaceConf struct {
ContentTypes []ContentType `yaml:"contentTypes"`
ContentType ContentType `yaml:"contentType"`
Version string `yaml:"version"`
Region string `yaml:"region"`
}

// GetLocales retrieves locale definition from Contentful
Expand Down Expand Up @@ -82,7 +83,7 @@ func getContentTypes(ctx context.Context, cma *contentful.Contentful, spaceID st
return
}

func getData(ctx context.Context, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string) (
func getData(ctx context.Context, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string, region string) (
finalContentTypes []ContentType, locales []Locale, err error,
) {
var contentTypes []ContentType
Expand All @@ -100,7 +101,7 @@ func getData(ctx context.Context, spaceID, cmaKey, environment, exportFile strin
locales = export.Locales
} else {
// Get client
CMA := contentful.NewCMA(cmaKey)
CMA := contentful.NewCMA(cmaKey).SetRegion(contentful.Region(region))
CMA.Debug = false
if environment != "" {
CMA.Environment = environment
Expand Down Expand Up @@ -141,8 +142,11 @@ func getData(ctx context.Context, spaceID, cmaKey, environment, exportFile strin
}

// GenerateAPI calls the generators
func GenerateAPI(ctx context.Context, dir, packageName, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string, version string) error {
contentTypes, locales, errGetData := getData(ctx, spaceID, cmaKey, environment, exportFile, flagContentTypes)
func GenerateAPI(ctx context.Context, dir, packageName, spaceID, cmaKey, environment, exportFile string, flagContentTypes []string, version string, region string) error {
if _, err := contentful.ParseRegion(region); err != nil {
return err
}
contentTypes, locales, errGetData := getData(ctx, spaceID, cmaKey, environment, exportFile, flagContentTypes, region)
if errGetData != nil {
return errors.Wrap(errGetData, "could not get data")
}
Expand All @@ -160,6 +164,7 @@ func GenerateAPI(ctx context.Context, dir, packageName, spaceID, cmaKey, environ
Locales: locales,
ContentTypes: contentTypes,
Version: version,
Region: region,
}
return generateCode(conf)
}
44 changes: 44 additions & 0 deletions erm/space_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package erm

import (
"context"
"os"
"path/filepath"
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

const testExportFile = "../test/test-space-export.json"

func TestGenerateAPI_EmbedRegionEU(t *testing.T) {
dir := t.TempDir()
err := GenerateAPI(context.Background(), dir, "testpkg", "", "", "", testExportFile, nil, "test", "eu")
require.NoError(t, err)

content, err := os.ReadFile(filepath.Join(dir, "testpkg", "gocontentfulvolib.go"))
require.NoError(t, err)

src := string(content)
assert.True(t, strings.Contains(src, `.SetRegion("eu")`), "generated lib should call SetRegion(\"eu\")")
}

func TestGenerateAPI_InvalidRegion(t *testing.T) {
err := GenerateAPI(context.Background(), t.TempDir(), "testpkg", "", "", "", testExportFile, nil, "test", "ap")
require.Error(t, err)
assert.ErrorContains(t, err, "unknown region")
}

func TestGenerateAPI_EmbedRegionEmpty(t *testing.T) {
dir := t.TempDir()
err := GenerateAPI(context.Background(), dir, "testpkg", "", "", "", testExportFile, nil, "test", "")
require.NoError(t, err)

content, err := os.ReadFile(filepath.Join(dir, "testpkg", "gocontentfulvolib.go"))
require.NoError(t, err)

src := string(content)
assert.False(t, strings.Contains(src, `.SetRegion(`), "generated lib should not call SetRegion for default region")
}
11 changes: 7 additions & 4 deletions erm/templates/contentful_vo_lib.gotmpl
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,7 @@ func NewOfflineContentfulClient(file []byte, logFn func(fields map[string]interf
}
cc := &ContentfulClient{
clientMode: ClientModeCDA,
Client: contentful.NewCDA(""),
Client: contentful.NewCDA(""){{ if .Region }}.SetRegion("{{ .Region }}"){{ end }},
Cache: &ContentfulCache{
contentTypes: []string{},
idContentTypeMap: map[string]string{},
Expand Down Expand Up @@ -1864,16 +1864,19 @@ func (cc *ContentfulClient) entryMapForContentTypeIsNil(contentType string) bool
}

func getContentfulAPIClient(clientMode ClientMode, clientKey string) (*contentful.Contentful, error) {
var c *contentful.Contentful
switch clientMode {
case ClientModeCDA:
return contentful.NewCDA(clientKey), nil
c = contentful.NewCDA(clientKey)
case ClientModeCPA:
return contentful.NewCPA(clientKey), nil
c = contentful.NewCPA(clientKey)
case ClientModeCMA:
return contentful.NewCMA(clientKey), nil
c = contentful.NewCMA(clientKey)
default:
return nil, errors.New("NewContentfulClient: Unknown ClientMode")
}
{{ if .Region }}c.SetRegion("{{ .Region }}")
{{ end }}return c, nil
}

func (cc *ContentfulClient) GetSpaceID() string {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/foomo/gocontentful
go 1.24.0

require (
github.com/foomo/contentful v0.6.0
github.com/foomo/contentful v0.6.3
github.com/mitchellh/mapstructure v1.5.0
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.9.3
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ github.com/aoliveti/curling v1.1.0/go.mod h1:xoDmoUg9vX3pMTltyG/rp9tFtIlweL2QeCJ
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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/foomo/contentful v0.6.0 h1:k9VOQj1ZxSbl5HR2oBvunNeuqTW9U48yAz6jYeDxYdE=
github.com/foomo/contentful v0.6.0/go.mod h1:xJ09xn+hTb5RKFyYL8vscxBh+DSH1wRzOljc9paEpp8=
github.com/foomo/contentful v0.6.3 h1:KlP4nYeV4Y3YlffYaz6t6xJ8CpF8ce/yA70j41YyOAY=
github.com/foomo/contentful v0.6.3/go.mod h1:xJ09xn+hTb5RKFyYL8vscxBh+DSH1wRzOljc9paEpp8=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/mitchellh/mapstructure v1.5.0 h1:jeMsZIYE/09sWLaz43PL7Gy6RuMjD2eJVyuac5Z2hdY=
Expand Down
4 changes: 3 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ func main() {
flagEnvironment := flag.String("environment", "", "[Optional] Contentful space environment")
flagGenerateFromExport := flag.String("exportfile", "", "Space export file to generate the API from")
flagContentTypes := flag.String("contenttypes", "", "[Optional] Content type IDs to parse, comma separated")
flagRegion := flag.String("region", "", "[Optional] Contentful infrastructure region (e.g. eu)")
flagVersion := flag.Bool("version", false, "Print version and exit")
flagHelp := flag.Bool("help", false, "Print version and exit")
flag.Parse()
Expand Down Expand Up @@ -95,6 +96,7 @@ func main() {
SpaceID: *flagSpaceID,
Environment: *flagEnvironment,
ExportFile: *flagGenerateFromExport,
Region: *flagRegion,
}
if *flagContentTypes != "" {
conf.ContentTypes = strings.Split(*flagContentTypes, ",")
Expand Down Expand Up @@ -140,7 +142,7 @@ func main() {
}
}

err = erm.GenerateAPI(context.Background(), filepath.Dir(path), packageName, conf.SpaceID, cmaKey, conf.Environment, conf.ExportFile, cleanContentTypes, VERSION)
err = erm.GenerateAPI(context.Background(), filepath.Dir(path), packageName, conf.SpaceID, cmaKey, conf.Environment, conf.ExportFile, cleanContentTypes, VERSION, conf.Region)
if err != nil {
fatal("Something went horribly wrong...", err)
}
Expand Down
8 changes: 5 additions & 3 deletions test/testapi/gocontentfulvolib.go

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

Loading