-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patharchitecture.go
142 lines (127 loc) · 3.89 KB
/
architecture.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
package alzlib
import (
"fmt"
"slices"
"github.com/Azure/alzlib/internal/processor"
mapset "github.com/deckarep/golang-set/v2"
)
// Architecture represents an Azure architecture that has not been deployed.
// Do not create this struct directly, use NewArchitecture instead.
type Architecture struct {
name string
mgs map[string]*ArchitectureManagementGroup
alzlib *AlzLib
}
// NewArchitecture creates a new Architecture with the given name and AlzLib.
func NewArchitecture(name string, az *AlzLib) *Architecture {
return &Architecture{
name: name,
mgs: make(map[string]*ArchitectureManagementGroup),
alzlib: az,
}
}
// Name returns the name of the architecture.
func (a *Architecture) Name() string {
return a.name
}
// RootMgs returns the top level management groups of the architecture.
func (a *Architecture) RootMgs() (res []*ArchitectureManagementGroup) {
for _, mg := range a.mgs {
if mg.parent != nil {
continue
}
res = append(res, mg)
}
slices.SortFunc(res, func(a, b *ArchitectureManagementGroup) int {
if a.id < b.id {
return -1
}
if a.id > b.id {
return 1
}
return 0
})
return res
}
// ArchitectureManagementGroup represents a management group in an undeployed architecture.
type ArchitectureManagementGroup struct {
id string
displayName string
children mapset.Set[*ArchitectureManagementGroup]
parent *ArchitectureManagementGroup
exists bool
archetypes mapset.Set[*Archetype]
architecture *Architecture
}
func newArchitectureManagementGroup(id, displayName string, exists bool, arch *Architecture) *ArchitectureManagementGroup {
return &ArchitectureManagementGroup{
id: id,
displayName: displayName,
children: mapset.NewThreadUnsafeSet[*ArchitectureManagementGroup](),
exists: exists,
archetypes: mapset.NewThreadUnsafeSet[*Archetype](),
architecture: arch,
}
}
// Archetypes returns the archetypes assigned to the management group.
func (mg *ArchitectureManagementGroup) Archetypes() (res []*Archetype) {
for arch := range mg.archetypes.Iter() {
res = append(res, arch.copy())
}
slices.SortFunc(res, func(a, b *Archetype) int {
if a.name < b.name {
return -1
}
if a.name > b.name {
return 1
}
return 0
})
return
}
// Children returns the child management groups of the management group.
func (mg *ArchitectureManagementGroup) Children() (res []*ArchitectureManagementGroup) {
for child := range mg.children.Iter() {
res = append(res, child)
}
return res
}
// DisplayName returns the display name of the management group.
func (mg *ArchitectureManagementGroup) DisplayName() string {
return mg.displayName
}
// Id returns the id of the management group.
func (mg *ArchitectureManagementGroup) Id() string {
return mg.id
}
// Exists returns the exists value.
func (mg *ArchitectureManagementGroup) Exists() bool {
return mg.exists
}
func (a *Architecture) addMgFromProcessor(libMg processor.LibArchitectureManagementGroup, az *AlzLib) error {
if _, ok := a.mgs[libMg.Id]; ok {
return fmt.Errorf("Architecture.addMg: management group %s already exists", libMg.Id)
}
mg := newArchitectureManagementGroup(libMg.Id, libMg.DisplayName, libMg.Exists, a)
// check parent exists and create parent-child relationship
if libMg.ParentId != nil {
parent, ok := a.mgs[*libMg.ParentId]
if !ok {
return fmt.Errorf("Architecture.addMg: parent management group does not exist %s", *libMg.ParentId)
}
mg.parent = parent
mg.parent.children.Add(mg)
}
mg.archetypes = mapset.NewThreadUnsafeSet[*Archetype]()
for archName := range libMg.Archetypes.Iter() {
arch, ok := az.archetypes[archName]
if !ok {
return fmt.Errorf("Architecture.addMg: archetype not found adding archetype `%s` to management group `%s`", archName, libMg.Id)
}
mg.archetypes.Add(arch)
}
a.mgs[mg.id] = mg
return nil
}