github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/client/payloads/facade.go (about)

     1  // Copyright 2017 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package payloads
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/apiserver/common"
    10  	"github.com/juju/juju/apiserver/facade"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/payload"
    13  	"github.com/juju/juju/payload/api"
    14  	"github.com/juju/juju/state"
    15  )
    16  
    17  // NewFacade provides the signature required for facade registration.
    18  func NewFacade(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*API, error) {
    19  	if !authorizer.AuthClient() {
    20  		return nil, common.ErrPerm
    21  	}
    22  	backend, err := st.ModelPayloads()
    23  	if err != nil {
    24  		return nil, errors.Trace(err)
    25  	}
    26  	return NewAPI(backend), nil
    27  }
    28  
    29  // payloadBackend exposes the State functionality for payloads in a model.
    30  type payloadBackend interface {
    31  	// ListAll returns information on the payload with the id on the unit.
    32  	ListAll() ([]payload.FullPayloadInfo, error)
    33  }
    34  
    35  // API serves payload-specific API methods.
    36  type API struct {
    37  	// State exposes the payload aspect of Juju's state.
    38  	backend payloadBackend
    39  }
    40  
    41  // NewAPI builds a new facade for the given backend.
    42  func NewAPI(backend payloadBackend) *API {
    43  	return &API{backend: backend}
    44  }
    45  
    46  // List builds the list of payloads being tracked for
    47  // the given unit and IDs. If no IDs are provided then all tracked
    48  // payloads for the unit are returned.
    49  func (a API) List(args params.PayloadListArgs) (params.PayloadListResults, error) {
    50  	var r params.PayloadListResults
    51  
    52  	payloads, err := a.backend.ListAll()
    53  	if err != nil {
    54  		return r, errors.Trace(err)
    55  	}
    56  
    57  	filters, err := payload.BuildPredicatesFor(args.Patterns)
    58  	if err != nil {
    59  		return r, errors.Trace(err)
    60  	}
    61  	payloads = payload.Filter(payloads, filters...)
    62  
    63  	for _, payload := range payloads {
    64  		apiInfo := api.Payload2api(payload)
    65  		r.Results = append(r.Results, apiInfo)
    66  	}
    67  	return r, nil
    68  }