Skip to content

Commit

Permalink
apiserver/facade: extract types/logic from common
Browse files Browse the repository at this point in the history
...and introduce facade.Context, used by the new facade.Factory type.

The global registry remains in place; and RegisterStandardFacade et al
work as they did before; but individual facades can be easily converted
to accept a Context, and this opens the door to facades that can read
and write mongodb data, sanely, without requiring a full *State.
  • Loading branch information
fwereade committed Jun 22, 2016
1 parent 1564435 commit c930ab1
Show file tree
Hide file tree
Showing 106 changed files with 1,079 additions and 734 deletions.
7 changes: 4 additions & 3 deletions apiserver/action/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gopkg.in/juju/names.v2"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
Expand All @@ -19,13 +20,13 @@ func init() {
// ActionAPI implements the client API for interacting with Actions
type ActionAPI struct {
state *state.State
resources *common.Resources
authorizer common.Authorizer
resources facade.Resources
authorizer facade.Authorizer
check *common.BlockChecker
}

// NewActionAPI returns an initialized ActionAPI
func NewActionAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*ActionAPI, error) {
func NewActionAPI(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*ActionAPI, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
}
Expand Down
5 changes: 3 additions & 2 deletions apiserver/agent/agent.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"gopkg.in/juju/names.v2"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/mongo"
"github.com/juju/juju/state"
Expand All @@ -28,12 +29,12 @@ type AgentAPIV2 struct {
*common.ModelWatcher

st *state.State
auth common.Authorizer
auth facade.Authorizer
}

// NewAgentAPIV2 returns an object implementing version 2 of the Agent API
// with the given authorizer representing the currently logged in client.
func NewAgentAPIV2(st *state.State, resources *common.Resources, auth common.Authorizer) (*AgentAPIV2, error) {
func NewAgentAPIV2(st *state.State, resources facade.Resources, auth facade.Authorizer) (*AgentAPIV2, error) {
// Agents are defined to be any user that's not a client user.
if !auth.AuthMachineAgent() && !auth.AuthUnitAgent() {
return nil, common.ErrPerm
Expand Down
5 changes: 3 additions & 2 deletions apiserver/agenttools/agenttools.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/juju/version"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
"github.com/juju/juju/environs/tools"
Expand All @@ -34,14 +35,14 @@ type stateInterface interface {
// AgentToolsAPI implements the API used by the machine model worker.
type AgentToolsAPI struct {
st stateInterface
authorizer common.Authorizer
authorizer facade.Authorizer
// tools lookup
findTools toolsFinder
envVersionUpdate envVersionUpdater
}

// NewAgentToolsAPI creates a new instance of the Model API.
func NewAgentToolsAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*AgentToolsAPI, error) {
func NewAgentToolsAPI(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*AgentToolsAPI, error) {
return &AgentToolsAPI{
st: st,
authorizer: authorizer,
Expand Down
7 changes: 4 additions & 3 deletions apiserver/annotations/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"gopkg.in/juju/names.v2"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
Expand All @@ -30,14 +31,14 @@ type Annotations interface {
// implementation of the api end point.
type API struct {
access annotationAccess
authorizer common.Authorizer
authorizer facade.Authorizer
}

// NewAPI returns a new charm annotator API facade.
func NewAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
resources facade.Resources,
authorizer facade.Authorizer,
) (*API, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
Expand Down
7 changes: 4 additions & 3 deletions apiserver/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
goyaml "gopkg.in/yaml.v2"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/instance"
jjj "github.com/juju/juju/juju"
Expand Down Expand Up @@ -41,14 +42,14 @@ type Application interface {
type API struct {
check *common.BlockChecker
state *state.State
authorizer common.Authorizer
authorizer facade.Authorizer
}

// NewAPI returns a new application API facade.
func NewAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
resources facade.Resources,
authorizer facade.Authorizer,
) (*API, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
Expand Down
5 changes: 3 additions & 2 deletions apiserver/applicationscaler/facade.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package applicationscaler
import (
"github.com/juju/errors"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
Expand All @@ -27,11 +28,11 @@ type Backend interface {
// Facade allows model-manager clients to watch and rescale services.
type Facade struct {
backend Backend
resources *common.Resources
resources facade.Resources
}

// NewFacade creates a new authorized Facade.
func NewFacade(backend Backend, res *common.Resources, auth common.Authorizer) (*Facade, error) {
func NewFacade(backend Backend, res facade.Resources, auth facade.Authorizer) (*Facade, error) {
if !auth.AuthModelManager() {
return nil, common.ErrPerm
}
Expand Down
3 changes: 2 additions & 1 deletion apiserver/applicationscaler/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/juju/errors"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/state"
)

Expand All @@ -20,7 +21,7 @@ func init() {
}

// newFacade wraps the supplied *state.State for the use of the Facade.
func newFacade(st *state.State, res *common.Resources, auth common.Authorizer) (*Facade, error) {
func newFacade(st *state.State, res facade.Resources, auth facade.Authorizer) (*Facade, error) {
return NewFacade(backendShim{st}, res, auth)
}

Expand Down
7 changes: 4 additions & 3 deletions apiserver/applicationscaler/util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ import (

"github.com/juju/juju/apiserver/applicationscaler"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)

// mockAuth implements common.Authorizer for the tests' convenience.
// mockAuth implements facade.Authorizer for the tests' convenience.
type mockAuth struct {
common.Authorizer
facade.Authorizer
modelManager bool
}

Expand All @@ -25,7 +26,7 @@ func (mock mockAuth) AuthModelManager() bool {
}

// auth is a convenience constructor for a mockAuth.
func auth(modelManager bool) common.Authorizer {
func auth(modelManager bool) facade.Authorizer {
return mockAuth{modelManager: modelManager}
}

Expand Down
5 changes: 3 additions & 2 deletions apiserver/backups/backups.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"gopkg.in/mgo.v2"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/controller"
"github.com/juju/juju/environs/config"
Expand Down Expand Up @@ -46,7 +47,7 @@ type API struct {
}

// NewAPI creates a new instance of the Backups API facade.
func NewAPI(backend Backend, resources *common.Resources, authorizer common.Authorizer) (*API, error) {
func NewAPI(backend Backend, resources facade.Resources, authorizer facade.Authorizer) (*API, error) {
if !authorizer.AuthClient() {
return nil, errors.Trace(common.ErrPerm)
}
Expand Down Expand Up @@ -83,7 +84,7 @@ func NewAPI(backend Backend, resources *common.Resources, authorizer common.Auth
return &b, nil
}

func extractResourceValue(resources *common.Resources, key string) (string, error) {
func extractResourceValue(resources facade.Resources, key string) (string, error) {
res := resources.Get(key)
strRes, ok := res.(common.StringResource)
if !ok {
Expand Down
3 changes: 2 additions & 1 deletion apiserver/backups/shim.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package backups
import (
"github.com/juju/errors"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/state"
)

Expand All @@ -31,6 +32,6 @@ func (s *stateShim) MachineSeries(id string) (string, error) {
return m.Series(), nil
}

func newAPI(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*API, error) {
func newAPI(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*API, error) {
return NewAPI(&stateShim{st}, resources, authorizer)
}
7 changes: 4 additions & 3 deletions apiserver/block/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/juju/errors"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
Expand All @@ -33,14 +34,14 @@ type Block interface {
// implementation of the api end point.
type API struct {
access blockAccess
authorizer common.Authorizer
authorizer facade.Authorizer
}

// NewAPI returns a new block API facade.
func NewAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
resources facade.Resources,
authorizer facade.Authorizer,
) (*API, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
Expand Down
9 changes: 5 additions & 4 deletions apiserver/charmrevisionupdater/updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/juju/loggo"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/charmstore"
"github.com/juju/juju/state"
Expand All @@ -28,17 +29,17 @@ type CharmRevisionUpdater interface {
// implementation of the api end point.
type CharmRevisionUpdaterAPI struct {
state *state.State
resources *common.Resources
authorizer common.Authorizer
resources facade.Resources
authorizer facade.Authorizer
}

var _ CharmRevisionUpdater = (*CharmRevisionUpdaterAPI)(nil)

// NewCharmRevisionUpdaterAPI creates a new server-side charmrevisionupdater API end point.
func NewCharmRevisionUpdaterAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
resources facade.Resources,
authorizer facade.Authorizer,
) (*CharmRevisionUpdaterAPI, error) {
if !authorizer.AuthMachineAgent() && !authorizer.AuthModelManager() {
return nil, common.ErrPerm
Expand Down
7 changes: 4 additions & 3 deletions apiserver/charms/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (

"github.com/juju/juju/api"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
)
Expand All @@ -33,14 +34,14 @@ type Charms interface {
// implementation of the api end point.
type API struct {
access charmsAccess
authorizer common.Authorizer
authorizer facade.Authorizer
}

// NewAPI returns a new charms API facade.
func NewAPI(
st *state.State,
resources *common.Resources,
authorizer common.Authorizer,
resources facade.Resources,
authorizer facade.Authorizer,
) (*API, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
Expand Down
7 changes: 4 additions & 3 deletions apiserver/cleaner/cleaner.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ package cleaner

import (
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/state"
"github.com/juju/juju/state/watcher"
Expand All @@ -20,14 +21,14 @@ func init() {
// CleanerAPI implements the API used by the cleaner worker.
type CleanerAPI struct {
st StateInterface
resources *common.Resources
resources facade.Resources
}

// NewCleanerAPI creates a new instance of the Cleaner API.
func NewCleanerAPI(
st *state.State,
res *common.Resources,
authorizer common.Authorizer,
res facade.Resources,
authorizer facade.Authorizer,
) (*CleanerAPI, error) {
if !authorizer.AuthModelManager() {
return nil, common.ErrPerm
Expand Down
7 changes: 4 additions & 3 deletions apiserver/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/juju/juju/api"
"github.com/juju/juju/apiserver/application"
"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/environs"
"github.com/juju/juju/environs/config"
Expand All @@ -32,8 +33,8 @@ var logger = loggo.GetLogger("juju.apiserver.client")

type API struct {
stateAccessor stateInterface
auth common.Authorizer
resources *common.Resources
auth facade.Authorizer
resources facade.Resources
client *Client
// statusSetter provides common methods for updating an entity's provisioning status.
statusSetter *common.StatusSetter
Expand All @@ -59,7 +60,7 @@ var getState = func(st *state.State) stateInterface {
}

// NewClient creates a new instance of the Client Facade.
func NewClient(st *state.State, resources *common.Resources, authorizer common.Authorizer) (*Client, error) {
func NewClient(st *state.State, resources facade.Resources, authorizer facade.Authorizer) (*Client, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
}
Expand Down
7 changes: 4 additions & 3 deletions apiserver/cloud/cloud.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"gopkg.in/juju/names.v2"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/facade"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/cloud"
"github.com/juju/juju/state"
Expand All @@ -25,18 +26,18 @@ func init() {
// the concrete implementation of the api end point.
type CloudAPI struct {
backend Backend
authorizer common.Authorizer
authorizer facade.Authorizer
apiUser names.UserTag
getCredentialsAuthFunc common.GetAuthFunc
}

func newFacade(st *state.State, resources *common.Resources, auth common.Authorizer) (*CloudAPI, error) {
func newFacade(st *state.State, resources facade.Resources, auth facade.Authorizer) (*CloudAPI, error) {
return NewCloudAPI(NewStateBackend(st), auth)
}

// NewCloudAPI creates a new API server endpoint for managing the controller's
// cloud definition and cloud credentials.
func NewCloudAPI(backend Backend, authorizer common.Authorizer) (*CloudAPI, error) {
func NewCloudAPI(backend Backend, authorizer facade.Authorizer) (*CloudAPI, error) {
if !authorizer.AuthClient() {
return nil, common.ErrPerm
}
Expand Down
Loading

0 comments on commit c930ab1

Please sign in to comment.