Skip to content

Commit

Permalink
feat: support rich text fields
Browse files Browse the repository at this point in the history
  • Loading branch information
demeyerthom committed Jun 7, 2024
1 parent c421212 commit bf76f6b
Show file tree
Hide file tree
Showing 17 changed files with 512 additions and 189 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set up Go
uses: actions/setup-go@v4
uses: actions/setup-go@v5
with:
go-version: "1.21"
go-version-file: 'go.mod'

- name: Import GPG key
id: import_gpg
Expand Down
10 changes: 5 additions & 5 deletions .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go 1.21
uses: actions/setup-go@v2
- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: "1.21"
go-version-file: 'go.mod'

- name: golangci-lint
continue-on-error: true
Expand Down Expand Up @@ -43,7 +43,7 @@ jobs:
fetch-depth: 0

- name: Prepare release
uses: labd/changie-release-action@v0.2.0
uses: labd/changie-release-action@v0.3.2
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
release-workflow: 'release.yaml'
34 changes: 34 additions & 0 deletions contentful/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package contentful

import (
"errors"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/labd/contentful-go"
"strings"
)

func parseError(err error) diag.Diagnostics {
if !errors.As(err, &contentful.ErrorResponse{}) {
return diag.FromErr(err)
}

var warnings []diag.Diagnostic
for _, e := range err.(contentful.ErrorResponse).Details.Errors {
var path []string
if e.Path != nil {
for _, p := range e.Path.([]interface{}) {
path = append(path, fmt.Sprintf("%v", p))
}
}
warnings = append(warnings, diag.Diagnostic{
Severity: diag.Warning,
Summary: fmt.Sprintf("%s (%s)", e.Details, strings.Join(path, ".")),
})
}

return append(warnings, diag.Diagnostic{
Severity: diag.Error,
Summary: err.(contentful.ErrorResponse).Message,
})
}
69 changes: 69 additions & 0 deletions contentful/errors_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package contentful

import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/labd/contentful-go"
"github.com/stretchr/testify/assert"
"testing"
)

func TestParseError_Nil(t *testing.T) {
d := parseError(nil)
assert.Nil(t, d)
}

func TestParseError_RegularErr(t *testing.T) {
d := parseError(fmt.Errorf("regular error"))
assert.True(t, d.HasError())
assert.Equal(t, d[0].Summary, "regular error")
}

func TestParseError_WithoutWarning(t *testing.T) {
d := parseError(&contentful.ErrorResponse{
Message: "error message",
})
assert.True(t, d.HasError())
assert.Equal(t, len(d), 1)
assert.Equal(t, d[0].Summary, "error message")
assert.Equal(t, d[0].Severity, diag.Error)
}

func TestParseError_WithWarning_WithoutPath(t *testing.T) {
d := parseError(contentful.ErrorResponse{
Message: "error message",
Details: &contentful.ErrorDetails{
Errors: []*contentful.ErrorDetail{
{
Details: "error detail",
},
},
},
})
assert.True(t, d.HasError())
assert.Equal(t, len(d), 2)
assert.Equal(t, d[0].Summary, "error detail ()")
assert.Equal(t, d[0].Severity, diag.Warning)
assert.Equal(t, d[1].Summary, "error message")
assert.Equal(t, d[1].Severity, diag.Error)
}

func TestParseError_WithWarning_WithPath(t *testing.T) {
d := parseError(contentful.ErrorResponse{
Message: "error message",
Details: &contentful.ErrorDetails{
Errors: []*contentful.ErrorDetail{
{
Path: []interface{}{"path", "to", "error"},
Details: "error detail",
},
},
},
})
assert.True(t, d.HasError())
assert.Equal(t, len(d), 2)
assert.Equal(t, d[0].Summary, "error detail (path.to.error)")
assert.Equal(t, d[0].Severity, diag.Warning)
assert.Equal(t, d[1].Summary, "error message")
assert.Equal(t, d[1].Severity, diag.Error)
}
5 changes: 4 additions & 1 deletion contentful/provider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ func TestProvider_impl(t *testing.T) {
}

