~nskaggs/+junk/xenial-test

« back to all changes in this revision

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

  • Committer: Nicholas Skaggs
  • Date: 2016-10-24 20:56:05 UTC
  • Revision ID: nicholas.skaggs@canonical.com-20161024205605-z8lta0uvuhtxwzwl
Initi with beta15

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 migrationtarget
 
5
 
 
6
import (
 
7
        "github.com/juju/errors"
 
8
        "gopkg.in/juju/names.v2"
 
9
 
 
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"
 
15
)
 
16
 
 
17
func init() {
 
18
        common.RegisterStandardFacade("MigrationTarget", 1, NewAPI)
 
19
}
 
20
 
 
21
// API implements the API required for the model migration
 
22
// master worker when communicating with the target controller.
 
23
type API struct {
 
24
        state      *state.State
 
25
        authorizer facade.Authorizer
 
26
        resources  facade.Resources
 
27
}
 
28
 
 
29
// NewAPI returns a new API.
 
30
func NewAPI(
 
31
        st *state.State,
 
32
        resources facade.Resources,
 
33
        authorizer facade.Authorizer,
 
34
) (*API, error) {
 
35
        if err := checkAuth(authorizer, st); err != nil {
 
36
                return nil, errors.Trace(err)
 
37
        }
 
38
        return &API{
 
39
                state:      st,
 
40
                authorizer: authorizer,
 
41
                resources:  resources,
 
42
        }, nil
 
43
}
 
44
 
 
45
func checkAuth(authorizer facade.Authorizer, st *state.State) error {
 
46
        if !authorizer.AuthClient() {
 
47
                return errors.Trace(common.ErrPerm)
 
48
        }
 
49
 
 
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)
 
54
        } else if !isAdmin {
 
55
                // The entire facade is only accessible to controller administrators.
 
56
                return errors.Trace(common.ErrPerm)
 
57
        }
 
58
        return nil
 
59
}
 
60
 
 
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)
 
65
        if err != nil {
 
66
                return err
 
67
        }
 
68
        defer st.Close()
 
69
        // TODO(mjs) - post import checks
 
70
        return err
 
71
}
 
72
 
 
73
func (api *API) getModel(args params.ModelArgs) (*state.Model, error) {
 
74
        tag, err := names.ParseModelTag(args.ModelTag)
 
75
        if err != nil {
 
76
                return nil, errors.Trace(err)
 
77
        }
 
78
        model, err := api.state.GetModel(tag)
 
79
        if err != nil {
 
80
                return nil, errors.Trace(err)
 
81
        }
 
82
        if model.MigrationMode() != state.MigrationModeImporting {
 
83
                return nil, errors.New("migration mode for the model is not importing")
 
84
        }
 
85
        return model, nil
 
86
}
 
87
 
 
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)
 
92
        if err != nil {
 
93
                return errors.Trace(err)
 
94
        }
 
95
 
 
96
        st, err := api.state.ForModel(model.ModelTag())
 
97
        if err != nil {
 
98
                return errors.Trace(err)
 
99
        }
 
100
        defer st.Close()
 
101
 
 
102
        return st.RemoveImportingModelDocs()
 
103
}
 
104
 
 
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)
 
109
        if err != nil {
 
110
                return errors.Trace(err)
 
111
        }
 
112
 
 
113
        return model.SetMigrationMode(state.MigrationModeActive)
 
114
}