-
Notifications
You must be signed in to change notification settings - Fork 10
/
tenant.go
202 lines (173 loc) · 6.49 KB
/
tenant.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package shared
import (
"errors"
"fmt"
"github.com/OctopusDeploy/cli/pkg/cmd"
sharedTenants "github.com/OctopusDeploy/cli/pkg/cmd/tenant/shared"
"github.com/OctopusDeploy/cli/pkg/question"
"github.com/OctopusDeploy/cli/pkg/question/selectors"
"github.com/OctopusDeploy/cli/pkg/util"
"github.com/OctopusDeploy/cli/pkg/util/flag"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/client"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/core"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/machines"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tagsets"
"github.com/OctopusDeploy/go-octopusdeploy/v2/pkg/tenants"
"github.com/spf13/cobra"
"sort"
"strings"
)
const (
FlagTenantedDeployment = "tenanted-mode"
FlagTenant = "tenant"
FlagTenantTag = "tenant-tag"
Untenanted = "Untenanted"
Tenanted = "Tenanted"
TenantedOrUntenanted = "TenantedOrUntenanted"
)
type GetAllTagsCallback func() ([]*tagsets.Tag, error)
type CreateTargetTenantFlags struct {
TenantedDeploymentMode *flag.Flag[string]
Tenants *flag.Flag[[]string]
TenantTags *flag.Flag[[]string]
}
type CreateTargetTenantOptions struct {
*cmd.Dependencies
GetAllTagsCallback
sharedTenants.GetAllTenantsCallback
}
func NewCreateTargetTenantFlags() *CreateTargetTenantFlags {
return &CreateTargetTenantFlags{
TenantedDeploymentMode: flag.New[string](FlagTenantedDeployment, false),
Tenants: flag.New[[]string](FlagTenant, false),
TenantTags: flag.New[[]string](FlagTenantTag, false),
}
}
func NewCreateTargetTenantOptions(dependencies *cmd.Dependencies) *CreateTargetTenantOptions {
return &CreateTargetTenantOptions{
Dependencies: dependencies,
GetAllTenantsCallback: func() ([]*tenants.Tenant, error) {
return sharedTenants.GetAllTenants(dependencies.Client)
},
GetAllTagsCallback: func() ([]*tagsets.Tag, error) {
return getAllTags(dependencies.Client)
},
}
}
func RegisterCreateTargetTenantFlags(cmd *cobra.Command, flags *CreateTargetTenantFlags) {
cmd.Flags().StringVar(&flags.TenantedDeploymentMode.Value, FlagTenantedDeployment, "", "\nChoose the kind of deployments where this deployment target should be included. Default is 'untenanted'")
cmd.Flags().StringSliceVar(&flags.Tenants.Value, FlagTenant, []string{}, "Associate the deployment target with tenants")
cmd.Flags().StringSliceVar(&flags.TenantTags.Value, FlagTenantTag, []string{}, "Associate the deployment target with tenant tags, should be in the format 'tag set name/tag name'")
}
func PromptForTenant(opts *CreateTargetTenantOptions, flags *CreateTargetTenantFlags) error {
if flags.TenantedDeploymentMode.Value == "" {
selectedOption, err := selectors.SelectOptions(opts.Ask, "Choose the kind of deployments where this deployment target should be included", getTenantDeploymentOptions)
if err != nil {
return err
}
flags.TenantedDeploymentMode.Value = selectedOption.Value
}
if isTenantedTarget(flags) && (util.Empty(flags.Tenants.Value) || util.Empty(flags.TenantTags.Value)) {
allTags, err := opts.GetAllTagsCallback()
if err != nil {
return err
}
allTenants, err := opts.GetAllTenantsCallback()
if err != nil {
return err
}
tenantsLookup := make(map[string]bool, len(allTenants))
tagsLookup := make(map[string]bool, len(allTags))
var combinedList []string
for _, tenant := range allTenants {
combinedList = append(combinedList, tenant.Name)
tenantsLookup[tenant.Name] = true
}
sort.Strings(combinedList)
canonicalTags := []string{}
for _, tag := range allTags {
canonicalTags = append(canonicalTags, tag.CanonicalTagName)
tagsLookup[tag.CanonicalTagName] = true
}
sort.Strings(canonicalTags)
combinedList = append(combinedList, canonicalTags...)
var selectedTenantsAndTags []string
switch flags.TenantedDeploymentMode.Value {
case "Tenanted":
selectedTenantsAndTags, err = question.MultiSelectMap(
opts.Ask,
"Select tenants this deployment target should be associated with",
combinedList,
func(item string) string { return item }, true)
case "TenantedOrUntenanted":
selectedTenantsAndTags, err = question.MultiSelectMap(
opts.Ask,
"Select tenants this deployment target should be associated with",
combinedList,
func(item string) string { return item }, false)
}
if err != nil {
return err
}
for _, selection := range selectedTenantsAndTags {
if tenantsLookup[selection] {
flags.Tenants.Value = append(flags.Tenants.Value, selection)
} else if tagsLookup[selection] {
flags.TenantTags.Value = append(flags.TenantTags.Value, selection)
} else {
return errors.New(fmt.Sprintf("unknown selection %s", selection))
}
}
}
return nil
}
func isTenantedTarget(flags *CreateTargetTenantFlags) bool {
return flags.TenantedDeploymentMode.Value == Tenanted || flags.TenantedDeploymentMode.Value == TenantedOrUntenanted
}
func getTenantDeploymentOptions() []*selectors.SelectOption[string] {
return []*selectors.SelectOption[string]{
{Display: "Exclude from tenanted deployments (default)", Value: Untenanted},
{Display: "Include only in tenanted deployments", Value: Tenanted},
{Display: "Include in both tenanted and untenanted deployments", Value: TenantedOrUntenanted},
}
}
func getAllTags(client *client.Client) ([]*tagsets.Tag, error) {
tagSets, err := client.TagSets.GetAll()
if err != nil {
return nil, err
}
tags := []*tagsets.Tag{}
for _, tagSet := range tagSets {
tags = append(tags, tagSet.Tags...)
}
return tags, nil
}
func ConfigureTenant(target *machines.DeploymentTarget, flags *CreateTargetTenantFlags, opts *CreateTargetTenantOptions) error {
target.TenantedDeploymentMode = core.TenantedDeploymentMode(flags.TenantedDeploymentMode.Value)
target.TenantTags = flags.TenantTags.Value
allTenants, err := opts.GetAllTenantsCallback()
if err != nil {
return err
}
nameLookup := make(map[string]*tenants.Tenant, len(allTenants))
idLookup := make(map[string]*tenants.Tenant, len(allTenants))
for _, tenant := range allTenants {
nameLookup[strings.ToLower(tenant.Name)] = tenant
idLookup[strings.ToLower(tenant.GetID())] = tenant
}
for _, tenantNameOrId := range flags.Tenants.Value {
nameOrId := strings.ToLower(tenantNameOrId)
t := nameLookup[nameOrId]
if t != nil {
target.TenantIDs = append(target.TenantIDs, t.GetID())
} else {
t = idLookup[nameOrId]
if t != nil {
target.TenantIDs = append(target.TenantIDs, t.GetID())
} else {
return fmt.Errorf("Cannot find tenant '%s'", tenantNameOrId)
}
}
}
return nil
}