github.com/niedbalski/juju@v0.0.0-20190215020005-8ff100488e47/apiserver/facades/agent/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/facade"
    11  	"github.com/juju/juju/apiserver/params"
    12  	"github.com/juju/juju/core/migration"
    13  )
    14  
    15  // API implements the API required for the model migration
    16  // master worker.
    17  type API struct {
    18  	backend    Backend
    19  	authorizer facade.Authorizer
    20  	resources  facade.Resources
    21  }
    22  
    23  // NewAPI creates a new API server endpoint for the model migration
    24  // master worker.
    25  func NewAPI(
    26  	backend Backend,
    27  	resources facade.Resources,
    28  	authorizer facade.Authorizer,
    29  ) (*API, error) {
    30  	if !(authorizer.AuthMachineAgent() || authorizer.AuthUnitAgent() || authorizer.AuthApplicationAgent()) {
    31  		return nil, common.ErrPerm
    32  	}
    33  	return &API{
    34  		backend:    backend,
    35  		authorizer: authorizer,
    36  		resources:  resources,
    37  	}, nil
    38  }
    39  
    40  // Watch starts watching for status updates for a migration attempt
    41  // for the model. It will report when a migration starts and when its
    42  // status changes (including when it finishes). An initial event will
    43  // be fired if there has ever been a migration attempt for the model.
    44  //
    45  // The MigrationStatusWatcher facade must be used to receive events
    46  // from the watcher.
    47  func (api *API) Watch() (params.NotifyWatchResult, error) {
    48  	w := api.backend.WatchMigrationStatus()
    49  	return params.NotifyWatchResult{
    50  		NotifyWatcherId: api.resources.Register(w),
    51  	}, nil
    52  }
    53  
    54  // Report allows a migration minion to submit whether it succeeded or
    55  // failed for a specific migration phase.
    56  func (api *API) Report(info params.MinionReport) error {
    57  	phase, ok := migration.ParsePhase(info.Phase)
    58  	if !ok {
    59  		return errors.New("unable to parse phase")
    60  	}
    61  
    62  	mig, err := api.backend.Migration(info.MigrationId)
    63  	if err != nil {
    64  		return errors.Trace(err)
    65  	}
    66  
    67  	err = mig.SubmitMinionReport(api.authorizer.GetAuthTag(), phase, info.Success)
    68  	return errors.Trace(err)
    69  }