github.com/makyo/juju@v0.0.0-20160425123129-2608902037e9/api/migrationmaster/client.go (about)

     1  // Copyright 2016 Canonical Ltd.
     2  // Licensed under the AGPLv3, see LICENCE file for details.
     3  
     4  package migrationmaster
     5  
     6  import (
     7  	"github.com/juju/errors"
     8  	"github.com/juju/names"
     9  
    10  	"github.com/juju/juju/api/base"
    11  	apiwatcher "github.com/juju/juju/api/watcher"
    12  	"github.com/juju/juju/apiserver/params"
    13  	"github.com/juju/juju/core/migration"
    14  	"github.com/juju/juju/watcher"
    15  )
    16  
    17  // Client describes the client side API for the MigrationMaster facade
    18  // (used by the migration master worker).
    19  type Client interface {
    20  	// Watch returns a watcher which reports when a migration is
    21  	// active for the model associated with the API connection.
    22  	Watch() (watcher.NotifyWatcher, error)
    23  
    24  	// GetMigrationStatus returns the details and progress of the
    25  	// latest model migration.
    26  	GetMigrationStatus() (MigrationStatus, error)
    27  
    28  	// SetPhase updates the phase of the currently active model
    29  	// migration.
    30  	SetPhase(migration.Phase) error
    31  
    32  	// Export returns a serialized representation of the model
    33  	// associated with the API connection.
    34  	Export() ([]byte, error)
    35  }
    36  
    37  // MigrationStatus returns the details for a migration as needed by
    38  // the migration master worker.
    39  type MigrationStatus struct {
    40  	ModelUUID  string
    41  	Attempt    int
    42  	Phase      migration.Phase
    43  	TargetInfo migration.TargetInfo
    44  }
    45  
    46  // NewClient returns a new Client based on an existing API connection.
    47  func NewClient(caller base.APICaller) Client {
    48  	return &client{base.NewFacadeCaller(caller, "MigrationMaster")}
    49  }
    50  
    51  // client implements Client.
    52  type client struct {
    53  	caller base.FacadeCaller
    54  }
    55  
    56  // Watch implements Client.
    57  func (c *client) Watch() (watcher.NotifyWatcher, error) {
    58  	var result params.NotifyWatchResult
    59  	err := c.caller.FacadeCall("Watch", nil, &result)
    60  	if err != nil {
    61  		return nil, errors.Trace(err)
    62  	}
    63  	if result.Error != nil {
    64  		return nil, result.Error
    65  	}
    66  	w := apiwatcher.NewNotifyWatcher(c.caller.RawAPICaller(), result)
    67  	return w, nil
    68  }
    69  
    70  // GetMigrationStatus implements Client.
    71  func (c *client) GetMigrationStatus() (MigrationStatus, error) {
    72  	var empty MigrationStatus
    73  	var status params.FullMigrationStatus
    74  	err := c.caller.FacadeCall("GetMigrationStatus", nil, &status)
    75  	if err != nil {
    76  		return empty, errors.Trace(err)
    77  	}
    78  
    79  	modelTag, err := names.ParseModelTag(status.Spec.ModelTag)
    80  	if err != nil {
    81  		return empty, errors.Annotatef(err, "parsing model tag")
    82  	}
    83  
    84  	phase, ok := migration.ParsePhase(status.Phase)
    85  	if !ok {
    86  		return empty, errors.New("unable to parse phase")
    87  	}
    88  
    89  	target := status.Spec.TargetInfo
    90  	controllerTag, err := names.ParseModelTag(target.ControllerTag)
    91  	if err != nil {
    92  		return empty, errors.Annotatef(err, "parsing controller tag")
    93  	}
    94  
    95  	authTag, err := names.ParseUserTag(target.AuthTag)
    96  	if err != nil {
    97  		return empty, errors.Annotatef(err, "unable to parse auth tag")
    98  	}
    99  
   100  	return MigrationStatus{
   101  		ModelUUID: modelTag.Id(),
   102  		Attempt:   status.Attempt,
   103  		Phase:     phase,
   104  		TargetInfo: migration.TargetInfo{
   105  			ControllerTag: controllerTag,
   106  			Addrs:         target.Addrs,
   107  			CACert:        target.CACert,
   108  			AuthTag:       authTag,
   109  			Password:      target.Password,
   110  		},
   111  	}, nil
   112  }
   113  
   114  // SetPhase implements Client.
   115  func (c *client) SetPhase(phase migration.Phase) error {
   116  	args := params.SetMigrationPhaseArgs{
   117  		Phase: phase.String(),
   118  	}
   119  	return c.caller.FacadeCall("SetPhase", args, nil)
   120  }
   121  
   122  // Export implements Client.
   123  func (c *client) Export() ([]byte, error) {
   124  	var serialized params.SerializedModel
   125  	err := c.caller.FacadeCall("Export", nil, &serialized)
   126  	if err != nil {
   127  		return nil, err
   128  	}
   129  	return serialized.Bytes, nil
   130  }