|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + |
| 8 | + "github.com/grafana/grafana-openapi-client-go/client/enterprise" |
| 9 | + "github.com/grafana/grafana-openapi-client-go/models" |
| 10 | + "github.com/grafana/terraform-provider-grafana/v3/internal/common" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 12 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 15 | +) |
| 16 | + |
| 17 | +// Note: The LBAC Rules API only supports GET and UPDATE operations. |
| 18 | +// There is no CREATE or DELETE API endpoint. The UPDATE operation is used |
| 19 | +// for all modifications (create/update/delete) by sending the complete desired |
| 20 | +// state. Deleting rules is done by sending an empty rules list. |
| 21 | + |
| 22 | +var ( |
| 23 | + // Check interface |
| 24 | + _ resource.ResourceWithImportState = (*resourceDataSourceConfigLBACRules)(nil) |
| 25 | +) |
| 26 | + |
| 27 | +var ( |
| 28 | + resourceDataSourceConfigLBACRulesName = "grafana_data_source_config_lbac_rules" |
| 29 | + resourceDataSourceConfigLBACRulesID = common.NewResourceID( |
| 30 | + common.StringIDField("datasource_uid"), |
| 31 | + ) |
| 32 | +) |
| 33 | + |
| 34 | +func makeResourceDataSourceConfigLBACRules() *common.Resource { |
| 35 | + resourceStruct := &resourceDataSourceConfigLBACRules{} |
| 36 | + return common.NewResource( |
| 37 | + common.CategoryGrafanaEnterprise, |
| 38 | + resourceDataSourceConfigLBACRulesName, |
| 39 | + resourceDataSourceConfigLBACRulesID, |
| 40 | + resourceStruct, |
| 41 | + ) |
| 42 | +} |
| 43 | + |
| 44 | +type resourceDataSourceConfigLBACRulesModel struct { |
| 45 | + ID types.String `tfsdk:"id"` |
| 46 | + DatasourceUID types.String `tfsdk:"datasource_uid"` |
| 47 | + Rules types.String `tfsdk:"rules"` |
| 48 | +} |
| 49 | + |
| 50 | +type resourceDataSourceConfigLBACRules struct { |
| 51 | + client *common.Client |
| 52 | +} |
| 53 | + |
| 54 | +func (r *resourceDataSourceConfigLBACRules) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 55 | + resp.TypeName = resourceDataSourceConfigLBACRulesName |
| 56 | +} |
| 57 | + |
| 58 | +func (r *resourceDataSourceConfigLBACRules) Schema(ctx context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 59 | + resp.Schema = schema.Schema{ |
| 60 | + MarkdownDescription: ` |
| 61 | +Manages LBAC rules for a data source. |
| 62 | +
|
| 63 | +!> Warning: The resource is experimental and will be subject to change. This resource manages the entire LBAC rules tree, and will overwrite any existing rules. |
| 64 | +
|
| 65 | +* [Official documentation](https://grafana.com/docs/grafana/latest/administration/data-source-management/teamlbac/) |
| 66 | +* [HTTP API](https://grafana.com/docs/grafana/latest/developers/http_api/datasource_lbac_rules/) |
| 67 | +
|
| 68 | +This resource requires Grafana >=11.5.0. |
| 69 | +`, |
| 70 | + Attributes: map[string]schema.Attribute{ |
| 71 | + "id": schema.StringAttribute{ |
| 72 | + Computed: true, |
| 73 | + }, |
| 74 | + "datasource_uid": schema.StringAttribute{ |
| 75 | + Required: true, |
| 76 | + Description: "The UID of the datasource.", |
| 77 | + }, |
| 78 | + "rules": schema.StringAttribute{ |
| 79 | + Required: true, |
| 80 | + Description: "JSON-encoded LBAC rules for the data source. Map of team UIDs to lists of rule strings.", |
| 81 | + }, |
| 82 | + }, |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +func (r *resourceDataSourceConfigLBACRules) Configure(ctx context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) { |
| 87 | + if req.ProviderData == nil { |
| 88 | + return |
| 89 | + } |
| 90 | + r.client = req.ProviderData.(*common.Client) |
| 91 | +} |
| 92 | + |
| 93 | +// Add this helper function to handle the common update logic |
| 94 | +func (r *resourceDataSourceConfigLBACRules) updateRules(ctx context.Context, data *resourceDataSourceConfigLBACRulesModel, rules map[string][]string) error { |
| 95 | + apiRules := make([]*models.TeamLBACRule, 0, len(rules)) |
| 96 | + for teamUID, ruleList := range rules { |
| 97 | + apiRules = append(apiRules, &models.TeamLBACRule{ |
| 98 | + TeamUID: teamUID, |
| 99 | + Rules: ruleList, |
| 100 | + }) |
| 101 | + } |
| 102 | + |
| 103 | + params := &enterprise.UpdateTeamLBACRulesAPIParams{ |
| 104 | + Context: ctx, |
| 105 | + UID: data.DatasourceUID.ValueString(), |
| 106 | + Body: &models.UpdateTeamLBACCommand{Rules: apiRules}, |
| 107 | + } |
| 108 | + |
| 109 | + _, err := r.client.GrafanaAPI.Enterprise.UpdateTeamLBACRulesAPI(params) |
| 110 | + if err != nil { |
| 111 | + return fmt.Errorf("failed to update LBAC rules for datasource %q: %w", data.DatasourceUID.ValueString(), err) |
| 112 | + } |
| 113 | + return nil |
| 114 | +} |
| 115 | + |
| 116 | +func (r *resourceDataSourceConfigLBACRules) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 117 | + var data resourceDataSourceConfigLBACRulesModel |
| 118 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 119 | + if resp.Diagnostics.HasError() { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + rulesMap := make(map[string][]string) |
| 124 | + if err := json.Unmarshal([]byte(data.Rules.ValueString()), &rulesMap); err != nil { |
| 125 | + resp.Diagnostics.AddError( |
| 126 | + "Invalid rules JSON", |
| 127 | + fmt.Sprintf("Failed to parse rules for datasource %q: %v. Please ensure the rules are valid JSON.", data.DatasourceUID.ValueString(), err), |
| 128 | + ) |
| 129 | + return |
| 130 | + } |
| 131 | + |
| 132 | + if err := r.updateRules(ctx, &data, rulesMap); err != nil { |
| 133 | + resp.Diagnostics.AddError("Failed to create LBAC rules", err.Error()) |
| 134 | + return |
| 135 | + } |
| 136 | + |
| 137 | + data.ID = types.StringValue(data.DatasourceUID.ValueString()) |
| 138 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 139 | +} |
| 140 | + |
| 141 | +func (r *resourceDataSourceConfigLBACRules) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 142 | + var data resourceDataSourceConfigLBACRulesModel |
| 143 | + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) |
| 144 | + if resp.Diagnostics.HasError() { |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + datasourceUID := data.DatasourceUID.ValueString() |
| 149 | + client := r.client.GrafanaAPI |
| 150 | + |
| 151 | + getResp, err := client.Enterprise.GetTeamLBACRulesAPI(datasourceUID) |
| 152 | + if err != nil { |
| 153 | + resp.Diagnostics.AddError( |
| 154 | + "Failed to get LBAC rules", |
| 155 | + fmt.Sprintf("Could not read LBAC rules for datasource %q: %v", datasourceUID, err), |
| 156 | + ) |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + rulesMap := make(map[string][]string) |
| 161 | + for _, rule := range getResp.Payload.Rules { |
| 162 | + rulesMap[rule.TeamUID] = rule.Rules |
| 163 | + } |
| 164 | + |
| 165 | + rulesJSON, err := json.Marshal(rulesMap) |
| 166 | + if err != nil { |
| 167 | + // Marshal error should never happen for a valid map |
| 168 | + resp.Diagnostics.AddError( |
| 169 | + "Failed to encode rules", |
| 170 | + fmt.Sprintf("Could not encode LBAC rules for datasource %q: %v. This is an internal error, please report it.", datasourceUID, err), |
| 171 | + ) |
| 172 | + return |
| 173 | + } |
| 174 | + |
| 175 | + data = resourceDataSourceConfigLBACRulesModel{ |
| 176 | + ID: types.StringValue(datasourceUID), |
| 177 | + DatasourceUID: types.StringValue(datasourceUID), |
| 178 | + Rules: types.StringValue(string(rulesJSON)), |
| 179 | + } |
| 180 | + |
| 181 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 182 | +} |
| 183 | + |
| 184 | +func (r *resourceDataSourceConfigLBACRules) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 185 | + var data resourceDataSourceConfigLBACRulesModel |
| 186 | + resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) |
| 187 | + if resp.Diagnostics.HasError() { |
| 188 | + return |
| 189 | + } |
| 190 | + |
| 191 | + rulesMap := make(map[string][]string) |
| 192 | + if err := json.Unmarshal([]byte(data.Rules.ValueString()), &rulesMap); err != nil { |
| 193 | + resp.Diagnostics.AddError( |
| 194 | + "Invalid rules JSON", |
| 195 | + fmt.Sprintf("Failed to parse updated rules for datasource %q: %v. Please ensure the rules are valid JSON.", data.DatasourceUID.ValueString(), err), |
| 196 | + ) |
| 197 | + return |
| 198 | + } |
| 199 | + |
| 200 | + if err := r.updateRules(ctx, &data, rulesMap); err != nil { |
| 201 | + resp.Diagnostics.AddError("Failed to update LBAC rules", err.Error()) |
| 202 | + return |
| 203 | + } |
| 204 | + |
| 205 | + data.ID = types.StringValue(data.DatasourceUID.ValueString()) |
| 206 | + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) |
| 207 | +} |
| 208 | + |
| 209 | +func (r *resourceDataSourceConfigLBACRules) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 210 | + var state resourceDataSourceConfigLBACRulesModel |
| 211 | + resp.Diagnostics.Append(req.State.Get(ctx, &state)...) |
| 212 | + if resp.Diagnostics.HasError() { |
| 213 | + return |
| 214 | + } |
| 215 | + |
| 216 | + // Pass empty rules map to clear all rules |
| 217 | + if err := r.updateRules(ctx, &state, make(map[string][]string)); err != nil { |
| 218 | + resp.Diagnostics.AddError( |
| 219 | + "Failed to delete LBAC rules", |
| 220 | + fmt.Sprintf("Could not delete LBAC rules for datasource %q: %v", state.DatasourceUID.ValueString(), err), |
| 221 | + ) |
| 222 | + return |
| 223 | + } |
| 224 | +} |
| 225 | + |
| 226 | +func (r *resourceDataSourceConfigLBACRules) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 227 | + datasourceUID := req.ID |
| 228 | + |
| 229 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), datasourceUID)...) |
| 230 | + resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("datasource_uid"), datasourceUID)...) |
| 231 | +} |
0 commit comments