forked from labd/terraform-provider-contentful
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created the entries resource. The entries can be created, updated and deleted. The archiving and publishing is also added into the resource, keep in mind that a resource can only be deleted when unpublished.
- Loading branch information
1 parent
aae92e7
commit ad5bf8b
Showing
5 changed files
with
422 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
Oops, something went wrong.