|
| 1 | +package tenants |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 8 | + "github.com/permitio/terraform-provider-permit-io/internal/provider/common" |
| 9 | +) |
| 10 | + |
| 11 | +// Ensure the implementation satisfies the expected interfaces. |
| 12 | +var ( |
| 13 | + _ resource.Resource = &TenantResource{} |
| 14 | + _ resource.ResourceWithConfigure = &TenantResource{} |
| 15 | +) |
| 16 | + |
| 17 | +func NewTenantResource() resource.Resource { |
| 18 | + return &TenantResource{} |
| 19 | +} |
| 20 | + |
| 21 | +type TenantResource struct { |
| 22 | + client tenantClient |
| 23 | +} |
| 24 | + |
| 25 | +func (r *TenantResource) Configure(ctx context.Context, request resource.ConfigureRequest, response *resource.ConfigureResponse) { |
| 26 | + permitClient := common.Configure(ctx, request, response) |
| 27 | + r.client = tenantClient{client: permitClient} |
| 28 | +} |
| 29 | + |
| 30 | +func (r *TenantResource) Metadata(_ context.Context, request resource.MetadataRequest, response *resource.MetadataResponse) { |
| 31 | + response.TypeName = request.ProviderTypeName + "_tenant" |
| 32 | +} |
| 33 | + |
| 34 | +func (r *TenantResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 35 | + attributes := common.CreateBaseResourceSchema() |
| 36 | + |
| 37 | + attributes["last_action_at"] = schema.StringAttribute{ |
| 38 | + MarkdownDescription: "Date and time when the tenant was last active (ISO_8601 format). In other words, this is the last time a permission check was done on a resource belonging to this tenant.", |
| 39 | + Computed: true, |
| 40 | + } |
| 41 | + |
| 42 | + attributes["attributes"] = schema.StringAttribute{ |
| 43 | + MarkdownDescription: "Arbitrary tenant attributes in JSON format that will be used to enforce attribute-based access control policies.", |
| 44 | + Optional: true, |
| 45 | + Computed: true, |
| 46 | + } |
| 47 | + |
| 48 | + resp.Schema = schema.Schema{ |
| 49 | + Attributes: attributes, |
| 50 | + MarkdownDescription: "Manages a Permit.io tenant. Tenants represent isolated groups or organizations within your application. See [the documentation](https://api.permit.io/v2/redoc#tag/Tenants) for more information about tenants.", |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +func (r *TenantResource) Create(ctx context.Context, request resource.CreateRequest, response *resource.CreateResponse) { |
| 55 | + var plan tenantModel |
| 56 | + |
| 57 | + response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...) |
| 58 | + |
| 59 | + if response.Diagnostics.HasError() { |
| 60 | + return |
| 61 | + } |
| 62 | + |
| 63 | + tenantRead, err := r.client.Create(ctx, plan) |
| 64 | + |
| 65 | + if err != nil { |
| 66 | + response.Diagnostics.AddError( |
| 67 | + "Unable to create tenant", |
| 68 | + fmt.Errorf("unable to create tenant: %w", err).Error(), |
| 69 | + ) |
| 70 | + return |
| 71 | + } |
| 72 | + |
| 73 | + response.Diagnostics.Append(response.State.Set(ctx, tenantRead)...) |
| 74 | +} |
| 75 | + |
| 76 | +func (r *TenantResource) Read(ctx context.Context, request resource.ReadRequest, response *resource.ReadResponse) { |
| 77 | + var model tenantModel |
| 78 | + |
| 79 | + response.Diagnostics.Append(request.State.Get(ctx, &model)...) |
| 80 | + |
| 81 | + if response.Diagnostics.HasError() { |
| 82 | + return |
| 83 | + } |
| 84 | + |
| 85 | + tenantRead, err := r.client.Read(ctx, model.Key.ValueString()) |
| 86 | + |
| 87 | + if err != nil { |
| 88 | + response.Diagnostics.AddError( |
| 89 | + "Unable to read tenant", |
| 90 | + fmt.Errorf("unable to read tenant: %w", err).Error(), |
| 91 | + ) |
| 92 | + return |
| 93 | + } |
| 94 | + |
| 95 | + response.Diagnostics.Append(response.State.Set(ctx, &tenantRead)...) |
| 96 | +} |
| 97 | + |
| 98 | +func (r *TenantResource) Update(ctx context.Context, request resource.UpdateRequest, response *resource.UpdateResponse) { |
| 99 | + var plan tenantModel |
| 100 | + |
| 101 | + response.Diagnostics.Append(request.Plan.Get(ctx, &plan)...) |
| 102 | + |
| 103 | + if response.Diagnostics.HasError() { |
| 104 | + return |
| 105 | + } |
| 106 | + |
| 107 | + tenantRead, err := r.client.Update(ctx, plan) |
| 108 | + |
| 109 | + if err != nil { |
| 110 | + response.Diagnostics.AddError( |
| 111 | + "Unable to update tenant", |
| 112 | + fmt.Errorf("unable to update tenant: %w", err).Error(), |
| 113 | + ) |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + response.Diagnostics.Append(response.State.Set(ctx, tenantRead)...) |
| 118 | +} |
| 119 | + |
| 120 | +func (r *TenantResource) Delete(ctx context.Context, request resource.DeleteRequest, response *resource.DeleteResponse) { |
| 121 | + var model tenantModel |
| 122 | + response.Diagnostics.Append(request.State.Get(ctx, &model)...) |
| 123 | + |
| 124 | + if response.Diagnostics.HasError() { |
| 125 | + return |
| 126 | + } |
| 127 | + |
| 128 | + err := r.client.Delete(ctx, model.Key.ValueString()) |
| 129 | + |
| 130 | + if err != nil { |
| 131 | + response.Diagnostics.AddError( |
| 132 | + "Unable to delete tenant", |
| 133 | + fmt.Errorf("unable to delete tenant: %w", err).Error(), |
| 134 | + ) |
| 135 | + } |
| 136 | +} |
0 commit comments