internal/pkg/policy/revision.go (37 lines of code) (raw):
// Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
// or more contributor license agreements. Licensed under the Elastic License;
// you may not use this file except in compliance with the Elastic License.
package policy
import (
"fmt"
"strconv"
"strings"
"github.com/elastic/fleet-server/v7/internal/pkg/model"
)
// Revision is a policy revision that is sent as an action ID to an agent.
type Revision struct {
PolicyID string
RevisionIdx int64
}
// RevisionFromPolicy creates the revision from the policy.
func RevisionFromPolicy(policy model.Policy) Revision {
return Revision{
PolicyID: policy.PolicyID,
RevisionIdx: policy.RevisionIdx,
}
}
// RevisionFromString converts the string to a policy revision.
func RevisionFromString(actionID string) (Revision, bool) {
split := strings.Split(actionID, ":")
// NOTE: len 3 is expected for any policy change generated by fleet server v8.15+
// len 4 includes the previously used coordinator_idx value that has been deprecated.
// If we receive a actionID with the coordinator_idx ignore the coordinator_idx value.
if len(split) < 3 || len(split) > 4 {
return Revision{}, false
}
if split[0] != "policy" {
return Revision{}, false
}
revIdx, err := strconv.ParseInt(split[2], 10, 64)
if err != nil {
return Revision{}, false
}
return Revision{
PolicyID: split[1],
RevisionIdx: revIdx,
}, true
}
// String returns the ID string for the policy revision.
func (a *Revision) String() string {
return fmt.Sprintf("policy:%s:%d", a.PolicyID, a.RevisionIdx)
}