|
| 1 | +import _ from 'lodash'; |
| 2 | + |
| 3 | +import factory, { isNodeItem } from './factory'; |
| 4 | + |
| 5 | +const defaultOptions = {}; |
| 6 | + |
| 7 | +const CLUSTER_KEY = 'cluster'; |
| 8 | +const NAME_KEY = 'name'; |
| 9 | +const DESC_KEY = 'description'; |
| 10 | +const SUBNET_KEY = 'subnet'; |
| 11 | +const ENV_KEY = 'env'; |
| 12 | + |
| 13 | +// setting placeholder |
| 14 | +const defaultNameSetting = { |
| 15 | + key: 'name', |
| 16 | + label: 'Name', |
| 17 | + type: 'string', |
| 18 | + default: '', |
| 19 | + required: false |
| 20 | +}; |
| 21 | + |
| 22 | +const defaultRuntimeSetting = { |
| 23 | + key: 'runtime', |
| 24 | + label: 'Runtime', |
| 25 | + renderType: 'radio', |
| 26 | + range: [] |
| 27 | +}; |
| 28 | + |
| 29 | +const defaultVersionSetting = { |
| 30 | + key: 'version', |
| 31 | + label: 'Version', |
| 32 | + items: [ |
| 33 | + // {name: 'x1', value: 'v1'}, |
| 34 | + ] |
| 35 | +}; |
| 36 | + |
| 37 | +export default class VMConfigParser { |
| 38 | + constructor(config, options = {}) { |
| 39 | + this.config = config; |
| 40 | + this.options = _.extend(defaultOptions, options); |
| 41 | + |
| 42 | + this.runtimeConf = _.clone(defaultRuntimeSetting); |
| 43 | + this.versionConf = _.clone(defaultVersionSetting); |
| 44 | + this.vxnetConf = { items: [] }; |
| 45 | + } |
| 46 | + |
| 47 | + setConfig(config = {}) { |
| 48 | + if (typeof config === 'string') { |
| 49 | + config = JSON.parse(config); |
| 50 | + } |
| 51 | + |
| 52 | + if (!this.validConfig(config)) { |
| 53 | + throw Error('invalid config input'); |
| 54 | + } |
| 55 | + |
| 56 | + // config.json |
| 57 | + this.config = config; |
| 58 | + } |
| 59 | + |
| 60 | + validConfig(config) { |
| 61 | + return ( |
| 62 | + _.isObject(config) && typeof config.type === 'string' && Array.isArray(config.properties) |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + isReady() { |
| 67 | + return !_.isEmpty(this.config); |
| 68 | + } |
| 69 | + |
| 70 | + getConfigByKey(config, key) { |
| 71 | + if (typeof config === 'string' && !key) { |
| 72 | + key = config; |
| 73 | + config = null; |
| 74 | + } |
| 75 | + |
| 76 | + if (typeof key !== 'string') { |
| 77 | + throw Error(`key should be string, but ${typeof key} given`); |
| 78 | + } |
| 79 | + |
| 80 | + config = config || this.config; |
| 81 | + if (_.isEmpty(config)) { |
| 82 | + return; |
| 83 | + } |
| 84 | + |
| 85 | + const properties = config.properties || config; |
| 86 | + |
| 87 | + if (_.isArray(properties)) { |
| 88 | + return _.find(properties, { key }); |
| 89 | + } |
| 90 | + if (_.isObject(properties)) { |
| 91 | + return _.extend({}, properties[key], { key }); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + getClusterSetting() { |
| 96 | + return this.getConfigByKey(CLUSTER_KEY); |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * mock basic setting for helm app |
| 101 | + */ |
| 102 | + getDefaultBasicSetting(override = {}) { |
| 103 | + const setting = []; |
| 104 | + const nameConf = _.clone(defaultNameSetting); |
| 105 | + setting.push(nameConf, this.runtimeConf, this.versionConf); |
| 106 | + |
| 107 | + if (!_.isEmpty(override)) { |
| 108 | + _.forEach(setting, setting => { |
| 109 | + if (setting.key === override.key) { |
| 110 | + _.extend(setting, override); |
| 111 | + } |
| 112 | + }); |
| 113 | + } |
| 114 | + |
| 115 | + return factory(setting, { keyPrefix: CLUSTER_KEY }); |
| 116 | + } |
| 117 | + |
| 118 | + getBasicSetting() { |
| 119 | + const clusterSetting = this.getClusterSetting(); |
| 120 | + const setting = []; |
| 121 | + const nameConf = this.getConfigByKey(clusterSetting, NAME_KEY); |
| 122 | + const descConf = this.getConfigByKey(clusterSetting, DESC_KEY); |
| 123 | + |
| 124 | + setting.push(nameConf, descConf, this.runtimeConf, this.versionConf); |
| 125 | + |
| 126 | + return factory(setting, { keyPrefix: CLUSTER_KEY }); |
| 127 | + } |
| 128 | + |
| 129 | + getNodeSetting() { |
| 130 | + const clusterSetting = this.getClusterSetting(); |
| 131 | + return factory(_.filter(clusterSetting.properties, isNodeItem), { keyPrefix: 'node' }); |
| 132 | + } |
| 133 | + |
| 134 | + getVxnetSetting() { |
| 135 | + return factory([this.vxnetConf], { keyPrefix: CLUSTER_KEY }); |
| 136 | + } |
| 137 | + |
| 138 | + getEnvSetting() { |
| 139 | + const envSetting = this.getConfigByKey(ENV_KEY); |
| 140 | + return factory(_.get(envSetting, 'properties', []), { keyPrefix: 'env', labelPrefix: 'env' }); |
| 141 | + } |
| 142 | + |
| 143 | + setRuntimes(runtimes = [], mergeProps) { |
| 144 | + this.runtimeConf.range = runtimes; |
| 145 | + !_.isEmpty(mergeProps) && _.extend(this.runtimeConf, mergeProps); |
| 146 | + } |
| 147 | + |
| 148 | + setVersions(versions = [], mergeProps) { |
| 149 | + this.versionConf.items = versions; |
| 150 | + !_.isEmpty(mergeProps) && _.extend(this.versionConf, mergeProps); |
| 151 | + } |
| 152 | + |
| 153 | + setSubnets(nets = [], mergeProps) { |
| 154 | + if (!this.vxnetConf.key) { |
| 155 | + const clusterSetting = this.getClusterSetting(); |
| 156 | + |
| 157 | + if (!_.isEmpty(clusterSetting)) { |
| 158 | + _.extend(this.vxnetConf, this.getConfigByKey(clusterSetting, SUBNET_KEY)); |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + this.vxnetConf.items = nets; |
| 163 | + |
| 164 | + !_.isEmpty(mergeProps) && _.extend(this.vxnetConf, mergeProps); |
| 165 | + } |
| 166 | +} |
0 commit comments