Skip to content

Commit

Permalink
Merge pull request #587 from asteris-llc/feature/raise-level-for-diffs
Browse files Browse the repository at this point in the history
add status method-raise level to will change when there are diffs
  • Loading branch information
rebeccaskinner authored Jan 11, 2017
2 parents af3eb1c + 018a1b6 commit f21bfd7
Show file tree
Hide file tree
Showing 11 changed files with 27 additions and 37 deletions.
5 changes: 4 additions & 1 deletion docs/content/resource-authors-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ smoothly. Of particular note are:
which allows you to add a message that will be displayed to the user
1. [`AddDifference`](https://godoc.org/github.com/asteris-llc/converge/resource#Status.AddDifference)
which inserts a difference that will be displayed to the user
1. [`RaiseLevelForDiffs`](https://godoc.org/github.com/asteris-llc/converge/resource#Status.RaiseLevelForDiffs)
which raises the level to `StatusWillChange` if there are any differences

`Status` has three fields: `Differences`, `Output`, and `Level`. They all have
accurate documentation on their fields, which we will not repeat here. However,
Expand All @@ -86,7 +88,8 @@ These two fields are how you control execution of your Apply method. They follow
these rules:

1. If the level is equal to
[`resource.StatusWillChange`](https://godoc.org/github.com/asteris-llc/converge/resource#StatusLevel)
[`resource.StatusWillChange`](https://godoc.org/github.com/asteris-llc/converge/resource#StatusLevel),
[`resource.StatusMayChange`](https://godoc.org/github.com/asteris-llc/converge/resource#StatusLevel),
or
[`resource.StatusCantChange`](https://godoc.org/github.com/asteris-llc/converge/resource#StatusLevel),
the Status will always show up as having changes
Expand Down
4 changes: 1 addition & 3 deletions resource/docker/container/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,7 @@ func (c *Container) Check(context.Context, resource.Renderer) (resource.TaskStat
status.AddDifference("name", "", c.Name, "<container-missing>")
}

if resource.AnyChanges(status.Differences) {
status.Level = resource.StatusWillChange
}
status.RaiseLevelForDiffs()

return status, nil
}
Expand Down
5 changes: 2 additions & 3 deletions resource/docker/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,8 @@ func (i *Image) Check(context.Context, resource.Renderer) (resource.TaskStatus,
}

status.AddDifference("image", original, repoTag, "<image-missing>")
if resource.AnyChanges(status.Differences) {
status.Level = resource.StatusWillChange
}
status.RaiseLevelForDiffs()

return status, nil
}

Expand Down
4 changes: 1 addition & 3 deletions resource/docker/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,7 @@ func (n *Network) Check(context.Context, resource.Renderer) (resource.TaskStatus
}
}

if resource.AnyChanges(status.Differences) {
status.RaiseLevel(resource.StatusWillChange)
}
status.RaiseLevelForDiffs()

return status, nil
}
Expand Down
4 changes: 1 addition & 3 deletions resource/docker/volume/volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,9 +83,7 @@ func (v *Volume) Check(context.Context, resource.Renderer) (resource.TaskStatus,
// https://docs.docker.com/engine/reference/api/docker_remote_api_v1.24/#/inspect-a-volume
}

if resource.AnyChanges(status.Differences) {
status.RaiseLevel(resource.StatusWillChange)
}
status.RaiseLevelForDiffs()

return status, nil
}
Expand Down
4 changes: 2 additions & 2 deletions resource/file/fetch/fetch.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,6 @@ func (f *Fetch) DiffFile(status *resource.Status, hsh hash.Hash) (*resource.Stat
} else if f.Force {
status.AddDifference("checksum", actual, f.Hash, "")
status.AddMessage("checksum mismatch")
status.RaiseLevel(resource.StatusWillChange)
} else {
status.AddMessage("checksum mismatch, use the \"force\" option to replace")
status.RaiseLevel(resource.StatusCantChange)
Expand All @@ -194,12 +193,13 @@ func (f *Fetch) DiffFile(status *resource.Status, hsh hash.Hash) (*resource.Stat
if f.Force {
status.AddDifference("destination", "<force fetch>", f.Destination, "")
status.AddMessage("file exists, will fetch due to \"force\"")
status.RaiseLevel(resource.StatusWillChange)
} else {
status.AddMessage("file exists")
}
}

status.RaiseLevelForDiffs()

return status, nil
}

Expand Down
9 changes: 2 additions & 7 deletions resource/group/group.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,
case g.NewName == "":
switch {
case nameNotFound:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "add group")
status.AddDifference("group", string(StateAbsent), fmt.Sprintf("group %s", g.Name), "")
case groupByName != nil:
Expand All @@ -130,7 +129,6 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,
status.Output = append(status.Output, fmt.Sprintf("group modify: group %s does not exist", g.Name))
return status, errors.New("cannot modify group")
case newNameNotFound:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "modify group name")
status.AddDifference("group", fmt.Sprintf("group %s", g.Name), fmt.Sprintf("group %s", g.NewName), "")
case groupByNewName != nil:
Expand All @@ -147,15 +145,13 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,
case g.NewName == "":
switch {
case nameNotFound && gidNotFound:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "add group with gid")
status.AddDifference("group", string(StateAbsent), fmt.Sprintf("group %s with gid %s", g.Name, g.GID), "")
case nameNotFound:
status.RaiseLevel(resource.StatusCantChange)
status.Output = append(status.Output, fmt.Sprintf("group add: gid %s already exists", g.GID))
return status, errors.New("cannot add group")
case gidNotFound:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "modify group gid")
status.AddDifference("group", fmt.Sprintf("group %s with gid %s", g.Name, groupByName.Gid), fmt.Sprintf("group %s with gid %s", g.Name, g.GID), "")
case groupByName != nil && groupByGid != nil && groupByName.Name != groupByGid.Name || groupByName.Gid != groupByGid.Gid:
Expand All @@ -172,7 +168,6 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,

