/
transaction-handling.go
112 lines (99 loc) · 3.11 KB
/
transaction-handling.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright 2019 HAProxy Technologies
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package configuration
import (
"fmt"
parser "github.com/haproxytech/config-parser/v5"
parser_options "github.com/haproxytech/config-parser/v5/options"
"github.com/haproxytech/config-parser/v5/types"
"github.com/haproxytech/client-native/v5/models"
)
type TransactionHandling interface {
InitTransactionParsers() error
GetParserTransactions() models.Transactions
GetFailedParserTransactionVersion(transactionID string) (int64, error)
IncrementTransactionVersion(transactionID string) error
}
// InitTransactionParsers checks transactions and initializes parsers map with transactions in_progress
func (c *client) InitTransactionParsers() error {
transactions, err := c.GetTransactions(models.TransactionStatusInProgress)
if err != nil {
return err
}
for _, t := range *transactions {
if err := c.AddParser(t.ID); err != nil {
continue
}
p, err := c.GetParser(t.ID)
if err != nil {
continue
}
tFile, err := c.GetTransactionFile(t.ID)
if err != nil {
return err
}
if err := p.LoadData(tFile); err != nil {
return NewConfError(ErrCannotReadConfFile, fmt.Sprintf("Cannot read %s", tFile))
}
}
return nil
}
// GetParserTransactions returns parser transactions
func (c *client) GetParserTransactions() models.Transactions {
transactions := models.Transactions{}
for tID := range c.parsers {
v, err := c.GetVersion(tID)
if err == nil {
t := &models.Transaction{
ID: tID,
Status: models.TransactionStatusInProgress,
Version: v,
}
transactions = append(transactions, t)
}
}
return transactions
}
func (c *client) GetFailedParserTransactionVersion(transactionID string) (int64, error) {
p, err := parser.New(parser_options.Path(transactionID))
if err != nil {
return 0, NewConfError(ErrCannotReadConfFile, fmt.Sprintf("cannot read %s", transactionID))
}
data, _ := p.Get(parser.Comments, parser.CommentsSectionName, "# _version", false)
ver, ok := data.(*types.ConfigVersion)
if !ok {
return 0, NewConfError(ErrCannotReadVersion, "")
}
return ver.Value, nil
}
func (c *client) IncrementTransactionVersion(transactionID string) error {
if transactionID == "" {
return c.incrementTransactionVersion(c.parser)
}
p, err := c.GetParser(transactionID)
if err != nil {
return err
}
return c.incrementTransactionVersion(p)
}
func (c *client) incrementTransactionVersion(p parser.Parser) error {
data, err := p.Get(parser.Comments, parser.CommentsSectionName, "# _version", true)
if err != nil {
return err
}
ver, _ := data.(*types.ConfigVersion)
ver.Value++
return nil
}