github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/resource/workers/charmstorepoller.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package workers
     5  
     6  import (
     7  	"time"
     8  
     9  	"github.com/juju/errors"
    10  	"github.com/juju/names"
    11  	charmresource "gopkg.in/juju/charm.v6-unstable/resource"
    12  
    13  	"github.com/juju/juju/charmstore"
    14  )
    15  
    16  // DataStore exposes the functionality of Juju state needed here.
    17  type DataStore interface {
    18  	// SetCharmStoreResources sets the "polled from the charm store"
    19  	// resources for the service to the provided values.
    20  	SetCharmStoreResources(serviceID string, info []charmresource.Resource, lastPolled time.Time) error
    21  }
    22  
    23  // LatestCharmHandler implements apiserver/charmrevisionupdater.LatestCharmHandler.
    24  type LatestCharmHandler struct {
    25  	store DataStore
    26  }
    27  
    28  // NewLatestCharmHandler returns a LatestCharmHandler that uses the
    29  // given data store.
    30  func NewLatestCharmHandler(store DataStore) *LatestCharmHandler {
    31  	return &LatestCharmHandler{
    32  		store: store,
    33  	}
    34  }
    35  
    36  // HandleLatest implements apiserver/charmrevisionupdater.LatestCharmHandler
    37  // by storing the charm's resources in state.
    38  func (handler LatestCharmHandler) HandleLatest(serviceID names.ServiceTag, info charmstore.CharmInfo) error {
    39  	if err := handler.store.SetCharmStoreResources(serviceID.Id(), info.LatestResources, info.Timestamp); err != nil {
    40  		return errors.Trace(err)
    41  	}
    42  	return nil
    43  }