Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 Add labels #6

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
13 changes: 13 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: mirogta
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
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
9 changes: 8 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
![](https://github.com/mesomorphic/terraform-provider-confluence/workflows/Build%20and%20Test/badge.svg)
![](https://img.shields.io/github/workflow/status/mirogta/terraform-provider-confluence/release)

# Terraform Provider for Confluence

Expand All @@ -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)
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.2
0.2.3
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
31 changes: 30 additions & 1 deletion 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 @@ -109,7 +116,7 @@ func contentFromResourceData(d *schema.ResourceData) *Content {
Body: &Body{
Storage: &Storage{
Value: d.Get("body").(string),
Representation: "storage",
Representation: "editor2",
},
},
Title: d.Get("title").(string),
Expand All @@ -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
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/mesomorphic/terraform-provider-confluence
module github.com/mirogta/terraform-provider-confluence

go 1.16

Expand Down
25 changes: 22 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,30 @@
package main

import (
"context"
"flag"
"log"

"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/mesomorphic/terraform-provider-confluence/confluence"
"github.com/mirogta/terraform-provider-confluence/confluence"
)

func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: confluence.Provider})
var debugMode bool
flag.BoolVar(&debugMode, "debug", false, "set to true to run the provider with support for debuggers like delve")
flag.Parse()

opts := &plugin.ServeOpts{
ProviderFunc: confluence.Provider,
}

if debugMode {
err := plugin.Debug(context.Background(), "mirogta/terraform-provider-confluence", opts)
if err != nil {
log.Fatal(err.Error())
}
return
}

plugin.Serve(opts)
}
4 changes: 2 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
site_name: "Terraform Provider: Confluence"
site_url: https://github.com/mesomorphic/terraform-provider-confluence
repo_url: https://github.com/mesomorphic/terraform-provider-confluence
site_url: https://github.com/mirogta/terraform-provider-confluence
repo_url: https://github.com/mirogta/terraform-provider-confluence
nav:
- Getting Started: index.md
- Resources:
Expand Down