Skip to content

Commit

Permalink
#6 add page labels
Browse files Browse the repository at this point in the history
  • Loading branch information
mirogta committed Nov 3, 2022
1 parent 2d9c9e5 commit e1316b0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 9 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,7 @@ secrets.*
# ignore some things terraform could produce in the examples
.terraform
terraform.tfstate*
vendor
vendor

__debug_bin
.vscode
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ $ cd terraform-provider-confluence
$ make install
```

## Debugging

See:

- <https://developer.hashicorp.com/terraform/plugin/debugging>
- <https://opencredo.com/blogs/running-a-terraform-provider-with-a-debugger/>

## Contributing

Contributions are welcome! Please read the contribution guidelines [Contributing to Terraform - Confluence Provider](.github/CONTRIBUTING.md)
28 changes: 20 additions & 8 deletions confluence/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ type Body struct {

// Content is a primary resource in Confluence
type Content struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Space *SpaceKey `json:"space,omitempty"`
Version *Version `json:"version,omitempty"`
Body *Body `json:"body,omitempty"`
Links *ContentLinks `json:"_links,omitempty"`
Ancestors []*Content `json:"ancestors,omitempty"`
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Space *SpaceKey `json:"space,omitempty"`
Version *Version `json:"version,omitempty"`
Body *Body `json:"body,omitempty"`
Links *ContentLinks `json:"_links,omitempty"`
Ancestors []*Content `json:"ancestors,omitempty"`
Metadata *ContentMetadata `json:"metadata,omitempty"`
}

// ContentLinks is part of Content
Expand All @@ -43,6 +44,17 @@ type Version struct {
Number int `json:"number,omitempty"`
}

// ContentMetadata is part of Content
type ContentMetadata struct {
Labels []*Label `json:"labels,omitempty"`
}

// Label is part of Metadata
type Label struct {
Prefix string `json:"prefix,omitempty"`
Name string `json:"name,omitempty"`
}

func (c *Client) CreateContent(content *Content) (*Content, error) {
var response Content
if err := c.Post("/rest/api/content", content, &response); err != nil {
Expand Down
29 changes: 29 additions & 0 deletions confluence/resource_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@ func resourceContent() *schema.Resource {
Default: "",
DiffSuppressFunc: resourceContentDiffParent,
},
"labels": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
Expand Down Expand Up @@ -127,6 +134,21 @@ func contentFromResourceData(d *schema.ResourceData) *Content {
},
}
}

labelsRaw := d.Get("labels").([]interface{})
if len(labelsRaw) > 0 {
labels := make([]*Label, len(labelsRaw))
for i, raw := range labelsRaw {
labels[i] = &Label{
Prefix: "global",
Name: raw.(string),
}
}
result.Metadata = &ContentMetadata{
Labels: labels,
}
}

return result
}

Expand All @@ -143,6 +165,13 @@ func updateResourceDataFromContent(d *schema.ResourceData, content *Content, cli
if len(content.Ancestors) > 1 {
m["parent"] = content.Ancestors[len(content.Ancestors)-1].Id
}
if content.Metadata != nil && len(content.Metadata.Labels) > 1 {
labels := []string{}
for i, label := range content.Metadata.Labels {
labels[i] = label.Name
}
m["labels"] = labels
}
for k, v := range m {
err := d.Set(k, v)
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions docs/resources/confluence_content.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ resource confluence_content "default" {
title = "Example Page"
body = "<p>This page was built with Terraform<p>"
parent = "123456"
labels = ["sample-label", "another-label"]
}
```

Expand All @@ -39,6 +40,8 @@ The following arguments are supported:

* `type` - (Optional) The content type (either "page" or "blogpost"). Default is page.

* `labels` - (Optional) List of page labels.

## Attributes Reference

This resource exports the following attributes:
Expand Down
1 change: 1 addition & 0 deletions examples/content/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ resource confluence_content "example" {
body = templatefile("${path.module}/example.tmpl", {
pets = [for p in random_pet.pets : title(p.id)]
})
labels = ["pets", "example"]
}

terraform {
Expand Down

0 comments on commit e1316b0

Please sign in to comment.