|
| 1 | +//go:build alpha |
| 2 | + |
| 3 | +package resource |
| 4 | + |
| 5 | +import ( |
| 6 | + "context" |
| 7 | + _ "embed" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 18 | + |
| 19 | + "github.com/ClickHouse/terraform-provider-clickhouse/pkg/internal/api" |
| 20 | + "github.com/ClickHouse/terraform-provider-clickhouse/pkg/resource/models" |
| 21 | +) |
| 22 | + |
| 23 | +//go:embed descriptions/grant_role.md |
| 24 | +var grantResourceDescription string |
| 25 | + |
| 26 | +var ( |
| 27 | + _ resource.Resource = &GrantRoleResource{} |
| 28 | + _ resource.ResourceWithConfigure = &GrantRoleResource{} |
| 29 | +) |
| 30 | + |
| 31 | +func NewGrantRoleResource() resource.Resource { |
| 32 | + return &GrantRoleResource{} |
| 33 | +} |
| 34 | + |
| 35 | +type GrantRoleResource struct { |
| 36 | + client api.Client |
| 37 | +} |
| 38 | + |
| 39 | +func (r *GrantRoleResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 40 | + resp.TypeName = req.ProviderTypeName + "_grant_role" |
| 41 | +} |
| 42 | + |
| 43 | +func (r *GrantRoleResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 44 | + resp.Schema = schema.Schema{ |
| 45 | + Attributes: map[string]schema.Attribute{ |
| 46 | + "service_id": schema.StringAttribute{ |
| 47 | + Description: "ClickHouse Service ID", |
| 48 | + Required: true, |
| 49 | + PlanModifiers: []planmodifier.String{ |
| 50 | + stringplanmodifier.RequiresReplace(), |
| 51 | + }, |
| 52 | + }, |
| 53 | + "role_name": schema.StringAttribute{ |
| 54 | + Required: true, |
| 55 | + Description: "Name of the role to be granted", |
| 56 | + PlanModifiers: []planmodifier.String{ |
| 57 | + stringplanmodifier.RequiresReplace(), |
| 58 | + }, |
| 59 | + }, |
| 60 | + "grantee_user_name": schema.StringAttribute{ |
| 61 | + Optional: true, |
| 62 | + Description: "Name of the `user` to grant `role_name` to.", |
| 63 | + PlanModifiers: []planmodifier.String{ |
| 64 | + stringplanmodifier.RequiresReplace(), |
| 65 | + }, |
| 66 | + Validators: []validator.String{ |
| 67 | + stringvalidator.ConflictsWith(path.Expressions{path.MatchRoot("grantee_role_name")}...), |
| 68 | + stringvalidator.AtLeastOneOf(path.Expressions{ |
| 69 | + path.MatchRoot("grantee_user_name"), |
| 70 | + path.MatchRoot("grantee_role_name"), |
| 71 | + }...), |
| 72 | + }, |
| 73 | + }, |
| 74 | + "grantee_role_name": schema.StringAttribute{ |
| 75 | + Optional: true, |
| 76 | + Description: "Name of the `role` to grant `role_name` to.", |
| 77 | + PlanModifiers: []planmodifier.String{ |
| 78 | + stringplanmodifier.RequiresReplace(), |
| 79 | + }, |
| 80 | + Validators: []validator.String{ |
| 81 | + stringvalidator.ConflictsWith(path.Expressions{path.MatchRoot("grantee_user_name")}...), |
| 82 | + stringvalidator.AtLeastOneOf(path.Expressions{ |
| 83 | + path.MatchRoot("grantee_user_name"), |
| 84 | + path.MatchRoot("grantee_role_name"), |
| 85 | + }...), |
| 86 | + }, |
| 87 | + }, |
| 88 | + "admin_option": schema.BoolAttribute{ |
| 89 | + Optional: true, |
| 90 | + Computed: true, |
| 91 | + Description: "If true, the grantee will be able to grant `role_name` to other `users` or `roles`.", |
| 92 | + PlanModifiers: []planmodifier.Bool{ |
| 93 | + boolplanmodifier.RequiresReplace(), |
| 94 | + }, |
| 95 | + }, |
| 96 | + }, |
| 97 | + MarkdownDescription: grantResourceDescription, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func (r *GrantRoleResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { |
| 102 | + if req.ProviderData == nil { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + r.client = req.ProviderData.(api.Client) |
| 107 | +} |
| 108 | + |
| 109 | +func (r *GrantRoleResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 110 | + var plan models.GrantRole |
| 111 | + diags := req.Plan.Get(ctx, &plan) |
| 112 | + resp.Diagnostics.Append(diags...) |
| 113 | + if resp.Diagnostics.HasError() { |
| 114 | + return |
| 115 | + } |
| 116 | + |
| 117 | + grant := api.GrantRole{ |
| 118 | + RoleName: plan.RoleName.ValueString(), |
| 119 | + GranteeUserName: plan.GranteeUserName.ValueStringPointer(), |
| 120 | + GranteeRoleName: plan.GranteeRoleName.ValueStringPointer(), |
| 121 | + AdminOption: plan.AdminOption.ValueBool(), |
| 122 | + } |
| 123 | + |
| 124 | + createdGrant, err := r.client.GrantRole(ctx, plan.ServiceID.ValueString(), grant) |
| 125 | + if err != nil { |
| 126 | + resp.Diagnostics.AddError( |
| 127 | + "Error Creating ClickHouse Role Grant", |
| 128 | + "Could not create role grant, unexpected error: "+err.Error(), |
| 129 | + ) |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + state := models.GrantRole{ |
| 134 | + ServiceID: plan.ServiceID, |
| 135 | + RoleName: types.StringValue(createdGrant.RoleName), |
| 136 | + GranteeUserName: types.StringPointerValue(createdGrant.GranteeUserName), |
| 137 | + GranteeRoleName: types.StringPointerValue(createdGrant.GranteeRoleName), |
| 138 | + AdminOption: types.BoolValue(createdGrant.AdminOption), |
| 139 | + } |
| 140 | + |
| 141 | + diags = resp.State.Set(ctx, state) |
| 142 | + resp.Diagnostics.Append(diags...) |
| 143 | + if resp.Diagnostics.HasError() { |
| 144 | + return |
| 145 | + } |
| 146 | +} |
| 147 | + |
| 148 | +func (r *GrantRoleResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 149 | + var state models.GrantRole |
| 150 | + diags := req.State.Get(ctx, &state) |
| 151 | + resp.Diagnostics.Append(diags...) |
| 152 | + if resp.Diagnostics.HasError() { |
| 153 | + return |
| 154 | + } |
| 155 | + |
| 156 | + grant, err := r.client.GetGrantRole(ctx, state.ServiceID.ValueString(), state.RoleName.ValueString(), state.GranteeUserName.ValueStringPointer(), state.GranteeRoleName.ValueStringPointer()) |
| 157 | + if err != nil { |
| 158 | + resp.Diagnostics.AddError( |
| 159 | + "Error Reading ClickHouse Role Grant", |
| 160 | + "Could not read role grant, unexpected error: "+err.Error(), |
| 161 | + ) |
| 162 | + return |
| 163 | + } |
| 164 | + |
| 165 | + if grant != nil { |
| 166 | + state.RoleName = types.StringValue(grant.RoleName) |
| 167 | + state.GranteeUserName = types.StringPointerValue(grant.GranteeUserName) |
| 168 | + state.GranteeRoleName = types.StringPointerValue(grant.GranteeRoleName) |
| 169 | + state.AdminOption = types.BoolValue(grant.AdminOption) |
| 170 | + |
| 171 | + diags = resp.State.Set(ctx, &state) |
| 172 | + resp.Diagnostics.Append(diags...) |
| 173 | + } else { |
| 174 | + resp.State.RemoveResource(ctx) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +func (r *GrantRoleResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 179 | + panic("Update of grant resource is not supported") |
| 180 | +} |
| 181 | + |
| 182 | +func (r *GrantRoleResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 183 | + var state models.GrantRole |
| 184 | + diags := req.State.Get(ctx, &state) |
| 185 | + resp.Diagnostics.Append(diags...) |
| 186 | + if resp.Diagnostics.HasError() { |
| 187 | + return |
| 188 | + } |
| 189 | + |
| 190 | + err := r.client.RevokeGrantRole(ctx, state.ServiceID.ValueString(), state.RoleName.ValueString(), state.GranteeUserName.ValueStringPointer(), state.GranteeRoleName.ValueStringPointer()) |
| 191 | + if err != nil { |
| 192 | + resp.Diagnostics.AddError( |
| 193 | + "Error Deleting ClickHouse Role Grant", |
| 194 | + "Could not delete role grant, unexpected error: "+err.Error(), |
| 195 | + ) |
| 196 | + return |
| 197 | + } |
| 198 | +} |
0 commit comments