Skip to content

Commit

Permalink
Fix import for incident workflow (#809)
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgajard committed Feb 16, 2024
1 parent 1c4076c commit 4ce0dee
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 22 deletions.
2 changes: 1 addition & 1 deletion pagerduty/data_source_pagerduty_incident_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func dataSourcePagerDutyIncidentWorkflowRead(ctx context.Context, d *schema.Reso
)
}

err = flattenIncidentWorkflow(d, found, false, nil)
err = flattenIncidentWorkflow(d, found, false, nil, false)
if err != nil {
return retry.NonRetryableError(err)
}
Expand Down
57 changes: 38 additions & 19 deletions pagerduty/resource_pagerduty_incident_workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func resourcePagerDutyIncidentWorkflow() *schema.Resource {
DeleteContext: resourcePagerDutyIncidentWorkflowDelete,
CreateContext: resourcePagerDutyIncidentWorkflowCreate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
StateContext: resourcePagerDutyIncidentWorkflowImport,
},
CustomizeDiff: customizeIncidentWorkflowDiff(),
Schema: map[string]*schema.Schema{
Expand Down Expand Up @@ -370,7 +370,7 @@ func resourcePagerDutyIncidentWorkflowCreate(ctx context.Context, d *schema.Reso
return diag.FromErr(err)
}

err = flattenIncidentWorkflow(d, createdWorkflow, true, specifiedSteps)
err = flattenIncidentWorkflow(d, createdWorkflow, true, specifiedSteps, false)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -379,7 +379,7 @@ func resourcePagerDutyIncidentWorkflowCreate(ctx context.Context, d *schema.Reso

func resourcePagerDutyIncidentWorkflowRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
log.Printf("[INFO] Reading PagerDuty incident workflow %s", d.Id())
err := fetchIncidentWorkflow(ctx, d, meta, handleNotFoundError)
err := fetchIncidentWorkflow(ctx, d, meta, handleNotFoundError, false)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -404,7 +404,7 @@ func resourcePagerDutyIncidentWorkflowUpdate(ctx context.Context, d *schema.Reso
return diag.FromErr(err)
}

err = flattenIncidentWorkflow(d, updatedWorkflow, true, specifiedSteps)
err = flattenIncidentWorkflow(d, updatedWorkflow, true, specifiedSteps, false)
if err != nil {
return diag.FromErr(err)
}
Expand All @@ -424,7 +424,12 @@ func resourcePagerDutyIncidentWorkflowDelete(ctx context.Context, d *schema.Reso
return nil
}

func fetchIncidentWorkflow(ctx context.Context, d *schema.ResourceData, meta interface{}, errorCallback func(error, *schema.ResourceData) error) error {
func resourcePagerDutyIncidentWorkflowImport(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
err := fetchIncidentWorkflow(ctx, d, m, handleNotFoundError, true)
return []*schema.ResourceData{d}, err
}

func fetchIncidentWorkflow(ctx context.Context, d *schema.ResourceData, meta interface{}, errorCallback func(error, *schema.ResourceData) error, isImport bool) error {
client, err := meta.(*Config).Client()
if err != nil {
return err
Expand Down Expand Up @@ -452,7 +457,7 @@ func fetchIncidentWorkflow(ctx context.Context, d *schema.ResourceData, meta int
return nil
}

if err := flattenIncidentWorkflow(d, iw, true, specifiedSteps); err != nil {
if err := flattenIncidentWorkflow(d, iw, true, specifiedSteps, isImport); err != nil {
return retry.NonRetryableError(err)
}
return nil
Expand All @@ -464,6 +469,7 @@ func flattenIncidentWorkflow(
iw *pagerduty.IncidentWorkflow,
includeSteps bool,
specifiedSteps []*SpecifiedStep,
isImport bool,
) error {
d.SetId(iw.ID)
d.Set("name", iw.Name)
Expand All @@ -475,42 +481,52 @@ func flattenIncidentWorkflow(
}

if includeSteps {
steps := flattenIncidentWorkflowSteps(iw, specifiedSteps)
steps := flattenIncidentWorkflowSteps(iw, specifiedSteps, isImport)
d.Set("step", steps)
}

return nil
}

func flattenIncidentWorkflowSteps(iw *pagerduty.IncidentWorkflow, specifiedSteps []*SpecifiedStep) []map[string]interface{} {
func flattenIncidentWorkflowSteps(iw *pagerduty.IncidentWorkflow, specifiedSteps []*SpecifiedStep, isImport bool) []map[string]interface{} {
newSteps := make([]map[string]interface{}, len(iw.Steps))
for i, s := range iw.Steps {
m := make(map[string]interface{})
specifiedStep := *specifiedSteps[i]
m["id"] = s.ID
m["name"] = s.Name
m["action"] = s.Configuration.ActionID
m["input"] = flattenIncidentWorkflowStepInput(s.Configuration.Inputs, specifiedStep.SpecifiedInputNames)
m["inline_steps_input"] = flattenIncidentWorkflowStepInlineStepsInput(
s.Configuration.InlineStepsInputs,
specifiedStep.SpecifiedInlineInputs,
)

if isImport {
m["input"] = flattenIncidentWorkflowStepInput(s.Configuration.Inputs, nil, true)
m["inline_steps_input"] = flattenIncidentWorkflowStepInlineStepsInput(s.Configuration.InlineStepsInputs, nil, true)
} else {
specifiedStep := *specifiedSteps[i]
m["input"] = flattenIncidentWorkflowStepInput(s.Configuration.Inputs, specifiedStep.SpecifiedInputNames, false)
m["inline_steps_input"] = flattenIncidentWorkflowStepInlineStepsInput(
s.Configuration.InlineStepsInputs,
specifiedStep.SpecifiedInlineInputs,
false,
)
}

newSteps[i] = m
}

return newSteps
}

func flattenIncidentWorkflowStepInput(inputs []*pagerduty.IncidentWorkflowActionInput, specifiedInputNames []string) *[]interface{} {
func flattenIncidentWorkflowStepInput(inputs []*pagerduty.IncidentWorkflowActionInput, specifiedInputNames []string, isImport bool) *[]interface{} {
newInputs := make([]interface{}, len(inputs))

for i, v := range inputs {
m := make(map[string]interface{})
m["name"] = v.Name
m["value"] = v.Value

if !isInputInNonGeneratedInputNames(v, specifiedInputNames) {
m["generated"] = true
if !isImport {
if !isInputInNonGeneratedInputNames(v, specifiedInputNames) {
m["generated"] = true
}
}

newInputs[i] = m
Expand All @@ -521,13 +537,14 @@ func flattenIncidentWorkflowStepInput(inputs []*pagerduty.IncidentWorkflowAction
func flattenIncidentWorkflowStepInlineStepsInput(
inlineStepsInputs []*pagerduty.IncidentWorkflowActionInlineStepsInput,
specifiedInlineInputs map[string][]*SpecifiedStep,
isImport bool,
) *[]interface{} {
newInlineStepsInputs := make([]interface{}, len(inlineStepsInputs))

for i, v := range inlineStepsInputs {
m := make(map[string]interface{})
m["name"] = v.Name
m["step"] = flattenIncidentWorkflowStepInlineStepsInputSteps(v.Value.Steps, specifiedInlineInputs[v.Name])
m["step"] = flattenIncidentWorkflowStepInlineStepsInputSteps(v.Value.Steps, specifiedInlineInputs[v.Name], isImport)

newInlineStepsInputs[i] = m
}
Expand All @@ -537,6 +554,7 @@ func flattenIncidentWorkflowStepInlineStepsInput(
func flattenIncidentWorkflowStepInlineStepsInputSteps(
inlineSteps []*pagerduty.IncidentWorkflowActionInlineStep,
specifiedSteps []*SpecifiedStep,
isImport bool,
) *[]interface{} {
newInlineSteps := make([]interface{}, len(inlineSteps))

Expand All @@ -545,14 +563,15 @@ func flattenIncidentWorkflowStepInlineStepsInputSteps(
specifiedStep := *specifiedSteps[i]
m["name"] = v.Name
m["action"] = v.Configuration.ActionID
m["input"] = flattenIncidentWorkflowStepInput(v.Configuration.Inputs, specifiedStep.SpecifiedInputNames)
m["input"] = flattenIncidentWorkflowStepInput(v.Configuration.Inputs, specifiedStep.SpecifiedInputNames, isImport)
if v.Configuration.InlineStepsInputs != nil && len(v.Configuration.InlineStepsInputs) > 0 {
// We should prefer to not set inline_steps_input if the array is empty. This doubles as a schema edge guard
// and prevents an invalid set if we try to set inline_steps_input to an empty array where the schema
// disallows setting any value whatsoever.
m["inline_steps_input"] = flattenIncidentWorkflowStepInlineStepsInput(
v.Configuration.InlineStepsInputs,
specifiedStep.SpecifiedInlineInputs,
isImport,
)
}

Expand Down
3 changes: 1 addition & 2 deletions pagerduty/resource_pagerduty_incident_workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,6 @@ func testAccCheckPagerDutyIncidentWorkflowExists(n string) resource.TestCheckFun
}

func TestFlattenIncidentWorkflowStepsOneGenerated(t *testing.T) {

iw := &pagerduty.IncidentWorkflow{
Steps: []*pagerduty.IncidentWorkflowStep{
{
Expand Down Expand Up @@ -536,7 +535,7 @@ func TestFlattenIncidentWorkflowStepsOneGenerated(t *testing.T) {
},
},
}
flattenedSteps := flattenIncidentWorkflowSteps(iw, specifiedSteps)
flattenedSteps := flattenIncidentWorkflowSteps(iw, specifiedSteps, false)
step1Inputs := flattenedSteps[0]["input"].(*[]interface{})
if len(*step1Inputs) != 3 {
t.Errorf("flattened step1 had wrong number of inputs. want 3 got %v", len(*step1Inputs))
Expand Down

0 comments on commit 4ce0dee

Please sign in to comment.