Skip to content

Commit

Permalink
destination link
Browse files Browse the repository at this point in the history
  • Loading branch information
obs-gh-owengoebel committed Jul 31, 2024
1 parent 393895e commit 985ac0f
Show file tree
Hide file tree
Showing 6 changed files with 199 additions and 17 deletions.
9 changes: 9 additions & 0 deletions client/internal/meta/operation/monitorv2_destination.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,12 @@ mutation deleteMonitorV2Destination($id: ObjectId!) {
}
}

mutation saveActionWithDestinationLinks(
$actionId: ObjectId!,
$destinationLinks: [ActionDestinationLinkInput!]!
) {
# @genqlient(flatten: true)
monitorV2Action: saveActionWithDestinationLinks(actionId: $actionId, destinationLinks: $destinationLinks) {
...MonitorV2Action
}
}
145 changes: 145 additions & 0 deletions client/meta/genqlient.generated.go

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

6 changes: 6 additions & 0 deletions client/meta/monitorv2_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ func (client *Client) DeleteMonitorV2Action(ctx context.Context, id string) erro
return resultStatusError(resp, err)
}

// used only when creating or updating a destination
func (client *Client) SaveActionWithDestinationLinks(ctx context.Context, actionId string, destinationLinks []ActionDestinationLinkInput) (*MonitorV2Action, error) {
resp, err := saveActionWithDestinationLinks(ctx, client.Gql, actionId, destinationLinks)
return monitorV2ActionOrError(resp, err)
}

func (m *MonitorV2Action) Oid() *oid.OID {
return &oid.OID{
Id: m.Id,
Expand Down
2 changes: 1 addition & 1 deletion client/meta/monitorv2_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ func (client *Client) DeleteMonitorV2Destination(ctx context.Context, id string)
func (m *MonitorV2Destination) Oid() *oid.OID {
return &oid.OID{
Id: m.Id,
Type: oid.TypeMonitorV2,
Type: oid.TypeMonitorV2Destination,
}
}
1 change: 1 addition & 0 deletions client/oid/oid.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ const (
TypeMonitor Type = "monitor"
TypeMonitorV2 Type = "monitorv2"
TypeMonitorV2Action Type = "monitorv2action"
TypeMonitorV2Destination Type = "monitorv2destination"
TypeMonitorAction Type = "monitoraction"
TypeMonitorActionAttachment Type = "monitoractionattachment"
TypePoller Type = "poller"
Expand Down
53 changes: 37 additions & 16 deletions observe/resource_monitor_v2_destination.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ func resourceMonitorV2Destination() *schema.Resource {
UpdateContext: resourceMonitorV2DestinationUpdate,
DeleteContext: resourceMonitorV2DestinationDelete,
Schema: map[string]*schema.Schema{
"workspace_id": { // ?
"workspace": { // ?
Type: schema.TypeString,
ForceNew: true,
Required: true,
ValidateDiagFunc: validateOID(oid.TypeWorkspace),
},
"inline": { // Boolean
Type: schema.TypeBool,
Optional: true,
"action": { // associated action
Type: schema.TypeString,
Required: true,
ValidateDiagFunc: validateOID(oid.TypeMonitorV2Action),
},
"type": { // MonitorV2ActionType!
Type: schema.TypeString,
Expand Down Expand Up @@ -59,7 +60,7 @@ func resourceMonitorV2Destination() *schema.Resource {
Optional: true,
},
// ^^^ end of input
"id": { // ObjectId!
"oid": { // ObjectId!
Type: schema.TypeString,
Computed: true,
},
Expand Down Expand Up @@ -111,12 +112,24 @@ func resourceMonitorV2DestinationCreate(ctx context.Context, data *schema.Resour
return diags
}

id, _ := oid.NewOID(data.Get("workspace_id").(string))
id, _ := oid.NewOID(data.Get("workspace").(string))
result, err := client.CreateMonitorV2Destination(ctx, id.Id, input)
if err != nil {
return diag.FromErr(err)
}

// update the link between action and destination
actOID, _ := oid.NewOID(data.Get("action").(string))
dstLinks := []gql.ActionDestinationLinkInput{
{
DestinationID: id.Id,
},
}
_, err = client.Meta.SaveActionWithDestinationLinks(ctx, actOID.Id, dstLinks)
if err != nil {
return diag.FromErr(err)
}

data.SetId(result.Id)
return append(diags, resourceMonitorV2DestinationRead(ctx, data, meta)...)
}
Expand All @@ -141,6 +154,18 @@ func resourceMonitorV2DestinationUpdate(ctx context.Context, data *schema.Resour
return diag.FromErr(err)
}

// update the link between action and destination
actOID, _ := oid.NewOID(data.Get("action").(string))
dstLinks := []gql.ActionDestinationLinkInput{
{
DestinationID: data.Id(),
},
}
_, err = client.Meta.SaveActionWithDestinationLinks(ctx, actOID.Id, dstLinks)
if err != nil {
return diag.FromErr(err)
}

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

Expand All @@ -156,7 +181,7 @@ func resourceMonitorV2DestinationRead(ctx context.Context, data *schema.Resource
}

// required
if err := data.Set("workspace_id", oid.WorkspaceOid(dest.WorkspaceId).String()); err != nil {
if err := data.Set("workspace", oid.WorkspaceOid(dest.WorkspaceId).String()); err != nil {
diags = append(diags, diag.FromErr(err)...)
}

Expand Down Expand Up @@ -191,14 +216,8 @@ func resourceMonitorV2DestinationRead(ctx context.Context, data *schema.Resource
}
}

if dest.Inline != nil {
if err := data.Set("inline", *dest.Inline); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
}

if dest.Webhook != nil {
if err := data.Set("inline", monitorv2FlattenWebhookDestination(*dest.Webhook)); err != nil {
if err := data.Set("webhook", monitorv2FlattenWebhookDestination(*dest.Webhook)); err != nil {
diags = append(diags, diag.FromErr(err)...)
}
}
Expand Down Expand Up @@ -247,9 +266,11 @@ func newMonitorV2DestinationInput(data *schema.ResourceData) (input *gql.Monitor
name := data.Get("name").(string)

// instantiation
inlineVal := true // we are currently only allowing destinations to be inlined
input = &gql.MonitorV2DestinationInput{
Type: actionType,
Name: name,
Type: actionType,
Name: name,
Inline: &inlineVal,
}

// optionals
Expand Down

0 comments on commit 985ac0f

Please sign in to comment.