github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/caasunitprovisioner/state.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasunitprovisioner 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/controller" 11 "github.com/juju/juju/core/application" 12 "github.com/juju/juju/core/constraints" 13 "github.com/juju/juju/core/status" 14 "github.com/juju/juju/environs/config" 15 "github.com/juju/juju/network" 16 "github.com/juju/juju/state" 17 ) 18 19 // CAASUnitProvisionerState provides the subset of global state 20 // required by the CAAS unit provisioner facade. 21 type CAASUnitProvisionerState interface { 22 ControllerConfig() (controller.Config, error) 23 Application(string) (Application, error) 24 FindEntity(names.Tag) (state.Entity, error) 25 Model() (Model, error) 26 WatchApplications() state.StringsWatcher 27 } 28 29 // StorageBackend provides the subset of backend storage 30 // functionality required by the CAAS unit provisioner facade. 31 type StorageBackend interface { 32 StorageInstance(names.StorageTag) (state.StorageInstance, error) 33 Filesystem(names.FilesystemTag) (state.Filesystem, error) 34 FilesystemAttachment(names.Tag, names.FilesystemTag) (state.FilesystemAttachment, error) 35 StorageInstanceFilesystem(names.StorageTag) (state.Filesystem, error) 36 UnitStorageAttachments(unit names.UnitTag) ([]state.StorageAttachment, error) 37 SetFilesystemInfo(names.FilesystemTag, state.FilesystemInfo) error 38 SetFilesystemAttachmentInfo(names.Tag, names.FilesystemTag, state.FilesystemAttachmentInfo) error 39 Volume(tag names.VolumeTag) (state.Volume, error) 40 StorageInstanceVolume(tag names.StorageTag) (state.Volume, error) 41 SetVolumeInfo(names.VolumeTag, state.VolumeInfo) error 42 SetVolumeAttachmentInfo(names.Tag, names.VolumeTag, state.VolumeAttachmentInfo) error 43 44 // These are for cleanup up orphaned filesystems when pods are recreated. 45 // TODO(caas) - record unit id on the filesystem so we can query by unit 46 AllFilesystems() ([]state.Filesystem, error) 47 DestroyStorageInstance(tag names.StorageTag, destroyAttachments bool) (err error) 48 DestroyFilesystem(tag names.FilesystemTag) (err error) 49 } 50 51 // DeviceBackend provides the subset of backend Device 52 // functionality required by the CAAS unit provisioner facade. 53 type DeviceBackend interface { 54 DeviceConstraints(id string) (map[string]state.DeviceConstraints, error) 55 } 56 57 // Model provides the subset of CAAS model state required 58 // by the CAAS unit provisioner facade. 59 type Model interface { 60 ModelConfig() (*config.Config, error) 61 PodSpec(tag names.ApplicationTag) (string, error) 62 WatchPodSpec(tag names.ApplicationTag) (state.NotifyWatcher, error) 63 Containers(providerIds ...string) ([]state.CloudContainer, error) 64 } 65 66 // Application provides the subset of application state 67 // required by the CAAS unit provisioner facade. 68 type Application interface { 69 GetScale() int 70 WatchScale() state.NotifyWatcher 71 ApplicationConfig() (application.ConfigAttributes, error) 72 AllUnits() (units []Unit, err error) 73 AddOperation(state.UnitUpdateProperties) *state.AddUnitOperation 74 UpdateUnits(*state.UpdateUnitsOperation) error 75 UpdateCloudService(providerId string, addreses []network.Address) error 76 DeviceConstraints() (map[string]state.DeviceConstraints, error) 77 Life() state.Life 78 Name() string 79 Constraints() (constraints.Value, error) 80 GetPlacement() string 81 SetOperatorStatus(sInfo status.StatusInfo) error 82 } 83 84 type stateShim struct { 85 *state.State 86 } 87 88 func (s stateShim) Application(id string) (Application, error) { 89 app, err := s.State.Application(id) 90 if err != nil { 91 return nil, err 92 } 93 return applicationShim{app}, nil 94 } 95 96 func (s stateShim) Model() (Model, error) { 97 model, err := s.State.Model() 98 if err != nil { 99 return nil, err 100 } 101 return model.CAASModel() 102 } 103 104 type applicationShim struct { 105 *state.Application 106 } 107 108 func (a applicationShim) AllUnits() ([]Unit, error) { 109 all, err := a.Application.AllUnits() 110 if err != nil { 111 return nil, errors.Trace(err) 112 } 113 result := make([]Unit, len(all)) 114 for i, u := range all { 115 result[i] = u 116 } 117 return result, nil 118 } 119 120 type Unit interface { 121 Name() string 122 Life() state.Life 123 UnitTag() names.UnitTag 124 ContainerInfo() (state.CloudContainer, error) 125 AgentStatus() (status.StatusInfo, error) 126 UpdateOperation(props state.UnitUpdateProperties) *state.UpdateUnitOperation 127 DestroyOperation() *state.DestroyUnitOperation 128 }