github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/controller/caasfirewaller/firewaller.go (about) 1 // Copyright 2017 Canonical Ltd. 2 // Licensed under the AGPLv3, see LICENCE file for details. 3 4 package caasfirewaller 5 6 import ( 7 "github.com/juju/errors" 8 "gopkg.in/juju/names.v2" 9 10 "github.com/juju/juju/apiserver/common" 11 "github.com/juju/juju/apiserver/facade" 12 "github.com/juju/juju/apiserver/params" 13 "github.com/juju/juju/state/watcher" 14 ) 15 16 type Facade struct { 17 *common.LifeGetter 18 *common.AgentEntityWatcher 19 resources facade.Resources 20 state CAASFirewallerState 21 } 22 23 // NewStateFacade provides the signature required for facade registration. 24 func NewStateFacade(ctx facade.Context) (*Facade, error) { 25 authorizer := ctx.Auth() 26 resources := ctx.Resources() 27 return NewFacade( 28 resources, 29 authorizer, 30 stateShim{ctx.State()}, 31 ) 32 } 33 34 // NewFacade returns a new CAAS firewaller Facade facade. 35 func NewFacade( 36 resources facade.Resources, 37 authorizer facade.Authorizer, 38 st CAASFirewallerState, 39 ) (*Facade, error) { 40 if !authorizer.AuthController() { 41 return nil, common.ErrPerm 42 } 43 accessApplication := common.AuthFuncForTagKind(names.ApplicationTagKind) 44 return &Facade{ 45 LifeGetter: common.NewLifeGetter( 46 st, common.AuthAny( 47 common.AuthFuncForTagKind(names.ApplicationTagKind), 48 common.AuthFuncForTagKind(names.UnitTagKind), 49 ), 50 ), 51 AgentEntityWatcher: common.NewAgentEntityWatcher( 52 st, 53 resources, 54 accessApplication, 55 ), 56 resources: resources, 57 state: st, 58 }, nil 59 } 60 61 // WatchApplications starts a StringsWatcher to watch CAAS applications 62 // deployed to this model. 63 func (f *Facade) WatchApplications() (params.StringsWatchResult, error) { 64 watch := f.state.WatchApplications() 65 if changes, ok := <-watch.Changes(); ok { 66 return params.StringsWatchResult{ 67 StringsWatcherId: f.resources.Register(watch), 68 Changes: changes, 69 }, nil 70 } 71 return params.StringsWatchResult{}, watcher.EnsureErr(watch) 72 } 73 74 // IsExposed returns whether the specified applications are exposed. 75 func (f *Facade) IsExposed(args params.Entities) (params.BoolResults, error) { 76 results := params.BoolResults{ 77 Results: make([]params.BoolResult, len(args.Entities)), 78 } 79 for i, arg := range args.Entities { 80 exposed, err := f.isExposed(f.state, arg.Tag) 81 if err != nil { 82 results.Results[i].Error = common.ServerError(err) 83 continue 84 } 85 results.Results[i].Result = exposed 86 } 87 return results, nil 88 } 89 90 func (f *Facade) isExposed(backend CAASFirewallerState, tagString string) (bool, error) { 91 tag, err := names.ParseApplicationTag(tagString) 92 if err != nil { 93 return false, errors.Trace(err) 94 } 95 app, err := backend.Application(tag.Id()) 96 if err != nil { 97 return false, errors.Trace(err) 98 } 99 return app.IsExposed(), nil 100 } 101 102 // ApplicationsConfig returns the config for the specified applications. 103 func (f *Facade) ApplicationsConfig(args params.Entities) (params.ApplicationGetConfigResults, error) { 104 results := params.ApplicationGetConfigResults{ 105 Results: make([]params.ConfigResult, len(args.Entities)), 106 } 107 for i, arg := range args.Entities { 108 result, err := f.getApplicationConfig(arg.Tag) 109 results.Results[i].Config = result 110 results.Results[i].Error = common.ServerError(err) 111 } 112 return results, nil 113 } 114 115 func (f *Facade) getApplicationConfig(tagString string) (map[string]interface{}, error) { 116 tag, err := names.ParseApplicationTag(tagString) 117 if err != nil { 118 return nil, errors.Trace(err) 119 } 120 app, err := f.state.Application(tag.Id()) 121 if err != nil { 122 return nil, errors.Trace(err) 123 } 124 return app.ApplicationConfig() 125 }