Skip to content

Commit

Permalink
update plan check logic to not error if the resource isn't found in t…
Browse files Browse the repository at this point in the history
…he plan
  • Loading branch information
stephybun committed Jan 30, 2025
1 parent 4aeddd3 commit bf47a29
Showing 1 changed file with 47 additions and 55 deletions.
102 changes: 47 additions & 55 deletions internal/acceptance/helpers/is_not_resource_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,67 +19,59 @@ type isNotResourceAction struct {

// CheckPlan implements the plan check logic.
func (e isNotResourceAction) CheckPlan(ctx context.Context, req plancheck.CheckPlanRequest, resp *plancheck.CheckPlanResponse) {
foundResource := false

for _, rc := range req.Plan.ResourceChanges {
if e.resourceAddress != rc.Address {
continue
}

switch e.actionType {
case plancheck.ResourceActionNoop:
if rc.Change.Actions.NoOp() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionCreate:
if rc.Change.Actions.Create() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
if e.resourceAddress == rc.Address {
switch e.actionType {
case plancheck.ResourceActionNoop:
if rc.Change.Actions.NoOp() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionCreate:
if rc.Change.Actions.Create() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionRead:
if rc.Change.Actions.Read() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionUpdate:
if rc.Change.Actions.Update() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionDestroy:
if rc.Change.Actions.Delete() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionDestroyBeforeCreate:
if rc.Change.Actions.DestroyBeforeCreate() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionCreateBeforeDestroy:
if rc.Change.Actions.CreateBeforeDestroy() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionReplace:
if rc.Change.Actions.Replace() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s, path: %v tried to update a value that is ForceNew", rc.Address, e.actionType, rc.Change.ReplacePaths)
return
}
default:
resp.Error = fmt.Errorf("%s - unexpected ResourceActionType: %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionRead:
if rc.Change.Actions.Read() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionUpdate:
if rc.Change.Actions.Update() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionDestroy:
if rc.Change.Actions.Delete() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionDestroyBeforeCreate:
if rc.Change.Actions.DestroyBeforeCreate() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionCreateBeforeDestroy:
if rc.Change.Actions.CreateBeforeDestroy() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s", rc.Address, e.actionType)
return
}
case plancheck.ResourceActionReplace:
if rc.Change.Actions.Replace() {
resp.Error = fmt.Errorf("'%s' - expected action to not be %s, path: %v tried to update a value that is ForceNew", rc.Address, e.actionType, rc.Change.ReplacePaths)
return
}
default:
resp.Error = fmt.Errorf("%s - unexpected ResourceActionType: %s", rc.Address, e.actionType)
return
}

foundResource = true
break
}

if !foundResource {
resp.Error = fmt.Errorf("%s - Resource not found in plan ResourceChanges", e.resourceAddress)
return
}
// If the resource we're looking for in the plan doesn't exist we don't return an error since we have tests in the provider
// that rely on a setup configuration before the pertinent test step is applied
return

Check failure on line 74 in internal/acceptance/helpers/is_not_resource_action.go

View workflow job for this annotation

GitHub Actions / golint

S1023: redundant `return` statement (gosimple)
}

// IsNotResourceAction returns a plan check that asserts that a given resource will not have a specific resource change type in the plan.
Expand Down

0 comments on commit bf47a29

Please sign in to comment.