hhctl CLI and Terraform provider for Hammerhead
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
bf340cdab4
19 files changed
+2442
No files matched your search
@@ -0,0 +1,151 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/path"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/boolplanmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
|
||||
"git.veillax.com/Veillax/hhctl/client"
|
||||
)
|
||||
|
||||
var (
|
||||
_ resource.Resource = &channelResource{}
|
||||
_ resource.ResourceWithConfigure = &channelResource{}
|
||||
_ resource.ResourceWithImportState = &channelResource{}
|
||||
)
|
||||
|
||||
func NewAlertChannel() resource.Resource { return &channelResource{} }
|
||||
|
||||
type channelResource struct{ c *client.Client }
|
||||
|
||||
type channelModel struct {
|
||||
ID types.String `tfsdk:"id"`
|
||||
Name types.String `tfsdk:"name"`
|
||||
Kind types.String `tfsdk:"kind"`
|
||||
Target types.String `tfsdk:"target"`
|
||||
Format types.String `tfsdk:"format"`
|
||||
Token types.String `tfsdk:"token"`
|
||||
Policy types.String `tfsdk:"policy"`
|
||||
AllowPrivate types.Bool `tfsdk:"allow_private"`
|
||||
Enabled types.Bool `tfsdk:"enabled"`
|
||||
Server types.Bool `tfsdk:"server"`
|
||||
}
|
||||
|
||||
func (r *channelResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
||||
resp.TypeName = req.ProviderTypeName + "_alert_channel"
|
||||
}
|
||||
|
||||
func (r *channelResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
||||
opt := func(d string) schema.StringAttribute {
|
||||
return schema.StringAttribute{Optional: true, Computed: true, Default: stringdefault.StaticString(""), Description: d}
|
||||
}
|
||||
resp.Schema = schema.Schema{
|
||||
Description: "Somewhere alerts are sent: an email address, a webhook or a Sandbar server.",
|
||||
Attributes: map[string]schema.Attribute{
|
||||
"id": schema.StringAttribute{Computed: true, PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
|
||||
"name": schema.StringAttribute{Required: true},
|
||||
"kind": schema.StringAttribute{Required: true, Description: "email, webhook or sandbar."},
|
||||
"target": schema.StringAttribute{Required: true, Sensitive: true, Description: "Address(es), webhook URL or Sandbar address. Webhook URLs often carry tokens, so Hammerhead never returns them."},
|
||||
"format": opt("Webhook body: json, slack, discord or ntfy."),
|
||||
"token": schema.StringAttribute{Optional: true, Sensitive: true, Description: "Sandbar API key."},
|
||||
"policy": opt("Sandbar escalation policy ID; empty sends the alert without paging anyone."),
|
||||
"allow_private": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false),
|
||||
Description: "Allow a webhook target on a private address (organization admins may need allow-local)."},
|
||||
"enabled": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(true)},
|
||||
"server": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(false),
|
||||
PlanModifiers: []planmodifier.Bool{boolplanmodifier.RequiresReplace()},
|
||||
Description: "A server-level channel (for edges) instead of the organization's. Needs a server key."},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (r *channelResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
|
||||
r.c = clientFrom(req.ProviderData)
|
||||
}
|
||||
|
||||
func (m *channelModel) toAPI() client.Channel {
|
||||
return client.Channel{ID: m.ID.ValueString(), Name: m.Name.ValueString(), Kind: m.Kind.ValueString(), Target: m.Target.ValueString(),
|
||||
Format: m.Format.ValueString(), Token: m.Token.ValueString(), Policy: m.Policy.ValueString(),
|
||||
AllowPrivate: m.AllowPrivate.ValueBool(), Enabled: m.Enabled.ValueBool()}
|
||||
}
|
||||
|
||||
func (r *channelResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
var plan channelModel
|
||||
if resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...); resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
id, err := r.c.CreateChannel(plan.Server.ValueBool(), plan.toAPI())
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Creating alert channel", err.Error())
|
||||
return
|
||||
}
|
||||
plan.ID = types.StringValue(id)
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
||||
}
|
||||
|
||||
func (r *channelResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
||||
var state channelModel
|
||||
if resp.Diagnostics.Append(req.State.Get(ctx, &state)...); resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
a, err := r.c.Alerts(state.Server.ValueBool())
|
||||
if err != nil {
|
||||
resp.Diagnostics.AddError("Reading alert channels", err.Error())
|
||||
return
|
||||
}
|
||||
for _, ch := range a.Channels {
|
||||
if ch.ID != state.ID.ValueString() {
|
||||
continue
|
||||
}
|
||||
state.Name, state.Kind, state.Format, state.Policy = types.StringValue(ch.Name), types.StringValue(ch.Kind), types.StringValue(ch.Format), types.StringValue(ch.Policy)
|
||||
state.AllowPrivate, state.Enabled = types.BoolValue(ch.AllowPrivate), types.BoolValue(ch.Enabled)
|
||||
if ch.Kind == "email" { // the only target the server sends back; URLs and keys stay as configured
|
||||
state.Target = types.StringValue(ch.Target)
|
||||
}
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||
return
|
||||
}
|
||||
resp.State.RemoveResource(ctx)
|
||||
}
|
||||
|
||||
func (r *channelResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
||||
var plan, state channelModel
|
||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
|
||||
if resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
plan.ID = state.ID
|
||||
if err := r.c.UpdateChannel(plan.Server.ValueBool(), plan.toAPI()); err != nil {
|
||||
resp.Diagnostics.AddError("Updating alert channel", err.Error())
|
||||
return
|
||||
}
|
||||
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
||||
}
|
||||
|
||||
func (r *channelResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
||||
var state channelModel
|
||||
if resp.Diagnostics.Append(req.State.Get(ctx, &state)...); resp.Diagnostics.HasError() {
|
||||
return
|
||||
}
|
||||
if err := r.c.DeleteChannel(state.Server.ValueBool(), state.ID.ValueString()); err != nil && !client.IsNotFound(err) {
|
||||
resp.Diagnostics.AddError("Deleting alert channel", err.Error())
|
||||
}
|
||||
}
|
||||
|
||||
// ImportState takes "<id>" for an organization channel or "server/<id>" for a server-level one. The target and
|
||||
// token are not readable from Hammerhead, so set them in the configuration and apply once after importing.
|
||||
func (r *channelResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
||||
id, server := strings.CutPrefix(req.ID, "server/")
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), id)...)
|
||||
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("server"), server)...)
|
||||
}
|
||||
Reference in new issue
Block a user