-
Notifications
You must be signed in to change notification settings - Fork 494
/
model.go
113 lines (90 loc) · 2.78 KB
/
model.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
// Copyright 2018 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package model
import (
"fmt"
"github.com/juju/errors"
"github.com/juju/version/v2"
"github.com/juju/juju/core/credential"
"github.com/juju/juju/core/life"
"github.com/juju/juju/core/user"
"github.com/juju/juju/internal/uuid"
)
// ModelType indicates a model type.
type ModelType string
const (
// IAAS is the type for IAAS models.
IAAS ModelType = "iaas"
// CAAS is the type for CAAS models.
CAAS ModelType = "caas"
)
// String returns m as a string.
func (m ModelType) String() string {
return string(m)
}
// IsValid returns true if the value of Type is a known valid type.
// Currently supported values are:
// - CAAS
// - IAAS
func (m ModelType) IsValid() bool {
switch m {
case CAAS, IAAS:
return true
}
return false
}
// Model represents the state of a model.
type Model struct {
// Name returns the human friendly name of the model.
Name string
// Life is the current state of the model.
// Options are alive, dying, dead. Every model starts as alive, only
// during the destruction of the model it transitions to dying and then
// dead.
Life life.Value
// UUID is the universally unique identifier of the model.
UUID UUID
// ModelType is the type of model.
ModelType ModelType
// AgentVersion is the target version for agents running under this model.
AgentVersion version.Number
// Cloud is the name of the cloud to associate with the model.
// Must not be empty for a valid struct.
Cloud string
// CloudRegion is the region that the model will use in the cloud.
CloudRegion string
// Credential is the id attributes for the credential to be associated with
// model. Credential must be for the same cloud as that of the model.
// Credential can be the zero value of the struct to not have a credential
// associated with the model.
Credential credential.Key
// Owner is the uuid of the user that owns this model in the Juju controller.
Owner user.UUID
// OwnerName is the name of the owner in the Juju controller.
OwnerName string
}
// UUID represents a model unique identifier.
type UUID string
// NewUUID is a convince function for generating a new model uuid.
func NewUUID() (UUID, error) {
uuid, err := uuid.NewUUID()
if err != nil {
return UUID(""), err
}
return UUID(uuid.String()), nil
}
// String implements the stringer interface for UUID.
func (u UUID) String() string {
return string(u)
}
// Validate ensures the consistency of the UUID. If the uuid is invalid an error
// satisfying [errors.NotValid] will be returned.
func (u UUID) Validate() error {
if u == "" {
return fmt.Errorf("%wuuid cannot be empty", errors.Hide(errors.NotValid))
}
if !uuid.IsValidUUIDString(string(u)) {
return fmt.Errorf("uuid %q %w", u, errors.NotValid)
}
return nil
}