Skip to content

Commit 6b3578f

Browse files
committed
2 parents eec104d + 7ce8bd9 commit 6b3578f

File tree

16 files changed

+231
-24
lines changed

16 files changed

+231
-24
lines changed

.github/workflows/mkdocs-gh-deploy.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ jobs:
2727
run: pip install -r requirements.txt
2828
- name: MkDocs
2929
env:
30-
GH_PAT: ${{ secrets.GH_PAT }}
30+
GH_PAT: ${{ secrets.GITHUB_TOKEN }}
3131
run: |
3232
git remote set-url origin https://x-access-token:${GH_PAT}@github.com/${GITHUB_REPOSITORY}.git
3333
mkdocs gh-deploy

.github/workflows/release.yml

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@ jobs:
2828
name: Set up Go
2929
uses: actions/setup-go@v2
3030
with:
31-
go-version: 1.16
31+
go-version: 1.19
3232
-
3333
name: Import GPG key
3434
id: import_gpg
35-
uses: hashicorp/ghaction-import-gpg@v2.1.0
36-
env:
35+
uses: crazy-max/ghaction-import-gpg@v5.0.0
36+
with:
3737
# These secrets will need to be configured for the repository:
38-
GPG_PRIVATE_KEY: ${{ secrets.GPG_PRIVATE_KEY }}
39-
PASSPHRASE: ${{ secrets.PASSPHRASE }}
38+
gpg_private_key: ${{ secrets.GPG_PRIVATE_KEY }}
39+
passphrase: ${{ secrets.PASSPHRASE }}
4040
-
4141
name: Run GoReleaser
42-
uses: goreleaser/goreleaser-action@v2.7.0
42+
uses: goreleaser/goreleaser-action@v3
4343
with:
4444
version: latest
4545
args: release --rm-dist

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
![](https://github.com/chesshacker/terraform-provider-confluence/workflows/Build%20and%20Test/badge.svg)
1+
![](https://github.com/mesomorphic/terraform-provider-confluence/workflows/Build%20and%20Test/badge.svg)
22

33
# Terraform Provider for Confluence
44

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

77
## Requirements
88

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

1212
## Build and install the provider
1313

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

1616
```sh
17-
$ git clone https://github.com/chesshacker/terraform-provider-confluence.git
17+
$ git clone https://github.com/mesomorphic/terraform-provider-confluence.git
1818
$ cd terraform-provider-confluence
1919
$ make install
2020
```

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.2.0
1+
0.2.1

confluence/attachment.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func (c *Client) CreateAttachment(attachment *Attachment, data, pageId string) (
3737
return nil, err
3838
}
3939
if len(response.Results) != 1 {
40-
return nil, errors.New("Unexpected number of results returned when creating attachment")
40+
return nil, errors.New("unexpected number of results returned when creating attachment")
4141
}
4242
return &response.Results[0], nil
4343
}
@@ -50,7 +50,7 @@ func (c *Client) UpdateAttachment(attachment *Attachment, data, pageId string) (
5050
return nil, err
5151
}
5252
if len(response.Results) != 1 {
53-
return nil, errors.New("Unexpected number of results returned when updating attachment")
53+
return nil, errors.New("unexpected number of results returned when updating attachment")
5454
}
5555
return &response.Results[0], nil
5656
}

