-
Notifications
You must be signed in to change notification settings - Fork 0
/
proxy.go
70 lines (56 loc) · 1.79 KB
/
proxy.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
// Copyright 2020 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package jujuclient
import (
"github.com/juju/errors"
"gopkg.in/yaml.v3"
"github.com/juju/juju/proxy"
)
const (
proxyConfConfigKey = "config"
proxyConfTypeKey = "type"
)
var (
NewProxierFactory func() (*proxy.Factory, error) = proxy.NewDefaultFactory
)
// ProxyConfWrapper is wrapper around proxier interfaces so that they can be
// serialized to json correctly.
type ProxyConfWrapper struct {
Proxier proxy.Proxier
}
// MarshalYAML implements marshalling method for yaml. This is so we can make
// sure the proxier type is outputted with the config for later ingestion
func (p *ProxyConfWrapper) MarshalYAML() (interface{}, error) {
return map[string]interface{}{
proxyConfTypeKey: p.Proxier.Type(),
proxyConfConfigKey: p.Proxier,
}, nil
}
// UnmarshalYAML ingests a previously outputted proxy config. It uses the proxy
// default factory to try and construct the correct proxy based on type.
func (p *ProxyConfWrapper) UnmarshalYAML(unmarshal func(interface{}) error) error {
factory, err := NewProxierFactory()
if err != nil {
return errors.Annotate(err, "building proxy factory for config")
}
proxyConf := struct {
Type string `yaml:"type"`
Config yaml.Node `yaml:"config"`
}{}
err = unmarshal(&proxyConf)
if err != nil {
return errors.Annotate(err, "unmarshalling raw proxy config")
}
maker, err := factory.MakerForTypeKey(proxyConf.Type)
if err != nil {
return errors.Trace(err)
}
if err = proxyConf.Config.Decode(maker.Config()); err != nil {
return errors.Annotatef(err, "decoding config for proxy of type %s", proxyConf.Type)
}
p.Proxier, err = maker.Make()
if err != nil {
return errors.Annotatef(err, "making proxier for type %s", proxyConf.Type)
}
return nil
}