|
| 1 | +package grafana |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + goapi "github.com/grafana/grafana-openapi-client-go/client" |
| 9 | + "github.com/grafana/terraform-provider-grafana/v2/internal/common" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 12 | + frameworkSchema "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 16 | +) |
| 17 | + |
| 18 | +type basePluginFrameworkResource struct { |
| 19 | + client *goapi.GrafanaHTTPAPI |
| 20 | + config *goapi.TransportConfig |
| 21 | +} |
| 22 | + |
| 23 | +func (r *basePluginFrameworkResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 24 | + if req.ProviderData == nil { |
| 25 | + resp.Diagnostics.AddError( |
| 26 | + "Unconfigured Grafana API client", |
| 27 | + "the Grafana API client is required for this resource. Set the auth and url provider attributes", |
| 28 | + ) |
| 29 | + |
| 30 | + return |
| 31 | + } |
| 32 | + |
| 33 | + client, ok := req.ProviderData.(*common.Client) |
| 34 | + |
| 35 | + if !ok { |
| 36 | + resp.Diagnostics.AddError( |
| 37 | + "Unexpected Resource Configure Type", |
| 38 | + fmt.Sprintf("Expected *common.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 39 | + ) |
| 40 | + |
| 41 | + return |
| 42 | + } |
| 43 | + |
| 44 | + r.client = client.GrafanaAPI |
| 45 | + r.config = client.GrafanaAPIConfig |
| 46 | +} |
| 47 | + |
| 48 | +// clientFromExistingOrgResource creates a client from the ID of an org-scoped resource |
| 49 | +// Those IDs are in the <orgID>:<resourceID> format |
| 50 | +func (r *basePluginFrameworkResource) clientFromExistingOrgResource(idFormat *common.ResourceID, id string) (*goapi.GrafanaHTTPAPI, int64, []any, error) { |
| 51 | + client := r.client.Clone() |
| 52 | + split, err := idFormat.Split(id) |
| 53 | + if err != nil { |
| 54 | + return nil, 0, nil, err |
| 55 | + } |
| 56 | + var orgID int64 |
| 57 | + if len(split) < len(idFormat.Fields()) { |
| 58 | + orgID = client.OrgID() |
| 59 | + } else { |
| 60 | + orgID = split[0].(int64) |
| 61 | + split = split[1:] |
| 62 | + client = client.WithOrgID(orgID) |
| 63 | + } |
| 64 | + return client, orgID, split, nil |
| 65 | +} |
| 66 | + |
| 67 | +// clientFromNewOrgResource creates an OpenAPI client from the `org_id` attribute of a resource |
| 68 | +// This client is meant to be used in `Create` functions when the ID hasn't already been baked into the resource ID |
| 69 | +func (r *basePluginFrameworkResource) clientFromNewOrgResource(orgIDStr string) (*goapi.GrafanaHTTPAPI, int64) { |
| 70 | + client := r.client.Clone() |
| 71 | + orgID, _ := strconv.ParseInt(orgIDStr, 10, 64) |
| 72 | + if orgID == 0 { |
| 73 | + orgID = client.OrgID() |
| 74 | + } else if orgID > 0 { |
| 75 | + client = client.WithOrgID(orgID) |
| 76 | + } |
| 77 | + return client, orgID |
| 78 | +} |
| 79 | + |
| 80 | +// To be used in non-org-scoped resources |
| 81 | +// func (r *basePluginFrameworkResource) globalClient() (*goapi.GrafanaHTTPAPI, error) { |
| 82 | +// client := r.client.Clone().WithOrgID(0) |
| 83 | +// if r.config.APIKey != "" { |
| 84 | +// return client, fmt.Errorf("global scope resources cannot be managed with an API key. Use basic auth instead") |
| 85 | +// } |
| 86 | +// return client, nil |
| 87 | +// } |
| 88 | + |
| 89 | +func pluginFrameworkOrgIDAttribute() frameworkSchema.Attribute { |
| 90 | + return frameworkSchema.StringAttribute{ |
| 91 | + Optional: true, |
| 92 | + Computed: true, |
| 93 | + Description: "The Organization ID. If not set, the Org ID defined in the provider block will be used.", |
| 94 | + PlanModifiers: []planmodifier.String{ |
| 95 | + stringplanmodifier.RequiresReplace(), |
| 96 | + &orgIDAttributePlanModifier{}, |
| 97 | + }, |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +type orgIDAttributePlanModifier struct{} |
| 102 | + |
| 103 | +func (d *orgIDAttributePlanModifier) Description(ctx context.Context) string { |
| 104 | + return "Ignores the org_id attribute when it is empty, and uses the provider's org_id instead." |
| 105 | +} |
| 106 | + |
| 107 | +func (d *orgIDAttributePlanModifier) MarkdownDescription(ctx context.Context) string { |
| 108 | + return d.Description(ctx) |
| 109 | +} |
| 110 | + |
| 111 | +func (d *orgIDAttributePlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { |
| 112 | + var orgID types.String |
| 113 | + diags := req.Plan.GetAttribute(ctx, path.Root("org_id"), &orgID) |
| 114 | + resp.Diagnostics.Append(diags...) |
| 115 | + if resp.Diagnostics.HasError() { |
| 116 | + return |
| 117 | + } |
| 118 | + |
| 119 | + // If the org_id is empty, we want to use the provider's org_id |
| 120 | + // We don't want to show any diff |
| 121 | + if (orgID.IsNull() || orgID.ValueString() == "") && !req.StateValue.IsNull() { |
| 122 | + resp.PlanValue = req.StateValue |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +type orgScopedAttributePlanModifier struct{} |
| 127 | + |
| 128 | +func (d *orgScopedAttributePlanModifier) Description(ctx context.Context) string { |
| 129 | + return "Ignores the orgID part of a resource ID." |
| 130 | +} |
| 131 | + |
| 132 | +func (d *orgScopedAttributePlanModifier) MarkdownDescription(ctx context.Context) string { |
| 133 | + return d.Description(ctx) |
| 134 | +} |
| 135 | + |
| 136 | +func (d *orgScopedAttributePlanModifier) PlanModifyString(ctx context.Context, req planmodifier.StringRequest, resp *planmodifier.StringResponse) { |
| 137 | + // Equality should ignore the org ID |
| 138 | + _, first := SplitOrgResourceID(req.StateValue.ValueString()) |
| 139 | + _, second := SplitOrgResourceID(resp.PlanValue.ValueString()) |
| 140 | + |
| 141 | + if first != "" && first == second { |
| 142 | + resp.PlanValue = req.StateValue |
| 143 | + } |
| 144 | +} |
0 commit comments