-
Notifications
You must be signed in to change notification settings - Fork 73
/
config.go
98 lines (78 loc) · 2.24 KB
/
config.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
package ari
import (
"errors"
"fmt"
"strings"
)
// Config represents a transport to the asterisk
// config ARI resource.
type Config interface {
// Get gets the reference to a config object
Get(key *Key) *ConfigHandle
// Data gets the data for the config object
Data(key *Key) (*ConfigData, error)
// Update creates or updates the given tuples
Update(key *Key, tuples []ConfigTuple) error
// Delete deletes the dynamic configuration object.
Delete(key *Key) error
}
// ConfigData contains the data for a given configuration object
type ConfigData struct {
// Key is the cluster-unique identifier for this configuration
Key *Key `json:"key"`
Class string
Type string
Name string
Fields []ConfigTuple
}
// ID returns the ID of the ConfigData structure
func (cd *ConfigData) ID() string {
return fmt.Sprintf("%s/%s/%s", cd.Class, cd.Type, cd.Name)
}
// ConfigTupleList wrap a list for asterisk ari require.
type ConfigTupleList struct {
Fields []ConfigTuple `json:"fields"`
}
// ConfigTuple is the key-value pair that defines a configuration entry
type ConfigTuple struct {
Attribute string `json:"attribute"`
Value string `json:"value"`
}
// A ConfigHandle is a reference to a Config object
// on the asterisk service
type ConfigHandle struct {
key *Key
c Config
}
// NewConfigHandle builds a new config handle
func NewConfigHandle(key *Key, c Config) *ConfigHandle {
return &ConfigHandle{
key: key,
c: c,
}
}
// ID returns the unique identifier for the config object
func (h *ConfigHandle) ID() string {
return h.key.ID
}
// Data gets the current data for the config handle
func (h *ConfigHandle) Data() (*ConfigData, error) {
return h.c.Data(h.key)
}
// Update creates or updates the given config tuples
func (h *ConfigHandle) Update(tuples []ConfigTuple) error {
return h.c.Update(h.key, tuples)
}
// Delete deletes the dynamic configuration object
func (h *ConfigHandle) Delete() error {
return h.c.Delete(h.key)
}
// ParseConfigID parses the provided Config ID into its Class, Type, and ID components
func ParseConfigID(input string) (class, kind, id string, err error) {
pieces := strings.Split(input, "/")
if len(pieces) < 3 {
err = errors.New("invalid input ID")
return
}
return pieces[0], pieces[1], pieces[2], nil
}