Skip to content

Commit 8bfdf4d

Browse files
authored
Add page-permissions resource (#107)
1 parent e6c30b4 commit 8bfdf4d

12 files changed

+650
-3
lines changed
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "port_page_permissions Resource - terraform-provider-port-labs"
4+
subcategory: ""
5+
description: |-
6+
Page Permissions resource
7+
Docs about page permissions can be found here https://docs.getport.io/customize-pages-dashboards-and-plugins/page/page-permissions?view-permissions=api.
8+
Example Usage
9+
Allow read access to all members:
10+
hcl
11+
resource "port_page_permissions" "microservices_permissions" {
12+
page_identifier = "microservices"
13+
read = {
14+
"roles": ["Member"],
15+
"users": [],
16+
"teams": [],
17+
}
18+
}
19+
20+
Allow read access to all admins and a specific user and team:
21+
hcl
22+
resource "port_page_permissions" "microservices_permissions" {
23+
page_identifier = "microservices"
24+
read = {
25+
"roles": [
26+
"Admin",
27+
],
28+
"users": ["[email protected]"],
29+
"teams": ["Team Spiderman"],
30+
}
31+
}
32+
33+
Allow read access to specific users and teams:
34+
hcl
35+
resource "port_page_permissions" "microservices_permissions" {
36+
page_identifier = "microservices"
37+
read = {
38+
"roles": [],
39+
"users": ["[email protected]"],
40+
"teams": ["Team Spiderman"],
41+
}
42+
}
43+
44+
Disclaimer
45+
Page permissions are created by default when page is first created, this means that you should use this resource when you want to change the default permissions of a page.When deleting a page permissions resource using terraform, the page permissions will not be deleted from Port, as they are required for the action to work, instead, the page permissions will be removed from the terraform state.
46+
---
47+
48+
# port_page_permissions (Resource)
49+
50+
# Page Permissions resource
51+
52+
Docs about page permissions can be found [here](https://docs.getport.io/customize-pages-dashboards-and-plugins/page/page-permissions?view-permissions=api).
53+
54+
## Example Usage
55+
56+
### Allow read access to all members:
57+
58+
```hcl
59+
resource "port_page_permissions" "microservices_permissions" {
60+
page_identifier = "microservices"
61+
read = {
62+
"roles": ["Member"],
63+
"users": [],
64+
"teams": [],
65+
}
66+
}
67+
```
68+
69+
### Allow read access to all admins and a specific user and team:
70+
71+
```hcl
72+
resource "port_page_permissions" "microservices_permissions" {
73+
page_identifier = "microservices"
74+
read = {
75+
"roles": [
76+
"Admin",
77+
],
78+
"users": ["[email protected]"],
79+
"teams": ["Team Spiderman"],
80+
}
81+
}
82+
```
83+
84+
### Allow read access to specific users and teams:
85+
86+
```hcl
87+
resource "port_page_permissions" "microservices_permissions" {
88+
page_identifier = "microservices"
89+
read = {
90+
"roles": [],
91+
"users": ["[email protected]"],
92+
"teams": ["Team Spiderman"],
93+
}
94+
}
95+
```
96+
97+
## Disclaimer
98+
99+
- Page permissions are created by default when page is first created, this means that you should use this resource when you want to change the default permissions of a page.
100+
- When deleting a page permissions resource using terraform, the page permissions will not be deleted from Port, as they are required for the action to work, instead, the page permissions will be removed from the terraform state.
101+
102+
103+
104+
<!-- schema generated by tfplugindocs -->
105+
## Schema
106+
107+
### Required
108+
109+
- `page_identifier` (String)
110+
- `read` (Attributes) The permission to read the page (see [below for nested schema](#nestedatt--read))
111+
112+
### Read-Only
113+
114+
- `id` (String) The ID of this resource.
115+
116+
<a id="nestedatt--read"></a>
117+
### Nested Schema for `read`
118+
119+
Optional:
120+
121+
- `roles` (List of String) The roles with read permission
122+
- `teams` (List of String) The teams with read permission
123+
- `users` (List of String) The users with read permission

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ require (
1313
require (
1414
github.com/ProtonMail/go-crypto v0.0.0-20230626094100-7e9e0395ebec // indirect
1515
github.com/cloudflare/circl v1.3.3 // indirect
16+
github.com/gertd/go-pluralize v0.2.1 // indirect
1617
github.com/hashicorp/terraform-plugin-sdk/v2 v2.27.0 // indirect
1718
github.com/vmihailenco/msgpack/v5 v5.3.5 // indirect
1819
github.com/vmihailenco/tagparser/v2 v2.0.0 // indirect

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ github.com/fatih/color v1.13.0/go.mod h1:kLAiJbzzSOZDVNGyDpeOxJ47H46qBXwg5ILebYF
3333
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
3434
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
3535
github.com/frankban/quicktest v1.14.4 h1:g2rn0vABPOOXmZUj+vbmUp0lPoXEMuhTpIluN0XL9UY=
36+
github.com/gertd/go-pluralize v0.2.1 h1:M3uASbVjMnTsPb0PNqg+E/24Vwigyo/tvyMTtAlLgiA=
37+
github.com/gertd/go-pluralize v0.2.1/go.mod h1:rbYaKDbsXxmRfr8uygAEKhOWsjyrrqrkHVpZvoOp8zk=
3638
github.com/go-git/gcfg v1.5.0 h1:Q5ViNfGF8zFgyJWPqYwA7qGFoMTEiBmdlkcfRmpIMa4=
3739
github.com/go-git/go-billy/v5 v5.4.1 h1:Uwp5tDRkPr+l/TnbHOQzp+tmJfLceOlbVucgpTz8ix4=
3840
github.com/go-git/go-git/v5 v5.6.1 h1:q4ZRqQl4pR/ZJHc1L5CFjGA1a10u76aV1iC+nh+bHsk=

internal/cli/models.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,16 @@ type (
235235
Widgets *[]map[string]any `json:"widgets,omitempty"`
236236
}
237237

238+
PageReadPermissions struct {
239+
Users []string `json:"users"`
240+
Roles []string `json:"roles"`
241+
Teams []string `json:"teams"`
242+
}
243+
244+
PagePermissions struct {
245+
Read PageReadPermissions `json:"read"`
246+
}
247+
238248
Relation struct {
239249
Identifier *string `json:"identifier,omitempty"`
240250
Title *string `json:"title,omitempty"`
@@ -337,6 +347,11 @@ type PortBody struct {
337347
Migration Migration `json:"migration"`
338348
}
339349

350+
type PortPagePermissionsBody struct {
351+
OK bool `json:"ok"`
352+
PagePermissions PagePermissions `json:"permissions"`
353+
}
354+
340355
type TeamUserBody struct {
341356
Email string `json:"email"`
342357
}

internal/cli/pagePermissions.go

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package cli
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"fmt"
7+
)
8+
9+
func (c *PortClient) GetPagePermissions(ctx context.Context, pageID string) (*PagePermissions, int, error) {
10+
pppb := &PortPagePermissionsBody{}
11+
url := "v1/pages/{page_identifier}/permissions"
12+
resp, err := c.Client.R().
13+
SetContext(ctx).
14+
SetHeader("Accept", "application/json").
15+
SetResult(pppb).
16+
SetPathParam("page_identifier", pageID).
17+
Get(url)
18+
if err != nil {
19+
return nil, resp.StatusCode(), err
20+
}
21+
if !pppb.OK {
22+
return nil, resp.StatusCode(), fmt.Errorf("failed to get page permissions, got: %s", resp.Body())
23+
}
24+
return &pppb.PagePermissions, resp.StatusCode(), nil
25+
26+
}
27+
28+
func (c *PortClient) UpdatePagePermissions(ctx context.Context, pageID string, permissions *PagePermissions) (*PagePermissions, error) {
29+
url := "v1/pages/{page_identifier}/permissions"
30+
31+
resp, err := c.Client.R().
32+
SetBody(permissions).
33+
SetContext(ctx).
34+
SetPathParam("page_identifier", pageID).
35+
Patch(url)
36+
if err != nil {
37+
return nil, err
38+
}
39+
var pppb PortPagePermissionsBody
40+
err = json.Unmarshal(resp.Body(), &pppb)
41+
if err != nil {
42+
return nil, err
43+
}
44+
if !pppb.OK {
45+
return nil, fmt.Errorf("failed to update page permissions, got: %s", resp.Body())
46+
}
47+
return &pppb.PagePermissions, nil
48+
}

port/page-permissions/model.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package page_permissions
2+
3+
import "github.com/hashicorp/terraform-plugin-framework/types"
4+
5+
type ReadPagePermissionsModel struct {
6+
Users []types.String `tfsdk:"users"`
7+
Roles []types.String `tfsdk:"roles"`
8+
Teams []types.String `tfsdk:"teams"`
9+
}
10+
11+
type PagePermissionsModel struct {
12+
ID types.String `tfsdk:"id"`
13+
PageIdentifier types.String `tfsdk:"page_identifier"`
14+
Read ReadPagePermissionsModel `tfsdk:"read"`
15+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package page_permissions
2+
3+
import (
4+
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
5+
"github.com/port-labs/terraform-provider-port-labs/internal/flex"
6+
)
7+
8+
func pagePermissionsToPortBody(state *PagePermissionsModel) (*cli.PagePermissions, error) {
9+
if state == nil {
10+
return nil, nil
11+
}
12+
13+
pagePermissions := cli.PagePermissions{
14+
Read: cli.PageReadPermissions{
15+
Users: flex.TerraformStringListToGoArray(state.Read.Users),
16+
Roles: flex.TerraformStringListToGoArray(state.Read.Roles),
17+
Teams: flex.TerraformStringListToGoArray(state.Read.Teams),
18+
},
19+
}
20+
return &pagePermissions, nil
21+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package page_permissions
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
"github.com/port-labs/terraform-provider-port-labs/internal/cli"
6+
)
7+
8+
func refreshPagePermissionsState(state *PagePermissionsModel, a *cli.PagePermissions, pageId string) error {
9+
state.ID = types.StringValue(pageId)
10+
state.PageIdentifier = types.StringValue(pageId)
11+
state.Read = ReadPagePermissionsModel{}
12+
13+
state.Read.Users = make([]types.String, len(a.Read.Users))
14+
for i, u := range a.Read.Users {
15+
state.Read.Users[i] = types.StringValue(u)
16+
}
17+
18+
state.Read.Roles = make([]types.String, len(a.Read.Roles))
19+
for i, u := range a.Read.Roles {
20+
state.Read.Roles[i] = types.StringValue(u)
21+
}
22+
23+
state.Read.Teams = make([]types.String, len(a.Read.Teams))
24+
for i, u := range a.Read.Teams {
25+
state.Read.Teams[i] = types.StringValue(u)
26+
}
27+
28+
return nil
29+
}

0 commit comments

Comments
 (0)