-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource.go
111 lines (94 loc) · 3.26 KB
/
resource.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package all
import (
jujucmd "github.com/juju/cmd"
"github.com/juju/errors"
"gopkg.in/juju/names.v3"
"github.com/juju/juju/api/base"
"github.com/juju/juju/apiserver/facades/controller/charmrevisionupdater"
"github.com/juju/juju/resource"
internalclient "github.com/juju/juju/resource/api/private/client"
"github.com/juju/juju/resource/context"
contextcmd "github.com/juju/juju/resource/context/cmd"
"github.com/juju/juju/resource/resourceadapters"
unitercontext "github.com/juju/juju/worker/uniter/runner/context"
"github.com/juju/juju/worker/uniter/runner/jujuc"
)
// resources exposes the registration methods needed
// for the top-level component machinery.
type resources struct{}
// RegisterForServer is the top-level registration method
// for the component in a jujud context.
func (r resources) registerForServer() error {
r.registerState()
r.registerAgentWorkers()
r.registerHookContext()
return nil
}
// RegisterForClient is the top-level registration method
// for the component in a "juju" command context.
func (r resources) registerForClient() error {
// needed for hook-tool
r.registerHookContextCommands()
return nil
}
// registerAgentWorkers adds the resources workers to the agents.
func (r resources) registerAgentWorkers() {
if !markRegistered(resource.ComponentName, "agent-workers") {
return
}
charmrevisionupdater.RegisterLatestCharmHandler("resources", resourceadapters.NewLatestCharmHandler)
}
// registerState registers the state functionality for resources.
func (resources) registerState() {
if !markRegistered(resource.ComponentName, "state") {
return
}
}
func (r resources) registerHookContext() {
if markRegistered(resource.ComponentName, "hook-context") == false {
return
}
unitercontext.RegisterComponentFunc(
resource.ComponentName,
func(config unitercontext.ComponentConfig) (jujuc.ContextComponent, error) {
unitID := names.NewUnitTag(config.UnitName).String()
hctxClient, err := r.newUnitFacadeClient(unitID, config.APICaller)
if err != nil {
return nil, errors.Trace(err)
}
// TODO(ericsnow) Pass the unit's tag through to the component?
return context.NewContextAPI(hctxClient, config.DataDir), nil
},
)
r.registerHookContextCommands()
}
func (r resources) registerHookContextCommands() {
if markRegistered(resource.ComponentName, "hook-context-commands") == false {
return
}
jujuc.RegisterCommand(
contextcmd.GetCmdName,
func(ctx jujuc.Context) (jujucmd.Command, error) {
compCtx, err := ctx.Component(resource.ComponentName)
if err != nil {
return nil, errors.Trace(err)
}
cmd, err := contextcmd.NewGetCmd(compCtx)
if err != nil {
return nil, errors.Trace(err)
}
return cmd, nil
},
)
}
func (r resources) newUnitFacadeClient(unitName string, caller base.APICaller) (context.APIClient, error) {
facadeCaller := base.NewFacadeCallerForVersion(caller, context.HookContextFacade, 1)
httpClient, err := caller.HTTPClient()
if err != nil {
return nil, errors.Trace(err)
}
unitHTTPClient := internalclient.NewUnitHTTPClient(caller.Context(), httpClient, unitName)
return internalclient.NewUnitFacadeClient(facadeCaller.RawAPICaller().Context(), facadeCaller, unitHTTPClient), nil
}