Skip to content

Commit

Permalink
Move payloads persistence into the state package.
Browse files Browse the repository at this point in the history
  • Loading branch information
ericsnowcurrently committed May 14, 2016
1 parent 9bd39c8 commit 2451133
Show file tree
Hide file tree
Showing 10 changed files with 87 additions and 106 deletions.
6 changes: 3 additions & 3 deletions component/all/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import (
internalserver "github.com/juju/juju/payload/api/private/server"
"github.com/juju/juju/payload/api/server"
"github.com/juju/juju/payload/context"
"github.com/juju/juju/payload/persistence"
payloadstate "github.com/juju/juju/payload/state"
"github.com/juju/juju/payload/status"
"github.com/juju/juju/state"
Expand Down Expand Up @@ -205,11 +204,12 @@ func (payloads) registerState() {
//state.RegisterMultiEnvCollections(persistence.Collections...)

newUnitPayloads := func(persist state.Persistence, unit, machine string) (state.UnitPayloads, error) {
return payloadstate.NewUnitPayloads(persist, unit, machine), nil
payloadsPersist := state.NewPayloadsPersistence(persist, unit)
return payloadstate.NewUnitPayloads(payloadsPersist, unit, machine), nil
}

newEnvPayloads := func(persist state.PayloadsEnvPersistence) (state.EnvPayloads, error) {
envPersist := persistence.NewEnvPersistence(persist)
envPersist := state.NewPayloadsAllPersistence(persist)
envPayloads := payloadstate.EnvPayloads{
Persist: envPersist,
}
Expand Down
14 changes: 0 additions & 14 deletions payload/persistence/package_test.go

This file was deleted.

10 changes: 4 additions & 6 deletions payload/state/unit.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,14 @@ import (
"github.com/juju/utils"

"github.com/juju/juju/payload"
"github.com/juju/juju/payload/persistence"
)

var logger = loggo.GetLogger("juju.payload.state")

// TODO(ericsnow) We need a worker to clean up dying payloads.

// The persistence methods needed for payloads in state.
type payloadsPersistence interface {
// Persistence exposes the persistence methods needed for payloads in state.
type Persistence interface {
Track(id string, info payload.Payload) (bool, error)
// SetStatus updates the status for a payload.
SetStatus(id, status string) (bool, error)
Expand All @@ -31,7 +30,7 @@ type payloadsPersistence interface {
// payloads, as needed by state.
type UnitPayloads struct {
// Persist is the persistence layer that will be used.
Persist payloadsPersistence
Persist Persistence

// Unit identifies the unit associated with the payloads. This
// is the "unit ID" of the targeted unit.
Expand All @@ -45,8 +44,7 @@ type UnitPayloads struct {
}

// NewUnitPayloads builds a UnitPayloads for a unit.
func NewUnitPayloads(st persistence.PersistenceBase, unit, machine string) *UnitPayloads {
persist := persistence.NewPersistence(st, unit)
func NewUnitPayloads(persist Persistence, unit, machine string) *UnitPayloads {
return &UnitPayloads{
Persist: persist,
Unit: unit,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package persistence
package state

import (
"fmt"
Expand All @@ -15,15 +15,15 @@ import (
"github.com/juju/juju/testing"
)

type BaseSuite struct {
type PayloadsBaseSuite struct {
testing.BaseSuite

Stub *gitjujutesting.Stub
State *fakeStatePersistence
Unit string
}

func (s *BaseSuite) SetUpTest(c *gc.C) {
func (s *PayloadsBaseSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)

s.Stub = &gitjujutesting.Stub{}
Expand All @@ -37,7 +37,7 @@ func (doc PayloadDoc) convert() *payloadDoc {
return (*payloadDoc)(&doc)
}

func (s *BaseSuite) NewDoc(id string, pl payload.Payload) *payloadDoc {
func (s *PayloadsBaseSuite) NewDoc(id string, pl payload.Payload) *payloadDoc {
return &payloadDoc{
DocID: "payload#" + s.Unit + "#" + id,
UnitID: s.Unit,
Expand All @@ -49,26 +49,26 @@ func (s *BaseSuite) NewDoc(id string, pl payload.Payload) *payloadDoc {
}
}

func (s *BaseSuite) SetDoc(id string, pl payload.Payload) *payloadDoc {
func (s *PayloadsBaseSuite) SetDoc(id string, pl payload.Payload) *payloadDoc {
payloadDoc := s.NewDoc(id, pl)
s.State.SetDocs(payloadDoc)
return payloadDoc
}

func (s *BaseSuite) RemoveDoc(id string) {
func (s *PayloadsBaseSuite) RemoveDoc(id string) {
docID := "payload#" + s.Unit + "#" + id
delete(s.State.docs, docID)
}

func (s *BaseSuite) NewPersistence() *Persistence {
return NewPersistence(s.State, s.Unit)
func (s *PayloadsBaseSuite) NewPersistence() *PayloadsPersistence {
return NewPayloadsPersistence(s.State, s.Unit)
}

func (s *BaseSuite) SetUnit(id string) {
func (s *PayloadsBaseSuite) SetUnit(id string) {
s.Unit = id
}

func (s *BaseSuite) NewPayloads(pType string, ids ...string) []payload.Payload {
func (s *PayloadsBaseSuite) NewPayloads(pType string, ids ...string) []payload.Payload {
var payloads []payload.Payload
for _, id := range ids {
pl := s.NewPayload(pType, id)
Expand All @@ -77,7 +77,7 @@ func (s *BaseSuite) NewPayloads(pType string, ids ...string) []payload.Payload {
return payloads
}

func (s *BaseSuite) NewPayload(pType string, id string) payload.Payload {
func (s *PayloadsBaseSuite) NewPayload(pType string, id string) payload.Payload {
name, pluginID := payload.ParseID(id)
if pluginID == "" {
pluginID = fmt.Sprintf("%s-%s", name, utils.MustNewUUID())
Expand Down
36 changes: 18 additions & 18 deletions payload/persistence/env.go → state/payloads_persistence_env.go
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package persistence
package state

import (
"github.com/juju/errors"

"github.com/juju/juju/payload"
)

// EnvPersistenceBase provides all the information needed to produce
// a new EnvPersistence value.
type EnvPersistenceBase interface {
PersistenceBase
// PayloadsEnvPersistenceBase provides all the information needed to produce
// a new PayloadsAllPersistence value.
type PayloadsEnvPersistenceBase interface {
PayloadsPersistenceBase

// Machines builds the list of the names that identify
// all machines in State.
Expand All @@ -30,28 +30,28 @@ type unitPersistence interface {
ListAll() ([]payload.Payload, error)
}

// EnvPersistence provides the persistence functionality for the
// PayloadsAllPersistence provides the persistence functionality for the
// Juju environment as a whole.
type EnvPersistence struct {
base EnvPersistenceBase
type PayloadsAllPersistence struct {
base PayloadsEnvPersistenceBase

newUnitPersist func(base PersistenceBase, name string) unitPersistence
newUnitPersist func(base PayloadsPersistenceBase, name string) unitPersistence
}

// NewEnvPersistence wraps the base in a new EnvPersistence.
func NewEnvPersistence(base EnvPersistenceBase) *EnvPersistence {
return &EnvPersistence{
// NewPayloadsAllPersistence wraps the base in a new PayloadsAllPersistence.
func NewPayloadsAllPersistence(base PayloadsEnvPersistenceBase) *PayloadsAllPersistence {
return &PayloadsAllPersistence{
base: base,
newUnitPersist: newUnitPersistence,
}
}

func newUnitPersistence(base PersistenceBase, unit string) unitPersistence {
return NewPersistence(base, unit)
func newUnitPersistence(base PayloadsPersistenceBase, unit string) unitPersistence {
return NewPayloadsPersistence(base, unit)
}

// ListAll returns the list of all payloads in the environment.
func (ep *EnvPersistence) ListAll() ([]payload.FullPayloadInfo, error) {
func (ep *PayloadsAllPersistence) ListAll() ([]payload.FullPayloadInfo, error) {
logger.Tracef("listing all payloads")

machines, err := ep.base.Machines()
Expand All @@ -69,7 +69,7 @@ func (ep *EnvPersistence) ListAll() ([]payload.FullPayloadInfo, error) {
for _, unit := range units {
persist := ep.newUnitPersist(ep.base, unit)

unitPayloads, err := listUnit(persist, unit, machine)
unitPayloads, err := listUnitPayloads(persist, unit, machine)
if err != nil {
return nil, errors.Trace(err)
}
Expand All @@ -79,8 +79,8 @@ func (ep *EnvPersistence) ListAll() ([]payload.FullPayloadInfo, error) {
return payloads, nil
}

// listUnit returns all the payloads for the given unit.
func listUnit(persist unitPersistence, unit, machine string) ([]payload.FullPayloadInfo, error) {
// listUnitPayloads returns all the payloads for the given unit.
func listUnitPayloads(persist unitPersistence, unit, machine string) ([]payload.FullPayloadInfo, error) {
payloads, err := persist.ListAll()
if err != nil {
return nil, errors.Trace(err)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package persistence
package state

import (
"reflect"
Expand All @@ -15,24 +15,24 @@ import (
"github.com/juju/juju/payload"
)

var _ = gc.Suite(&envPersistenceSuite{})
var _ = gc.Suite(&payloadsEnvPersistenceSuite{})

type envPersistenceSuite struct {
BaseSuite
type payloadsEnvPersistenceSuite struct {
PayloadsBaseSuite

base *stubEnvPersistenceBase
}

func (s *envPersistenceSuite) SetUpTest(c *gc.C) {
s.BaseSuite.SetUpTest(c)
func (s *payloadsEnvPersistenceSuite) SetUpTest(c *gc.C) {
s.PayloadsBaseSuite.SetUpTest(c)

s.base = &stubEnvPersistenceBase{
PersistenceBase: s.State,
stub: s.Stub,
PayloadsPersistenceBase: s.State,
stub: s.Stub,
}
}

func (s *envPersistenceSuite) newPayload(name string) payload.FullPayloadInfo {
func (s *payloadsEnvPersistenceSuite) newPayload(name string) payload.FullPayloadInfo {
return payload.FullPayloadInfo{
Payload: payload.Payload{
PayloadClass: charm.PayloadClass{
Expand All @@ -48,15 +48,15 @@ func (s *envPersistenceSuite) newPayload(name string) payload.FullPayloadInfo {
}
}

func (s *envPersistenceSuite) TestListAllOkay(c *gc.C) {
func (s *payloadsEnvPersistenceSuite) TestListAllOkay(c *gc.C) {
s.base.setUnits("0")
s.base.setUnits("1", "a-service/0", "a-service/1")
s.base.setUnits("2", "a-service/2")
p1 := s.newPayload("spam")
p2 := s.newPayload("eggs")
s.base.setPayloads(p1, p2)

persist := NewEnvPersistence(s.base)
persist := NewPayloadsAllPersistence(s.base)
persist.newUnitPersist = s.base.newUnitPersistence

payloads, err := persist.ListAll()
Expand All @@ -80,10 +80,10 @@ func (s *envPersistenceSuite) TestListAllOkay(c *gc.C) {
)
}

func (s *envPersistenceSuite) TestListAllEmpty(c *gc.C) {
func (s *payloadsEnvPersistenceSuite) TestListAllEmpty(c *gc.C) {
s.base.setUnits("0")
s.base.setUnits("1", "a-service/0", "a-service/1")
persist := NewEnvPersistence(s.base)
persist := NewPayloadsAllPersistence(s.base)
persist.newUnitPersist = s.base.newUnitPersistence

payloads, err := persist.ListAll()
Expand All @@ -103,11 +103,11 @@ func (s *envPersistenceSuite) TestListAllEmpty(c *gc.C) {
)
}

func (s *envPersistenceSuite) TestListAllFailed(c *gc.C) {
func (s *payloadsEnvPersistenceSuite) TestListAllFailed(c *gc.C) {
failure := errors.Errorf("<failed!>")
s.Stub.SetErrors(failure)

persist := NewEnvPersistence(s.base)
persist := NewPayloadsAllPersistence(s.base)
persist.newUnitPersist = s.base.newUnitPersistence

_, err := persist.ListAll()
Expand Down Expand Up @@ -151,7 +151,7 @@ func checkPayloads(c *gc.C, payloads []payload.FullPayloadInfo, expectedList ...
}

type stubEnvPersistenceBase struct {
PersistenceBase
PayloadsPersistenceBase
stub *testing.Stub
machines []string
units map[string]map[string]bool
Expand Down Expand Up @@ -190,7 +190,7 @@ func (s *stubEnvPersistenceBase) setUnits(machine string, units ...string) {
}
}

func (s *stubEnvPersistenceBase) newUnitPersistence(base PersistenceBase, unit string) unitPersistence {
func (s *stubEnvPersistenceBase) newUnitPersistence(base PayloadsPersistenceBase, unit string) unitPersistence {
s.stub.AddCall("newUnitPersistence", base, unit)
s.stub.NextErr() // pop one off

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2015 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package persistence
package state

import (
"github.com/juju/errors"
Expand Down
Loading

0 comments on commit 2451133

Please sign in to comment.