~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/tools/lxdclient/client.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
6
6
package lxdclient
7
7
 
8
8
import (
 
9
        "fmt"
9
10
        "net"
10
11
 
11
12
        "github.com/lxc/lxd"
24
25
        *profileClient
25
26
        *instanceClient
26
27
        *imageClient
 
28
        baseURL string
 
29
}
 
30
 
 
31
func (c Client) String() string {
 
32
        return fmt.Sprintf("Client(%s)", c.baseURL)
27
33
}
28
34
 
29
35
// Connect opens an API connection to LXD and returns a high-level
35
41
 
36
42
        remote := cfg.Remote.ID()
37
43
 
38
 
        raw, err := newRawClient(cfg)
 
44
        raw, err := newRawClient(cfg.Remote)
39
45
        if err != nil {
40
46
                return nil, errors.Trace(err)
41
47
        }
46
52
                profileClient:      &profileClient{raw},
47
53
                instanceClient:     &instanceClient{raw, remote},
48
54
                imageClient:        &imageClient{raw},
 
55
                baseURL:            raw.BaseURL,
49
56
        }
50
57
        return conn, nil
51
58
}
52
59
 
53
 
var lxdNewClient = lxd.NewClient
54
60
var lxdNewClientFromInfo = lxd.NewClientFromInfo
55
 
var lxdLoadConfig = lxd.LoadConfig
56
 
 
57
 
func newRawClient(cfg Config) (*lxd.Client, error) {
58
 
        logger.Debugf("using LXD remote %q", cfg.Remote.ID())
59
 
        remote := cfg.Remote.ID()
60
 
        host := cfg.Remote.Host
61
 
        if remote == remoteIDForLocal || host == "" {
 
61
 
 
62
// newRawClient connects to the LXD host that is defined in Config.
 
63
func newRawClient(remote Remote) (*lxd.Client, error) {
 
64
        host := remote.Host
 
65
        logger.Debugf("connecting to LXD remote %q: %q", remote.ID(), host)
 
66
 
 
67
        if remote.ID() == remoteIDForLocal || host == "" {
62
68
                host = "unix://" + lxdshared.VarPath("unix.socket")
63
69
        } else {
64
70
                _, _, err := net.SplitHostPort(host)
69
75
        }
70
76
 
71
77
        clientCert := ""
72
 
        if cfg.Remote.Cert != nil && cfg.Remote.Cert.CertPEM != nil {
73
 
                clientCert = string(cfg.Remote.Cert.CertPEM)
 
78
        if remote.Cert != nil && remote.Cert.CertPEM != nil {
 
79
                clientCert = string(remote.Cert.CertPEM)
74
80
        }
75
81
 
76
82
        clientKey := ""
77
 
        if cfg.Remote.Cert != nil && cfg.Remote.Cert.KeyPEM != nil {
78
 
                clientKey = string(cfg.Remote.Cert.KeyPEM)
 
83
        if remote.Cert != nil && remote.Cert.KeyPEM != nil {
 
84
                clientKey = string(remote.Cert.KeyPEM)
 
85
        }
 
86
 
 
87
        static := false
 
88
        public := false
 
89
        if remote.Protocol == SimplestreamsProtocol {
 
90
                static = true
 
91
                public = true
79
92
        }
80
93
 
81
94
        client, err := lxdNewClientFromInfo(lxd.ConnectInfo{
82
 
                Name:          cfg.Remote.ID(),
83
 
                Addr:          host,
 
95
                Name: remote.ID(),
 
96
                RemoteConfig: lxd.RemoteConfig{
 
97
                        Addr:     host,
 
98
                        Static:   static,
 
99
                        Public:   public,
 
100
                        Protocol: string(remote.Protocol),
 
101
                },
84
102
                ClientPEMCert: clientCert,
85
103
                ClientPEMKey:  clientKey,
86
 
                ServerPEMCert: cfg.Remote.ServerPEMCert,
 
104
                ServerPEMCert: remote.ServerPEMCert,
87
105
        })
88
106
        if err != nil {
89
 
                if remote == remoteIDForLocal {
 
107
                if remote.ID() == remoteIDForLocal {
90
108
                        return nil, errors.Annotate(err, "can't connect to the local LXD server")
91
109
                }
92
110
                return nil, errors.Trace(err)