|
| 1 | +package provider |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/diag" |
| 8 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 9 | + identityv1 "go.temporal.io/cloud-sdk/api/identity/v1" |
| 10 | + |
| 11 | + "github.com/temporalio/terraform-provider-temporalcloud/internal/provider/enums" |
| 12 | + internaltypes "github.com/temporalio/terraform-provider-temporalcloud/internal/types" |
| 13 | +) |
| 14 | + |
| 15 | +type ( |
| 16 | + usersDataModel struct { |
| 17 | + ID types.String `tfsdk:"id"` |
| 18 | + Users []userDataModel `tfsdk:"users"` |
| 19 | + } |
| 20 | + |
| 21 | + userDataModel struct { |
| 22 | + ID types.String `tfsdk:"id"` |
| 23 | + Email types.String `tfsdk:"email"` |
| 24 | + State types.String `tfsdk:"state"` |
| 25 | + AccountAccess internaltypes.CaseInsensitiveStringValue `tfsdk:"account_access"` |
| 26 | + NamespaceAccesses types.Set `tfsdk:"namespace_accesses"` |
| 27 | + CreatedAt types.String `tfsdk:"created_at"` |
| 28 | + UpdatedAt types.String `tfsdk:"updated_at"` |
| 29 | + } |
| 30 | + |
| 31 | + userNSAccessModel struct { |
| 32 | + NamespaceID types.String `tfsdk:"namespace_id"` |
| 33 | + Permission types.String `tfsdk:"permission"` |
| 34 | + } |
| 35 | +) |
| 36 | + |
| 37 | +func userToUserDataModel(ctx context.Context, sa *identityv1.User) (*userDataModel, diag.Diagnostics) { |
| 38 | + var diags diag.Diagnostics |
| 39 | + stateStr, err := enums.FromResourceState(sa.State) |
| 40 | + if err != nil { |
| 41 | + diags.AddError("Unable to convert user state", err.Error()) |
| 42 | + return nil, diags |
| 43 | + } |
| 44 | + |
| 45 | + userModel := &userDataModel{ |
| 46 | + ID: types.StringValue(sa.Id), |
| 47 | + Email: types.StringValue(sa.GetSpec().GetEmail()), |
| 48 | + State: types.StringValue(stateStr), |
| 49 | + CreatedAt: types.StringValue(sa.GetCreatedTime().AsTime().GoString()), |
| 50 | + UpdatedAt: types.StringValue(sa.GetLastModifiedTime().AsTime().GoString()), |
| 51 | + } |
| 52 | + |
| 53 | + role, err := enums.FromAccountAccessRole(sa.GetSpec().GetAccess().GetAccountAccess().GetRole()) |
| 54 | + if err != nil { |
| 55 | + diags.AddError("Failed to convert account access role", err.Error()) |
| 56 | + return nil, diags |
| 57 | + } |
| 58 | + |
| 59 | + userModel.AccountAccess = internaltypes.CaseInsensitiveString(role) |
| 60 | + |
| 61 | + namespaceAccesses := types.SetNull(types.ObjectType{AttrTypes: userNamespaceAccessAttrs}) |
| 62 | + |
| 63 | + if len(sa.GetSpec().GetAccess().GetNamespaceAccesses()) > 0 { |
| 64 | + namespaceAccessObjects := make([]types.Object, 0) |
| 65 | + for ns, namespaceAccess := range sa.GetSpec().GetAccess().GetNamespaceAccesses() { |
| 66 | + permission, err := enums.FromNamespaceAccessPermission(namespaceAccess.GetPermission()) |
| 67 | + if err != nil { |
| 68 | + diags.AddError("Failed to convert namespace access permission", err.Error()) |
| 69 | + return nil, diags |
| 70 | + } |
| 71 | + |
| 72 | + model := userNSAccessModel{ |
| 73 | + NamespaceID: types.StringValue(ns), |
| 74 | + Permission: types.StringValue(permission), |
| 75 | + } |
| 76 | + obj, d := types.ObjectValueFrom(ctx, userNamespaceAccessAttrs, model) |
| 77 | + diags.Append(d...) |
| 78 | + if diags.HasError() { |
| 79 | + return nil, diags |
| 80 | + } |
| 81 | + |
| 82 | + namespaceAccessObjects = append(namespaceAccessObjects, obj) |
| 83 | + } |
| 84 | + |
| 85 | + accesses, d := types.SetValueFrom(ctx, types.ObjectType{AttrTypes: userNamespaceAccessAttrs}, namespaceAccessObjects) |
| 86 | + diags.Append(d...) |
| 87 | + if diags.HasError() { |
| 88 | + return nil, diags |
| 89 | + } |
| 90 | + namespaceAccesses = accesses |
| 91 | + } |
| 92 | + userModel.NamespaceAccesses = namespaceAccesses |
| 93 | + |
| 94 | + return userModel, diags |
| 95 | +} |
| 96 | + |
| 97 | +func userSchema(idRequired bool) map[string]schema.Attribute { |
| 98 | + idAttribute := schema.StringAttribute{ |
| 99 | + Description: "The unique identifier of the User.", |
| 100 | + } |
| 101 | + |
| 102 | + switch idRequired { |
| 103 | + case true: |
| 104 | + idAttribute.Required = true |
| 105 | + case false: |
| 106 | + idAttribute.Computed = true |
| 107 | + } |
| 108 | + |
| 109 | + return map[string]schema.Attribute{ |
| 110 | + "id": idAttribute, |
| 111 | + "email": schema.StringAttribute{ |
| 112 | + Description: "The email of the User.", |
| 113 | + Computed: true, |
| 114 | + }, |
| 115 | + "state": schema.StringAttribute{ |
| 116 | + Description: "The current state of the User.", |
| 117 | + Computed: true, |
| 118 | + }, |
| 119 | + "account_access": schema.StringAttribute{ |
| 120 | + CustomType: internaltypes.CaseInsensitiveStringType{}, |
| 121 | + Description: "The role on the account. Must be one of admin, developer, or read (case-insensitive).", |
| 122 | + Computed: true, |
| 123 | + }, |
| 124 | + "namespace_accesses": schema.SetNestedAttribute{ |
| 125 | + Description: "The set of namespace permissions for this user, including each namespace and its role.", |
| 126 | + Optional: true, |
| 127 | + Computed: true, |
| 128 | + NestedObject: schema.NestedAttributeObject{ |
| 129 | + Attributes: map[string]schema.Attribute{ |
| 130 | + "namespace_id": schema.StringAttribute{ |
| 131 | + Description: "The namespace to assign permissions to.", |
| 132 | + Computed: true, |
| 133 | + }, |
| 134 | + "permission": schema.StringAttribute{ |
| 135 | + CustomType: types.StringType, |
| 136 | + Description: "The permission to assign. Must be one of admin, write, or read (case-insensitive)", |
| 137 | + Computed: true, |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + "created_at": schema.StringAttribute{ |
| 143 | + Description: "The creation time of the User.", |
| 144 | + Computed: true, |
| 145 | + }, |
| 146 | + "updated_at": schema.StringAttribute{ |
| 147 | + Description: "The last update time of the User.", |
| 148 | + Computed: true, |
| 149 | + }, |
| 150 | + } |
| 151 | +} |
0 commit comments