diff --git a/client/monitor_create.go b/client/monitor_create.go index 7c8c334..2c7cdc1 100644 --- a/client/monitor_create.go +++ b/client/monitor_create.go @@ -2,11 +2,12 @@ package client import ( "context" + "encoding/json" hreq "github.com/imroc/req/v3" ) -type CreateMonitorRequest struct { +type MonitorRequest struct { Active bool `json:"active"` Body string `json:"body"` Description string `json:"description"` @@ -22,14 +23,19 @@ type CreateMonitorRequest struct { Url string `json:"url"` } -func CreateMonitor(ctx context.Context, c *hreq.Client, request CreateMonitorRequest) { - // TODO - Implement this method +func CreateMonitor(ctx context.Context, c *hreq.Client, request MonitorRequest) (*MonitorRequest, error) { - err := c.Post("monitor").SetBody(&request).Do() + response := c.Post("monitor").SetBody(&request).Do() - if err != nil { - panic(err) + if response.Err != nil { + return nil, response.Err + + } + + var monitor MonitorRequest + if err := json.NewDecoder(response.Body).Decode(&monitor); err != nil { + return nil, err } - return + return &monitor, nil } diff --git a/client/monitor_delete.go b/client/monitor_delete.go new file mode 100644 index 0000000..aec7688 --- /dev/null +++ b/client/monitor_delete.go @@ -0,0 +1,19 @@ +package client + +import ( + "context" + + hreq "github.com/imroc/req/v3" +) + +func DeleteMonitor(ctx context.Context, c *hreq.Client, id string) *error { + + req := c.Delete("monitor/" + id).Do() + + if req.Err != nil { + return &req.Err + + } + return nil + +} diff --git a/client/monitor_get.go b/client/monitor_get.go new file mode 100644 index 0000000..cab4a26 --- /dev/null +++ b/client/monitor_get.go @@ -0,0 +1,23 @@ +package client + +import ( + "context" + "encoding/json" + + hreq "github.com/imroc/req/v3" +) + +func GetMonitor(ctx context.Context, c *hreq.Client, id string) (*MonitorRequest, error) { + + request := c.Get("monitor/" + id).Do() + + if request.Err != nil { + return nil, request.Err + } + var monitor MonitorRequest + if err := json.NewDecoder(request.Body).Decode(&monitor); err != nil { + return nil, err + } + + return &monitor, nil +} diff --git a/docs/index.md b/docs/index.md new file mode 100644 index 0000000..0774559 --- /dev/null +++ b/docs/index.md @@ -0,0 +1,20 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "openstatus Provider" +subcategory: "" +description: |- + +--- + +# openstatus Provider + + + + + + +## Schema + +### Required + +- `openstatus_api_token` (String) openstatus.dev api token. diff --git a/docs/resources/monitor.md b/docs/resources/monitor.md new file mode 100644 index 0000000..cefe2d2 --- /dev/null +++ b/docs/resources/monitor.md @@ -0,0 +1,43 @@ +--- +# generated by https://github.com/hashicorp/terraform-plugin-docs +page_title: "openstatus_monitor Resource - terraform-provider-openstatus" +subcategory: "" +description: |- + +--- + +# openstatus_monitor (Resource) + + + + + + +## Schema + +### Required + +- `periodicity` (String) How often the monitor should run +- `regions` (List of String) The regions to use +- `url` (String) The url to monitor + +### Optional + +- `active` (Boolean) If the monitor is active +- `body` (String) The body +- `description` (String) The description of your monitor +- `headers` (Attributes List) The headers of your request (see [below for nested schema](#nestedatt--headers)) +- `method` (String) +- `name` (String) The name of the monitor + +### Read-Only + +- `id` (Number) The id of the monitor + + +### Nested Schema for `headers` + +Required: + +- `key` (String) +- `value` (String) diff --git a/internal/provider/monitor_resource.go b/internal/provider/monitor_resource.go index 1077299..bf873c2 100644 --- a/internal/provider/monitor_resource.go +++ b/internal/provider/monitor_resource.go @@ -2,7 +2,7 @@ package provider import ( "context" - "fmt" + "math/big" "terraform-provider-openstatus/client" "terraform-provider-openstatus/internal/resource_monitor" @@ -80,7 +80,7 @@ func (r *monitorResource) Create(ctx context.Context, req resource.CreateRequest }) } - client.CreateMonitor(ctx, r.client, client.CreateMonitorRequest{ + out, err := client.CreateMonitor(ctx, r.client, client.MonitorRequest{ Active: true, Body: data.Body.ValueString(), Description: data.Description.ValueString(), @@ -93,19 +93,49 @@ func (r *monitorResource) Create(ctx context.Context, req resource.CreateRequest Method: data.Method.ValueString(), }) + if err != nil { + resp.Diagnostics.AddError("Error creating monitor", "Could not create the monitor:"+err.Error()) + return + } + + data.Id = types.NumberValue(big.NewFloat(float64(out.Id))) + data.Active = types.BoolValue(out.Active) + data.Body = types.StringValue(out.Body) + data.Description = types.StringValue(out.Description) + data.Url = types.StringValue(out.Url) + data.Name = types.StringValue(out.Name) + data.Periodicity = types.StringValue(out.Periodicity) + data.Method = types.StringValue(out.Method) + resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) } func (r *monitorResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { var data resource_monitor.MonitorModel - resp.Diagnostics.Append(bindObject(ctx, &data)...) + resp.Diagnostics.Append(req.State.Get(ctx, &data)...) + if resp.Diagnostics.HasError() { - fmt.Print("Error") return } + if !data.Id.IsNull() { + monitor, err := client.GetMonitor(ctx, r.client, data.Id.String()) + if err != nil { + resp.Diagnostics.AddError("Error reading monitor", "Could not read the monitor:"+err.Error()) + return + } + data.Id = types.NumberValue(big.NewFloat(float64(monitor.Id))) + data.Active = types.BoolValue(monitor.Active) + data.Body = types.StringValue(monitor.Body) + data.Description = types.StringValue(monitor.Description) + data.Url = types.StringValue(monitor.Url) + data.Name = types.StringValue(monitor.Name) + data.Periodicity = types.StringValue(monitor.Periodicity) + data.Method = types.StringValue(monitor.Method) + } resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) + } func (r *monitorResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { @@ -131,6 +161,11 @@ func (r *monitorResource) Delete(ctx context.Context, req resource.DeleteRequest return } + err := client.DeleteMonitor(ctx, r.client, data.Id.String()) + if err != nil { + resp.Diagnostics.AddError("Error creating monitor", "Could not create the monitor:") + return + } } func bindObject(ctx context.Context, monitor *resource_monitor.MonitorModel) diag.Diagnostics { diff --git a/internal/resource_monitor/monitor_resource_gen.go b/internal/resource_monitor/monitor_resource_gen.go index 56b69bc..a1e6de5 100644 --- a/internal/resource_monitor/monitor_resource_gen.go +++ b/internal/resource_monitor/monitor_resource_gen.go @@ -5,16 +5,19 @@ package resource_monitor import ( "context" "fmt" + "strings" + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" "github.com/hashicorp/terraform-plugin-framework/attr" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/resource/schema/booldefault" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/numberplanmodifier" + "github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier" "github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" "github.com/hashicorp/terraform-plugin-framework/schema/validator" "github.com/hashicorp/terraform-plugin-framework/types" "github.com/hashicorp/terraform-plugin-framework/types/basetypes" "github.com/hashicorp/terraform-plugin-go/tftypes" - "strings" "github.com/hashicorp/terraform-plugin-framework/resource/schema" ) @@ -27,7 +30,7 @@ func MonitorResourceSchema(ctx context.Context) schema.Schema { Computed: true, Description: "If the monitor is active", MarkdownDescription: "If the monitor is active", - Default: booldefault.StaticBool(false), + Default: booldefault.StaticBool(true), }, "body": schema.StringAttribute{ Optional: true, @@ -66,6 +69,9 @@ func MonitorResourceSchema(ctx context.Context) schema.Schema { Computed: true, Description: "The id of the monitor", MarkdownDescription: "The id of the monitor", + PlanModifiers: []planmodifier.Number{ + numberplanmodifier.UseStateForUnknown(), + }, }, "method": schema.StringAttribute{ Optional: true, diff --git a/main.go b/main.go index ad93701..9e49163 100644 --- a/main.go +++ b/main.go @@ -11,7 +11,7 @@ import ( func main() { opts := providerserver.ServeOpts{ - Address: "registry.terraform.io/openstatus/openstatus", + Address: "registry.terraform.io/openstatusHQ/openstatus", } err := providerserver.Serve(context.Background(), provider.New(), opts)