forked from juju/juju
/
state.go
109 lines (92 loc) · 3.01 KB
/
state.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
// Copyright 2016 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package resourceadapters
import (
"fmt"
"github.com/juju/errors"
"gopkg.in/juju/charm.v6-unstable"
"gopkg.in/juju/names.v2"
"github.com/juju/juju/resource"
"github.com/juju/juju/resource/state"
corestate "github.com/juju/juju/state"
)
type service struct {
*corestate.Application
}
// ID returns the application's tag.
func (s *service) ID() names.ApplicationTag {
return names.NewApplicationTag(s.Name())
}
// CharmURL implements resource/workers.Service.
func (s *service) CharmURL() *charm.URL {
cURL, _ := s.Application.CharmURL()
return cURL
}
// rawState is a wrapper around state.State that supports the needs
// of resources.
type rawState struct {
base *corestate.State
persist corestate.Persistence
}
// NewResourceState is a function that may be passed to
// state.SetResourcesComponent().
func NewResourceState(persist corestate.Persistence, base *corestate.State) corestate.Resources {
return state.NewState(&rawState{
base: base,
persist: persist,
})
}
// Persistence implements resource/state.RawState.
func (st rawState) Persistence() state.Persistence {
persist := corestate.NewResourcePersistence(st.persist)
return resourcePersistence{persist}
}
// Storage implements resource/state.RawState.
func (st rawState) Storage() state.Storage {
return st.persist.NewStorage()
}
// Units returns the tags for all units in the application.
func (st rawState) Units(applicationID string) (tags []names.UnitTag, err error) {
svc, err := st.base.Application(applicationID)
if err != nil {
return nil, errors.Trace(err)
}
units, err := svc.AllUnits()
if err != nil {
return nil, errors.Trace(err)
}
for _, u := range units {
tags = append(tags, u.UnitTag())
}
return tags, nil
}
// VerifyService implements resource/state.RawState.
func (st rawState) VerifyService(id string) error {
svc, err := st.base.Application(id)
if err != nil {
return errors.Trace(err)
}
if svc.Life() != corestate.Alive {
return errors.NewNotFound(nil, fmt.Sprintf("application %q dying or dead", id))
}
return nil
}
type resourcePersistence struct {
*corestate.ResourcePersistence
}
// StageResource implements state.resourcePersistence.
func (p resourcePersistence) StageResource(res resource.Resource, storagePath string) (state.StagedResource, error) {
return p.ResourcePersistence.StageResource(res, storagePath)
}
// NewResourcePersistence is a function that may be passed to
// state.SetResourcesPersistence(). It will be used in the core state
// package to produce the resource persistence.
func NewResourcePersistence(persist corestate.Persistence) corestate.ResourcesPersistence {
return corestate.NewResourcePersistence(persist)
}
// CleanUpBlob is a cleanup handler that will be used in state cleanup.
func CleanUpBlob(st *corestate.State, persist corestate.Persistence, storagePath string) error {
// TODO(ericsnow) Move this to state.RemoveResource().
storage := persist.NewStorage()
return storage.Remove(storagePath)
}