github.com/altoros/juju-vmware@v0.0.0-20150312064031-f19ae857ccca/apiserver/imagemanager/imagemanager.go (about) 1 // Copyright 2015 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package imagemanager 5 6 import ( 7 "github.com/juju/errors" 8 "github.com/juju/loggo" 9 10 "github.com/juju/juju/apiserver/common" 11 "github.com/juju/juju/apiserver/params" 12 "github.com/juju/juju/state" 13 "github.com/juju/juju/state/imagestorage" 14 ) 15 16 var logger = loggo.GetLogger("juju.apiserver.imagemanager") 17 18 func init() { 19 common.RegisterStandardFacade("ImageManager", 1, NewImageManagerAPI) 20 } 21 22 // ImageManager defines the methods on the imagemanager API end point. 23 type ImageManager interface { 24 ListImages(arg params.ImageFilterParams) (params.ListImageResult, error) 25 DeleteImages(arg params.ImageFilterParams) (params.ErrorResults, error) 26 } 27 28 // ImageManagerAPI implements the ImageManager interface and is the concrete 29 // implementation of the api end point. 30 type ImageManagerAPI struct { 31 state stateInterface 32 resources *common.Resources 33 authorizer common.Authorizer 34 check *common.BlockChecker 35 } 36 37 var _ ImageManager = (*ImageManagerAPI)(nil) 38 39 var getState = func(st *state.State) stateInterface { 40 return stateShim{st} 41 } 42 43 // NewImageManagerAPI creates a new server-side imagemanager API end point. 44 func NewImageManagerAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*ImageManagerAPI, error) { 45 // Only clients can access the image manager service. 46 if !authorizer.AuthClient() { 47 return nil, common.ErrPerm 48 } 49 return &ImageManagerAPI{ 50 state: getState(st), 51 resources: resources, 52 authorizer: authorizer, 53 check: common.NewBlockChecker(st), 54 }, nil 55 } 56 57 // ListImages returns images matching the specified filter. 58 func (api *ImageManagerAPI) ListImages(arg params.ImageFilterParams) (params.ListImageResult, error) { 59 var result params.ListImageResult 60 if len(arg.Images) > 1 { 61 return result, errors.New("image filter with multiple terms not supported") 62 } 63 filter := imagestorage.ImageFilter{} 64 if len(arg.Images) == 1 { 65 filter = imagestorage.ImageFilter{ 66 Kind: arg.Images[0].Kind, 67 Series: arg.Images[0].Series, 68 Arch: arg.Images[0].Arch, 69 } 70 } 71 stor := api.state.ImageStorage() 72 metadata, err := stor.ListImages(filter) 73 if err != nil { 74 return result, nil 75 } 76 result.Result = make([]params.ImageMetadata, len(metadata)) 77 for i, m := range metadata { 78 result.Result[i] = params.ImageMetadata{ 79 Kind: m.Kind, 80 Series: m.Series, 81 Arch: m.Arch, 82 URL: m.SourceURL, 83 Created: m.Created, 84 } 85 } 86 return result, nil 87 } 88 89 // DeleteImages deletes the images matching the specified filter. 90 func (api *ImageManagerAPI) DeleteImages(arg params.ImageFilterParams) (params.ErrorResults, error) { 91 if err := api.check.ChangeAllowed(); err != nil { 92 return params.ErrorResults{}, errors.Trace(err) 93 } 94 var result params.ErrorResults 95 result.Results = make([]params.ErrorResult, len(arg.Images)) 96 stor := api.state.ImageStorage() 97 for i, imageSpec := range arg.Images { 98 filter := imagestorage.ImageFilter{ 99 Kind: imageSpec.Kind, 100 Series: imageSpec.Series, 101 Arch: imageSpec.Arch, 102 } 103 imageMetadata, err := stor.ListImages(filter) 104 if err != nil { 105 result.Results[i].Error = common.ServerError(err) 106 continue 107 } 108 if len(imageMetadata) != 1 { 109 result.Results[i].Error = common.ServerError( 110 errors.NotFoundf("image %s/%s/%s", filter.Kind, filter.Series, filter.Arch)) 111 continue 112 } 113 logger.Infof("deleting image with metadata %+v", *imageMetadata[0]) 114 err = stor.DeleteImage(imageMetadata[0]) 115 if err != nil { 116 result.Results[i].Error = common.ServerError(err) 117 } 118 } 119 return result, nil 120 }