github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/statushistory/pruner.go (about)

     1  // Copyright 2015 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package statushistory
     5  
     6  import (
     7  	"github.com/juju/juju/apiserver/common"
     8  	"github.com/juju/juju/apiserver/facade"
     9  	"github.com/juju/juju/apiserver/params"
    10  	"github.com/juju/juju/state"
    11  )
    12  
    13  // API is the concrete implementation of the Pruner endpoint.
    14  type API struct {
    15  	*common.ModelWatcher
    16  	st         *state.State
    17  	authorizer facade.Authorizer
    18  }
    19  
    20  // NewAPI returns an API Instance.
    21  func NewAPI(st *state.State, r facade.Resources, auth facade.Authorizer) (*API, error) {
    22  	m, err := st.Model()
    23  	if err != nil {
    24  		return nil, err
    25  	}
    26  
    27  	return &API{
    28  		ModelWatcher: common.NewModelWatcher(m, r, auth),
    29  		st:           st,
    30  		authorizer:   auth,
    31  	}, nil
    32  }
    33  
    34  // Prune endpoint removes status history entries until
    35  // only the ones newer than now - p.MaxHistoryTime remain and
    36  // the history is smaller than p.MaxHistoryMB.
    37  func (api *API) Prune(p params.StatusHistoryPruneArgs) error {
    38  	if !api.authorizer.AuthController() {
    39  		return common.ErrPerm
    40  	}
    41  	return state.PruneStatusHistory(api.st, p.MaxHistoryTime, p.MaxHistoryMB)
    42  }