-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Abstract problem solving so that it can be extended other than import
- Loading branch information
1 parent
0f0a961
commit 2e04eae
Showing
7 changed files
with
157 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
package migration | ||
|
||
import ( | ||
tfjson "github.com/hashicorp/terraform-json" | ||
"github.com/minamijoyo/tfedit/migration/schema" | ||
) | ||
|
||
// Conflict is a planned resource change. | ||
// It also has a status of whether it has already been resolved. | ||
type Conflict struct { | ||
// A planned resource change. | ||
rc *tfjson.ResourceChange | ||
// A flag indicating that it has already been resolved. | ||
// The state mv operation reduces two conflicts to a single state migration | ||
// action, so we need a flag to see if it has already been processed. | ||
resolved bool | ||
} | ||
|
||
// NewConflict returns a new instalce of Conflict. | ||
func NewConflict(rc *tfjson.ResourceChange) *Conflict { | ||
return &Conflict{ | ||
rc: rc, | ||
resolved: false, | ||
} | ||
} | ||
|
||
// MarkAsResolved marks the conflict as resolved. | ||
func (c *Conflict) MarkAsResolved() { | ||
c.resolved = true | ||
} | ||
|
||
// IsResolved return true if the conflict has already been resolved. | ||
func (c *Conflict) IsResolved() bool { | ||
return c.resolved | ||
} | ||
|
||
// PlannedActionType returns a string that represents the type of action. | ||
// Currently some actions that may be included in the plan are not supported. | ||
// It returns "unknown" if not supported. | ||
// The valid values are: | ||
// - create | ||
// - unknown | ||
func (c *Conflict) PlannedActionType() string { | ||
switch { | ||
case c.rc.Change.Actions.Create(): | ||
return "create" | ||
default: | ||
return "unknown" | ||
} | ||
|
||
} | ||
|
||
// ResourceType returns a resource type. (e.g. aws_s3_bucket_acl) | ||
func (c *Conflict) ResourceType() string { | ||
return c.rc.Type | ||
} | ||
|
||
// Address returns an absolute address. (e.g. aws_s3_bucket_acl.example) | ||
func (c *Conflict) Address() string { | ||
return c.rc.Address | ||
} | ||
|
||
// ResourceAfter retruns a planned resource after change. | ||
// It doesn't contains attributes known after apply. | ||
func (c *Conflict) ResourceAfter() schema.Resource { | ||
after := c.rc.Change.After.(map[string]interface{}) | ||
return schema.Resource(after) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package migration | ||
|
||
// Subject is a problem to be solved. It contains multiple conflicts. | ||
type Subject struct { | ||
// A list of conflicts to be solved. | ||
conflicts []*Conflict | ||
} | ||
|
||
// NewSubject finds conflicts contained in a given plan and defines a problem. | ||
func NewSubject(plan *Plan) *Subject { | ||
conflicts := []*Conflict{} | ||
for _, rc := range plan.ResourceChanges() { | ||
c := NewConflict(rc) | ||
conflicts = append(conflicts, c) | ||
} | ||
|
||
return &Subject{ | ||
conflicts: conflicts, | ||
} | ||
} | ||
|
||
// Conflicts returns a list of conflicts. It may include already resolved. | ||
func (s *Subject) Conflicts() []*Conflict { | ||
return s.conflicts | ||
} | ||
|
||
// IsResolved returns true if all conflicts have been resolved, otherwise false. | ||
func (s *Subject) IsResolved() bool { | ||
for _, c := range s.conflicts { | ||
if !c.IsResolved() { | ||
return false | ||
} | ||
} | ||
|
||
return true | ||
} |