confluence/content.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ type Content struct {
1414
Id string `json:"id,omitempty"`
1515
Type string `json:"type,omitempty"`
1616
Title string `json:"title,omitempty"`
17-
Space *Space `json:"space,omitempty"`
17+
Space *SpaceKey `json:"space,omitempty"`
1818
Version *Version `json:"version,omitempty"`
1919
Body *Body `json:"body,omitempty"`
2020
Links *ContentLinks `json:"_links,omitempty"`
@@ -27,8 +27,8 @@ type ContentLinks struct {
2727
WebUI string `json:"webui,omitempty"`
2828
}
2929

30-
// Space is part of Content
31-
type Space struct {
30+
// SpaceKey is part of Content
31+
type SpaceKey struct {
3232
Key string `json:"key,omitempty"`
3333
}
3434

confluence/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func Provider() *schema.Provider {
5555
ResourcesMap: map[string]*schema.Resource{
5656
"confluence_content": resourceContent(),
5757
"confluence_attachment": resourceAttachment(),
58+
"confluence_space": resourceSpace(),
5859
},
5960
ConfigureFunc: providerConfigure,
6061
}

confluence/resource_content.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ func contentFromResourceData(d *schema.ResourceData) *Content {
103103
result := &Content{
104104
Id: d.Id(),
105105
Type: d.Get("type").(string),
106-
Space: &Space{
106+
Space: &SpaceKey{
107107
Key: d.Get("space").(string),
108108
},
109109
Body: &Body{

confluence/resource_space.go

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
package confluence
2+
3+
import (
4+
"strconv"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
8+
)
9+
10+
func resourceSpace() *schema.Resource {
11+
return &schema.Resource{
12+
Create: resourceSpaceCreate,
13+
Read: resourceSpaceRead,
14+
Update: resourceSpaceUpdate,
15+
Delete: resourceSpaceDelete,
16+
Importer: &schema.ResourceImporter{
17+
StateContext: schema.ImportStatePassthroughContext,
18+
},
19+
20+
Schema: map[string]*schema.Schema{
21+
"key": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
},
25+
"name": {
26+
Type: schema.TypeString,
27+
Required: true,
28+
ValidateFunc: validation.StringLenBetween(0, 255),
29+
},
30+
"url": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
},
35+
}
36+
}
37+
38+
func resourceSpaceCreate(d *schema.ResourceData, m interface{}) error {
39+
client := m.(*Client)
40+
contentRequest := spaceFromResourceData(d)
41+
contentResponse, err := client.CreateSpace(contentRequest)
42+
43+
if err != nil {
44+
return err
45+
}
46+
d.SetId(strconv.Itoa(contentResponse.Id))
47+
return resourceSpaceRead(d, m)
48+
}
49+
50+
func resourceSpaceRead(d *schema.ResourceData, m interface{}) error {
51+
client := m.(*Client)
52+
contentResponse, err := client.GetSpace(d.Get("key").(string))
53+
if err != nil {
54+
d.SetId("")
55+
return err
56+
}
57+
return updateResourceDataFromSpace(d, contentResponse, client)
58+
}
59+
60+
func resourceSpaceUpdate(d *schema.ResourceData, m interface{}) error {
61+
client := m.(*Client)
62+
contentRequest := spaceFromResourceData(d)
63+
_, err := client.UpdateSpace(contentRequest)
64+
if err != nil {
65+
d.SetId("")
66+
return err
67+
}
68+
return resourceSpaceRead(d, m)
69+
}
70+
71+
func resourceSpaceDelete(d *schema.ResourceData, m interface{}) error {
72+
client := m.(*Client)
73+
err := client.DeleteSpace(d.Get("key").(string))
74+
if err != nil {
75+
return err
76+
}
77+
// d.SetId("") is automatically called assuming delete returns no errors
78+
return nil
79+
}
80+
81+
func spaceFromResourceData(d *schema.ResourceData) *Space {
82+
id, _ := strconv.Atoi(d.Id())
83+
result := &Space{
84+
Id: id,
85+
Key: d.Get("key").(string),
86+
Name: d.Get("name").(string),
87+
}
88+
return result
89+
}
90+
91+
func updateResourceDataFromSpace(d *schema.ResourceData, space *Space, client *Client) error {
92+
d.SetId(strconv.Itoa(space.Id))
93+
m := map[string]interface{}{
94+
"key": space.Key,
95+
"name": space.Name,
96+
"url": space.Links.Base + space.Links.WebUI,
97+
}
98+
for k, v := range m {
99+
err := d.Set(k, v)
100+
if err != nil {
101+
return err
102+
}
103+
}
104+
return nil
105+
}

confluence/space.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package confluence
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
)
7+
8+
// Content is a primary resource in Confluence
9+
type Space struct {
10+
Id int `json:"id,omitempty"`
11+
Name string `json:"name,omitempty"`
12+
Key string `json:"key,omitempty"`
13+
Links *SpaceLinks `json:"_links,omitempty"`
14+
}
15+
16+
// ContentLinks is part of Content
17+
type SpaceLinks struct {
18+
Base string `json:"base,omitempty"`
19+
WebUI string `json:"webui,omitempty"`
20+
}
21+
22+
func (c *Client) CreateSpace(space *Space) (*Space, error) {
23+
var response Space
24+
if err := c.Post("/rest/api/space", space, &response); err != nil {
25+
return nil, err
26+
}
27+
return &response, nil
28+
}
29+
30+
func (c *Client) GetSpace(id string) (*Space, error) {
31+
var response Space
32+
path := fmt.Sprintf("/rest/api/space/%s", id)
33+
if err := c.Get(path, &response); err != nil {
34+
return nil, err
35+
}
36+
37+
return &response, nil
38+
}
39+
40+
func (c *Client) UpdateSpace(space *Space) (*Space, error) {
41+
var response Space
42+
43+
path := fmt.Sprintf("/rest/api/space/%s", space.Key)
44+
if err := c.Put(path, space, &response); err != nil {
45+
return nil, err
46+
}
47+
return &response, nil
48+
}
49+
50+
func (c *Client) DeleteSpace(id string) error {
51+
path := fmt.Sprintf("/rest/api/space/%s", id)
52+
if err := c.Delete(path); err != nil {
53+
if strings.HasPrefix(err.Error(), "202 ") {
54+
//202 is the delete API success response
55+
//Other APIs return 204. Because, reasons.
56+
return nil
57+
}
58+
return err
59+
}
60+
return nil
61+
}

0 commit comments

Comments
 (0)