/
config.go
66 lines (52 loc) · 1.65 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
package native
import (
"github.com/rotisserie/eris"
"github.com/CyCoreSystems/ari/v6"
)
// Config provides the ARI Configuration accessors for a native client
type Config struct {
client *Client
}
// Get gets a lazy handle to a configuration object
func (c *Config) Get(key *ari.Key) *ari.ConfigHandle {
return ari.NewConfigHandle(key, c)
}
// Data retrieves the state of a configuration object
func (c *Config) Data(key *ari.Key) (*ari.ConfigData, error) {
if key == nil || key.ID == "" {
return nil, eris.New("config key not supplied")
}
class, kind, name, err := ari.ParseConfigID(key.ID)
if err != nil {
return nil, eris.Wrap(err, "failed to parse configuration key")
}
data := &ari.ConfigData{
Key: c.client.stamp(key),
Class: class,
Type: kind,
Name: name,
}
err = c.client.get("/asterisk/config/dynamic/"+key.ID, &data.Fields)
if err != nil {
return nil, dataGetError(err, "config", "%v", key.ID)
}
return data, nil
}
// Update updates the given configuration object
func (c *Config) Update(key *ari.Key, tuples []ari.ConfigTuple) (err error) {
class, kind, name, err := ari.ParseConfigID(key.ID)
if err != nil {
return eris.Wrap(err, "failed to parse key")
}
cfgList := ari.ConfigTupleList{}
cfgList.Fields = append(cfgList.Fields, tuples...)
return c.client.put("/asterisk/config/dynamic/"+class+"/"+kind+"/"+name, nil, &cfgList)
}
// Delete deletes the configuration object
func (c *Config) Delete(key *ari.Key) error {
class, kind, name, err := ari.ParseConfigID(key.ID)
if err != nil {
return eris.Wrap(err, "failed to parse key")
}
return c.client.del("/asterisk/config/dynamic/"+class+"/"+kind+"/"+name, nil, "")
}