-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add condition "nodes" that triggers a task everytime a Consul node is updated #358
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,88 @@ | ||||||
package config | ||||||
|
||||||
import "fmt" | ||||||
|
||||||
const nodesConditionTypes = "nodes" | ||||||
|
||||||
type NodesConditionConfig struct { | ||||||
Datacenter *string `mapstructure:"datacenter"` | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see that the query parameter I like the omission of the |
||||||
|
||||||
services []string | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I support the idea that the If that's the case, can you remove this and add the validation within the |
||||||
} | ||||||
|
||||||
func (n *NodesConditionConfig) Copy() ConditionConfig { | ||||||
if n == nil { | ||||||
return nil | ||||||
} | ||||||
|
||||||
var o NodesConditionConfig | ||||||
o.Datacenter = n.Datacenter | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor nit to avoid copying the pointer here to avoid unintended changes between copies and merges.
Suggested change
|
||||||
copy(o.services, n.services) | ||||||
|
||||||
return &o | ||||||
} | ||||||
|
||||||
func (n *NodesConditionConfig) Merge(o ConditionConfig) ConditionConfig { | ||||||
if n == nil { | ||||||
if isConditionNil(o) { | ||||||
return nil | ||||||
} | ||||||
return o.Copy() | ||||||
} | ||||||
|
||||||
if isConditionNil(o) { | ||||||
return n.Copy() | ||||||
} | ||||||
|
||||||
r := n.Copy() | ||||||
o2, ok := o.(*NodesConditionConfig) | ||||||
if !ok { | ||||||
return r | ||||||
} | ||||||
|
||||||
r2 := r.(*NodesConditionConfig) | ||||||
|
||||||
if o2.Datacenter != nil { | ||||||
r2.Datacenter = StringCopy(o2.Datacenter) | ||||||
} | ||||||
|
||||||
r2.services = append(r2.services, o2.services...) | ||||||
|
||||||
return r2 | ||||||
} | ||||||
|
||||||
func (n *NodesConditionConfig) Finalize(services []string) { | ||||||
if n == nil { | ||||||
return | ||||||
} | ||||||
|
||||||
if n.Datacenter == nil { | ||||||
n.Datacenter = String("") | ||||||
} | ||||||
|
||||||
n.services = services | ||||||
} | ||||||
|
||||||
func (n *NodesConditionConfig) Validate() error { | ||||||
if n == nil { | ||||||
return nil | ||||||
} | ||||||
|
||||||
if len(n.services) != 0 { | ||||||
return fmt.Errorf("task.services cannot be set when using condition %q", nodesConditionTypes) | ||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func (n *NodesConditionConfig) GoString() string { | ||||||
if n == nil { | ||||||
return "(*NodesConditionConfig)(nil)" | ||||||
} | ||||||
|
||||||
return fmt.Sprintf("&NodesConditionConfig{"+ | ||||||
"Datacenter:%s, "+ | ||||||
"}", | ||||||
StringVal(n.Datacenter), | ||||||
) | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,165 @@ | ||||||||||||||||||||||||||||||
package config | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
import ( | ||||||||||||||||||||||||||||||
"testing" | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
"github.com/stretchr/testify/assert" | ||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func TestNodesConditionConfig_Copy(t *testing.T) { | ||||||||||||||||||||||||||||||
cases := []struct { | ||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||
a *NodesConditionConfig | ||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"nil", | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"empty", | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"fully_configured", | ||||||||||||||||||||||||||||||
&NodesConditionConfig{ | ||||||||||||||||||||||||||||||
Datacenter: String("dc2"), | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
for _, tc := range cases { | ||||||||||||||||||||||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||
r := tc.a.Copy() | ||||||||||||||||||||||||||||||
if tc.a == nil { | ||||||||||||||||||||||||||||||
// returned nil interface has nil type, which is unequal to tc.a | ||||||||||||||||||||||||||||||
assert.Nil(t, r) | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
assert.Equal(t, tc.a, r) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func TestNodesConditionConfig_Merge(t *testing.T) { | ||||||||||||||||||||||||||||||
cases := []struct { | ||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||
a *NodesConditionConfig | ||||||||||||||||||||||||||||||
b *NodesConditionConfig | ||||||||||||||||||||||||||||||
r *NodesConditionConfig | ||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"nil_a", | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"nil_b", | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"nil_both", | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"empty", | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"datacenter_overrides", | ||||||||||||||||||||||||||||||
&NodesConditionConfig{Datacenter: String("same")}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{Datacenter: String("different")}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{Datacenter: String("different")}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Adding a couple of test cases for completion
Suggested change
|
||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
for _, tc := range cases { | ||||||||||||||||||||||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||
r := tc.a.Merge(tc.b) | ||||||||||||||||||||||||||||||
if tc.r == nil { | ||||||||||||||||||||||||||||||
// returned nil interface has nil type, which is unequal to tc.r | ||||||||||||||||||||||||||||||
assert.Nil(t, r) | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
assert.Equal(t, tc.r, r) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func TestNodesConditionConfig_Finalize(t *testing.T) { | ||||||||||||||||||||||||||||||
cases := []struct { | ||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||
s []string | ||||||||||||||||||||||||||||||
i *NodesConditionConfig | ||||||||||||||||||||||||||||||
r *NodesConditionConfig | ||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"empty", | ||||||||||||||||||||||||||||||
[]string{}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{ | ||||||||||||||||||||||||||||||
Datacenter: String(""), | ||||||||||||||||||||||||||||||
services: []string{}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"pass_in_services", | ||||||||||||||||||||||||||||||
[]string{"api"}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{}, | ||||||||||||||||||||||||||||||
&NodesConditionConfig{ | ||||||||||||||||||||||||||||||
Datacenter: String(""), | ||||||||||||||||||||||||||||||
services: []string{"api"}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
for _, tc := range cases { | ||||||||||||||||||||||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||
tc.i.Finalize(tc.s) | ||||||||||||||||||||||||||||||
assert.Equal(t, tc.r, tc.i) | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
func TestNodesConditionConfig_Validate(t *testing.T) { | ||||||||||||||||||||||||||||||
cases := []struct { | ||||||||||||||||||||||||||||||
name string | ||||||||||||||||||||||||||||||
expectErr bool | ||||||||||||||||||||||||||||||
services []string | ||||||||||||||||||||||||||||||
}{ | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"nil", | ||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||
nil, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"empty", | ||||||||||||||||||||||||||||||
false, | ||||||||||||||||||||||||||||||
[]string{}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||
"services set", | ||||||||||||||||||||||||||||||
true, | ||||||||||||||||||||||||||||||
[]string{"api"}, | ||||||||||||||||||||||||||||||
}, | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||
for _, tc := range cases { | ||||||||||||||||||||||||||||||
t.Run(tc.name, func(t *testing.T) { | ||||||||||||||||||||||||||||||
n := &NodesConditionConfig{} | ||||||||||||||||||||||||||||||
n.Finalize(tc.services) | ||||||||||||||||||||||||||||||
err := n.Validate() | ||||||||||||||||||||||||||||||
if tc.expectErr { | ||||||||||||||||||||||||||||||
assert.Error(t, err) | ||||||||||||||||||||||||||||||
} else { | ||||||||||||||||||||||||||||||
assert.NoError(t, err) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
}) | ||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would you mind adding a compile check for
NodesConditionConfig
to satisfy the interfaceConditionConfig
below the const? It helps us keep all of the condition types standardized.