/
unit.go
117 lines (106 loc) · 3.25 KB
/
unit.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
114
115
116
117
package project
import (
"fmt"
"sync"
"github.com/apex/log"
)
// Unit interface for unit drivers.
type Unit interface {
Name() string
Stack() *Stack
Project() *Project
Backend() Backend
Prepare() error // Prepare scan all markers in unit, and build project unit links, and unit dependencies.
Dependencies() *UnitLinksT
Build() error
Init() error
Apply() error
Plan() error
Destroy() error
Key() string
GetState() Unit
GetDiffData() interface{}
GetStateDiffData() interface{}
LoadState(interface{}, string, *StateProject) error
KindKey() string
CodeDir() string
UpdateProjectRuntimeData(p *Project) error
WasApplied() bool
ForceApply() bool
Mux() *sync.Mutex
IsTainted() bool
SetTainted(newValue bool, err error)
SetExecStatus(ExecutionStatus)
GetExecStatus() ExecutionStatus
ExecError() error
}
type UnitDriver interface {
AddTemplateFunctions(projectPtr *Project) error
GetScanners() []MarkerScanner
}
type UnitFactory interface {
New(map[string]interface{}, *Stack) (Unit, error)
NewFromState(map[string]interface{}, string, *StateProject) (Unit, error)
}
func RegisterUnitFactory(f UnitFactory, modType string) error {
if _, exists := UnitFactoriesMap[modType]; exists {
return fmt.Errorf("unit driver with provider name '%v' already exists", modType)
}
UnitFactoriesMap[modType] = f
return nil
}
var UnitFactoriesMap = map[string]UnitFactory{}
// NewUnit creates and return unit with needed driver.
func NewUnit(spec map[string]interface{}, stack *Stack) (Unit, error) {
mType, ok := spec["type"].(string)
if !ok {
return nil, fmt.Errorf("incorrect unit type")
}
uName, ok := spec["name"].(string)
if !ok {
return nil, fmt.Errorf("incorrect unit name")
}
modDrv, exists := UnitFactoriesMap[mType]
// TODO remove deprecated unit type 'kubernetes'
// if mType == "kubernetes" {
// log.Warnf("Unit: '%v'. Unit type 'kubernetes' is deprecated and will be removed in future releases. Use 'k8s-manifest' instead", fmt.Sprintf("%v.%v", stack.Name, uName))
// }
if !exists {
// TODO remove deprecated unit type 'terraform'
if mType == "terraform" {
log.Warnf("Unit: '%v'. Unit type 'terraform' is deprecated and will be removed in future releases. Use 'tfmodule' instead", fmt.Sprintf("%v.%v", stack.Name, uName))
modDrv = UnitFactoriesMap["tfmodule"]
} else {
return nil, fmt.Errorf("new unit: bad unit type in state '%v'", mType)
}
}
return modDrv.New(spec, stack)
}
// NewUnitFromState creates unit from saved state.
func NewUnitFromState(state map[string]interface{}, name string, p *StateProject) (Unit, error) {
mType, ok := state["type"].(string)
if !ok {
return nil, fmt.Errorf("internal error: unit type field in state does not found")
}
modDrv, exists := UnitFactoriesMap[mType]
if !exists {
// TODO remove deprecated unit type 'terraform'
if mType == "terraform" {
modDrv = UnitFactoriesMap["tfmodule"]
} else {
return nil, fmt.Errorf("internal error: bad unit type in state '%v'", mType)
}
}
// stateUnit, err := modDrv.NewFromState(state, name, p)
// if err != nil {
// return nil, err
// }
// curUnit := p.LoaderProjectPtr.Units[stateUnit.Key()]
// if curUnit != nil {
// curUnit.SetTainted()
// }
return modDrv.NewFromState(state, name, p)
}
type UnitState interface {
GetType() string
}