Skip to content

Commit

Permalink
Move resource unit hook context to apiserver
Browse files Browse the repository at this point in the history
This is part of extracting the resources API facdes to the apiserver
package (like all other facades).
  • Loading branch information
Menno Smits committed Mar 19, 2017
1 parent 9be079c commit 2a03921
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 50 deletions.
14 changes: 14 additions & 0 deletions apiserver/resourceshookcontext/package_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package resourceshookcontext_test

import (
"testing"

gc "gopkg.in/check.v1"
)

func Test(t *testing.T) {
gc.TestingT(t)
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2016 Canonical Ltd.
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package server_test
package resourceshookcontext_test

import (
"io"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,47 @@
// Copyright 2016 Canonical Ltd.
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package server
package resourceshookcontext

import (
"reflect"

"github.com/juju/errors"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/resource"
"github.com/juju/juju/resource/api"
"github.com/juju/juju/state"
)

// FacadeVersion is the version of the current API facade.
// (We start at 1 to distinguish from the default value.)
const FacadeVersion = 1
func init() {
common.RegisterHookContextFacade(
"ResourcesHookContext", 1,
newHookContextFacade,
reflect.TypeOf(&UnitFacade{}),
)
}

func newHookContextFacade(st *state.State, unit *state.Unit) (interface{}, error) {
res, err := st.Resources()
if err != nil {
return nil, errors.Trace(err)
}
return NewUnitFacade(&resourcesUnitDataStore{res, unit}), nil
}

// resourcesUnitDatastore is a shim to elide serviceName from
// ListResources.
type resourcesUnitDataStore struct {
resources state.Resources
unit *state.Unit
}

// ListResources implements resource/api/private/server.UnitDataStore.
func (ds *resourcesUnitDataStore) ListResources() (resource.ServiceResources, error) {
return ds.resources.ListResources(ds.unit.ApplicationName())
}

// UnitDataStore exposes the data storage functionality needed here.
// All functionality is tied to the unit's application.
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2016 Canonical Ltd.
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package server_test
package resourceshookcontext_test

import (
"github.com/juju/errors"
Expand All @@ -11,9 +11,9 @@ import (

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/apiserver/resourceshookcontext"
"github.com/juju/juju/resource"
"github.com/juju/juju/resource/api"
"github.com/juju/juju/resource/api/private/server"
"github.com/juju/juju/resource/resourcetesting"
)

Expand All @@ -34,7 +34,7 @@ func (s *UnitFacadeSuite) SetUpTest(c *gc.C) {
func (s *UnitFacadeSuite) TestNewUnitFacade(c *gc.C) {
expected := &stubUnitDataStore{Stub: s.stub}

uf := server.NewUnitFacade(expected)
uf := resourceshookcontext.NewUnitFacade(expected)

s.stub.CheckNoCalls(c)
c.Check(uf.DataStore, gc.Equals, expected)
Expand All @@ -49,7 +49,7 @@ func (s *UnitFacadeSuite) TestGetResourceInfoOkay(c *gc.C) {
store.ReturnListResources = resource.ServiceResources{
Resources: []resource.Resource{res1, res2},
}
uf := server.UnitFacade{DataStore: store}
uf := resourceshookcontext.UnitFacade{DataStore: store}

results, err := uf.GetResourceInfo(params.ListUnitResourcesArgs{
ResourceNames: []string{"spam", "eggs"},
Expand All @@ -72,7 +72,7 @@ func (s *UnitFacadeSuite) TestGetResourceInfoEmpty(c *gc.C) {
store.ReturnListResources = resource.ServiceResources{
Resources: []resource.Resource{opened.Resource},
}
uf := server.UnitFacade{DataStore: store}
uf := resourceshookcontext.UnitFacade{DataStore: store}

results, err := uf.GetResourceInfo(params.ListUnitResourcesArgs{
ResourceNames: []string{},
Expand All @@ -91,7 +91,7 @@ func (s *UnitFacadeSuite) TestGetResourceInfoNotFound(c *gc.C) {
store.ReturnListResources = resource.ServiceResources{
Resources: []resource.Resource{opened.Resource},
}
uf := server.UnitFacade{DataStore: store}
uf := resourceshookcontext.UnitFacade{DataStore: store}

results, err := uf.GetResourceInfo(params.ListUnitResourcesArgs{
ResourceNames: []string{"eggs"},
Expand Down
38 changes: 2 additions & 36 deletions component/all/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package all

import (
"os"
"reflect"

jujucmd "github.com/juju/cmd"
"github.com/juju/errors"
Expand All @@ -23,7 +22,6 @@ import (
"github.com/juju/juju/resource/api/client"
privateapi "github.com/juju/juju/resource/api/private"
internalclient "github.com/juju/juju/resource/api/private/client"
internalserver "github.com/juju/juju/resource/api/private/server"
"github.com/juju/juju/resource/cmd"
"github.com/juju/juju/resource/context"
contextcmd "github.com/juju/juju/resource/context/cmd"
Expand Down Expand Up @@ -144,8 +142,6 @@ func (r resources) registerPublicCommands() {
})
}

// TODO(katco): This seems to be common across components. Pop up a
// level and genericize?
func (r resources) registerHookContext() {
if markRegistered(resource.ComponentName, "hook-context") == false {
return
Expand All @@ -165,7 +161,6 @@ func (r resources) registerHookContext() {
)

r.registerHookContextCommands()
r.registerHookContextFacade()
r.registerUnitDownloadEndpoint()
}

Expand All @@ -190,16 +185,7 @@ func (r resources) registerHookContextCommands() {
)
}

func (r resources) registerHookContextFacade() {
common.RegisterHookContextFacade(
context.HookContextFacade,
internalserver.FacadeVersion,
r.newHookContextFacade,
reflect.TypeOf(&internalserver.UnitFacade{}),
)

}

// XXX
func (r resources) registerUnitDownloadEndpoint() {
common.RegisterAPIModelEndpoint(privateapi.HTTPEndpointPattern, apihttp.HandlerSpec{
Constraints: apihttp.HandlerConstraints{
Expand All @@ -211,29 +197,9 @@ func (r resources) registerUnitDownloadEndpoint() {
})
}

// resourcesUnitDatastore is a shim to elide serviceName from
// ListResources.
type resourcesUnitDataStore struct {
resources corestate.Resources
unit *corestate.Unit
}

// ListResources implements resource/api/private/server.UnitDataStore.
func (ds *resourcesUnitDataStore) ListResources() (resource.ServiceResources, error) {
return ds.resources.ListResources(ds.unit.ApplicationName())
}

func (r resources) newHookContextFacade(st *corestate.State, unit *corestate.Unit) (interface{}, error) {
res, err := st.Resources()
if err != nil {
return nil, errors.Trace(err)
}
return internalserver.NewUnitFacade(&resourcesUnitDataStore{res, unit}), nil
}

func (r resources) newUnitFacadeClient(unitName string, caller base.APICaller) (context.APIClient, error) {

facadeCaller := base.NewFacadeCallerForVersion(caller, context.HookContextFacade, internalserver.FacadeVersion)
facadeCaller := base.NewFacadeCallerForVersion(caller, context.HookContextFacade, 1)
httpClient, err := caller.HTTPClient()
if err != nil {
return nil, errors.Trace(err)
Expand Down

0 comments on commit 2a03921

Please sign in to comment.