github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/payload/persistence/env.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package persistence
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/payload"
    10  )
    11  
    12  // EnvPersistenceBase provides all the information needed to produce
    13  // a new EnvPersistence value.
    14  type EnvPersistenceBase interface {
    15  	PersistenceBase
    16  
    17  	// Machines builds the list of the names that identify
    18  	// all machines in State.
    19  	Machines() ([]string, error)
    20  
    21  	// MachineUnits builds the list of names that identify all units
    22  	// for a given machine.
    23  	MachineUnits(machineName string) ([]string, error)
    24  }
    25  
    26  // unitPersistence describes the per-unit functionality needed
    27  // for env persistence.
    28  type unitPersistence interface {
    29  	// ListAll returns all payloads associated with the unit.
    30  	ListAll() ([]payload.Payload, error)
    31  }
    32  
    33  // EnvPersistence provides the persistence functionality for the
    34  // Juju environment as a whole.
    35  type EnvPersistence struct {
    36  	base EnvPersistenceBase
    37  
    38  	newUnitPersist func(base PersistenceBase, name string) unitPersistence
    39  }
    40  
    41  // NewEnvPersistence wraps the base in a new EnvPersistence.
    42  func NewEnvPersistence(base EnvPersistenceBase) *EnvPersistence {
    43  	return &EnvPersistence{
    44  		base:           base,
    45  		newUnitPersist: newUnitPersistence,
    46  	}
    47  }
    48  
    49  func newUnitPersistence(base PersistenceBase, unit string) unitPersistence {
    50  	return NewPersistence(base, unit)
    51  }
    52  
    53  // ListAll returns the list of all payloads in the environment.
    54  func (ep *EnvPersistence) ListAll() ([]payload.FullPayloadInfo, error) {
    55  	logger.Tracef("listing all payloads")
    56  
    57  	machines, err := ep.base.Machines()
    58  	if err != nil {
    59  		return nil, errors.Trace(err)
    60  	}
    61  
    62  	var payloads []payload.FullPayloadInfo
    63  	for _, machine := range machines {
    64  		units, err := ep.base.MachineUnits(machine)
    65  		if err != nil {
    66  			return nil, errors.Trace(err)
    67  		}
    68  
    69  		for _, unit := range units {
    70  			persist := ep.newUnitPersist(ep.base, unit)
    71  
    72  			unitPayloads, err := listUnit(persist, unit, machine)
    73  			if err != nil {
    74  				return nil, errors.Trace(err)
    75  			}
    76  			payloads = append(payloads, unitPayloads...)
    77  		}
    78  	}
    79  	return payloads, nil
    80  }
    81  
    82  // listUnit returns all the payloads for the given unit.
    83  func listUnit(persist unitPersistence, unit, machine string) ([]payload.FullPayloadInfo, error) {
    84  	payloads, err := persist.ListAll()
    85  	if err != nil {
    86  		return nil, errors.Trace(err)
    87  	}
    88  
    89  	var fullPayloads []payload.FullPayloadInfo
    90  	for _, pl := range payloads {
    91  		fullPayloads = append(fullPayloads, payload.FullPayloadInfo{
    92  			Payload: pl,
    93  			Machine: machine,
    94  		})
    95  	}
    96  	return fullPayloads, nil
    97  }