Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
NeilMeso committed Aug 25, 2022
2 parents eec104d + 7ce8bd9 commit 6b3578f
Show file tree
Hide file tree
Showing 16 changed files with 231 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mkdocs-gh-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ jobs:
run: pip install -r requirements.txt
- name: MkDocs
env:
GH_PAT: ${{ secrets.GH_PAT }}
GH_PAT: ${{ secrets.GITHUB_TOKEN }}
run: |
git remote set-url origin https://x-access-token:${GH_PAT}@github.com/${GITHUB_REPOSITORY}.git
mkdocs gh-deploy
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,18 @@ jobs:
name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.16
go-version: 1.19
-
name: Import GPG key
id: import_gpg
uses: hashicorp/ghaction-import-gpg@v2.1.0
env:
uses: crazy-max/ghaction-import-gpg@v5.0.0
with:
# These secrets will need to be configured for the repository:
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
PASSPHRASE: ${{ secrets.PASSPHRASE }}
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
passphrase: ${{ secrets.PASSPHRASE }}
-
name: Run GoReleaser
uses: goreleaser/goreleaser-action@v2.7.0
uses: goreleaser/goreleaser-action@v3
with:
version: latest
args: release --rm-dist
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
![](https://github.com/chesshacker/terraform-provider-confluence/workflows/Build%20and%20Test/badge.svg)
![](https://github.com/mesomorphic/terraform-provider-confluence/workflows/Build%20and%20Test/badge.svg)

# Terraform Provider for Confluence

[User Documentation](https://chesshacker.github.io/terraform-provider-confluence/)
[User Documentation](https://mesomorphic.github.io/terraform-provider-confluence/)

## Requirements

- [Terraform](https://www.terraform.io/downloads.html)
- [Go](https://golang.org/doc/install)
- [Terraform](https://www.terraform.io/downloads.html)
- [Go](https://golang.org/doc/install)

## Build and install the provider

Clone this repository, enter the provider directory, build and install the provider:

```sh
$ git clone https://github.com/chesshacker/terraform-provider-confluence.git
$ git clone https://github.com/mesomorphic/terraform-provider-confluence.git
$ cd terraform-provider-confluence
$ make install
```
Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.0
0.2.1
4 changes: 2 additions & 2 deletions confluence/attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func (c *Client) CreateAttachment(attachment *Attachment, data, pageId string) (
return nil, err
}
if len(response.Results) != 1 {
return nil, errors.New("Unexpected number of results returned when creating attachment")
return nil, errors.New("unexpected number of results returned when creating attachment")
}
return &response.Results[0], nil
}
Expand All @@ -50,7 +50,7 @@ func (c *Client) UpdateAttachment(attachment *Attachment, data, pageId string) (
return nil, err
}
if len(response.Results) != 1 {
return nil, errors.New("Unexpected number of results returned when updating attachment")
return nil, errors.New("unexpected number of results returned when updating attachment")
}
return &response.Results[0], nil
}
Expand Down
6 changes: 3 additions & 3 deletions confluence/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ type Content struct {
Id string `json:"id,omitempty"`
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Space *Space `json:"space,omitempty"`
Space *SpaceKey `json:"space,omitempty"`
Version *Version `json:"version,omitempty"`
Body *Body `json:"body,omitempty"`
Links *ContentLinks `json:"_links,omitempty"`
Expand All @@ -27,8 +27,8 @@ type ContentLinks struct {
WebUI string `json:"webui,omitempty"`
}

// Space is part of Content
type Space struct {
// SpaceKey is part of Content
type SpaceKey struct {
Key string `json:"key,omitempty"`
}

Expand Down
1 change: 1 addition & 0 deletions confluence/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ func Provider() *schema.Provider {
ResourcesMap: map[string]*schema.Resource{
"confluence_content": resourceContent(),
"confluence_attachment": resourceAttachment(),
"confluence_space": resourceSpace(),
},
ConfigureFunc: providerConfigure,
}
Expand Down
2 changes: 1 addition & 1 deletion confluence/resource_content.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func contentFromResourceData(d *schema.ResourceData) *Content {
result := &Content{
Id: d.Id(),
Type: d.Get("type").(string),
Space: &Space{
Space: &SpaceKey{
Key: d.Get("space").(string),
},
Body: &Body{
Expand Down
105 changes: 105 additions & 0 deletions confluence/resource_space.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package confluence

import (
"strconv"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)

func resourceSpace() *schema.Resource {
return &schema.Resource{
Create: resourceSpaceCreate,
Read: resourceSpaceRead,
Update: resourceSpaceUpdate,
Delete: resourceSpaceDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},

Schema: map[string]*schema.Schema{
"key": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringLenBetween(0, 255),
},
"url": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceSpaceCreate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
contentRequest := spaceFromResourceData(d)
contentResponse, err := client.CreateSpace(contentRequest)

if err != nil {
return err
}
d.SetId(strconv.Itoa(contentResponse.Id))
return resourceSpaceRead(d, m)
}

func resourceSpaceRead(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
contentResponse, err := client.GetSpace(d.Get("key").(string))
if err != nil {
d.SetId("")
return err
}
return updateResourceDataFromSpace(d, contentResponse, client)
}

func resourceSpaceUpdate(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
contentRequest := spaceFromResourceData(d)
_, err := client.UpdateSpace(contentRequest)
if err != nil {
d.SetId("")
return err
}
return resourceSpaceRead(d, m)
}

func resourceSpaceDelete(d *schema.ResourceData, m interface{}) error {
client := m.(*Client)
err := client.DeleteSpace(d.Get("key").(string))
if err != nil {
return err
}
// d.SetId("") is automatically called assuming delete returns no errors
return nil
}

func spaceFromResourceData(d *schema.ResourceData) *Space {
id, _ := strconv.Atoi(d.Id())
result := &Space{
Id: id,
Key: d.Get("key").(string),
Name: d.Get("name").(string),
}
return result
}

func updateResourceDataFromSpace(d *schema.ResourceData, space *Space, client *Client) error {
d.SetId(strconv.Itoa(space.Id))
m := map[string]interface{}{
"key": space.Key,
"name": space.Name,
"url": space.Links.Base + space.Links.WebUI,
}
for k, v := range m {
err := d.Set(k, v)
if err != nil {
return err
}
}
return nil
}
61 changes: 61 additions & 0 deletions confluence/space.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package confluence

import (
"fmt"
"strings"
)

// Content is a primary resource in Confluence
type Space struct {
Id int `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Key string `json:"key,omitempty"`
Links *SpaceLinks `json:"_links,omitempty"`
}

// ContentLinks is part of Content
type SpaceLinks struct {
Base string `json:"base,omitempty"`
WebUI string `json:"webui,omitempty"`
}

func (c *Client) CreateSpace(space *Space) (*Space, error) {
var response Space
if err := c.Post("/rest/api/space", space, &response); err != nil {
return nil, err
}
return &response, nil
}

func (c *Client) GetSpace(id string) (*Space, error) {
var response Space
path := fmt.Sprintf("/rest/api/space/%s", id)
if err := c.Get(path, &response); err != nil {
return nil, err
}

return &response, nil
}

func (c *Client) UpdateSpace(space *Space) (*Space, error) {
var response Space

path := fmt.Sprintf("/rest/api/space/%s", space.Key)
if err := c.Put(path, space, &response); err != nil {
return nil, err
}
return &response, nil
}

func (c *Client) DeleteSpace(id string) error {
path := fmt.Sprintf("/rest/api/space/%s", id)
if err := c.Delete(path); err != nil {
if strings.HasPrefix(err.Error(), "202 ") {
//202 is the delete API success response
//Other APIs return 204. Because, reasons.
return nil
}
return err
}
return nil
}
33 changes: 33 additions & 0 deletions examples/space/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
provider "confluence" {
site = var.site
user = var.user
token = var.token
}
resource confluence_space "example" {
key = var.space
name = "Terraformed Space"
}
resource confluence_content "example" {
space = confluence_space.example.key
title = "Terraformed Page"
body = "Terraformed Content"
}

variable "site" {
type = string
}

variable "user" {
type = string
}

variable "token" {
type = string
}

variable "space" {
type = string
}
output "example" {
value = confluence_space.example
}
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/chesshacker/terraform-provider-confluence
module github.com/mesomorphic/terraform-provider-confluence

go 1.16

Expand Down
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package main

import (
"github.com/chesshacker/terraform-provider-confluence/confluence"
"github.com/hashicorp/terraform-plugin-sdk/v2/plugin"
"github.com/mesomorphic/terraform-provider-confluence/confluence"
)

func main() {
Expand Down
5 changes: 3 additions & 2 deletions mkdocs.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
site_name: "Terraform Provider: Confluence"
site_url: https://chesshacker.github.io/terraform-provider-confluence/
repo_url: https://github.com/chesshacker/terraform-provider-confluence
site_url: https://github.com/mesomorphic/terraform-provider-confluence
repo_url: https://github.com/mesomorphic/terraform-provider-confluence
nav:
- Getting Started: index.md
- Resources:
- confluence_attachment: resources/confluence_attachment.md
- confluence_content: resources/confluence_content.md
- confluence_space: resources/confluence_space.md
theme: readthedocs
2 changes: 1 addition & 1 deletion scripts/changelog-links.sh
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ else
SED="sed -i.bak -r -e"
fi

PROVIDER_URL="https:\/\/github.com\/chesshacker\/terraform-provider-confluence\/issues"
PROVIDER_URL="https:\/\/github.com\/mesomorphic\/terraform-provider-confluence\/issues"

$SED "s/GH-([0-9]+)/\[#\1\]\($PROVIDER_URL\/\1\)/g" -e 's/\[\[#(.+)([0-9])\)]$/(\[#\1\2))/g' CHANGELOG.md

Expand Down
6 changes: 6 additions & 0 deletions terraform-registry-manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"version": 1,
"metadata": {
"protocol_versions": ["5.0"]
}
}

0 comments on commit 6b3578f

Please sign in to comment.