github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/state/resources.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package state 5 6 import ( 7 "io" 8 "time" 9 10 "github.com/juju/errors" 11 charmresource "gopkg.in/juju/charm.v6-unstable/resource" 12 "gopkg.in/mgo.v2/txn" 13 14 "github.com/juju/juju/resource" 15 ) 16 17 // Resources describes the state functionality for resources. 18 type Resources interface { 19 // ListResources returns the list of resources for the given service. 20 ListResources(serviceID string) (resource.ServiceResources, error) 21 22 // AddPendingResource adds the resource to the data store in a 23 // "pending" state. It will stay pending (and unavailable) until 24 // it is resolved. The returned ID is used to identify the pending 25 // resources when resolving it. 26 AddPendingResource(serviceID, userID string, chRes charmresource.Resource, r io.Reader) (string, error) 27 28 // GetResource returns the identified resource. 29 GetResource(serviceID, name string) (resource.Resource, error) 30 31 // GetPendingResource returns the identified resource. 32 GetPendingResource(serviceID, name, pendingID string) (resource.Resource, error) 33 34 // SetResource adds the resource to blob storage and updates the metadata. 35 SetResource(serviceID, userID string, res charmresource.Resource, r io.Reader) (resource.Resource, error) 36 37 // UpdatePendingResource adds the resource to blob storage and updates the metadata. 38 UpdatePendingResource(serviceID, pendingID, userID string, res charmresource.Resource, r io.Reader) (resource.Resource, error) 39 40 // OpenResource returns the metadata for a resource and a reader for the resource. 41 OpenResource(serviceID, name string) (resource.Resource, io.ReadCloser, error) 42 43 // OpenResourceForUniter returns the metadata for a resource and a reader for the resource. 44 OpenResourceForUniter(unit resource.Unit, name string) (resource.Resource, io.ReadCloser, error) 45 46 // SetCharmStoreResources sets the "polled" resources for the 47 // service to the provided values. 48 SetCharmStoreResources(serviceID string, info []charmresource.Resource, lastPolled time.Time) error 49 50 // TODO(ericsnow) Move this down to ResourcesPersistence. 51 52 // NewResolvePendingResourcesOps generates mongo transaction operations 53 // to set the identified resources as active. 54 NewResolvePendingResourcesOps(serviceID string, pendingIDs map[string]string) ([]txn.Op, error) 55 } 56 57 var newResources func(Persistence, *State) Resources 58 59 // SetResourcesComponent registers the function that provide the state 60 // functionality related to resources. 61 func SetResourcesComponent(fn func(Persistence, *State) Resources) { 62 newResources = fn 63 } 64 65 // Resources returns the resources functionality for the current state. 66 func (st *State) Resources() (Resources, error) { 67 if newResources == nil { 68 return nil, errors.NotSupportedf("resources") 69 } 70 71 persist := st.newPersistence() 72 resources := newResources(persist, st) 73 return resources, nil 74 } 75 76 // ResourcesPersistence exposes the resources persistence functionality 77 // needed by state. 78 type ResourcesPersistence interface { 79 // NewRemoveUnitResourcesOps returns mgo transaction operations 80 // that remove resource information specific to the unit from state. 81 NewRemoveUnitResourcesOps(unitID string) ([]txn.Op, error) 82 83 // NewRemoveResourcesOps returns mgo transaction operations that 84 // remove all the service's resources from state. 85 NewRemoveResourcesOps(serviceID string) ([]txn.Op, error) 86 } 87 88 var newResourcesPersistence func(Persistence) ResourcesPersistence 89 90 // SetResourcesPersistence registers the function that provides the 91 // state persistence functionality related to resources. 92 func SetResourcesPersistence(fn func(Persistence) ResourcesPersistence) { 93 newResourcesPersistence = fn 94 } 95 96 // ResourcesPersistence returns the resources persistence functionality 97 // for the current state. 98 func (st *State) ResourcesPersistence() (ResourcesPersistence, error) { 99 if newResourcesPersistence == nil { 100 return nil, errors.NotSupportedf("resources") 101 } 102 103 base := st.newPersistence() 104 persist := newResourcesPersistence(base) 105 return persist, nil 106 }