Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for pagerduty_automation_actions_action #599

Merged
merged 8 commits into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ go 1.16
require (
cloud.google.com/go v0.71.0 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.10.1
github.com/heimweh/go-pagerduty v0.0.0-20221209184748-d82f94f8ccce
github.com/heimweh/go-pagerduty v0.0.0-20221215133901-7c3850d0bcf4
github.com/klauspost/compress v1.15.9 // indirect
github.com/montanaflynn/stats v0.6.6 // indirect
go.mongodb.org/mongo-driver v1.10.2 // indirect
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,8 @@ github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734/go.mod
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d h1:kJCB4vdITiW1eC1vq2e6IsrXKrZit1bv/TDYFGMp4BQ=
github.com/hashicorp/yamux v0.0.0-20181012175058-2f1d1f20f75d/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
github.com/heimweh/go-pagerduty v0.0.0-20221209184748-d82f94f8ccce h1:k75NUAniITI3zZswM52TswleNZDS8jFEIrHpQId2lYA=
github.com/heimweh/go-pagerduty v0.0.0-20221209184748-d82f94f8ccce/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY=
github.com/heimweh/go-pagerduty v0.0.0-20221215133901-7c3850d0bcf4 h1:lM8slTKIJNiHnSo+him7GC3mOSSlz3Fbs4mqx2FfMpI=
github.com/heimweh/go-pagerduty v0.0.0-20221215133901-7c3850d0bcf4/go.mod h1:t9vftsO1IjYHGdgJXeemZtomCWnxi2SRgu0PRcRb2oY=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
github.com/ianlancetaylor/demangle v0.0.0-20200824232613-28f6c0f3b639/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc=
Expand Down
29 changes: 29 additions & 0 deletions pagerduty/import_pagerduty_automation_actions_action_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package pagerduty

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
)

func TestAccPagerDutyAutomationActionsAction_import(t *testing.T) {
actionName := fmt.Sprintf("tf-%s", acctest.RandString(5))

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckPagerDutyAutomationActionsActionDestroy,
Steps: []resource.TestStep{
{
Config: testAccCheckPagerDutyAutomationActionsActionTypeProcessAutomationConfig(actionName),
},
{
ResourceName: "pagerduty_automation_actions_action.foo",
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions pagerduty/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func Provider() *schema.Provider {
"pagerduty_event_orchestration_unrouted": resourcePagerDutyEventOrchestrationPathUnrouted(),
"pagerduty_event_orchestration_service": resourcePagerDutyEventOrchestrationPathService(),
"pagerduty_automation_actions_runner": resourcePagerDutyAutomationActionsRunner(),
"pagerduty_automation_actions_action": resourcePagerDutyAutomationActionsAction(),
},
}

Expand Down
324 changes: 324 additions & 0 deletions pagerduty/resource_pagerduty_automation_actions_action.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
package pagerduty

import (
"errors"
"log"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/heimweh/go-pagerduty/pagerduty"
)

func resourcePagerDutyAutomationActionsAction() *schema.Resource {
return &schema.Resource{
Create: resourcePagerDutyAutomationActionsActionCreate,
Read: resourcePagerDutyAutomationActionsActionRead,
Delete: resourcePagerDutyAutomationActionsActionDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"action_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateValueFunc([]string{
"script",
"process_automation",
}),
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"runner_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"action_data_reference": {
Type: schema.TypeList,
Required: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"process_automation_job_id": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"process_automation_job_arguments": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"script": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"invocation_command": {
Type: schema.TypeString,
Optional: true,
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
},
},
},
"type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"action_classification": {
Type: schema.TypeString,
Optional: true,
ValidateFunc: validateValueFunc([]string{
"diagnostic",
"remediation",
}),
ForceNew: true, // Requires creation of new resource while support for update is not implemented
},
"runner_type": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"creation_time": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"modify_time": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}
}

func buildAutomationActionsActionStruct(d *schema.ResourceData) (*pagerduty.AutomationActionsAction, error) {

automationActionsAction := pagerduty.AutomationActionsAction{
Name: d.Get("name").(string),
ActionType: d.Get("action_type").(string),
}

// The API does not allow new actions without a description, but legacy actions without a description exist
if attr, ok := d.GetOk("description"); ok {
val := attr.(string)
automationActionsAction.Description = &val
} else {
return nil, errors.New("action description must be specified when creating an action")
}

if attr, ok := d.GetOk("runner_id"); ok {
val := attr.(string)
automationActionsAction.RunnerID = &val
}

if attr, ok := d.GetOk("action_data_reference"); ok {
automationActionsAction.ActionDataReference = expandActionDataReference(attr)
} else {
return nil, errors.New("action_data_reference must be specified when creating an action")
}

if attr, ok := d.GetOk("type"); ok {
val := attr.(string)
automationActionsAction.Type = &val
}

if attr, ok := d.GetOk("action_classification"); ok {
val := attr.(string)
automationActionsAction.ActionClassification = &val
}

if attr, ok := d.GetOk("runner_type"); ok {
val := attr.(string)
automationActionsAction.RunnerType = &val
}

if attr, ok := d.GetOk("creation_time"); ok {
val := attr.(string)
automationActionsAction.CreationTime = &val
}

if attr, ok := d.GetOk("modify_time"); ok {
val := attr.(string)
automationActionsAction.ModifyTime = &val
}

return &automationActionsAction, nil
}

