1
// Copyright 2016 Canonical Ltd.
2
// Licensed under the AGPLv3, see LICENCE file for details.
4
package migrationtarget
7
"github.com/juju/errors"
8
"gopkg.in/juju/names.v2"
10
"github.com/juju/juju/apiserver/common"
11
"github.com/juju/juju/apiserver/facade"
12
"github.com/juju/juju/apiserver/params"
13
"github.com/juju/juju/migration"
14
"github.com/juju/juju/state"
18
common.RegisterStandardFacade("MigrationTarget", 1, NewAPI)
21
// API implements the API required for the model migration
22
// master worker when communicating with the target controller.
25
authorizer facade.Authorizer
26
resources facade.Resources
29
// NewAPI returns a new API.
32
resources facade.Resources,
33
authorizer facade.Authorizer,
35
if err := checkAuth(authorizer, st); err != nil {
36
return nil, errors.Trace(err)
40
authorizer: authorizer,
45
func checkAuth(authorizer facade.Authorizer, st *state.State) error {
46
if !authorizer.AuthClient() {
47
return errors.Trace(common.ErrPerm)
50
// Type assertion is fine because AuthClient is true.
51
apiUser := authorizer.GetAuthTag().(names.UserTag)
52
if isAdmin, err := st.IsControllerAdministrator(apiUser); err != nil {
53
return errors.Trace(err)
55
// The entire facade is only accessible to controller administrators.
56
return errors.Trace(common.ErrPerm)
61
// Import takes a serialized Juju model, deserializes it, and
62
// recreates it in the receiving controller.
63
func (api *API) Import(serialized params.SerializedModel) error {
64
_, st, err := migration.ImportModel(api.state, serialized.Bytes)
69
// TODO(mjs) - post import checks
73
func (api *API) getModel(args params.ModelArgs) (*state.Model, error) {
74
tag, err := names.ParseModelTag(args.ModelTag)
76
return nil, errors.Trace(err)
78
model, err := api.state.GetModel(tag)
80
return nil, errors.Trace(err)
82
if model.MigrationMode() != state.MigrationModeImporting {
83
return nil, errors.New("migration mode for the model is not importing")
88
// Abort removes the specified model from the database. It is an error to
89
// attempt to Abort a model that has a migration mode other than importing.
90
func (api *API) Abort(args params.ModelArgs) error {
91
model, err := api.getModel(args)
93
return errors.Trace(err)
96
st, err := api.state.ForModel(model.ModelTag())
98
return errors.Trace(err)
102
return st.RemoveImportingModelDocs()
105
// Activate sets the migration mode of the model to "active". It is an error to
106
// attempt to Abort a model that has a migration mode other than importing.
107
func (api *API) Activate(args params.ModelArgs) error {
108
model, err := api.getModel(args)
110
return errors.Trace(err)
113
return model.SetMigrationMode(state.MigrationModeActive)