Skip to content

Commit

Permalink
πŸš€ Improve terraform providers
Browse files Browse the repository at this point in the history
* πŸš€ improve terraform

* πŸš€ improve terraform
  • Loading branch information
thibaultleouay authored Feb 7, 2024
1 parent 8dd864c commit e3bcc46
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 14 deletions.
20 changes: 13 additions & 7 deletions client/monitor_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"`
Expand All @@ -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
}
19 changes: 19 additions & 0 deletions client/monitor_delete.go
Original file line number Diff line number Diff line change
@@ -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

}
23 changes: 23 additions & 0 deletions client/monitor_get.go
Original file line number Diff line number Diff line change
@@ -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
}
20 changes: 20 additions & 0 deletions docs/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "openstatus Provider"
subcategory: ""
description: |-
---

# openstatus Provider





<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `openstatus_api_token` (String) openstatus.dev api token.
43 changes: 43 additions & 0 deletions docs/resources/monitor.md
Original file line number Diff line number Diff line change
@@ -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 generated by tfplugindocs -->
## 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

<a id="nestedatt--headers"></a>
### Nested Schema for `headers`

Required:

- `key` (String)
- `value` (String)
43 changes: 39 additions & 4 deletions internal/provider/monitor_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package provider

import (
"context"
"fmt"
"math/big"

"terraform-provider-openstatus/client"
"terraform-provider-openstatus/internal/resource_monitor"
Expand Down Expand Up @@ -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(),
Expand All @@ -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) {
Expand All @@ -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 {
Expand Down
10 changes: 8 additions & 2 deletions internal/resource_monitor/monitor_resource_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit e3bcc46

Please sign in to comment.