Skip to content

Commit

Permalink
BUG/MINOR: raw: read the raw config file in full on GET and POST
Browse files Browse the repository at this point in the history
Also added Cluster-Version and Configuration-Checksum headers for both
GET and POST.
  • Loading branch information
mjuraga committed Sep 6, 2023
1 parent 63ef2df commit 3004514
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 10 deletions.
51 changes: 41 additions & 10 deletions configuration/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,58 +26,89 @@ import (
)

type Raw interface {
GetRawConfigurationWithClusterData(transactionID string, version int64) (int64, int64, string, string, error)
GetRawConfiguration(transactionID string, version int64) (int64, string, error)
PostRawConfiguration(config *string, version int64, skipVersionCheck bool, onlyValidate ...bool) error
}

func (c *client) GetRawConfiguration(transactionID string, version int64) (int64, string, error) {
v, _, _, data, err := c.getRawConfiguration(transactionID, version)
if err != nil {
return 0, "", err
}
return v, data, nil
}

func (c *client) GetRawConfigurationWithClusterData(transactionID string, version int64) (int64, int64, string, string, error) {
return c.getRawConfiguration(transactionID, version)
}

// GetRawConfiguration returns configuration version and a
// string containing raw config file
func (c *client) GetRawConfiguration(transactionID string, version int64) (int64, string, error) {
func (c *client) getRawConfiguration(transactionID string, version int64) (int64, int64, string, string, error) {
config := c.ConfigurationFile
var err error
if transactionID != "" && version != 0 {
return 0, "", NewConfError(ErrBothVersionTransaction, "Both version and transactionID specified, specify only one")
return 0, 0, "", "", NewConfError(ErrBothVersionTransaction, "Both version and transactionID specified, specify only one")
}
if transactionID != "" {
config, err = c.GetTransactionFile(transactionID)
if err != nil {
return 0, "", err
return 0, 0, "", "", err
}
} else if version != 0 {
config, err = c.getBackupFile(version)
if err != nil {
return 0, "", err
return 0, 0, "", "", err
}
}
file, err := os.Open(config)
if err != nil {
return 0, "", NewConfError(ErrCannotReadConfFile, err.Error())
return 0, 0, "", "", NewConfError(ErrCannotReadConfFile, err.Error())
}
defer file.Close()

dataStr := ""
ondiskV := int64(0)
ondiskClusterV := int64(0)
ondiskMD5Hash := ""
scanner := bufio.NewScanner(file)
// parse out version
for scanner.Scan() {
line := scanner.Text()
if strings.HasPrefix(line, "# _version=") {
switch line := scanner.Text(); {
case strings.HasPrefix(line, "# _version="):
w := strings.Split(line, "=")
if len(w) == 2 {
ondiskV, err = strconv.ParseInt(w[1], 10, 64)
if err != nil {
ondiskV = int64(0)
}
}
} else {
dataStr += line + "\n"
case strings.HasPrefix(line, "# _md5hash="):
w := strings.Split(line, "=")
if len(w) == 2 {
ondiskMD5Hash = strings.TrimSpace(w[1])
}
dataStr += line + "\n"
case strings.HasPrefix(line, "# _cluster_version="):
w := strings.Split(line, "=")
if len(w) == 2 {
ondiskClusterV, err = strconv.ParseInt(w[1], 10, 64)
if err != nil {
ondiskClusterV = int64(0)
}
}
dataStr += line + "\n"
default:
dataStr += line + "\n"
}
}
if err = scanner.Err(); err != nil {
return ondiskV, "", NewConfError(ErrCannotReadConfFile, err.Error())
return ondiskV, 0, "", "", NewConfError(ErrCannotReadConfFile, err.Error())
}

return ondiskV, dataStr, nil
return ondiskV, ondiskClusterV, ondiskMD5Hash, dataStr, nil
}

// PostRawConfiguration pushes given string to the config file if the version
Expand Down
25 changes: 25 additions & 0 deletions specification/build/haproxy_spec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18081,6 +18081,12 @@ paths:
"200":
description: Operation successful
headers:
Cluster-Version:
description: Cluster configuration version
type: string
Configuration-Checksum:
description: Configuration file md5 checksum
type: string
Configuration-Version:
description: Configuration file version
type: string
Expand Down Expand Up @@ -18139,11 +18145,30 @@ paths:
responses:
"201":
description: New HAProxy configuration pushed
headers:
Cluster-Version:
description: Cluster configuration version
type: string
Configuration-Checksum:
description: Configuration file md5 checksum
type: string
Configuration-Version:
description: Configuration file version
type: string
schema:
type: string
"202":
description: Configuration change accepted and reload requested
headers:
Cluster-Version:
description: Cluster configuration version
type: string
Configuration-Checksum:
description: Configuration file md5 checksum
type: string
Configuration-Version:
description: Configuration file version
type: string
Reload-ID:
description: ID of the requested reload
type: string
Expand Down
26 changes: 26 additions & 0 deletions specification/paths/configuration/raw.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,15 @@ configuration:
'200':
description: Operation successful
headers:
Cluster-Version:
description: Cluster configuration version
type: string
Configuration-Version:
description: Configuration file version
type: string
Configuration-Checksum:
description: Configuration file md5 checksum
type: string
schema:
type: object
required:
Expand Down Expand Up @@ -77,12 +83,32 @@ configuration:
Reload-ID:
description: ID of the requested reload
type: string
Cluster-Version:
description: Cluster configuration version
type: string
Configuration-Version:
description: Configuration file version
type: string
Configuration-Checksum:
description: Configuration file md5 checksum
type: string
schema:
type: string
'201':
description: New HAProxy configuration pushed
headers:
Cluster-Version:
description: Cluster configuration version
type: string
Configuration-Version:
description: Configuration file version
type: string
Configuration-Checksum:
description: Configuration file md5 checksum
type: string
schema:
type: string

'400':
$ref: '#/responses/BadRequest'
'default':
Expand Down

0 comments on commit 3004514

Please sign in to comment.