func testAccPreCheck(t *testing.T) {
var cmaToken, organizationID string
var cmaToken, organizationID, sId string
if cmaToken = CMAToken; cmaToken == "" {
t.Fatal("CONTENTFUL_MANAGEMENT_TOKEN must set with a valid Contentful Content Management API Token for acceptance tests")
}
if organizationID = orgID; organizationID == "" {
t.Fatal("CONTENTFUL_ORGANIZATION_ID must set with a valid Contentful Organization ID for acceptance tests")
}
if sId = spaceID; sId == "" {
t.Fatal("SPACE_ID must set with a valid Space ID for acceptance tests")
}
}
50 changes: 32 additions & 18 deletions contentful/resource_contentful_apikey.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package contentful

import (
"context"
"errors"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/labd/contentful-go"
)
Expand All @@ -9,10 +12,10 @@ func resourceContentfulAPIKey() *schema.Resource {
return &schema.Resource{
Description: "A Contentful API Key represents a token that can be used to authenticate against the Contentful Content Delivery API and Content Preview API.",

Create: resourceCreateAPIKey,
Read: resourceReadAPIKey,
Update: resourceUpdateAPIKey,
Delete: resourceDeleteAPIKey,
CreateContext: resourceCreateAPIKey,
ReadContext: resourceReadAPIKey,
UpdateContext: resourceUpdateAPIKey,
DeleteContext: resourceDeleteAPIKey,

Schema: map[string]*schema.Schema{
"version": {
Expand All @@ -39,80 +42,91 @@ func resourceContentfulAPIKey() *schema.Resource {
}
}

func resourceCreateAPIKey(d *schema.ResourceData, m interface{}) (err error) {
func resourceCreateAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*contentful.Client)

apiKey := &contentful.APIKey{
Name: d.Get("name").(string),
Description: d.Get("description").(string),
}

err = client.APIKeys.Upsert(d.Get("space_id").(string), apiKey)
err := client.APIKeys.Upsert(d.Get("space_id").(string), apiKey)
if err != nil {
return err
return parseError(err)
}

if err := setAPIKeyProperties(d, apiKey); err != nil {
return err
return parseError(err)
}

d.SetId(apiKey.Sys.ID)

return nil
}

func resourceUpdateAPIKey(d *schema.ResourceData, m interface{}) (err error) {
func resourceUpdateAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
apiKeyID := d.Id()

apiKey, err := client.APIKeys.Get(spaceID, apiKeyID)
if err != nil {
return err
return parseError(err)
}

apiKey.Name = d.Get("name").(string)
apiKey.Description = d.Get("description").(string)

err = client.APIKeys.Upsert(spaceID, apiKey)
if err != nil {
return err
return parseError(err)
}

if err := setAPIKeyProperties(d, apiKey); err != nil {
return err
return parseError(err)
}

d.SetId(apiKey.Sys.ID)

return nil
}

func resourceReadAPIKey(d *schema.ResourceData, m interface{}) (err error) {
func resourceReadAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
apiKeyID := d.Id()

apiKey, err := client.APIKeys.Get(spaceID, apiKeyID)
if _, ok := err.(contentful.NotFoundError); ok {
var notFoundError contentful.NotFoundError
if errors.As(err, &notFoundError) {
d.SetId("")
return nil
}

return setAPIKeyProperties(d, apiKey)
err = setAPIKeyProperties(d, apiKey)
if err != nil {
return parseError(err)
}

return nil
}

func resourceDeleteAPIKey(d *schema.ResourceData, m interface{}) (err error) {
func resourceDeleteAPIKey(_ context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
apiKeyID := d.Id()

apiKey, err := client.APIKeys.Get(spaceID, apiKeyID)
if err != nil {
return err
return parseError(err)
}

return client.APIKeys.Delete(spaceID, apiKey)
err = client.APIKeys.Delete(spaceID, apiKey)
if err != nil {
return parseError(err)
}

return nil
}

func setAPIKeyProperties(d *schema.ResourceData, apiKey *contentful.APIKey) error {
Expand Down
Loading

0 comments on commit bf76f6b

Please sign in to comment.