github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/payload/api/server/public.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package server
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/payload"
    10  	"github.com/juju/juju/payload/api"
    11  )
    12  
    13  // EnvPayloads exposes the State functionality for payloads in an env.
    14  type EnvPayloads interface {
    15  	// ListAll returns information on the payload with the id on the unit.
    16  	ListAll() ([]payload.FullPayloadInfo, error)
    17  }
    18  
    19  // PublicAPI serves payload-specific API methods.
    20  type PublicAPI struct {
    21  	// State exposes the payload aspect of Juju's state.
    22  	State EnvPayloads
    23  }
    24  
    25  // NewHookContextAPI builds a new facade for the given State.
    26  func NewPublicAPI(st EnvPayloads) *PublicAPI {
    27  	return &PublicAPI{State: st}
    28  }
    29  
    30  // List builds the list of payloads being tracked for
    31  // the given unit and IDs. If no IDs are provided then all tracked
    32  // payloads for the unit are returned.
    33  func (a PublicAPI) List(args api.EnvListArgs) (api.EnvListResults, error) {
    34  	var r api.EnvListResults
    35  
    36  	payloads, err := a.State.ListAll()
    37  	if err != nil {
    38  		return r, errors.Trace(err)
    39  	}
    40  
    41  	filters, err := payload.BuildPredicatesFor(args.Patterns)
    42  	if err != nil {
    43  		return r, errors.Trace(err)
    44  	}
    45  	payloads = payload.Filter(payloads, filters...)
    46  
    47  	for _, payload := range payloads {
    48  		apiInfo := api.Payload2api(payload)
    49  		r.Results = append(r.Results, apiInfo)
    50  	}
    51  	return r, nil
    52  }