~nskaggs/+junk/xenial-test

« back to all changes in this revision

Viewing changes to src/github.com/juju/juju/apiserver/utils.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 2013 Canonical Ltd.
 
2
// Licensed under the AGPLv3, see LICENCE file for details.
 
3
 
 
4
package apiserver
 
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/state"
 
12
)
 
13
 
 
14
// isMachineWithJob returns whether the given entity is a machine that
 
15
// is configured to run the given job.
 
16
func isMachineWithJob(e state.Entity, j state.MachineJob) bool {
 
17
        m, ok := e.(*state.Machine)
 
18
        if !ok {
 
19
                return false
 
20
        }
 
21
        for _, mj := range m.Jobs() {
 
22
                if mj == j {
 
23
                        return true
 
24
                }
 
25
        }
 
26
        return false
 
27
}
 
28
 
 
29
type validateArgs struct {
 
30
        statePool *state.StatePool
 
31
        modelUUID string
 
32
        // strict validation does not allow empty UUID values
 
33
        strict bool
 
34
        // controllerModelOnly only validates the controller model
 
35
        controllerModelOnly bool
 
36
}
 
37
 
 
38
// validateModelUUID is the common validator for the various
 
39
// apiserver components that need to check for a valid model
 
40
// UUID.  An empty modelUUID means that the connection has come in at
 
41
// the root of the URL space and refers to the controller
 
42
// model.
 
43
//
 
44
// It returns the validated model UUID.
 
45
func validateModelUUID(args validateArgs) (string, error) {
 
46
        ssState := args.statePool.SystemState()
 
47
        if args.modelUUID == "" {
 
48
                // We allow the modelUUID to be empty so that:
 
49
                //    TODO: server a limited API at the root (empty modelUUID)
 
50
                //    just the user manager and model manager are able to accept
 
51
                //    requests that don't require a modelUUID, like add-model.
 
52
                if args.strict {
 
53
                        return "", errors.Trace(common.UnknownModelError(args.modelUUID))
 
54
                }
 
55
                logger.Debugf("validate model uuid: empty modelUUID")
 
56
                return ssState.ModelUUID(), nil
 
57
        }
 
58
        if args.modelUUID == ssState.ModelUUID() {
 
59
                logger.Debugf("validate model uuid: controller model - %s", args.modelUUID)
 
60
                return args.modelUUID, nil
 
61
        }
 
62
        if args.controllerModelOnly {
 
63
                return "", errors.Unauthorizedf("requested model %q is not the controller model", args.modelUUID)
 
64
        }
 
65
        if !names.IsValidModel(args.modelUUID) {
 
66
                return "", errors.Trace(common.UnknownModelError(args.modelUUID))
 
67
        }
 
68
        modelTag := names.NewModelTag(args.modelUUID)
 
69
        if _, err := ssState.GetModel(modelTag); err != nil {
 
70
                return "", errors.Wrap(err, common.UnknownModelError(args.modelUUID))
 
71
        }
 
72
        logger.Debugf("validate model uuid: %s", args.modelUUID)
 
73
        return args.modelUUID, nil
 
74
}