refactor to use same pattern as ConfigEntry

This commit is contained in:
Dhia Ayachi 2021-07-22 10:30:39 -04:00
parent caac67e311
commit 5d38dc5bd0
4 changed files with 12 additions and 16 deletions

View File

@ -89,6 +89,7 @@ func (s *HTTPHandlers) ConnectCAConfigurationSet(req *http.Request) (interface{}
var args structs.CARequest
s.parseDC(req, &args.Datacenter)
s.parseToken(req, &args.Token)
// Check for cas value
if casStr := req.URL.Query().Get("cas"); casStr != "" {
casVal, err := strconv.ParseUint(casStr, 10, 64)
if err != nil {
@ -96,7 +97,11 @@ func (s *HTTPHandlers) ConnectCAConfigurationSet(req *http.Request) (interface{}
Reason: fmt.Sprintf("Request decode failed: %v", err),
}
}
args.Cas = casVal
args.Op = structs.CAOpSetConfigCAS
args.Index = casVal
} else {
args.Op = structs.CAOpSetConfig
args.Index = 0
}
if err := decodeBody(req.Body, &args.Config); err != nil {
return nil, BadRequestError{

View File

@ -422,12 +422,12 @@ func (c *FSM) applyConnectCAOperation(buf []byte, index uint64) interface{} {
defer metrics.MeasureSinceWithLabels([]string{"fsm", "ca"}, time.Now(),
[]metrics.Label{{Name: "op", Value: string(req.Op)}})
switch req.Op {
case structs.CAOpSetConfig:
if req.Cas != 0 {
return c.state.CACheckAndSetConfig(index, req.Cas, req.Config)
}
case structs.CAOpSetConfigCAS:
return c.state.CACheckAndSetConfig(index, req.Index, req.Config)
case structs.CAOpSetConfig:
return c.state.CASetConfig(index, req.Config)
case structs.CAOpSetRoots:
act, err := c.state.CARootSetCAS(index, req.Index, req.Roots)
if err != nil {
@ -457,7 +457,7 @@ func (c *FSM) applyConnectCAOperation(buf []byte, index uint64) interface{} {
return act
}
return c.state.CACheckAndSetConfig(index+1, req.Cas, req.Config)
return c.state.CACheckAndSetConfig(index+1, req.Index, req.Config)
case structs.CAOpIncrementProviderSerialNumber:
sn, err := c.state.CAIncrementProviderSerialNumber(index)

View File

@ -217,7 +217,6 @@ func (c *CAManager) initializeCAConfig() (*structs.CAConfiguration, error) {
req := structs.CARequest{
Op: structs.CAOpSetConfig,
Config: config,
Cas: config.ModifyIndex,
}
if resp, err := c.delegate.ApplyCARequest(&req); err != nil {
return nil, err
@ -504,7 +503,6 @@ func (c *CAManager) initializeRootCA(provider ca.Provider, conf *structs.CAConfi
req := structs.CARequest{
Op: structs.CAOpSetConfig,
Config: conf,
Cas: conf.ModifyIndex,
}
if _, err = c.delegate.ApplyCARequest(&req); err != nil {
return fmt.Errorf("error persisting provider state: %v", err)
@ -762,7 +760,6 @@ func (c *CAManager) persistNewRootAndConfig(provider ca.Provider, newActiveRoot
Index: idx,
Roots: newRoots,
Config: &newConf,
Cas: newConf.ModifyIndex,
}
resp, err := c.delegate.ApplyCARequest(args)
if err != nil {
@ -904,7 +901,6 @@ func (c *CAManager) UpdateConfiguration(args *structs.CARequest) (reterr error)
// If the root didn't change, just update the config and return.
if root != nil && root.ID == newActiveRoot.ID {
args.Op = structs.CAOpSetConfig
resp, err := c.delegate.ApplyCARequest(args)
if err != nil {
return err
@ -1007,7 +1003,6 @@ func (c *CAManager) UpdateConfiguration(args *structs.CARequest) (reterr error)
args.Op = structs.CAOpSetRootsAndConfig
args.Index = idx
args.Config.ModifyIndex = confIdx
args.Cas = confIdx
args.Roots = newRoots
resp, err := c.delegate.ApplyCARequest(args)
if err != nil {

View File

@ -192,6 +192,7 @@ type CAOp string
const (
CAOpSetRoots CAOp = "set-roots"
CAOpSetConfig CAOp = "set-config"
CAOpSetConfigCAS CAOp = "set-config-cas"
CAOpSetProviderState CAOp = "set-provider-state"
CAOpDeleteProviderState CAOp = "delete-provider-state"
CAOpSetRootsAndConfig CAOp = "set-roots-config"
@ -221,11 +222,6 @@ type CARequest struct {
// ProviderState is the state for the builtin CA provider.
ProviderState *CAConsulProviderState
// Cas is an int, Specifies to use a Check-And-Set operation.
// If the index is 0, Consul will only store the entry if it does not already exist.
// If the index is non-zero, the entry is only set if the current index matches the ModifyIndex of that entry
Cas uint64
// WriteRequest is a common struct containing ACL tokens and other
// write-related common elements for requests.
WriteRequest