1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package resourceadapters
9
"github.com/juju/errors"
10
"gopkg.in/juju/charm.v6-unstable"
11
"gopkg.in/juju/names.v2"
13
"github.com/juju/juju/resource"
14
"github.com/juju/juju/resource/state"
15
corestate "github.com/juju/juju/state"
19
*corestate.Application
22
// ID returns the application's tag.
23
func (s *service) ID() names.ApplicationTag {
24
return names.NewApplicationTag(s.Name())
27
// CharmURL implements resource/workers.Service.
28
func (s *service) CharmURL() *charm.URL {
29
cURL, _ := s.Application.CharmURL()
33
// rawState is a wrapper around state.State that supports the needs
35
type rawState struct {
37
persist corestate.Persistence
40
// NewResourceState is a function that may be passed to
41
// state.SetResourcesComponent().
42
func NewResourceState(persist corestate.Persistence, base *corestate.State) corestate.Resources {
43
return state.NewState(&rawState{
49
// Persistence implements resource/state.RawState.
50
func (st rawState) Persistence() state.Persistence {
51
persist := corestate.NewResourcePersistence(st.persist)
52
return resourcePersistence{persist}
55
// Storage implements resource/state.RawState.
56
func (st rawState) Storage() state.Storage {
57
return st.persist.NewStorage()
60
// Units returns the tags for all units in the application.
61
func (st rawState) Units(applicationID string) (tags []names.UnitTag, err error) {
62
svc, err := st.base.Application(applicationID)
64
return nil, errors.Trace(err)
66
units, err := svc.AllUnits()
68
return nil, errors.Trace(err)
70
for _, u := range units {
71
tags = append(tags, u.UnitTag())
76
// VerifyService implements resource/state.RawState.
77
func (st rawState) VerifyService(id string) error {
78
svc, err := st.base.Application(id)
80
return errors.Trace(err)
82
if svc.Life() != corestate.Alive {
83
return errors.NewNotFound(nil, fmt.Sprintf("application %q dying or dead", id))
88
type resourcePersistence struct {
89
*corestate.ResourcePersistence
92
// StageResource implements state.resourcePersistence.
93
func (p resourcePersistence) StageResource(res resource.Resource, storagePath string) (state.StagedResource, error) {
94
return p.ResourcePersistence.StageResource(res, storagePath)
97
// NewResourcePersistence is a function that may be passed to
98
// state.SetResourcesPersistence(). It will be used in the core state
99
// package to produce the resource persistence.
100
func NewResourcePersistence(persist corestate.Persistence) corestate.ResourcesPersistence {
101
return corestate.NewResourcePersistence(persist)
104
// CleanUpBlob is a cleanup handler that will be used in state cleanup.
105
func CleanUpBlob(st *corestate.State, persist corestate.Persistence, storagePath string) error {
106
// TODO(ericsnow) Move this to state.RemoveResource().
107
storage := persist.NewStorage()
108
return storage.Remove(storagePath)