Skip to content

Commit

Permalink
working locally
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-owengoebel committed Jul 31, 2024
1 parent 793fc25 commit d3cd882
Show file tree
Hide file tree
Showing 5 changed files with 157 additions and 145 deletions.
8 changes: 8 additions & 0 deletions client/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,14 @@ func (c *Client) DeleteMonitorV2Action(ctx context.Context, id string) error {
return c.Meta.DeleteMonitorV2Action(ctx, id)
}

func (c *Client) SaveActionWithDestinationLinks(ctx context.Context, actionId string, destinationLinks []meta.ActionDestinationLinkInput) (*meta.MonitorV2Action, error) {
if !c.Flags[flagObs2110] {
c.obs2110.Lock()
defer c.obs2110.Unlock()
}
return c.Meta.SaveActionWithDestinationLinks(ctx, actionId, destinationLinks)
}

func (c *Client) CreateMonitorV2Destination(ctx context.Context, workspaceId string, input *meta.MonitorV2DestinationInput) (*meta.MonitorV2Destination, error) {
if !c.Flags[flagObs2110] {
c.obs2110.Lock()
Expand Down
41 changes: 41 additions & 0 deletions docs/resources/monitor_v2_action.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ description: |-
### Optional

- `description` (String)
- `destination` (Block List, Max: 1) (see [below for nested schema](#nestedblock--destination))
- `email` (Block List, Max: 1) (see [below for nested schema](#nestedblock--email))
- `icon_url` (String)
- `webhook` (Block List, Max: 1) (see [below for nested schema](#nestedblock--webhook))
Expand All @@ -30,6 +31,46 @@ description: |-
- `id` (String) The ID of this resource.
- `oid` (String)

<a id="nestedblock--destination"></a>
### Nested Schema for `destination`

Required:

- `action` (String)
- `name` (String)
- `type` (String)
- `workspace` (String)

Optional:

- `description` (String)
- `email` (Block List) (see [below for nested schema](#nestedblock--destination--email))
- `icon_url` (String)
- `webhook` (Block List) (see [below for nested schema](#nestedblock--destination--webhook))

Read-Only:

- `oid` (String)

<a id="nestedblock--destination--email"></a>
### Nested Schema for `destination.email`

Optional:

- `addresses` (List of String)
- `users` (List of String)


<a id="nestedblock--destination--webhook"></a>
### Nested Schema for `destination.webhook`

Required:

- `method` (String)
- `url` (String)



<a id="nestedblock--email"></a>
### Nested Schema for `email`

Expand Down
1 change: 0 additions & 1 deletion observe/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,6 @@ func Provider() *schema.Provider {
"observe_monitor": resourceMonitor(),
"observe_monitor_v2": resourceMonitorV2(),
"observe_monitor_v2_action": resourceMonitorV2Action(),
"observe_monitor_v2_destination": resourceMonitorV2Destination(),
"observe_board": resourceBoard(),
"observe_poller": resourcePoller(),
"observe_datastream": resourceDatastream(),
Expand Down
82 changes: 77 additions & 5 deletions observe/resource_monitor_v2_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package observe
import (
"context"
"fmt"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
Expand Down Expand Up @@ -64,6 +65,12 @@ func resourceMonitorV2Action() *schema.Resource {
Type: schema.TypeString,
Computed: true,
},
"destination": { //
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: resourceMonitorV2Destination(),
},
},
}
}
Expand Down Expand Up @@ -135,24 +142,60 @@ func resourceMonitorV2ActionCreate(ctx context.Context, data *schema.ResourceDat
}

workspaceID, _ := oid.NewOID(data.Get("workspace").(string))
result, err := client.CreateMonitorV2Action(ctx, workspaceID.Id, input)
actResult, err := client.CreateMonitorV2Action(ctx, workspaceID.Id, input)
if err != nil {
return diag.Errorf("failed to create monitor action: %s", err.Error())
}

dstInput, diags := newMonitorV2DestinationInput(data, "destination.0.")
dstResult, err := client.CreateMonitorV2Destination(ctx, workspaceID.Id, dstInput)
if err != nil {
return diag.Errorf("failed to create monitor action: %s", err.Error())
}

dstLinks := []gql.ActionDestinationLinkInput{
{
DestinationID: dstResult.Id,
},
}
_, err = client.SaveActionWithDestinationLinks(ctx, actResult.Id, dstLinks)
if err != nil {
return diag.Errorf("failed to create monitor action: %s", err.Error())
}

data.SetId(result.Id)
data.SetId(fmt.Sprintf("%s.%s", actResult.Id, dstResult.Id))
return append(diags, resourceMonitorV2ActionRead(ctx, data, meta)...)
}

func resourceMonitorV2ActionUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
client := meta.(*observe.Client)

input, diags := newMonitorV2ActionInput(data)
actId := strings.Split(data.Id(), ".")[0]
dstId := strings.Split(data.Id(), ".")[1]

actInput, diags := newMonitorV2ActionInput(data)
if diags.HasError() {
return diags
}

dstInput, diags := newMonitorV2DestinationInput(data, "destination.0.")
if diags.HasError() {
return diags
}

_, err := client.UpdateMonitorV2Action(ctx, data.Id(), input)
_, err := client.UpdateMonitorV2Action(ctx, actId, actInput)
if err != nil {
if gql.HasErrorCode(err, "NOT_FOUND") {
diags = resourceMonitorV2ActionCreate(ctx, data, meta)
if diags.HasError() {
return diags
}
return nil
}
return diag.Errorf("failed to create monitor action: %s", err.Error())
}

_, err = client.UpdateMonitorV2Destination(ctx, dstId, dstInput)
if err != nil {
if gql.HasErrorCode(err, "NOT_FOUND") {
diags = resourceMonitorV2ActionCreate(ctx, data, meta)
Expand All @@ -164,6 +207,16 @@ func resourceMonitorV2ActionUpdate(ctx context.Context, data *schema.ResourceDat
return diag.Errorf("failed to create monitor action: %s", err.Error())
}

dstLinks := []gql.ActionDestinationLinkInput{
{
DestinationID: dstId,
},
}
_, err = client.Meta.SaveActionWithDestinationLinks(ctx, actId, dstLinks)
if err != nil {
return diag.FromErr(err)
}

return append(diags, resourceMonitorV2ActionRead(ctx, data, meta)...)
}

Expand All @@ -172,13 +225,28 @@ func resourceMonitorV2ActionDelete(ctx context.Context, data *schema.ResourceDat
if err := client.DeleteMonitorV2Action(ctx, data.Id()); err != nil {
return diag.Errorf("failed to delete monitor action: %s", err.Error())
}
if err := client.DeleteMonitorV2Destination(ctx, data.Id()); err != nil {
return diag.FromErr(err)
}
return diags
}

func resourceMonitorV2ActionRead(ctx context.Context, data *schema.ResourceData, meta interface{}) (diags diag.Diagnostics) {
client := meta.(*observe.Client)

action, err := client.GetMonitorV2Action(ctx, data.Id())
actId := strings.Split(data.Id(), ".")[0]
dstId := strings.Split(data.Id(), ".")[1]

action, err := client.GetMonitorV2Action(ctx, actId)
if err != nil {
if gql.HasErrorCode(err, "NOT_FOUND") {
data.SetId("")
return nil
}
return diag.Errorf("failed to read monitorv2 action: %s", err.Error())
}

dst, err := client.GetMonitorV2Destination(ctx, dstId)
if err != nil {
if gql.HasErrorCode(err, "NOT_FOUND") {
data.SetId("")
Expand All @@ -187,6 +255,10 @@ func resourceMonitorV2ActionRead(ctx context.Context, data *schema.ResourceData,
return diag.Errorf("failed to read monitorv2 action: %s", err.Error())
}

if err := data.Set("destination", monitorV2FlattenDestination(*dst)); err != nil {
diags = append(diags, diag.FromErr(err)...)
}

if err := data.Set("workspace", oid.WorkspaceOid(action.WorkspaceId).String()); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
Expand Down
Loading

0 comments on commit d3cd882

Please sign in to comment.