switch {
case newNameNotFound && gidNotFound:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "modify group name and gid")
status.AddDifference("group", fmt.Sprintf("group %s with gid %s", g.Name, groupByName.Gid), fmt.Sprintf("group %s with gid %s", g.NewName, g.GID), "")
case gidNotFound:
Expand All @@ -195,7 +190,6 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,
case nameNotFound:
status.Output = append(status.Output, fmt.Sprintf("group delete: group %s does not exist", g.Name))
case groupByName != nil:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "delete group")
status.AddDifference("group", fmt.Sprintf("group %s", g.Name), string(StateAbsent), "")
}
Expand All @@ -219,7 +213,6 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,
status.Output = append(status.Output, fmt.Sprintf("group delete: group %s and gid %s belong to different groups", g.Name, g.GID))
return status, errors.New("cannot delete group")
case groupByName != nil && groupByGid != nil && *groupByName == *groupByGid:
status.RaiseLevel(resource.StatusWillChange)
status.Output = append(status.Output, "delete group with gid")
status.AddDifference("group", fmt.Sprintf("group %s with gid %s", g.Name, g.GID), string(StateAbsent), "")
}
Expand All @@ -229,6 +222,8 @@ func (g *Group) Check(context.Context, resource.Renderer) (resource.TaskStatus,
return status, fmt.Errorf("group: unrecognized state %s", g.State)
}

status.RaiseLevelForDiffs()

return status, nil
}

Expand Down
4 changes: 1 addition & 3 deletions resource/lvm/fs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,7 @@ func (r *resourceFS) Check(context.Context, resource.Renderer) (resource.TaskSta
return nil, err
}

if resource.AnyChanges(status.Differences) {
status.Level = resource.StatusWillChange
}
status.RaiseLevelForDiffs()

return status, nil
}
Expand Down
5 changes: 2 additions & 3 deletions resource/lvm/vg/vg.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ func (r *resourceVG) Check(context.Context, resource.Renderer) (resource.TaskSta
status.AddDifference(r.name, "<not exists>", strings.Join(r.devicesToAdd, ", "), "")
}

if resource.AnyChanges(status.Differences) {
status.Level = resource.StatusWillChange
}
status.RaiseLevelForDiffs()

return status, nil
}

Expand Down
8 changes: 8 additions & 0 deletions resource/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ func (t *Status) RaiseLevel(level StatusLevel) {
}
}

// RaiseLevelForDiffs raises the status level to StatusWillChange if there are
// differences in the Differences map
func (t *Status) RaiseLevelForDiffs() {
if AnyChanges(t.Differences) {
t.RaiseLevel(StatusWillChange)
}
}

// Diff represents a difference
type Diff interface {
Original() string
Expand Down
12 changes: 3 additions & 9 deletions resource/user/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -329,9 +329,7 @@ func (u *User) DiffAdd(status *resource.Status) (*AddUserOptions, error) {
status.AddDifference("expiry", "<default expiry>", options.Expiry, "")
}

if resource.AnyChanges(status.Differences) {
status.RaiseLevel(resource.StatusWillChange)
}
status.RaiseLevelForDiffs()

return options, nil
}
Expand Down Expand Up @@ -362,9 +360,7 @@ func (u *User) DiffDel(status *resource.Status, userByName *user.User, nameNotFo
}
}

if resource.AnyChanges(status.Differences) {
status.RaiseLevel(resource.StatusWillChange)
}
status.RaiseLevelForDiffs()

return nil
}
Expand Down Expand Up @@ -459,9 +455,7 @@ func (u *User) DiffMod(status *resource.Status, currUser *user.User) (*ModUserOp
}
}

if resource.AnyChanges(status.Differences) {
status.RaiseLevel(resource.StatusWillChange)
}
status.RaiseLevelForDiffs()

return options, nil
}

0 comments on commit f21bfd7

Please sign in to comment.