|
| 1 | +package resource |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + |
| 7 | + "terraform-provider-plural/internal/common" |
| 8 | + "terraform-provider-plural/internal/model" |
| 9 | + |
| 10 | + "terraform-provider-plural/internal/client" |
| 11 | + |
| 12 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 13 | + "github.com/hashicorp/terraform-plugin-framework/path" |
| 14 | + "github.com/hashicorp/terraform-plugin-framework/resource" |
| 15 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema" |
| 16 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" |
| 17 | + "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier" |
| 18 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 19 | + gqlclient "github.com/pluralsh/console/go/client" |
| 20 | + "github.com/pluralsh/polly/algorithms" |
| 21 | +) |
| 22 | + |
| 23 | +var _ resource.Resource = &ObservabilityWebhookResource{} |
| 24 | +var _ resource.ResourceWithImportState = &ObservabilityWebhookResource{} |
| 25 | + |
| 26 | +func NewObservabilityWebhookResource() resource.Resource { |
| 27 | + return &ObservabilityWebhookResource{} |
| 28 | +} |
| 29 | + |
| 30 | +// ObservabilityWebhookResource defines the observability webhook resource implementation. |
| 31 | +type ObservabilityWebhookResource struct { |
| 32 | + client *client.Client |
| 33 | +} |
| 34 | + |
| 35 | +func (r *ObservabilityWebhookResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { |
| 36 | + resp.TypeName = req.ProviderTypeName + "_observability_webhook" |
| 37 | +} |
| 38 | + |
| 39 | +func (r *ObservabilityWebhookResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { |
| 40 | + resp.Schema = schema.Schema{ |
| 41 | + Attributes: map[string]schema.Attribute{ |
| 42 | + "id": schema.StringAttribute{ |
| 43 | + Computed: true, |
| 44 | + Description: "Internal identifier of this observability webhook.", |
| 45 | + MarkdownDescription: "Internal identifier of this observability webhook.", |
| 46 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 47 | + }, |
| 48 | + "name": schema.StringAttribute{ |
| 49 | + Description: "Human-readable name of this observability webhook.", |
| 50 | + MarkdownDescription: "Human-readable name of this observability webhook.", |
| 51 | + Required: true, |
| 52 | + PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}, |
| 53 | + }, |
| 54 | + "type": schema.StringAttribute{ |
| 55 | + Description: "Observability webhook type.", |
| 56 | + MarkdownDescription: "Observability webhook type.", |
| 57 | + Required: true, |
| 58 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 59 | + Validators: []validator.String{ |
| 60 | + stringvalidator.OneOf( |
| 61 | + algorithms.Map(gqlclient.AllObservabilityWebhookType, |
| 62 | + func(t gqlclient.ObservabilityWebhookType) string { return string(t) })...), |
| 63 | + }, |
| 64 | + }, |
| 65 | + "url": schema.StringAttribute{ |
| 66 | + Description: "Observability webhook URL.", |
| 67 | + MarkdownDescription: "Observability webhook URL.", |
| 68 | + Computed: true, |
| 69 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 70 | + }, |
| 71 | + "secret": schema.StringAttribute{ |
| 72 | + Description: "Observability webhook secret.", |
| 73 | + MarkdownDescription: "Observability webhook secret.", |
| 74 | + Optional: true, |
| 75 | + PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func (r *ObservabilityWebhookResource) Configure(_ context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) { |
| 82 | + if req.ProviderData == nil { |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + data, ok := req.ProviderData.(*common.ProviderData) |
| 87 | + if !ok { |
| 88 | + resp.Diagnostics.AddError( |
| 89 | + "Unexpected Observability Webhook Resource Configure Type", |
| 90 | + fmt.Sprintf("Expected *common.ProviderData, got: %T. Please report this issue to the provider developers.", req.ProviderData), |
| 91 | + ) |
| 92 | + |
| 93 | + return |
| 94 | + } |
| 95 | + |
| 96 | + r.client = data.Client |
| 97 | +} |
| 98 | + |
| 99 | +func (r *ObservabilityWebhookResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { |
| 100 | + data := new(model.ObservabilityWebhook) |
| 101 | + resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) |
| 102 | + if resp.Diagnostics.HasError() { |
| 103 | + return |
| 104 | + } |
| 105 | + |
| 106 | + sc, err := r.client.UpsertObservabilityWebhook(ctx, data.Attributes()) |
| 107 | + if err != nil { |
| 108 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create observability webhook, got error: %s", err)) |
| 109 | + return |
| 110 | + } |
| 111 | + |
| 112 | + data.From(sc.UpsertObservabilityWebhook) |
| 113 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 114 | +} |
| 115 | + |
| 116 | +func (r *ObservabilityWebhookResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { |
| 117 | + data := new(model.ObservabilityWebhook) |
| 118 | + resp.Diagnostics.Append(req.State.Get(ctx, data)...) |
| 119 | + if resp.Diagnostics.HasError() { |
| 120 | + return |
| 121 | + } |
| 122 | + |
| 123 | + response, err := r.client.GetObservabilityWebhook(ctx, nil, data.Name.ValueStringPointer()) |
| 124 | + if err != nil { |
| 125 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read observability webhook, got error: %s", err)) |
| 126 | + return |
| 127 | + } |
| 128 | + if response.ObservabilityWebhook == nil { |
| 129 | + resp.Diagnostics.AddError("Client Error", "Unable to find observability webhook, got no error") |
| 130 | + return |
| 131 | + } |
| 132 | + |
| 133 | + data.From(response.ObservabilityWebhook) |
| 134 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 135 | +} |
| 136 | + |
| 137 | +func (r *ObservabilityWebhookResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { |
| 138 | + data := new(model.ObservabilityWebhook) |
| 139 | + resp.Diagnostics.Append(req.Plan.Get(ctx, data)...) |
| 140 | + if resp.Diagnostics.HasError() { |
| 141 | + return |
| 142 | + } |
| 143 | + |
| 144 | + _, err := r.client.UpsertObservabilityWebhook(ctx, data.Attributes()) |
| 145 | + if err != nil { |
| 146 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update observability webhook, got error: %s", err)) |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + resp.Diagnostics.Append(resp.State.Set(ctx, data)...) |
| 151 | +} |
| 152 | + |
| 153 | +func (r *ObservabilityWebhookResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { |
| 154 | + data := new(model.ObservabilityWebhook) |
| 155 | + resp.Diagnostics.Append(req.State.Get(ctx, data)...) |
| 156 | + if resp.Diagnostics.HasError() { |
| 157 | + return |
| 158 | + } |
| 159 | + |
| 160 | + _, err := r.client.DeleteObservabilityWebhook(ctx, data.Id.ValueString()) |
| 161 | + if err != nil { |
| 162 | + resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete observability webhook, got error: %s", err)) |
| 163 | + return |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +func (r *ObservabilityWebhookResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) { |
| 168 | + resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp) |
| 169 | +} |
0 commit comments