-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Clusterclass variables #16
Merged
mantis-toboggan-md
merged 23 commits into
rancher:main
from
mantis-toboggan-md:clusterclass-variables
Jan 23, 2024
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
0c2ae5c
enums
mantis-toboggan-md 89782ce
required validation
mantis-toboggan-md 850caa2
style
mantis-toboggan-md 7a1106b
more validators
mantis-toboggan-md c19c624
fix length validators
mantis-toboggan-md 51566eb
remove test page
mantis-toboggan-md f07b726
jsdoc openapiv3schema validator function
mantis-toboggan-md eabe146
fix variable widths and new rows
mantis-toboggan-md da8dac3
refactor error handling
mantis-toboggan-md 1423371
remove test file
mantis-toboggan-md ae0d883
re-set variables when clusterclass changes
mantis-toboggan-md c069fb2
fix spacer element visibility when cluster class changes
mantis-toboggan-md f74e868
update required validator to account for a required boolean
mantis-toboggan-md b30c156
add machine-scoped ccvariables
mantis-toboggan-md dff4855
exclude patches using builtin vars
mantis-toboggan-md f6469e8
update list component validation, required validator
mantis-toboggan-md 4ffe731
add string format validation
mantis-toboggan-md 8fba487
cleanup following review
mantis-toboggan-md 18d82ed
fix component imports
mantis-toboggan-md da0982a
fix ts warnings
mantis-toboggan-md dceb67d
typescript fixes 2 electric bugaloo
mantis-toboggan-md 30c7bec
fix vscode settings after rebase
mantis-toboggan-md c8bfd1e
updates following review
mantis-toboggan-md File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,195 @@ | ||
<script lang="ts"> | ||
import { defineComponent } from 'vue'; | ||
import type { PropType } from 'vue'; | ||
import LabeledInput from '@components/Form/LabeledInput/LabeledInput.vue'; | ||
import Checkbox from '@components/Form/Checkbox/Checkbox.vue'; | ||
import KeyValue from '@shell/components/form/KeyValue.vue'; | ||
import ArrayList from '@shell/components/form/ArrayList.vue'; | ||
import LabeledSelect from '@shell/components/form/LabeledSelect.vue'; | ||
import { Validator } from '@shell/utils/validators/formRules'; | ||
|
||
import { mapGetters } from 'vuex'; | ||
import { Translation } from '@rancher/shell/types/t'; | ||
import type { ClusterClassVariable } from '../../types/clusterClass'; | ||
import { isDefined, openAPIV3SchemaValidators } from '../../util/validators'; | ||
export default defineComponent({ | ||
name: 'CCVariable', | ||
|
||
props: { | ||
variable: { | ||
type: Object as PropType<ClusterClassVariable>, | ||
required: true | ||
}, | ||
|
||
value: { | ||
type: [String, Object, Boolean, Array, Number], | ||
default: () => null | ||
}, | ||
|
||
validateRequired: { | ||
type: Boolean, | ||
default: true | ||
} | ||
}, | ||
|
||
watch: { | ||
isValid(neu) { | ||
this.$emit('validation-passed', neu); | ||
} | ||
}, | ||
|
||
created() { | ||
if (!this.isValid) { | ||
this.$emit('validation-passed', false); | ||
} | ||
}, | ||
|
||
computed: { | ||
...mapGetters({ t: 'i18n/t' }), | ||
componentForType() { | ||
const { type } = this.schema; | ||
let out = null; | ||
|
||
if (this.variableOptions) { | ||
out = { component: LabeledSelect, name: 'text-var' }; | ||
} else { | ||
switch (type) { | ||
case 'object': | ||
out = { component: KeyValue, name: 'keyvalue-var' }; | ||
break; | ||
case 'array': | ||
out = { component: ArrayList, name: 'arraylist-var' }; | ||
break; | ||
case 'string': | ||
out = { component: LabeledInput, name: 'text-var' }; | ||
break; | ||
case 'integer': | ||
out = { component: LabeledInput, name: 'text-var' }; | ||
|
||
break; | ||
case 'number': | ||
out = { component: LabeledInput, name: 'text-var' }; | ||
|
||
break; | ||
case 'boolean': | ||
out = { component: Checkbox, name: 'checkbox-var' }; | ||
|
||
break; | ||
default: | ||
break; | ||
} | ||
} | ||
|
||
return out; | ||
}, | ||
|
||
schema() { | ||
return this.variable?.schema?.openAPIV3Schema; | ||
}, | ||
|
||
// options may be arrays or objects - stringify them to display in labeledselect | ||
variableOptions() { | ||
const opts = this.schema?.enum; | ||
|
||
if (!opts || !opts.length) { | ||
return null; | ||
} | ||
|
||
return opts.map((opt: any) => { | ||
return typeof opt === 'object' ? JSON.stringify(opt) : opt; | ||
}); | ||
}, | ||
|
||
validationRules() { | ||
const t = this.t as Translation; | ||
const out = openAPIV3SchemaValidators(t, { key: this.variable.name }, this.schema); | ||
|
||
const required = this.variable?.required; | ||
|
||
if (required && this.validateRequired) { | ||
out.push(val => !isDefined(val) ? t('validation.required', { key: this.variable.name }) : undefined); | ||
} | ||
|
||
return out; | ||
}, | ||
|
||
isValid() { | ||
return !this.validationErrors.length; | ||
}, | ||
|
||
validationErrors() { | ||
return this.validationRules.reduce((errs: string[], rule: Validator) => { | ||
const message = rule(this.value); | ||
|
||
if (message) { | ||
errs.push(message); | ||
} | ||
|
||
return errs; | ||
}, []); | ||
}, | ||
|
||
listComponent() { | ||
return this.componentForType?.name === 'arraylist-var' || this.componentForType?.name === 'keyvalue-var'; | ||
} | ||
}, | ||
|
||
methods: { | ||
setValue(e: any) { | ||
let out = e; | ||
|
||
const { type } = this.schema; | ||
|
||
if (type === 'object') { | ||
try { | ||
out = JSON.parse(e); | ||
} catch {} | ||
} | ||
this.$emit('input', out); | ||
} | ||
}, | ||
}); | ||
</script> | ||
|
||
<template> | ||
<div v-if="componentForType" :class="{'wider': listComponent, 'align-center': componentForType?.name==='checkbox-var', [`${componentForType.name}`]: true}"> | ||
<component | ||
:is="componentForType.component" | ||
v-if="componentForType" | ||
:value="value" | ||
:label="variable.name" | ||
:placeholder="schema.example" | ||
:tooltip="schema.description" | ||
:required="variable.required && validateRequired" | ||
:title="variable.name" | ||
:options="variableOptions" | ||
:rules="!listComponent ? validationRules : []" | ||
:type="schema.type === 'number' || schema.type === 'integer' ? 'number' : 'text'" | ||
@input="setValue" | ||
> | ||
<template #title> | ||
<div class="input-label"> | ||
<span>{{ variable.name }} | ||
<i v-if="schema.description" v-clean-tooltip="schema.description" class="icon icon-sm icon-info" /> | ||
<i v-if="!isValid" v-clean-tooltip="validationErrors.join(' ')" class="icon icon-warning" /> | ||
</span> | ||
</div> | ||
</template> | ||
</component> | ||
<div class="flexbox-newline" /> | ||
</div> | ||
</template> | ||
<style lang="scss" scoped> | ||
.align-center { | ||
align-self: 'center' | ||
} | ||
.input-label{ | ||
color: var(--input-label); | ||
margin-bottom: 5px; | ||
display: block; | ||
width:100%; | ||
.icon-warning{ | ||
color: var(--error) | ||
} | ||
} | ||
</style> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
type is missing