~juju-qa/ubuntu/xenial/juju/xenial-2.0-beta3

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/migrationmaster/migrationmaster.go

  • Committer: Martin Packman
  • Date: 2016-03-30 19:31:08 UTC
  • mfrom: (1.1.41)
  • Revision ID: martin.packman@canonical.com-20160330193108-h9iz3ak334uk0z5r
Merge new upstream source 2.0~beta3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
9
        "github.com/juju/juju/apiserver/common"
 
10
        "github.com/juju/juju/apiserver/params"
 
11
        coremigration "github.com/juju/juju/core/migration"
 
12
        "github.com/juju/juju/migration"
 
13
        "github.com/juju/juju/state"
 
14
)
 
15
 
 
16
func init() {
 
17
        common.RegisterStandardFacade("MigrationMaster", 1, NewAPI)
 
18
}
 
19
 
 
20
// API implements the API required for the model migration
 
21
// master worker.
 
22
type API struct {
 
23
        backend    Backend
 
24
        authorizer common.Authorizer
 
25
        resources  *common.Resources
 
26
}
 
27
 
 
28
// NewAPI creates a new API server endpoint for the model migration
 
29
// master worker.
 
30
func NewAPI(
 
31
        st *state.State,
 
32
        resources *common.Resources,
 
33
        authorizer common.Authorizer,
 
34
) (*API, error) {
 
35
        if !authorizer.AuthModelManager() {
 
36
                return nil, common.ErrPerm
 
37
        }
 
38
        return &API{
 
39
                backend:    getBackend(st),
 
40
                authorizer: authorizer,
 
41
                resources:  resources,
 
42
        }, nil
 
43
}
 
44
 
 
45
// Watch starts watching for an active migration for the model
 
46
// associated with the API connection. The returned id should be used
 
47
// with Next on the MigrationMasterWatcher endpoint to receive deltas.
 
48
func (api *API) Watch() (params.NotifyWatchResult, error) {
 
49
        w, err := api.backend.WatchForModelMigration()
 
50
        if err != nil {
 
51
                return params.NotifyWatchResult{}, errors.Trace(err)
 
52
        }
 
53
        return params.NotifyWatchResult{
 
54
                NotifyWatcherId: api.resources.Register(w),
 
55
        }, nil
 
56
}
 
57
 
 
58
// SetPhase sets the phase of the active model migration. The provided
 
59
// phase must be a valid phase value, for example QUIESCE" or
 
60
// "ABORT". See the core/migration package for the complete list.
 
61
func (api *API) SetPhase(args params.SetMigrationPhaseArgs) error {
 
62
        mig, err := api.backend.GetModelMigration()
 
63
        if err != nil {
 
64
                return errors.Annotate(err, "could not get migration")
 
65
        }
 
66
 
 
67
        phase, ok := coremigration.ParsePhase(args.Phase)
 
68
        if !ok {
 
69
                return errors.Errorf("invalid phase: %q", args.Phase)
 
70
        }
 
71
 
 
72
        err = mig.SetPhase(phase)
 
73
        return errors.Annotate(err, "failed to set phase")
 
74
}
 
75
 
 
76
var exportModel = migration.ExportModel
 
77
 
 
78
// Export serializes the model associated with the API connection.
 
79
func (api *API) Export() (params.SerializedModel, error) {
 
80
        var serialized params.SerializedModel
 
81
 
 
82
        bytes, err := exportModel(api.backend)
 
83
        if err != nil {
 
84
                return serialized, err
 
85
        }
 
86
 
 
87
        serialized.Bytes = bytes
 
88
        return serialized, nil
 
89
}