-
Notifications
You must be signed in to change notification settings - Fork 83
Expand file tree
/
Copy pathInstanceStep.vue
More file actions
472 lines (445 loc) · 19.9 KB
/
InstanceStep.vue
File metadata and controls
472 lines (445 loc) · 19.9 KB
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
<template>
<section class="ff-instance-step text-center flex flex-col gap-4 pt-6" data-step="instance">
<h2>Setup Your Instance</h2>
<form class="max-w-2xl m-auto text-left flex flex-col gap-7">
<FeatureUnavailableToTeam v-if="teamRuntimeLimitReached" fullMessage="You have reached the runtime limit for this team." />
<FeatureUnavailableToTeam v-else-if="teamInstanceLimitReached" fullMessage="You have reached the instance limit for this team." />
<div class="ff-instance-name ff-input-wrapper flex flex-col gap-1">
<label class="mb-1">Name</label>
<div class="ff-input-wrapper flex gap-3 items-center relative mb-4">
<ff-text-input
v-model="input.name"
label="instance-name"
:error="errors.name"
data-el="instance-name"
:disabled="nameDisabled"
/>
<ff-button kind="secondary" @click="refreshName">
<template #icon>
<RefreshIcon />
</template>
</ff-button>
</div>
<div class="details ml-3 flex flex-col gap-3">
<span v-if="errors.name && input.name.length > 0" class="left-4 top-9 text-red-600 text-sm" data-el="instance-name-error">{{ errors.name }}</span>
<p v-if="hasValidName" class="flex gap-2 text-green-600 items-center">
<CheckCircleIcon class=" ff-icon-sm" />
<span>Your instance hostname will be "<i>{{ instanceName }}</i>".</span>
</p>
<p class="opacity-50 text-sm">
The instance name is used to access the editor, so it must be suitable for use in a URL. It is not currently possible to rename the instance after it has been created.
</p>
</div>
</div>
<transition name="fade" mode="out-in">
<Loading v-if="loading" size="small" />
<div v-else class="flex flex-col gap-4">
<div class="instance-types ff-input-wrapper flex flex-wrap items-stretch" data-group="instance-types">
<label class="w-full mb-2 block">Choose your Instance Type</label>
<template v-if="hasInstanceTypes && activeInstanceTypeCount > 0">
<InstanceCreditBanner :subscription="subscription" />
<ff-tile-selection v-model="input.instanceType" data-form="project-type">
<ff-tile-selection-option
v-for="(projType, index) in filteredProjectTypes"
:key="index"
:label="projType.name"
:description="projType.description"
:price="projType.price"
:price-interval="projType.priceInterval"
:value="projType.id"
:disabled="projType.disabled"
/>
</ff-tile-selection>
</template>
<template v-else-if="hasInstanceTypesAndAllAreDisabled">
<p class="text-center center my-5 w-full text-gray-500">
No instance types available at this moment.
</p>
</template>
<template v-else>
<p class="text-center center my-5 w-full text-gray-500">
No instance types available. Ask an Administrator to create a new instance type
</p>
</template>
</div>
<div
v-if="hasMultipleTemplates"
class="instance-templates ff-input-wrapper flex flex-wrap items-stretch"
data-group="templates"
>
<label class="mb-2 block w-full">Choose your Template</label>
<template v-if="hasTemplates">
<ff-tile-selection v-model="input.template" data-form="project-template">
<ff-tile-selection-option
v-for="(template, index) in instanceTemplates"
:key="index"
:label="template.name"
:description="template.description"
:value="template.id"
:disabled="template.disabled"
/>
</ff-tile-selection>
</template>
<template v-else>
<p class="text-center center my-5 w-full text-gray-500">
No templates available. Ask an Administrator to create a new template
</p>
</template>
</div>
<div class="node-red-version ff-input-wrapper flex flex-col gap-1">
<label class="mb-1">Node-RED Version</label>
<template v-if="hasNodeRedVersions">
<ff-listbox
v-model="input.nodeREDVersion"
data-el="node-red-listbox"
:options="nodeRedVersions"
:disabled="!input.instanceType || !input.template"
:placeholder="nodeRedVersionPlaceholder"
/>
</template>
<template v-else>
<p class="text-center center my-5 w-full text-gray-500">
No Node-RED Versions available for this instance type. Ask an Administrator to create a Node-RED Version stack definition
</p>
</template>
</div>
</div>
</transition>
<!-- Billing details -->
<div v-if="features.billing" class="my-5 text-left">
<InstanceChargesTable
:project-type="selectedInstanceType"
:subscription="subscription"
:trialMode="isTrialProjectSelected"
:prorationMode="team?.type?.properties?.billing?.proration"
/>
</div>
</form>
</section>
</template>
<script>
import { CheckCircleIcon, RefreshIcon } from '@heroicons/vue/outline'
import { mapState } from 'pinia'
import billingApi from '../../../../api/billing.js'
import instanceTypesApi from '../../../../api/instanceTypes.js'
import instancesApi from '../../../../api/instances.js'
import stacksApi from '../../../../api/stacks.js'
import templatesApi from '../../../../api/templates.js'
import {
useInstanceFormHelper
} from '../../../../composables/Components/multi-step-forms/instance/InstanceFormHelper.js'
import { getTeamProperty } from '../../../../composables/TeamProperties.js'
import InstanceChargesTable from '../../../../pages/instance/components/InstanceChargesTable.vue'
import InstanceCreditBanner from '../../../../pages/instance/components/InstanceCreditBanner.vue'
import FfListbox from '../../../../ui-components/components/form/ListBox.vue'
import FfTextInput from '../../../../ui-components/components/form/TextInput.vue'
import { debounce } from '../../../../utils/eventHandling.js'
import NameGenerator from '../../../../utils/name-generator/index.js'
import Loading from '../../../Loading.vue'
import FeatureUnavailableToTeam from '../../../banners/FeatureUnavailableToTeam.vue'
import { useAccountSettingsStore } from '@/stores/account-settings.js'
import { useContextStore } from '@/stores/context.js'
export default {
name: 'InstanceStep',
components: {
InstanceChargesTable,
FeatureUnavailableToTeam,
RefreshIcon,
CheckCircleIcon,
Loading,
InstanceCreditBanner,
FfListbox,
FfTextInput
},
props: {
slug: {
required: true,
type: String
},
state: {
required: false,
type: Object,
default: () => ({})
},
initialErrors: {
required: false,
type: Object,
default: () => ({})
}
},
emits: ['step-updated'],
setup (props) {
const { decorateInstanceTypes, teamRuntimeLimitReached } = useInstanceFormHelper()
return {
initialState: props.state,
decorateInstanceTypes,
teamRuntimeLimitReached
}
},
data () {
return {
input: {
name: this.initialState.name ?? NameGenerator(),
instanceType: this.initialState.instanceType ?? null,
nodeREDVersion: this.initialState.nodeREDVersion ?? null,
template: this.initialState.template ?? null
},
errors: {
name: this.initialErrors.name ?? null,
instanceType: this.initialErrors.instanceType ?? null,
nodeREDVersion: this.initialErrors.nodeREDVersion ?? null,
template: this.initialErrors.template ?? null
},
nodeRedVersions: [],
instanceTypes: [],
decoratedInstanceTypes: [],
instanceTemplates: [],
subscription: null,
loading: true,
nameDisabled: false
}
},
computed: {
...mapState(useAccountSettingsStore, ['features']),
...mapState(useContextStore, ['team']),
activeInstanceTypeCount () {
return this.decoratedInstanceTypes.filter(instance => !instance.disabled).length
},
filteredProjectTypes () {
return this.decoratedInstanceTypes.filter(instanceType => !instanceType.disabled)
},
hasInstanceTypes () {
return this.instanceTypes.length > 0
},
hasInstanceTypesAndAllAreDisabled () {
return this.hasInstanceTypes && this.activeInstanceTypeCount === 0
},
hasMultipleTemplates () {
return this.instanceTemplates.length > 1
},
hasNodeRedVersions () {
return this.nodeRedVersions.length > 0
},
hasTemplates () {
return this.instanceTemplates.length > 0
},
hasValidName () {
return /^[a-zA-Z][a-zA-Z0-9-\s]*$/.test(this.input.name) && !this.errors.name
},
isTrialProjectSelected () {
// - Team is in trial mode, and
// - Team billing is not configured, or
// - team billing is configured, but they still have an available
// trial instance to create, and they have selected the trial
// instance type
return this.team.billing?.trial && (
!this.team.billing?.active || (
this.team.billing.trialProjectAllowed &&
this.selectedProjectType?.id === this.settings['user:team:trial-mode:projectType']
)
)
},
instanceName () {
return this.input.name.trim().replace(/\s/g, '-').toLowerCase()
},
nodeRedVersionPlaceholder () {
if (!this.input.instanceType) {
return 'Please select an Instance Type First'
}
if (!this.input.template) {
return 'Please select a Template First'
}
return 'Please select'
},
teamInstanceLimitReached () {
// this.projectTypes.length > 0 : There are Instance Types defined
// this.activeInstanceTypeCount : How instance types are available for the user to select
// taking into account their limits
// Hence, if activeInstanceTypeCount === 0, then they are at their limit of usage
return this.instanceTypes.length > 0 && this.activeInstanceTypeCount === 0
},
selectedInstanceType () {
if (this.input.instanceType) {
return this.instanceTypes.find(type => type.id === this.input.instanceType)
}
return null
}
},
watch: {
input: {
immediate: true,
deep: true,
handler () {
this.updateParent()
}
},
'input.instanceType' () {
this.input.nodeREDVersion = null
this.getNodeRedVersions()
},
'input.name': {
immediate: true,
handler: debounce(function (value) {
const allowedCharacters = /^([a-zA-Z0-9-]+)(:\d+)?(\/[^\s<>"'#]*)?$/
if (allowedCharacters.test(value)) {
instancesApi.nameCheck(value)
.then((res) => {
this.errors.name = null
if (res.headers['x-ratelimit-remaining'] === '0') {
const timeout = parseInt(res.headers['x-ratelimit-reset']) * 1000
if (timeout > 750) {
setTimeout(() => {
this.errors.name = null
this.nameDisabled = false
}, timeout)
this.errors.name = 'Please wait, checking name'
this.nameDisabled = true
}
}
})
.catch(e => {
if (e.status === 409) {
if (e.response?.data?.error === 'name in use') {
this.errors.name = 'Instance name already in use.'
} else if (e.response?.data?.error === 'name not allowed') {
this.errors.name = 'Instance name not allowed.'
}
} else if (e.status === 429) {
this.errors.name = 'Name check rate limit exceeded'
}
})
} else {
this.errors.name = 'Invalid character in use.'
}
}, 750)
},
errors: {
deep: true,
handler: function () {
this.updateParent()
}
}
},
async mounted () {
this.getSubscription()
.then(() => this.getInstanceTypes())
.then(() => this.getNodeRedVersions())
.then(() => this.getTemplates())
.catch(e => e)
.finally(() => {
this.loading = false
})
},
methods: {
async getNodeRedVersions () {
const versions = await stacksApi.getStacks(null, null, null, this.input.instanceType)
this.nodeRedVersions = versions.stacks
.filter(version => version.active)
.map(version => { return { ...version, value: version.id, label: version.label || version.name } })
if (this.input.instanceType) {
const instanceType = this.getInstanceType(this.input.instanceType)
if (instanceType?.defaultStack) {
const version = this.nodeRedVersions.find(version => version.id === instanceType.defaultStack)
if (version) {
this.input.nodeREDVersion = instanceType.defaultStack
} else if (this.nodeRedVersions.length > 0) {
// Default to first in the list; don't force a selection to be made
this.input.nodeREDVersion = this.nodeRedVersions[0]?.id
}
}
}
},
async getInstanceTypes () {
const instanceTypes = await instanceTypesApi.getInstanceTypes()
const { decorateInstanceTypes } = useInstanceFormHelper()
this.instanceTypes = instanceTypes.types ?? []
this.decoratedInstanceTypes = decorateInstanceTypes(instanceTypes.types ?? [])
const enabledTypes = this.decoratedInstanceTypes.filter(type => !type.disabled)
if (enabledTypes.length === 1) {
// pre-select the instance type if only one available
this.input.instanceType = enabledTypes[0].id
}
},
async getSubscription () {
if (this.features.billing && !this.team.billing?.unmanaged && !getTeamProperty(this.team, 'billing.disabled')) {
try {
this.subscription = await billingApi.getSubscriptionInfo(this.team.id)
} catch (err) {
if (err.response?.data?.code === 'not_found') {
// This team has no subscription.
if (!this.team.billing?.trial || this.team.billing?.trialEnded) {
throw err
}
}
}
}
},
getTemplates () {
return templatesApi.getTemplates()
.then((response) => {
const templates = response.templates.filter(template => template.active)
if (templates.length === 1) {
this.input.template = templates[0].id
}
this.instanceTemplates = templates
})
},
refreshName () {
this.input.name = NameGenerator()
},
getInstanceType (id) {
return this.instanceTypes.find(instanceType => instanceType.id === id)
},
updateParent () {
let hasErrors = false
let template = null
if (!this.hasValidName) {
// this.errors.name = 'Invalid instance name.'
} else { this.errors.name = null }
if (!this.input.instanceType) {
this.errors.instanceType = 'Instance Type is mandatory'
} else { this.errors.instanceType = null }
if (!this.input.nodeREDVersion) {
this.errors.nodeREDVersion = 'Node-RED Version is mandatory'
} else { this.errors.nodeREDVersion = null }
if (this.hasMultipleTemplates && !this.input.template) {
this.errors.template = 'Template is mandatory'
} else { this.errors.template = null }
Object.keys(this.errors).forEach(key => {
if (this.errors[key] !== null) {
hasErrors = true
}
})
if (this.hasMultipleTemplates) {
template = this.input.template ?? null
} else {
template = this.input.template ?? this.instanceTemplates[0]?.id ?? null
}
this.$emit('step-updated', {
[this.slug]: {
input: {
...(this.input ?? {}),
template,
name: this.instanceName
},
hasErrors,
errors: this.errors
}
})
}
}
}
</script>
<style scoped lang="scss">
.ff-instance-step {
form {
.ff-instance-name {
.ff-input-wrapper {
button {
padding: 5px 10px;
}
}
}
}
}
</style>