|
| 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 | +} |
0 commit comments