Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(654)

Delta Between Two Patch Sets: juju/conn.go

Issue 6243067: Add State method to juju.Conn
Left Patch Set: Add State method to juju.Conn Created 12 years, 10 months ago
Right Patch Set: Add State method to juju.Conn Created 12 years, 10 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « [revision details] ('k') | juju/conn_test.go » ('j') | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 package juju 1 package juju
2 2
3 import ( 3 import (
4 "launchpad.net/juju/go/environs" 4 "launchpad.net/juju/go/environs"
5 "launchpad.net/juju/go/state" 5 "launchpad.net/juju/go/state"
6 "regexp" 6 "regexp"
7 "sync"
7 ) 8 )
8 9
9 var ( 10 var (
10 ValidService = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0- 9]*)*$") 11 ValidService = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0- 9]*)*$")
11 ValidUnit = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0- 9]*)*/[0-9]+$") 12 ValidUnit = regexp.MustCompile("^[a-z][a-z0-9]*(-[a-z0-9]*[a-z][a-z0- 9]*)*/[0-9]+$")
12 ) 13 )
13 14
14 // Conn holds a connection to a juju. 15 // Conn holds a connection to a juju.
15 type Conn struct { 16 type Conn struct {
16 Environ environs.Environ 17 Environ environs.Environ
17 state *state.State 18 state *state.State
19 mu sync.Mutex
18 } 20 }
19 21
20 // NewConn returns a Conn pointing at the environName environment, or the 22 // NewConn returns a Conn pointing at the environName environment, or the
21 // default environment if not specified. 23 // default environment if not specified.
22 func NewConn(environName string) (*Conn, error) { 24 func NewConn(environName string) (*Conn, error) {
23 environs, err := environs.ReadEnvirons("") 25 environs, err := environs.ReadEnvirons("")
24 if err != nil { 26 if err != nil {
25 return nil, err 27 return nil, err
26 } 28 }
27 environ, err := environs.Open(environName) 29 environ, err := environs.Open(environName)
28 if err != nil { 30 if err != nil {
29 return nil, err 31 return nil, err
30 } 32 }
31 return &Conn{Environ: environ}, nil 33 return &Conn{Environ: environ}, nil
32 } 34 }
33 35
34 // Bootstrap initializes the Conn's environment and makes it ready to deploy 36 // Bootstrap initializes the Conn's environment and makes it ready to deploy
35 // services. 37 // services.
36 func (c *Conn) Bootstrap(uploadTools bool) error { 38 func (c *Conn) Bootstrap(uploadTools bool) error {
37 return c.Environ.Bootstrap(uploadTools) 39 return c.Environ.Bootstrap(uploadTools)
38 } 40 }
39 41
40 // Destroy destroys the Conn's environment and all its instances. 42 // Destroy destroys the Conn's environment and all its instances.
41 func (c *Conn) Destroy() error { 43 func (c *Conn) Destroy() error {
42 return c.Environ.Destroy(nil) 44 return c.Environ.Destroy(nil)
43 } 45 }
44 46
45 // State returns the conn's State. 47 // State returns the conn's State.
46 func (c *Conn) State() (*state.State, error) { 48 func (c *Conn) State() (*state.State, error) {
49 c.mu.Lock()
50 defer c.mu.Unlock()
47 if c.state == nil { 51 if c.state == nil {
48 info, err := c.Environ.StateInfo() 52 info, err := c.Environ.StateInfo()
49 if err != nil { 53 if err != nil {
50 return nil, err 54 return nil, err
51 } 55 }
52 st, err := state.Open(info) 56 st, err := state.Open(info)
53 if err != nil { 57 if err != nil {
54 return nil, err 58 return nil, err
55 } 59 }
56 c.state = st 60 c.state = st
57 } 61 }
58 return c.state, nil 62 return c.state, nil
59 } 63 }
LEFTRIGHT

Powered by Google App Engine
RSS Feeds Recent Issues | This issue
This is Rietveld f62528b