Skip to content

Commit 4b28fbb

Browse files
authored
Merge pull request #34 from svantepihl/user-attributes
Add support for User Attribute resource type
2 parents da5301d + 1a3836c commit 4b28fbb

File tree

9 files changed

+395
-9
lines changed

9 files changed

+395
-9
lines changed

README.md

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,10 @@
44

55
Permit.io is a cloud-based authorization service that allows you to define and manage permissions for your application.
66
In order to make it easier and safer to manage your objects and policies in Permit.io, we have created a Terraform provider.
7-
8-
97

108
_This provider repository is built on the [Terraform Plugin Framework](https://github.com/hashicorp/terraform-plugin-framework). The template repository built on the [Terraform Plugin SDK](https://github.com/hashicorp/terraform-plugin-sdk) can be found at [terraform-provider-scaffolding](https://github.com/hashicorp/terraform-provider-scaffolding). See [Which SDK Should I Use?](https://developer.hashicorp.com/terraform/plugin/framework-benefits) in the Terraform documentation for additional information._
119

12-
## Usage
10+
## Usage
1311

1412
The [examples directory](./examples) contains a number of examples of how to use the provider.
1513

@@ -26,8 +24,8 @@ terraform {
2624
}
2725
```
2826

29-
3027
### Configure the Provider
28+
3129
```hcl
3230
provider "permitio" {
3331
api_url = "https://api.permit.io" # Defaults to - "https://api.permit.io - Can be set as an environment variable PERMITIO_API_URL
@@ -38,6 +36,7 @@ provider "permitio" {
3836
### Creating Objects in Permitio
3937

4038
#### Create a Resource
39+
4140
```hcl
4241
resource "permitio_resource" "document" {
4342
key = "document"
@@ -74,6 +73,15 @@ resource "permitio_role" "reader" {
7473
}
7574
```
7675

76+
#### Create a User Attribute
77+
78+
```hcl
79+
resource "permitio_user_attribute" "department" {
80+
key = "department"
81+
description = "The department of the user"
82+
type = "string"
83+
}
84+
```
7785

7886
## Requirements
7987

@@ -118,7 +126,7 @@ To generate or update documentation, run `go generate`.
118126

119127
In order to run the full suite of Acceptance tests, run `make testacc`.
120128

121-
*Note:* Acceptance tests create real resources, and often cost money to run.
129+
_Note:_ Acceptance tests create real resources, and often cost money to run.
122130

123131
```shell
124132
make testacc
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "permitio_user_attribute Resource - terraform-provider-permit-io"
4+
subcategory: ""
5+
description: |-
6+
See the documentation https://api.permit.io/v2/redoc#tag/User-Attributes/operation/create_user_attribute for more information about User Attributes
7+
---
8+
9+
# permitio_user_attribute (Resource)
10+
11+
See [the documentation](https://api.permit.io/v2/redoc#tag/User-Attributes/operation/create_user_attribute) for more information about User Attributes
12+
13+
14+
15+
<!-- schema generated by tfplugindocs -->
16+
## Schema
17+
18+
### Required
19+
20+
- `description` (String) The description of the attribute
21+
- `key` (String) The key of the attribute
22+
- `type` (String) The type of the attribute
23+
24+
### Optional
25+
26+
- `updated_at` (String) The update timestamp. This is a timestamp for when the object was last updated.
27+
28+
### Read-Only
29+
30+
- `created_at` (String) The creation timestamp. This is a timestamp for when the object was created.
31+
- `environment_id` (String) The environment ID. This is a unique identifier for the environment.
32+
- `id` (String) The resource ID. This is a unique identifier for the resource.
33+
- `organization_id` (String) The organization ID. This is a unique identifier for the organization.
34+
- `project_id` (String) The project ID. This is a unique identifier for the project.
35+
- `resource_id` (String) The ID of the User resource
36+
- `resource_key` (String) The key of the User resource, will always be `__user`
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package common
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
8+
"github.com/permitio/permit-golang/pkg/models"
9+
)
10+
11+
type AttributeTypeValidator struct{}
12+
13+
func (a AttributeTypeValidator) Description(ctx context.Context) string {
14+
return "The type of the attribute in the resource."
15+
}
16+
17+
func (a AttributeTypeValidator) MarkdownDescription(ctx context.Context) string {
18+
return "The type of the attribute in the resource."
19+
}
20+
21+
func (a AttributeTypeValidator) ValidateString(ctx context.Context, request validator.StringRequest, response *validator.StringResponse) {
22+
if request.ConfigValue.IsUnknown() {
23+
response.Diagnostics.AddError("Unable to read resource attribute type",
24+
fmt.Sprintf("Unable to read resource attribute type: %s", request.Path.String()),
25+
)
26+
return
27+
}
28+
if request.ConfigValue.IsNull() {
29+
response.Diagnostics.AddError("Invalid resource attribute type",
30+
fmt.Sprintf("Invalid null resource attribute type: %s", request.Path.String()),
31+
)
32+
return
33+
}
34+
35+
value := request.ConfigValue.ValueString()
36+
if !models.AttributeType(value).IsValid() {
37+
response.Diagnostics.AddError("Invalid resource attribute type",
38+
fmt.Sprintf("Invalid resource attribute type: %s. Valid types are: %v", value, models.AllowedAttributeTypeEnumValues),
39+
)
40+
return
41+
}
42+
}

internal/provider/provider.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@ package provider
55

66
import (
77
"context"
8+
"os"
9+
"strconv"
10+
"time"
11+
812
"github.com/hashicorp/terraform-plugin-framework/path"
913
"github.com/hashicorp/terraform-plugin-log/tflog"
1014
permitConfig "github.com/permitio/permit-golang/pkg/config"
@@ -16,9 +20,7 @@ import (
1620
"github.com/permitio/terraform-provider-permit-io/internal/provider/resources"
1721
"github.com/permitio/terraform-provider-permit-io/internal/provider/role_derivations"
1822
"github.com/permitio/terraform-provider-permit-io/internal/provider/roles"
19-
"os"
20-
"strconv"
21-
"time"
23+
"github.com/permitio/terraform-provider-permit-io/internal/provider/user_attributes"
2224

2325
"github.com/hashicorp/terraform-plugin-framework/datasource"
2426
"github.com/hashicorp/terraform-plugin-framework/provider"
@@ -184,6 +186,7 @@ func (p *PermitProvider) Resources(_ context.Context) []func() resource.Resource
184186
proxy_configs.NewProxyConfigResource,
185187
relations.NewRelationResource,
186188
role_derivations.NewRoleDerivationResource,
189+
user_attributes.NewUserAttributeResource,
187190
}
188191
}
189192

internal/provider/resources/resource_resource.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ package resources
33
import (
44
"context"
55
"fmt"
6+
67
"github.com/hashicorp/terraform-plugin-framework/resource"
78
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
89
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
910
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1011
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
1112
"github.com/hashicorp/terraform-plugin-log/tflog"
1213
"github.com/permitio/permit-golang/pkg/permit"
14+
"github.com/permitio/terraform-provider-permit-io/internal/provider/common"
1315
)
1416

1517
// Ensure the implementation satisfies the expected interfaces.
@@ -141,7 +143,7 @@ func (r *ResourceResource) Schema(_ context.Context, _ resource.SchemaRequest, r
141143
"type": schema.StringAttribute{
142144
Required: true,
143145
Validators: []validator.String{
144-
attributeTypeValidator{},
146+
common.AttributeTypeValidator{},
145147
},
146148
},
147149
"description": schema.StringAttribute{
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package user_attributes
2+
3+
import (
4+
"context"
5+
6+
"github.com/permitio/permit-golang/pkg/models"
7+
"github.com/permitio/permit-golang/pkg/permit"
8+
)
9+
10+
type userAttributesClient struct {
11+
client *permit.Client
12+
}
13+
14+
func (c *userAttributesClient) Create(ctx context.Context, plan userAttributeModel) (userAttributeModel, error) {
15+
16+
attributeType, err := models.NewAttributeTypeFromValue(plan.Type.ValueString())
17+
18+
if err != nil {
19+
return userAttributeModel{}, err
20+
}
21+
22+
attributeCreate := models.ResourceAttributeCreate{}
23+
attributeCreate.SetKey(plan.Key.ValueString())
24+
attributeCreate.SetType(*attributeType)
25+
attributeCreate.SetDescription(plan.Description.ValueString())
26+
27+
createdAttribute, err := c.client.Api.ResourceAttributes.Create(ctx, UserKey, attributeCreate)
28+
29+
if err != nil {
30+
return userAttributeModel{}, err
31+
}
32+
33+
return tfModelFromSDK(*createdAttribute), nil
34+
}
35+
36+
func (c *userAttributesClient) Read(ctx context.Context, key string) (userAttributeModel, error) {
37+
readAttribute, err := c.client.Api.ResourceAttributes.Get(ctx, UserKey, key)
38+
39+
if err != nil {
40+
return userAttributeModel{}, err
41+
}
42+
43+
return tfModelFromSDK(*readAttribute), nil
44+
}
45+
46+
func (c *userAttributesClient) Delete(ctx context.Context, key string) error {
47+
return c.client.Api.ResourceAttributes.Delete(ctx, UserKey, key)
48+
}
49+
50+
func (c *userAttributesClient) Update(ctx context.Context, key string, plan userAttributeModel) (userAttributeModel, error) {
51+
attributeType, err := models.NewAttributeTypeFromValue(plan.Type.ValueString())
52+
53+
if err != nil {
54+
return userAttributeModel{}, err
55+
}
56+
57+
attributeUpdate := models.ResourceAttributeUpdate{}
58+
attributeUpdate.SetType(*attributeType)
59+
attributeUpdate.SetDescription(plan.Description.ValueString())
60+
61+
updatedAttribute, err := c.client.Api.ResourceAttributes.Update(ctx, UserKey, key, attributeUpdate)
62+
63+
if err != nil {
64+
return userAttributeModel{}, err
65+
}
66+
67+
return tfModelFromSDK(*updatedAttribute), nil
68+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package user_attributes
2+
3+
import (
4+
"github.com/hashicorp/terraform-plugin-framework/types"
5+
"github.com/permitio/permit-golang/pkg/models"
6+
)
7+
8+
const UserKey = "__user"
9+
10+
type userAttributeModel struct {
11+
Id types.String `tfsdk:"id"`
12+
OrganizationId types.String `tfsdk:"organization_id"`
13+
ProjectId types.String `tfsdk:"project_id"`
14+
EnvironmentId types.String `tfsdk:"environment_id"`
15+
16+
ResourceId types.String `tfsdk:"resource_id"`
17+
ResourceKey types.String `tfsdk:"resource_key"` // Will always be "__user"
18+
19+
CreatedAt types.String `tfsdk:"created_at"`
20+
UpdatedAt types.String `tfsdk:"updated_at"`
21+
22+
Type types.String `tfsdk:"type"`
23+
Key types.String `tfsdk:"key"`
24+
Description types.String `tfsdk:"description"`
25+
}
26+
27+
func tfModelFromSDK(m models.ResourceAttributeRead) userAttributeModel {
28+
return userAttributeModel{
29+
Id: types.StringValue(m.Id),
30+
OrganizationId: types.StringValue(m.OrganizationId),
31+
ProjectId: types.StringValue(m.ProjectId),
32+
EnvironmentId: types.StringValue(m.EnvironmentId),
33+
34+
ResourceId: types.StringValue(m.ResourceId),
35+
ResourceKey: types.StringValue(UserKey), // Will always be "__user"
36+
37+
CreatedAt: types.StringValue(m.CreatedAt.String()),
38+
UpdatedAt: types.StringValue(m.UpdatedAt.String()),
39+
40+
Type: types.StringValue(string(m.Type)),
41+
Key: types.StringValue(m.Key),
42+
Description: types.StringValue(*m.Description),
43+
}
44+
}

0 commit comments

Comments
 (0)