Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Check if boolean policies equal true #1264

Merged
merged 1 commit into from
Aug 2, 2018
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
8 changes: 4 additions & 4 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ func (d *Daemon) ListServices(ctx context.Context, namespace string) ([]v6.Contr
Status: service.Status,
Antecedent: service.Antecedent,
Labels: service.Labels,
Automated: policies.Contains(policy.Automated),
Locked: policies.Contains(policy.Locked),
Ignore: policies.Contains(policy.Ignore),
Automated: policies.Has(policy.Automated),
Locked: policies.Has(policy.Locked),
Ignore: policies.Has(policy.Ignore),
Policies: policies.ToStringMap(),
})
}
Expand Down Expand Up @@ -342,7 +342,7 @@ func (d *Daemon) updatePolicy(spec update.Spec, updates policy.Updates) updateFu
var anythingAutomated bool

for serviceID, u := range updates {
if policy.Set(u.Add).Contains(policy.Automated) {
if policy.Set(u.Add).Has(policy.Automated) {
anythingAutomated = true
}
// find the service manifest
Expand Down
12 changes: 8 additions & 4 deletions policy/policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,14 @@ func clone(s Set) Set {
return newMap
}

// Contains method determines if a resource has a particular policy present
func (s Set) Contains(needle Policy) bool {
for p := range s {
// Has returns true if a resource has a particular policy present, and
// for boolean policies, if it is set to true.
func (s Set) Has(needle Policy) bool {
for p, v := range s {
if p == needle {
if Boolean(needle) {
return v == "true"
}
return true
}
}
Expand Down Expand Up @@ -162,7 +166,7 @@ func (s ResourceMap) Without(other ResourceMap) ResourceMap {
func (s ResourceMap) OnlyWithPolicy(p Policy) ResourceMap {
newMap := ResourceMap{}
for k, v := range s {
if _, ok := v[p]; ok {
if v.Has(p) {
newMap[k] = v
}
}
Expand Down
2 changes: 1 addition & 1 deletion policy/policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestJSON(t *testing.T) {
boolPolicy = boolPolicy.Add(Locked)
policy := boolPolicy.Set(LockedUser, "[email protected]")

if !(policy.Contains(Ignore) && policy.Contains(Locked)) {
if !(policy.Has(Ignore) && policy.Has(Locked)) {
t.Errorf("Policy did not include those added")
}
if val, ok := policy.Get(LockedUser); !ok || val != "[email protected]" {
Expand Down
10 changes: 5 additions & 5 deletions sync/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,29 +49,29 @@ func prepareSyncDelete(logger log.Logger, repoResources map[string]resource.Reso
if len(repoResources) == 0 {
return
}
if res.Policy().Contains(policy.Ignore) {
if res.Policy().Has(policy.Ignore) {
logger.Log("resource", res.ResourceID(), "ignore", "delete")
return
}
if _, ok := repoResources[id]; !ok {
sync.Actions = append(sync.Actions, cluster.SyncAction{
Delete: res,
Delete: res,
})
}
}

func prepareSyncApply(logger log.Logger, clusterResources map[string]resource.Resource, id string, res resource.Resource, sync *cluster.SyncDef) {
if res.Policy().Contains(policy.Ignore) {
if res.Policy().Has(policy.Ignore) {
logger.Log("resource", res.ResourceID(), "ignore", "apply")
return
}
if cres, ok := clusterResources[id]; ok {
if cres.Policy().Contains(policy.Ignore) {
if cres.Policy().Has(policy.Ignore) {
logger.Log("resource", res.ResourceID(), "ignore", "apply")
return
}
}
sync.Actions = append(sync.Actions, cluster.SyncAction{
Apply: res,
Apply: res,
})
}