Skip to content

Commit

Permalink
Move payloads unit context facade to apiserver
Browse files Browse the repository at this point in the history
This is the final apiserver part to move out of payloads/api. Further
cleanups to follow.
  • Loading branch information
Menno Smits committed Mar 10, 2017
1 parent 6ec0d8f commit 0a86abf
Show file tree
Hide file tree
Showing 12 changed files with 201 additions and 216 deletions.
1 change: 1 addition & 0 deletions apiserver/allfacades.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ import (
_ "github.com/juju/juju/apiserver/modelconfig" // ModelUser Write
_ "github.com/juju/juju/apiserver/modelmanager" // ModelUser Write
_ "github.com/juju/juju/apiserver/payloads"
_ "github.com/juju/juju/apiserver/payloadshookcontext"
_ "github.com/juju/juju/apiserver/provisioner"
_ "github.com/juju/juju/apiserver/proxyupdater"
_ "github.com/juju/juju/apiserver/reboot"
Expand Down
58 changes: 29 additions & 29 deletions payload/api/private/data.go → apiserver/params/payloads_unit.go
Original file line number Diff line number Diff line change
@@ -1,63 +1,63 @@
// Copyright 2015 Canonical Ltd.
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package private
package params

import "github.com/juju/juju/apiserver/params"

// TrackArgs are the arguments for the Track endpoint.
type TrackArgs struct {
// TrackPayloadArgs are the arguments for the
// PayloadsHookContext.Track endpoint.
type TrackPayloadArgs struct {
// Payloads is the list of Payloads to track
Payloads []params.Payload `json:"payloads"`
Payloads []Payload `json:"payloads"`
}

// List uses params.Entities.

// LookUpArgs are the arguments for the LookUp endpoint.
type LookUpArgs struct {
// LookUpPayloadArgs are the arguments for the LookUp endpoint.
type LookUpPayloadArgs struct {
// Args is the list of arguments to pass to this function.
Args []LookUpArg `json:"args"`
Args []LookUpPayloadArg `json:"args"`
}

// LookUpArg contains all the information necessary to identify a payload.
type LookUpArg struct {
// LookUpPayloadArg contains all the information necessary to identify
// a payload.
type LookUpPayloadArg struct {
// Name is the payload name.
Name string `json:"name"`

// ID uniquely identifies the payload for the given name.
ID string `json:"id"`
}

// SetStatusArgs are the arguments for the SetStatus endpoint.
type SetStatusArgs struct {
// SetPayloadStatusArgs are the arguments for the
// PayloadsHookContext.SetStatus endpoint.
type SetPayloadStatusArgs struct {
// Args is the list of arguments to pass to this function.
Args []SetStatusArg `json:"args"`
Args []SetPayloadStatusArg `json:"args"`
}

// SetStatusArg are the arguments for a single call to the
// SetPayloadStatusArg are the arguments for a single call to the
// SetStatus endpoint.
type SetStatusArg struct {
params.Entity
type SetPayloadStatusArg struct {
Entity

// Status is the new status of the payload.
Status string `json:"status"`
}

// Untrack uses params.Entities.

// PayloadResults is the result for a call that makes one or more requests
// about payloads.
// PayloadResults is the result for a call that makes one or more
// requests about payloads.
type PayloadResults struct {
Results []PayloadResult `json:"results"`
}

// TODO(ericsnow) Eliminate the NotFound field?

// PayloadResult contains the result for a single call.
type PayloadResult struct {
params.Entity
Entity

// Payload holds the details of the payload, if any.
Payload *params.Payload `json:"payload"`
Payload *Payload `json:"payload"`

// NotFound indicates that the payload was not found in state.
NotFound bool `json:"not-found"`

// Error is the error (if any) for the call referring to ID.
Error *params.Error `json:"error,omitempty"`
Error *Error `json:"error,omitempty"`
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2015 Canonical Ltd.
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package server
package payloadshookcontext_test

import (
"testing"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,75 @@
// Copyright 2015 Canonical Ltd.
// Copyright 2017 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.

package server

// TODO(ericsnow) Eliminate the params import if possible.
package payloadshookcontext

import (
"reflect"

"github.com/juju/errors"
"github.com/juju/loggo"

"github.com/juju/juju/apiserver/common"
"github.com/juju/juju/apiserver/params"
"github.com/juju/juju/payload"
"github.com/juju/juju/payload/api"
internal "github.com/juju/juju/payload/api/private"
internal "github.com/juju/juju/payload/api/private" // XXX temp
"github.com/juju/juju/state"
)

// UnitPayloads exposes the State functionality for a unit's payloads.
type UnitPayloads interface {
var logger = loggo.GetLogger("juju.apiserver.payloadshookcontext")

func init() {
common.RegisterHookContextFacade(
"PayloadsHookContext", 1,
NewHookContextFacade,
reflect.TypeOf(&UnitFacade{}),
)
}

// NewHookContextFacade returns a new payloads hook context facade for
// the State and Unit given. It is used for facade registration.
func NewHookContextFacade(st *state.State, unit *state.Unit) (interface{}, error) {
up, err := st.UnitPayloads(unit)
if err != nil {
return nil, errors.Trace(err)
}
return NewUnitFacade(up), nil
}

// UnitPayloadBackend exposes the State functionality for a unit's payloads.
type UnitPayloadBackend interface {
// Track tracks a payload for the unit and info.
Track(info payload.Payload) error

// List returns information on the payload with the id on the unit.
List(ids ...string) ([]payload.Result, error)

// Settatus sets the status for the payload with the given id on the unit.
SetStatus(id, status string) error

// LookUp returns the payload ID for the given name/rawID pair.
LookUp(name, rawID string) (string, error)

// Untrack removes the information for the payload with the given id.
Untrack(id string) error
}

// UnitFacade serves payload-specific API methods.
type UnitFacade struct {
// State exposes the payload aspect of Juju's state.
State UnitPayloads
backend UnitPayloadBackend
}

// NewUnitFacade builds a new facade for the given State.
func NewUnitFacade(st UnitPayloads) *UnitFacade {
return &UnitFacade{State: st}
// NewUnitFacade builds a new facade for the given backend.
func NewUnitFacade(backend UnitPayloadBackend) *UnitFacade {
return &UnitFacade{backend: backend}
}

// Track stores a payload to be tracked in state.
func (uf UnitFacade) Track(args internal.TrackArgs) (internal.PayloadResults, error) {
func (uf UnitFacade) Track(args params.TrackPayloadArgs) (params.PayloadResults, error) {
logger.Debugf("tracking %d payloads from API", len(args.Payloads))

var r internal.PayloadResults
var r params.PayloadResults
for _, apiPayload := range args.Payloads {
pl, err := api.API2Payload(apiPayload)
if err != nil {
Expand All @@ -59,10 +85,10 @@ func (uf UnitFacade) Track(args internal.TrackArgs) (internal.PayloadResults, er
}

func (uf UnitFacade) track(pl payload.Payload) (string, error) {
if err := uf.State.Track(pl); err != nil {
if err := uf.backend.Track(pl); err != nil {
return "", errors.Trace(err)
}
id, err := uf.State.LookUp(pl.Name, pl.ID)
id, err := uf.backend.LookUp(pl.Name, pl.ID)
if err != nil {
return "", errors.Trace(err)
}
Expand All @@ -72,44 +98,44 @@ func (uf UnitFacade) track(pl payload.Payload) (string, error) {
// List builds the list of payload being tracked for
// the given unit and IDs. If no IDs are provided then all tracked
// payloads for the unit are returned.
func (uf UnitFacade) List(args params.Entities) (internal.PayloadResults, error) {
func (uf UnitFacade) List(args params.Entities) (params.PayloadResults, error) {
if len(args.Entities) == 0 {
return uf.listAll()
}

var ids []string
for _, entity := range args.Entities {
id, err := internal.API2ID(entity.Tag)
id, err := internal.API2ID(entity.Tag) // XXX
if err != nil {
return internal.PayloadResults{}, errors.Trace(err)
return params.PayloadResults{}, errors.Trace(err)
}
ids = append(ids, id)
}

results, err := uf.State.List(ids...)
results, err := uf.backend.List(ids...)
if err != nil {
return internal.PayloadResults{}, errors.Trace(err)
return params.PayloadResults{}, errors.Trace(err)
}

var r internal.PayloadResults
var r params.PayloadResults
for _, result := range results {
res := internal.Result2api(result)
res := internal.Result2api(result) //XXX
r.Results = append(r.Results, res)
}
return r, nil
}

func (uf UnitFacade) listAll() (internal.PayloadResults, error) {
var r internal.PayloadResults
func (uf UnitFacade) listAll() (params.PayloadResults, error) {
var r params.PayloadResults

results, err := uf.State.List()
results, err := uf.backend.List()
if err != nil {
return r, errors.Trace(err)
}

for _, result := range results {
pl := result.Payload
id, err := uf.State.LookUp(pl.Name, pl.ID)
id, err := uf.backend.LookUp(pl.Name, pl.ID)
if err != nil {
logger.Errorf("failed to look up ID for %q: %v", pl.FullID(), err)
id = ""
Expand All @@ -124,42 +150,42 @@ func (uf UnitFacade) listAll() (internal.PayloadResults, error) {
}

// LookUp identifies the payload with the provided name and raw ID.
func (uf UnitFacade) LookUp(args internal.LookUpArgs) (internal.PayloadResults, error) {
var r internal.PayloadResults
func (uf UnitFacade) LookUp(args params.LookUpPayloadArgs) (params.PayloadResults, error) {
var r params.PayloadResults
for _, arg := range args.Args {
id, err := uf.State.LookUp(arg.Name, arg.ID)
id, err := uf.backend.LookUp(arg.Name, arg.ID)
res := internal.NewPayloadResult(id, err)
r.Results = append(r.Results, res)
}
return r, nil
}

// SetStatus sets the raw status of a payload.
func (uf UnitFacade) SetStatus(args internal.SetStatusArgs) (internal.PayloadResults, error) {
var r internal.PayloadResults
func (uf UnitFacade) SetStatus(args params.SetPayloadStatusArgs) (params.PayloadResults, error) {
var r params.PayloadResults
for _, arg := range args.Args {
id, err := internal.API2ID(arg.Tag)
if err != nil {
return r, errors.Trace(err)
}

err = uf.State.SetStatus(id, arg.Status)
err = uf.backend.SetStatus(id, arg.Status)
res := internal.NewPayloadResult(id, err)
r.Results = append(r.Results, res)
}
return r, nil
}

// Untrack marks the identified payload as no longer being tracked.
func (uf UnitFacade) Untrack(args params.Entities) (internal.PayloadResults, error) {
var r internal.PayloadResults
func (uf UnitFacade) Untrack(args params.Entities) (params.PayloadResults, error) {
var r params.PayloadResults
for _, entity := range args.Entities {
id, err := internal.API2ID(entity.Tag)
if err != nil {
return r, errors.Trace(err)
}

err = uf.State.Untrack(id)
err = uf.backend.Untrack(id)
res := internal.NewPayloadResult(id, err)
r.Results = append(r.Results, res)
}
Expand Down
Loading

0 comments on commit 0a86abf

Please sign in to comment.