~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/resource/resourceadapters/state.go

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2016 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package resourceadapters
 
5
 
 
6
import (
 
7
        "fmt"
 
8
 
 
9
        "github.com/juju/errors"
 
10
        "gopkg.in/juju/charm.v6-unstable"
 
11
        "gopkg.in/juju/names.v2"
 
12
 
 
13
        "github.com/juju/juju/resource"
 
14
        "github.com/juju/juju/resource/state"
 
15
        corestate "github.com/juju/juju/state"
 
16
)
 
17
 
 
18
type service struct {
 
19
        *corestate.Application
 
20
}
 
21
 
 
22
// ID returns the application's tag.
 
23
func (s *service) ID() names.ApplicationTag {
 
24
        return names.NewApplicationTag(s.Name())
 
25
}
 
26
 
 
27
// CharmURL implements resource/workers.Service.
 
28
func (s *service) CharmURL() *charm.URL {
 
29
        cURL, _ := s.Application.CharmURL()
 
30
        return cURL
 
31
}
 
32
 
 
33
// rawState is a wrapper around state.State that supports the needs
 
34
// of resources.
 
35
type rawState struct {
 
36
        base    *corestate.State
 
37
        persist corestate.Persistence
 
38
}
 
39
 
 
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{
 
44
                base:    base,
 
45
                persist: persist,
 
46
        })
 
47
}
 
48
 
 
49
// Persistence implements resource/state.RawState.
 
50
func (st rawState) Persistence() state.Persistence {
 
51
        persist := corestate.NewResourcePersistence(st.persist)
 
52
        return resourcePersistence{persist}
 
53
}
 
54
 
 
55
// Storage implements resource/state.RawState.
 
56
func (st rawState) Storage() state.Storage {
 
57
        return st.persist.NewStorage()
 
58
}
 
59
 
 
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)
 
63
        if err != nil {
 
64
                return nil, errors.Trace(err)
 
65
        }
 
66
        units, err := svc.AllUnits()
 
67
        if err != nil {
 
68
                return nil, errors.Trace(err)
 
69
        }
 
70
        for _, u := range units {
 
71
                tags = append(tags, u.UnitTag())
 
72
        }
 
73
        return tags, nil
 
74
}
 
75
 
 
76
// VerifyService implements resource/state.RawState.
 
77
func (st rawState) VerifyService(id string) error {
 
78
        svc, err := st.base.Application(id)
 
79
        if err != nil {
 
80
                return errors.Trace(err)
 
81
        }
 
82
        if svc.Life() != corestate.Alive {
 
83
                return errors.NewNotFound(nil, fmt.Sprintf("application %q dying or dead", id))
 
84
        }
 
85
        return nil
 
86
}
 
87
 
 
88
type resourcePersistence struct {
 
89
        *corestate.ResourcePersistence
 
90
}
 
91
 
 
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)
 
95
}
 
96
 
 
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)
 
102
}
 
103
 
 
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)
 
109
}