func expandActionDataReference(v interface{}) pagerduty.AutomationActionsActionDataReference {
attr_map := v.([]interface{})[0].(map[string]interface{})
adr := pagerduty.AutomationActionsActionDataReference{}

if v, ok := attr_map["process_automation_job_id"]; ok {
v_str := v.(string)
if v_str != "" {
adr.ProcessAutomationJobId = &v_str
}
}

if v, ok := attr_map["process_automation_job_arguments"]; ok {
v_str := v.(string)
if v_str != "" {
adr.ProcessAutomationJobArguments = &v_str
}
}

if v, ok := attr_map["script"]; ok {
v_str := v.(string)
if v_str != "" {
adr.Script = &v_str
}
}

if v, ok := attr_map["invocation_command"]; ok {
v_str := v.(string)
if v_str != "" {
adr.InvocationCommand = &v_str
}
}

return adr
}

func resourcePagerDutyAutomationActionsActionCreate(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
}

automationActionsAction, err := buildAutomationActionsActionStruct(d)
if err != nil {
return err
}

log.Printf("[INFO] Creating PagerDuty AutomationActionsAction %s", automationActionsAction.Name)

retryErr := resource.Retry(10*time.Second, func() *resource.RetryError {
if automationActionsAction, _, err := client.AutomationActionsAction.Create(automationActionsAction); err != nil {
if isErrCode(err, 400) || isErrCode(err, 429) {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
}

return resource.NonRetryableError(err)
} else if automationActionsAction != nil {
d.SetId(automationActionsAction.ID)
}
return nil
})

if retryErr != nil {
return retryErr
}

return resourcePagerDutyAutomationActionsActionRead(d, meta)
}

func resourcePagerDutyAutomationActionsActionRead(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Reading PagerDuty AutomationActionsAction %s", d.Id())

return resource.Retry(30*time.Second, func() *resource.RetryError {
if automationActionsAction, _, err := client.AutomationActionsAction.Get(d.Id()); err != nil {
time.Sleep(2 * time.Second)
return resource.RetryableError(err)
} else if automationActionsAction != nil {
d.Set("name", automationActionsAction.Name)
d.Set("type", automationActionsAction.Type)
d.Set("action_type", automationActionsAction.ActionType)
d.Set("creation_time", automationActionsAction.CreationTime)

if automationActionsAction.Description != nil {
d.Set("description", &automationActionsAction.Description)
}

f_adr := flattenActionDataReference(automationActionsAction.ActionDataReference)
if err := d.Set("action_data_reference", f_adr); err != nil {
return resource.NonRetryableError(err)
}

if automationActionsAction.ModifyTime != nil {
d.Set("modify_time", &automationActionsAction.ModifyTime)
}

if automationActionsAction.RunnerID != nil {
d.Set("runner_id", &automationActionsAction.RunnerID)
}

if automationActionsAction.RunnerType != nil {
d.Set("runner_type", &automationActionsAction.RunnerType)
}

if automationActionsAction.ActionClassification != nil {
d.Set("action_classification", &automationActionsAction.ActionClassification)
}
}
return nil
})
}

func flattenActionDataReference(adr pagerduty.AutomationActionsActionDataReference) []interface{} {
adr_map := map[string]interface{}{}

if adr.ProcessAutomationJobId != nil {
adr_map["process_automation_job_id"] = *adr.ProcessAutomationJobId
}

if adr.ProcessAutomationJobArguments != nil {
adr_map["process_automation_job_arguments"] = *adr.ProcessAutomationJobArguments
}

if adr.Script != nil {
adr_map["script"] = *adr.Script
}

if adr.InvocationCommand != nil {
adr_map["invocation_command"] = *adr.InvocationCommand
}

return []interface{}{adr_map}
}

func resourcePagerDutyAutomationActionsActionDelete(d *schema.ResourceData, meta interface{}) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
}

log.Printf("[INFO] Deleting PagerDuty AutomationActionsAction %s", d.Id())

retryErr := resource.Retry(2*time.Minute, func() *resource.RetryError {
if _, err := client.AutomationActionsAction.Delete(d.Id()); err != nil {
return resource.RetryableError(err)
}
return nil
})
if retryErr != nil {
time.Sleep(2 * time.Second)
return retryErr
}
d.SetId("")

// giving the API time to catchup
time.Sleep(time.Second)
return nil
}
Loading