~nskaggs/+junk/juju-packaging-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/worker/apicaller/manifold.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-27 20:23:11 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161027202311-sux4jk2o73p1d6rg
Re-add src

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2015 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package apicaller
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
 
 
9
        "github.com/juju/juju/agent"
 
10
        "github.com/juju/juju/api"
 
11
        "github.com/juju/juju/api/base"
 
12
        "github.com/juju/juju/worker"
 
13
        "github.com/juju/juju/worker/dependency"
 
14
)
 
15
 
 
16
// ManifoldConfig defines the names of the manifolds on which a Manifold will depend.
 
17
type ManifoldConfig struct {
 
18
        AgentName string
 
19
}
 
20
 
 
21
// Manifold returns a manifold whose worker wraps an API connection made on behalf of
 
22
// the dependency identified by AgentName.
 
23
func Manifold(config ManifoldConfig) dependency.Manifold {
 
24
        return dependency.Manifold{
 
25
                Inputs: []string{
 
26
                        config.AgentName,
 
27
                },
 
28
                Output: outputFunc,
 
29
                Start:  startFunc(config),
 
30
        }
 
31
}
 
32
 
 
33
// startFunc returns a StartFunc that creates a worker based on the manifolds
 
34
// named in the supplied config.
 
35
func startFunc(config ManifoldConfig) dependency.StartFunc {
 
36
        return func(getResource dependency.GetResourceFunc) (worker.Worker, error) {
 
37
 
 
38
                // Get dependencies and open a connection.
 
39
                var a agent.Agent
 
40
                if err := getResource(config.AgentName, &a); err != nil {
 
41
                        return nil, err
 
42
                }
 
43
                conn, err := openConnection(a)
 
44
                if err != nil {
 
45
                        return nil, errors.Annotate(err, "cannot open api")
 
46
                }
 
47
 
 
48
                // Add the environment uuid to agent config if not present.
 
49
                currentConfig := a.CurrentConfig()
 
50
                if currentConfig.Model().Id() == "" {
 
51
                        err := a.ChangeConfig(func(setter agent.ConfigSetter) error {
 
52
                                modelTag, err := conn.ModelTag()
 
53
                                if err != nil {
 
54
                                        return errors.Annotate(err, "no model uuid set on api")
 
55
                                }
 
56
                                return setter.Migrate(agent.MigrateParams{
 
57
                                        Model: modelTag,
 
58
                                })
 
59
                        })
 
60
                        if err != nil {
 
61
                                logger.Warningf("unable to save model uuid: %v", err)
 
62
                                // Not really fatal, just annoying.
 
63
                        }
 
64
                }
 
65
 
 
66
                // Return the worker.
 
67
                return newApiConnWorker(conn)
 
68
        }
 
69
}
 
70
 
 
71
// outputFunc extracts an API connection from a *apiConnWorker.
 
72
func outputFunc(in worker.Worker, out interface{}) error {
 
73
        inWorker, _ := in.(*apiConnWorker)
 
74
        if inWorker == nil {
 
75
                return errors.Errorf("in should be a %T; got %T", inWorker, in)
 
76
        }
 
77
 
 
78
        switch outPointer := out.(type) {
 
79
        case *base.APICaller:
 
80
                *outPointer = inWorker.conn
 
81
        case *api.Connection:
 
82
                // Using api.Connection is strongly discouraged as consumers
 
83
                // of this API connection should not be able to close it. This
 
84
                // option is only available to support legacy upgrade steps.
 
85
                *outPointer = inWorker.conn
 
86
        default:
 
87
                return errors.Errorf("out should be *base.APICaller or *api.Connection; got %T", out)
 
88
        }
 
89
        return nil
 
90
}