github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/hostkeyreporter/facade.go (about) 1 // Copyright 2016 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 // Package hostkeyreporter implements the API facade used by the 5 // hostkeyreporter worker. 6 package hostkeyreporter 7 8 import ( 9 "github.com/juju/names" 10 11 "github.com/juju/juju/apiserver/common" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/state" 14 ) 15 16 func init() { 17 common.RegisterStandardFacade("HostKeyReporter", 1, newFacade) 18 } 19 20 // Backend defines the State API used by the hostkeyreporter facade. 21 type Backend interface { 22 SetSSHHostKeys(names.MachineTag, state.SSHHostKeys) error 23 } 24 25 // Facade implements the API required by the hostkeyreporter worker. 26 type Facade struct { 27 backend Backend 28 getCanModify common.GetAuthFunc 29 } 30 31 // New returns a new API facade for the hostkeyreporter worker. 32 func New(backend Backend, _ *common.Resources, authorizer common.Authorizer) (*Facade, error) { 33 return &Facade{ 34 backend: backend, 35 getCanModify: func() (common.AuthFunc, error) { 36 return authorizer.AuthOwner, nil 37 }, 38 }, nil 39 } 40 41 // ReportKeys sets the SSH host keys for one or more entities. 42 func (facade *Facade) ReportKeys(args params.SSHHostKeySet) (params.ErrorResults, error) { 43 results := params.ErrorResults{ 44 Results: make([]params.ErrorResult, len(args.EntityKeys)), 45 } 46 47 canModify, err := facade.getCanModify() 48 if err != nil { 49 return results, err 50 } 51 52 for i, arg := range args.EntityKeys { 53 tag, err := names.ParseMachineTag(arg.Tag) 54 if err != nil { 55 results.Results[i].Error = common.ServerError(common.ErrPerm) 56 continue 57 } 58 err = common.ErrPerm 59 if canModify(tag) { 60 err = facade.backend.SetSSHHostKeys(tag, state.SSHHostKeys(arg.PublicKeys)) 61 } 62 results.Results[i].Error = common.ServerError(err) 63 } 64 return results, nil 65 }