consul/agent/structs/config_entry_sameness_group.go
Dhia Ayachi f2b26ac194
Hash based config entry replication (#19795)
* add a hash to config entries when normalizing

* add GetHash and implement comparing hashes

* only update if the Hash is different

* only update if the Hash is different and not 0

* fix proto to include the Hash

* fix proto gen

* buf format

* add SetHash and fix tests

* fix config load tests

* fix state test and config test

* recalculate hash when restoring config entries

* fix snapshot restore test

* add changelog

* fix missing normalize, fix proto indexes and add normalize test
2023-12-12 08:29:13 -05:00

92 lines
2.3 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package structs
import (
"encoding/json"
"fmt"
"github.com/hashicorp/consul/acl"
)
type SamenessGroupConfigEntry struct {
Name string
DefaultForFailover bool `json:",omitempty" alias:"default_for_failover"`
IncludeLocal bool `json:",omitempty" alias:"include_local"`
Members []SamenessGroupMember
Meta map[string]string `json:",omitempty"`
Hash uint64 `json:",omitempty" hash:"ignore"`
acl.EnterpriseMeta `hcl:",squash" mapstructure:",squash"`
RaftIndex `hash:"ignore"`
}
func (s *SamenessGroupConfigEntry) SetHash(h uint64) {
s.Hash = h
}
func (s *SamenessGroupConfigEntry) GetHash() uint64 {
return s.Hash
}
func (s *SamenessGroupConfigEntry) GetKind() string { return SamenessGroup }
func (s *SamenessGroupConfigEntry) GetName() string { return s.Name }
func (s *SamenessGroupConfigEntry) GetMeta() map[string]string { return s.Meta }
func (s *SamenessGroupConfigEntry) GetCreateIndex() uint64 { return s.CreateIndex }
func (s *SamenessGroupConfigEntry) GetModifyIndex() uint64 { return s.ModifyIndex }
func (s *SamenessGroupConfigEntry) GetRaftIndex() *RaftIndex {
if s == nil {
return &RaftIndex{}
}
return &s.RaftIndex
}
func (s *SamenessGroupConfigEntry) GetEnterpriseMeta() *acl.EnterpriseMeta {
if s == nil {
return nil
}
return &s.EnterpriseMeta
}
func (s *SamenessGroupConfigEntry) Normalize() error {
if s == nil {
return fmt.Errorf("config entry is nil")
}
s.EnterpriseMeta.Normalize()
h, err := HashConfigEntry(s)
if err != nil {
return err
}
s.Hash = h
return nil
}
func (s *SamenessGroupConfigEntry) CanRead(authz acl.Authorizer) error {
return nil
}
func (s *SamenessGroupConfigEntry) CanWrite(authz acl.Authorizer) error {
var authzContext acl.AuthorizerContext
s.FillAuthzContext(&authzContext)
return authz.ToAllowAuthorizer().MeshWriteAllowed(&authzContext)
}
func (s *SamenessGroupConfigEntry) MarshalJSON() ([]byte, error) {
type Alias SamenessGroupConfigEntry
source := &struct {
Kind string
*Alias
}{
Kind: SamenessGroup,
Alias: (*Alias)(s),
}
return json.Marshal(source)
}
type SamenessGroupMember struct {
Partition string `json:",omitempty"`
Peer string `json:",omitempty"`
}