github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/apiserver/migrationminion/migrationminion.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationminion
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  
     9  	"github.com/juju/juju/apiserver/common"
    10  	"github.com/juju/juju/apiserver/params"
    11  	"github.com/juju/juju/state"
    12  )
    13  
    14  func init() {
    15  	common.RegisterStandardFacade("MigrationMinion", 1, NewAPI)
    16  }
    17  
    18  // API implements the API required for the model migration
    19  // master worker.
    20  type API struct {
    21  	backend    Backend
    22  	authorizer common.Authorizer
    23  	resources  *common.Resources
    24  }
    25  
    26  // NewAPI creates a new API server endpoint for the model migration
    27  // master worker.
    28  func NewAPI(
    29  	st *state.State,
    30  	resources *common.Resources,
    31  	authorizer common.Authorizer,
    32  ) (*API, error) {
    33  	if !(authorizer.AuthMachineAgent() || authorizer.AuthUnitAgent()) {
    34  		return nil, common.ErrPerm
    35  	}
    36  	return &API{
    37  		backend:    getBackend(st),
    38  		authorizer: authorizer,
    39  		resources:  resources,
    40  	}, nil
    41  }
    42  
    43  // Watch starts watching for status updates for a migration attempt
    44  // for the model. It will report when a migration starts and when its
    45  // status changes (including when it finishes). An initial event will
    46  // be fired if there has ever been a migration attempt for the model.
    47  //
    48  // The MigrationStatusWatcher facade must be used to receive events
    49  // from the watcher.
    50  func (api *API) Watch() (params.NotifyWatchResult, error) {
    51  	w, err := api.backend.WatchMigrationStatus()
    52  	if err != nil {
    53  		return params.NotifyWatchResult{}, errors.Trace(err)
    54  	}
    55  	return params.NotifyWatchResult{
    56  		NotifyWatcherId: api.resources.Register(w),
    57  	}, nil
    58  }