Skip to content

Commit

Permalink
Merge pull request #5 from labd/features/entries
Browse files Browse the repository at this point in the history
Added support for entries.
  • Loading branch information
jberghoef authored May 6, 2020
2 parents 4c2c1f2 + 54f9c72 commit 6954668
Show file tree
Hide file tree
Showing 5 changed files with 422 additions and 8 deletions.
1 change: 1 addition & 0 deletions contentful/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Provider() terraform.ResourceProvider {
"contentful_webhook": resourceContentfulWebhook(),
"contentful_locale": resourceContentfulLocale(),
"contentful_environment": resourceContentfulEnvironment(),
"contentful_entry": resourceContentfulEntry(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
14 changes: 7 additions & 7 deletions contentful/resource_contentful_contenttype_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ func TestAccContentfulContentType_Basic(t *testing.T) {
{
Config: testAccContentfulContentTypeConfig,
Check: resource.TestCheckResourceAttr(
"contentful_contenttype.mycontenttype", "name", "TF Acc Test CT 1"),
"contentful_contenttype.mycontenttype", "name", "tf_test1"),
},
{
Config: testAccContentfulContentTypeUpdateConfig,
Check: resource.TestCheckResourceAttr(
"contentful_contenttype.mycontenttype", "name", "TF Acc Test CT name change"),
"contentful_contenttype.mycontenttype", "name", "tf_test1"),
},
{
Config: testAccContentfulContentTypeLinkConfig,
Check: resource.TestCheckResourceAttr(
"contentful_contenttype.mylinked_contenttype", "name", "TF Acc Test Linked CT"),
"contentful_contenttype.mylinked_contenttype", "name", "tf_linked"),
},
},
})
Expand Down Expand Up @@ -91,7 +91,7 @@ func testAccCheckContentfulContentTypeDestroy(s *terraform.State) (err error) {
var testAccContentfulContentTypeConfig = `
resource "contentful_contenttype" "mycontenttype" {
space_id = "` + spaceID + `"
name = "TF Acc Test CT 1"
name = "tf_test1"
description = "Terraform Acc Test Content Type"
display_field = "field1"
field {
Expand All @@ -118,7 +118,7 @@ resource "contentful_contenttype" "mycontenttype" {
var testAccContentfulContentTypeUpdateConfig = `
resource "contentful_contenttype" "mycontenttype" {
space_id = "` + spaceID + `"
name = "TF Acc Test CT name change"
name = "tf_test1"
description = "Terraform Acc Test Content Type description change"
display_field = "field1"
field {
Expand All @@ -145,7 +145,7 @@ resource "contentful_contenttype" "mycontenttype" {
var testAccContentfulContentTypeLinkConfig = `
resource "contentful_contenttype" "mycontenttype" {
space_id = "` + spaceID + `"
name = "TF Acc Test CT name change"
name = "tf_test1"
description = "Terraform Acc Test Content Type description change"
display_field = "field1"
field {
Expand All @@ -170,7 +170,7 @@ resource "contentful_contenttype" "mycontenttype" {
resource "contentful_contenttype" "mylinked_contenttype" {
space_id = "` + spaceID + `"
name = "TF Acc Test Linked CT"
name = "tf_linked"
description = "Terraform Acc Test Content Type with links"
display_field = "asset_field"
field {
Expand Down
206 changes: 206 additions & 0 deletions contentful/resource_contentful_entry.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
package contentful

import (
"github.com/hashicorp/terraform/helper/schema"
contentful "github.com/labd/contentful-go"
)

func resourceContentfulEntry() *schema.Resource {
return &schema.Resource{
Create: resourceCreateEntry,
Read: resourceReadEntry,
Update: resourceUpdateEntry,
Delete: resourceDeleteEntry,

Schema: map[string]*schema.Schema{
"entry_id": {
Type: schema.TypeString,
Required: true,
},
"version": {
Type: schema.TypeInt,
Computed: true,
},
"space_id": {
Type: schema.TypeString,
Required: true,
},
"contenttype_id": {
Type: schema.TypeString,
Required: true,
},
"locale": {
Type: schema.TypeString,
Required: true,
},
"field": {
Type: schema.TypeList,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Required: true,
},
"content": {
Type: schema.TypeString,
Required: true,
},
"locale": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"published": {
Type: schema.TypeBool,
Required: true,
},
"archived": {
Type: schema.TypeBool,
Required: true,
},
},
}
}

func resourceCreateEntry(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Client)

fieldProperties := map[string]interface{}{}
rawField := d.Get("field").([]interface{})
for i := 0; i < len(rawField); i++ {
field := rawField[i].(map[string]interface{})
fieldProperties[field["id"].(string)] = map[string]interface{}{}
fieldProperties[field["id"].(string)].(map[string]interface{})[field["locale"].(string)] = field["content"].(string)
}

entry := &contentful.Entry{
Locale: d.Get("locale").(string),
Fields: fieldProperties,
Sys: &contentful.Sys{
ID: d.Get("entry_id").(string),
},
}

err = client.Entries.Upsert(d.Get("space_id").(string), d.Get("contenttype_id").(string), entry)
if err != nil {
return err
}

err = setEntryState(d, m)
if err != nil {
return err
}

if err := setEntryProperties(d, entry); err != nil {
return err
}

d.SetId(entry.Sys.ID)
return nil
}

func resourceUpdateEntry(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
entryID := d.Id()

entry, err := client.Entries.Get(spaceID, entryID)
if err != nil {
return err
}

fieldProperties := map[string]interface{}{}
rawField := d.Get("field").([]interface{})
for i := 0; i < len(rawField); i++ {
field := rawField[i].(map[string]interface{})
fieldProperties[field["id"].(string)] = map[string]interface{}{}
fieldProperties[field["id"].(string)].(map[string]interface{})[field["locale"].(string)] = field["content"].(string)
}

entry.Fields = fieldProperties
entry.Locale = d.Get("locale").(string)

err = client.Entries.Upsert(d.Get("space_id").(string), d.Get("contenttype_id").(string), entry)
if err != nil {
return err
}

if err := setEntryState(d, m); err != nil {
return err
}

if err := setEntryProperties(d, entry); err != nil {
return err
}
d.SetId(entry.Sys.ID)

return nil
}

func setEntryState(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
entryID := d.Id()

entry, _ := client.Entries.Get(spaceID, entryID)

if d.Get("published").(bool) && entry.Sys.PublishedAt == "" {
err = client.Entries.Publish(spaceID, entry)
} else if d.Get("published").(bool) && entry.Sys.PublishedAt != "" {
err = client.Entries.Unpublish(spaceID, entry)
}

if d.Get("archived").(bool) && entry.Sys.ArchivedAt == "" {
err = client.Entries.Publish(spaceID, entry)
} else if d.Get("archived").(bool) && entry.Sys.ArchivedAt != "" {
err = client.Entries.Unpublish(spaceID, entry)
}

return nil
}

func resourceReadEntry(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
entryID := d.Id()

entry, err := client.Entries.Get(spaceID, entryID)
if _, ok := err.(contentful.NotFoundError); ok {
d.SetId("")
return nil
}

return setEntryProperties(d, entry)
}

func resourceDeleteEntry(d *schema.ResourceData, m interface{}) (err error) {
client := m.(*contentful.Client)
spaceID := d.Get("space_id").(string)
entryID := d.Id()

_, err = client.Entries.Get(spaceID, entryID)
if err != nil {
return err
}

return client.Entries.Delete(spaceID, entryID)
}

func setEntryProperties(d *schema.ResourceData, entry *contentful.Entry) error {
if err := d.Set("space_id", entry.Sys.Space.Sys.ID); err != nil {
return err
}

if err := d.Set("version", entry.Sys.Version); err != nil {
return err
}

if err := d.Set("contenttype_id", entry.Sys.ContentType.Sys.ID); err != nil {
return err
}

return nil
}
Loading

0 comments on commit 6954668

Please sign in to comment.