Skip to content

Commit c9827a8

Browse files
phmichalpristas
andauthored
[Elastic Agent] Rename *ConfigChange to PolicyChange (#20779)
[Elastic Agent] Rename *ConfigChange to PolicyChange (#20779) Co-authored-by: Michal Pristas <[email protected]>
1 parent 2996b6f commit c9827a8

File tree

13 files changed

+72
-71
lines changed

13 files changed

+72
-71
lines changed

x-pack/elastic-agent/CHANGELOG.next.asciidoc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Docker container is not run as root by default. {pull}21213[21213]
1111

1212
==== Bugfixes
13+
- Fix rename *ConfigChange to *PolicyChange to align on changes in the UI. {pull}20779[20779]
1314
- Thread safe sorted set {pull}21290[21290]
1415
- Copy Action store on upgrade {pull}21298[21298]
1516
- Include inputs in action store actions {pull}21298[21298]

x-pack/elastic-agent/dev-tools/cmd/fakewebapi/action_example.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"action": "checkin",
33
"actions": [
44
{
5-
"type": "CONFIG_CHANGE",
5+
"type": "POLICY_CHANGE",
66
"data": {
7-
"config": {
7+
"policy": {
88
"id": "default",
99
"outputs": {
1010
"default": {

x-pack/elastic-agent/pkg/agent/application/action_store.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) {
3535
}
3636
defer reader.Close()
3737

38-
var action actionConfigChangeSerializer
38+
var action ActionPolicyChangeSerializer
3939

4040
dec := yaml.NewDecoder(reader)
4141
err = dec.Decode(&action)
@@ -49,7 +49,7 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) {
4949
return nil, err
5050
}
5151

52-
apc := fleetapi.ActionConfigChange(action)
52+
apc := fleetapi.ActionPolicyChange(action)
5353

5454
return &actionStore{
5555
log: log,
@@ -62,7 +62,7 @@ func newActionStore(log *logger.Logger, store storeLoad) (*actionStore, error) {
6262
// any other type of action will be silently ignored.
6363
func (s *actionStore) Add(a action) {
6464
switch v := a.(type) {
65-
case *fleetapi.ActionConfigChange, *fleetapi.ActionUnenroll:
65+
case *fleetapi.ActionPolicyChange, *fleetapi.ActionUnenroll:
6666
// Only persist the action if the action is different.
6767
if s.action != nil && s.action.ID() == v.ID() {
6868
return
@@ -79,8 +79,8 @@ func (s *actionStore) Save() error {
7979
}
8080

8181
var reader io.Reader
82-
if apc, ok := s.action.(*fleetapi.ActionConfigChange); ok {
83-
serialize := actionConfigChangeSerializer(*apc)
82+
if apc, ok := s.action.(*fleetapi.ActionPolicyChange); ok {
83+
serialize := ActionPolicyChangeSerializer(*apc)
8484

8585
r, err := yamlToReader(&serialize)
8686
if err != nil {
@@ -120,7 +120,7 @@ func (s *actionStore) Actions() []action {
120120
return []action{s.action}
121121
}
122122

123-
// actionConfigChangeSerializer is a struct that adds a YAML serialization, I don't think serialization
123+
// ActionPolicyChangeSerializer is a struct that adds a YAML serialization, I don't think serialization
124124
// is a concern of the fleetapi package. I went this route so I don't have to do much refactoring.
125125
//
126126
// There are four ways to achieve the same results:
@@ -130,14 +130,14 @@ func (s *actionStore) Actions() []action {
130130
// 4. We have two sets of type.
131131
//
132132
// This could be done in a refactoring.
133-
type actionConfigChangeSerializer struct {
133+
type ActionPolicyChangeSerializer struct {
134134
ActionID string `yaml:"action_id"`
135135
ActionType string `yaml:"action_type"`
136-
Config map[string]interface{} `yaml:"config"`
136+
Policy map[string]interface{} `yaml:"policy"`
137137
}
138138

139139
// Add a guards between the serializer structs and the original struct.
140-
var _ actionConfigChangeSerializer = actionConfigChangeSerializer(fleetapi.ActionConfigChange{})
140+
var _ ActionPolicyChangeSerializer = ActionPolicyChangeSerializer(fleetapi.ActionPolicyChange{})
141141

142142
// actionUnenrollSerializer is a struct that adds a YAML serialization,
143143
type actionUnenrollSerializer struct {

x-pack/elastic-agent/pkg/agent/application/action_store_test.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ func TestActionStore(t *testing.T) {
5757

5858
t.Run("can save to disk known action type",
5959
withFile(func(t *testing.T, file string) {
60-
actionConfigChange := &fleetapi.ActionConfigChange{
60+
ActionPolicyChange := &fleetapi.ActionPolicyChange{
6161
ActionID: "abc123",
62-
ActionType: "CONFIG_CHANGE",
63-
Config: map[string]interface{}{
62+
ActionType: "POLICY_CHANGE",
63+
Policy: map[string]interface{}{
6464
"hello": "world",
6565
},
6666
}
@@ -70,7 +70,7 @@ func TestActionStore(t *testing.T) {
7070
require.NoError(t, err)
7171

7272
require.Equal(t, 0, len(store.Actions()))
73-
store.Add(actionConfigChange)
73+
store.Add(ActionPolicyChange)
7474
err = store.Save()
7575
require.NoError(t, err)
7676
require.Equal(t, 1, len(store.Actions()))
@@ -82,12 +82,12 @@ func TestActionStore(t *testing.T) {
8282
actions := store1.Actions()
8383
require.Equal(t, 1, len(actions))
8484

85-
require.Equal(t, actionConfigChange, actions[0])
85+
require.Equal(t, ActionPolicyChange, actions[0])
8686
}))
8787

8888
t.Run("when we ACK we save to disk",
8989
withFile(func(t *testing.T, file string) {
90-
actionConfigChange := &fleetapi.ActionConfigChange{
90+
ActionPolicyChange := &fleetapi.ActionPolicyChange{
9191
ActionID: "abc123",
9292
}
9393

@@ -98,7 +98,7 @@ func TestActionStore(t *testing.T) {
9898
acker := newActionStoreAcker(&testAcker{}, store)
9999
require.Equal(t, 0, len(store.Actions()))
100100

101-
require.NoError(t, acker.Ack(context.Background(), actionConfigChange))
101+
require.NoError(t, acker.Ack(context.Background(), ActionPolicyChange))
102102
require.Equal(t, 1, len(store.Actions()))
103103
}))
104104
}

x-pack/elastic-agent/pkg/agent/application/fleet_gateway_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,10 +208,10 @@ func TestFleetGateway(t *testing.T) {
208208
{
209209
"actions": [
210210
{
211-
"type": "CONFIG_CHANGE",
211+
"type": "POLICY_CHANGE",
212212
"id": "id1",
213213
"data": {
214-
"config": {
214+
"policy": {
215215
"id": "policy-id"
216216
}
217217
}

x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,24 @@ import (
1313
"github.com/elastic/beats/v7/x-pack/elastic-agent/pkg/fleetapi"
1414
)
1515

16-
type handlerConfigChange struct {
16+
type handlerPolicyChange struct {
1717
log *logger.Logger
1818
emitter emitterFunc
1919
}
2020

21-
func (h *handlerConfigChange) Handle(ctx context.Context, a action, acker fleetAcker) error {
22-
h.log.Debugf("handlerConfigChange: action '%+v' received", a)
23-
action, ok := a.(*fleetapi.ActionConfigChange)
21+
func (h *handlerPolicyChange) Handle(ctx context.Context, a action, acker fleetAcker) error {
22+
h.log.Debugf("handlerPolicyChange: action '%+v' received", a)
23+
action, ok := a.(*fleetapi.ActionPolicyChange)
2424
if !ok {
25-
return fmt.Errorf("invalid type, expected ActionConfigChange and received %T", a)
25+
return fmt.Errorf("invalid type, expected ActionPolicyChange and received %T", a)
2626
}
2727

28-
c, err := LoadConfig(action.Config)
28+
c, err := LoadConfig(action.Policy)
2929
if err != nil {
3030
return errors.New(err, "could not parse the configuration from the policy", errors.TypeConfig)
3131
}
3232

33-
h.log.Debugf("handlerConfigChange: emit configuration for action %+v", a)
33+
h.log.Debugf("handlerPolicyChange: emit configuration for action %+v", a)
3434
if err := h.emitter(c); err != nil {
3535
return err
3636
}

x-pack/elastic-agent/pkg/agent/application/handler_action_policy_change_test.go

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,13 @@ func TestPolicyChange(t *testing.T) {
3636
emitter := &mockEmitter{}
3737

3838
conf := map[string]interface{}{"hello": "world"}
39-
action := &fleetapi.ActionConfigChange{
39+
action := &fleetapi.ActionPolicyChange{
4040
ActionID: "abc123",
41-
ActionType: "CONFIG_CHANGE",
42-
Config: conf,
41+
ActionType: "POLICY_CHANGE",
42+
Policy: conf,
4343
}
4444

45-
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}
45+
handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
4646

4747
err := handler.Handle(context.Background(), action, ack)
4848
require.NoError(t, err)
@@ -54,13 +54,13 @@ func TestPolicyChange(t *testing.T) {
5454
emitter := &mockEmitter{err: mockErr}
5555

5656
conf := map[string]interface{}{"hello": "world"}
57-
action := &fleetapi.ActionConfigChange{
57+
action := &fleetapi.ActionPolicyChange{
5858
ActionID: "abc123",
59-
ActionType: "CONFIG_CHANGE",
60-
Config: conf,
59+
ActionType: "POLICY_CHANGE",
60+
Policy: conf,
6161
}
6262

63-
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}
63+
handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
6464

6565
err := handler.Handle(context.Background(), action, ack)
6666
require.Error(t, err)
@@ -77,13 +77,13 @@ func TestPolicyAcked(t *testing.T) {
7777

7878
config := map[string]interface{}{"hello": "world"}
7979
actionID := "abc123"
80-
action := &fleetapi.ActionConfigChange{
80+
action := &fleetapi.ActionPolicyChange{
8181
ActionID: actionID,
82-
ActionType: "CONFIG_CHANGE",
83-
Config: config,
82+
ActionType: "POLICY_CHANGE",
83+
Policy: config,
8484
}
8585

86-
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}
86+
handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
8787

8888
err := handler.Handle(context.Background(), action, tacker)
8989
require.Error(t, err)
@@ -99,13 +99,13 @@ func TestPolicyAcked(t *testing.T) {
9999

100100
config := map[string]interface{}{"hello": "world"}
101101
actionID := "abc123"
102-
action := &fleetapi.ActionConfigChange{
102+
action := &fleetapi.ActionPolicyChange{
103103
ActionID: actionID,
104-
ActionType: "CONFIG_CHANGE",
105-
Config: config,
104+
ActionType: "POLICY_CHANGE",
105+
Policy: config,
106106
}
107107

108-
handler := &handlerConfigChange{log: log, emitter: emitter.Emitter}
108+
handler := &handlerPolicyChange{log: log, emitter: emitter.Emitter}
109109

110110
err := handler.Handle(context.Background(), action, tacker)
111111
require.NoError(t, err)

x-pack/elastic-agent/pkg/agent/application/inspect_config_cmd.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,13 +106,13 @@ func loadFleetConfig(cfg *config.Config) (map[string]interface{}, error) {
106106
}
107107

108108
for _, c := range as.Actions() {
109-
cfgChange, ok := c.(*fleetapi.ActionConfigChange)
109+
cfgChange, ok := c.(*fleetapi.ActionPolicyChange)
110110
if !ok {
111111
continue
112112
}
113113

114114
fmt.Println("Action ID:", cfgChange.ID())
115-
return cfgChange.Config, nil
115+
return cfgChange.Policy, nil
116116
}
117117
return nil, nil
118118
}

x-pack/elastic-agent/pkg/agent/application/managed_mode.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,8 @@ func newManaged(
207207
acker)
208208

209209
actionDispatcher.MustRegister(
210-
&fleetapi.ActionConfigChange{},
211-
&handlerConfigChange{
210+
&fleetapi.ActionPolicyChange{},
211+
&handlerPolicyChange{
212212
log: log,
213213
emitter: emit,
214214
},

x-pack/elastic-agent/pkg/agent/application/managed_mode_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ func TestManagedModeRouting(t *testing.T) {
4040
require.NoError(t, err)
4141

4242
actionDispatcher.MustRegister(
43-
&fleetapi.ActionConfigChange{},
44-
&handlerConfigChange{
43+
&fleetapi.ActionPolicyChange{},
44+
&handlerPolicyChange{
4545
log: log,
4646
emitter: emit,
4747
},
@@ -100,9 +100,9 @@ const fleetResponse = `
100100
"action": "checkin",
101101
"actions": [{
102102
"agent_id": "17e93530-7f42-11ea-9330-71e968b29fa4",
103-
"type": "CONFIG_CHANGE",
103+
"type": "POLICY_CHANGE",
104104
"data": {
105-
"config": {
105+
"policy": {
106106
"id": "86561d50-7f3b-11ea-9fab-3db3bdb4efa4",
107107
"outputs": {
108108
"default": {

0 commit comments

Comments
 (0)