/
config-sync.go
94 lines (78 loc) · 2.54 KB
/
config-sync.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
package f5
import (
"fmt"
"io/ioutil"
"net/http"
"github.com/mitchellh/mapstructure"
)
//
const (
PathConfigSync = "/mgmt/tm/cm/config-sync"
)
// ConfigSyncOptions represents available parameters for config-sync query.
type ConfigSyncOptions struct {
FromGroup string `mapstructure:"from-group,omitempty"`
ToGroup string `mapstructure:"to-group,omitempty"`
RecoverSync bool `mapstructure:"recover-sync,omitempty"`
ForceFullLoadPush bool `mapstructure:"force-full-load-push,omitempty"`
}
// ConfigSyncOption is a function prototype that sets the
type ConfigSyncOption func(*ConfigSyncOptions)
// WithFromGroup sets the name of from-group parameter.
func WithFromGroup(name string) ConfigSyncOption {
return func(o *ConfigSyncOptions) {
o.FromGroup = name
}
}
// WithToGroup sets the name of to-group parameter.
func WithToGroup(name string) ConfigSyncOption {
return func(o *ConfigSyncOptions) {
o.ToGroup = name
}
}
// WithRecoverSync sets recover-sync parameter to true.
func WithRecoverSync() ConfigSyncOption {
return func(o *ConfigSyncOptions) {
o.RecoverSync = true
}
}
// WithForceFullLoadPush sets force-full-load-push parameter to true.
func WithForceFullLoadPush() ConfigSyncOption {
return func(o *ConfigSyncOptions) {
o.ForceFullLoadPush = true
}
}
// ConfigSync performs the config-sync operation. It only starts the sync
// and does not wait for it to complete. The synchronization status must be
// check manually.
func (c *Client) ConfigSync(opts ...ConfigSyncOption) error {
options := ConfigSyncOptions{}
for _, o := range opts {
o(&options)
}
var reqOptions = struct {
Command string `json:"command"`
Options []map[string]interface{} `json:"options"`
}{Command: "run", Options: make([]map[string]interface{}, 0)}
mapOpts := make(map[string]interface{})
err := mapstructure.Decode(options, &mapOpts)
if err != nil {
return fmt.Errorf("f5#ConfigSync: failed to transform options: %w", err)
}
for k, v := range mapOpts {
reqOptions.Options = append(reqOptions.Options, map[string]interface{}{k: v})
}
resp, err := c.SendRequest("POST", PathConfigSync, reqOptions)
if err != nil {
return fmt.Errorf("f5#ConfigSync: failed to send request: %w", err)
}
defer resp.Body.Close()
if resp.StatusCode != http.StatusOK {
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return fmt.Errorf("f5#ConfigSync: failed to read response body: %w", err)
}
return fmt.Errorf("f5#ConfigSync: server returned non 200 response: %s, %s", resp.Status, string(body))
}
return nil
}