|
1 | | -function isConsole({ storeGet }) { |
2 | | - const owner = storeGet('/route/params/user') |
3 | | - const path = storeGet('/route/path') |
4 | | - const prefix = `/${owner}/kubernetes` |
5 | | - if (path.startsWith(prefix)) return true |
6 | | - return false |
7 | | -} |
| 1 | +const { ref, computed, axios, watch, useOperator, store } = window.vueHelpers || {} |
8 | 2 |
|
9 | | -async function getDatabases({ axios, storeGet }, group, version, resource) { |
10 | | - const owner = storeGet('/route/params/user') || '' |
11 | | - const cluster = storeGet('/route/params/cluster') || '' |
12 | | - if (isConsole({ storeGet })) { |
13 | | - try { |
14 | | - const resp = await axios.get( |
15 | | - `/clusters/${owner}/${cluster}/proxy/${group}/${version}/${resource}`, |
16 | | - { |
17 | | - params: { |
18 | | - convertToTable: true, |
19 | | - labelSelector: 'k8s.io/group=kubedb.com', |
20 | | - }, |
21 | | - }, |
22 | | - ) |
| 3 | +export const useFunc = (model) => { |
| 4 | + const { getValue, setDiscriminatorValue, commit, storeGet, discriminator } = useOperator( |
| 5 | + model, |
| 6 | + store.state, |
| 7 | + ) |
23 | 8 |
|
24 | | - const resources = (resp && resp.data && resp.data.rows) || [] |
| 9 | + function isConsole() { |
| 10 | + const owner = storeGet('/route/params/user') |
| 11 | + const path = storeGet('/route/path') |
| 12 | + const prefix = `/${owner}/kubernetes` |
| 13 | + if (path.startsWith(prefix)) return true |
| 14 | + return false |
| 15 | + } |
25 | 16 |
|
26 | | - resources.map((item) => { |
27 | | - const name = (item.cells && item.cells[0].data) || '' |
28 | | - const namespace = (item.cells?.length > 0 && item.cells[1].data) || '' |
29 | | - const resource = (item.cells?.length > 1 && item.cells[2].data) || '' |
30 | | - item.text = name |
31 | | - item.value = { |
32 | | - name: name, |
33 | | - namespace: namespace, |
34 | | - resource: resource, |
35 | | - } |
36 | | - return true |
37 | | - }) |
38 | | - const filteredResources = resources.filter( |
39 | | - (item) => item.value.resource.toLowerCase() === 'elasticsearch', |
40 | | - ) |
41 | | - return filteredResources |
42 | | - } catch (e) { |
43 | | - console.log(e) |
44 | | - return [] |
45 | | - } |
46 | | - } else return [] |
47 | | -} |
| 17 | + async function getDatabases(group, version, resource) { |
| 18 | + const owner = storeGet('/route/params/user') || '' |
| 19 | + const cluster = storeGet('/route/params/cluster') || '' |
| 20 | + if (isConsole()) { |
| 21 | + try { |
| 22 | + const resp = await axios.get( |
| 23 | + `/clusters/${owner}/${cluster}/proxy/${group}/${version}/${resource}`, |
| 24 | + { |
| 25 | + params: { |
| 26 | + convertToTable: true, |
| 27 | + labelSelector: 'k8s.io/group=kubedb.com', |
| 28 | + }, |
| 29 | + }, |
| 30 | + ) |
48 | 31 |
|
49 | | -function isDbSelected({ getValue, storeGet, discriminator, watchDependency }) { |
50 | | - if (!isConsole({ storeGet })) return true |
51 | | - watchDependency('discriminator#/database') |
52 | | - const val = getValue(discriminator, '/database') || {} |
53 | | - return val && val.name ? true : false |
54 | | -} |
| 32 | + const resources = (resp && resp.data && resp.data.rows) || [] |
55 | 33 |
|
56 | | -function setRoleName({ watchDependency, getValue, model }) { |
57 | | - watchDependency('model#/spec/secretEngineRef/name') |
58 | | - const engineName = getValue(model, '/spec/secretEngineRef/name') || '' |
59 | | - const timestamp = `${Math.floor(Date.now() / 1000)}` |
60 | | - return engineName ? `${engineName}-role-${timestamp}` : engineName |
61 | | -} |
| 34 | + resources.map((item) => { |
| 35 | + const name = (item.cells && item.cells[0].data) || '' |
| 36 | + const namespace = (item.cells?.length > 0 && item.cells[1].data) || '' |
| 37 | + const resource = (item.cells?.length > 1 && item.cells[2].data) || '' |
| 38 | + item.text = name |
| 39 | + item.value = { |
| 40 | + name: name, |
| 41 | + namespace: namespace, |
| 42 | + resource: resource, |
| 43 | + } |
| 44 | + return true |
| 45 | + }) |
| 46 | + const filteredResources = resources.filter( |
| 47 | + (item) => item.value.resource.toLowerCase() === 'elasticsearch', |
| 48 | + ) |
| 49 | + return filteredResources |
| 50 | + } catch (e) { |
| 51 | + console.log(e) |
| 52 | + return [] |
| 53 | + } |
| 54 | + } else return [] |
| 55 | + } |
62 | 56 |
|
63 | | -function getDbNamespace({ getValue, storeGet, discriminator, watchDependency }) { |
64 | | - if (isConsole({ storeGet })) { |
65 | | - watchDependency('discriminator#/database') |
66 | | - const data = getValue(discriminator, '/database') || {} |
67 | | - return (data && data.namespace) || '' |
68 | | - } else { |
69 | | - const namespace = storeGet('/route/query/namespace') || '' |
70 | | - return namespace |
| 57 | + function isDbSelected() { |
| 58 | + if (!isConsole()) return true |
| 59 | + // watchDependency('discriminator#/database') |
| 60 | + const val = getValue(discriminator, '/database') || {} |
| 61 | + return val && val.name ? true : false |
71 | 62 | } |
72 | | -} |
73 | 63 |
|
74 | | -async function getEngines({ axios, storeGet, getValue, discriminator }, group, version, resource) { |
75 | | - const owner = storeGet('/route/params/user') || '' |
76 | | - const cluster = storeGet('/route/params/cluster') || '' |
77 | | - const dbValue = getValue(discriminator, '/database') || {} |
78 | | - const dbName = storeGet('/route/params/name') || dbValue.name || '' |
79 | | - const dbNamespace = storeGet('/route/query/namespace') || dbValue.namespace || '' |
80 | | - if (dbName) { |
81 | | - try { |
82 | | - const resp = await axios.get( |
83 | | - `/clusters/${owner}/${cluster}/proxy/${group}/${version}/${resource}`, |
84 | | - ) |
| 64 | + function setRoleName() { |
| 65 | + // watchDependency('model#/spec/secretEngineRef/name') |
| 66 | + const engineName = getValue(model, '/spec/secretEngineRef/name') || '' |
| 67 | + const timestamp = `${Math.floor(Date.now() / 1000)}` |
| 68 | + |
| 69 | + return engineName ? `${engineName}-role-${timestamp}` : engineName |
| 70 | + } |
85 | 71 |
|
86 | | - const resources = (resp && resp.data && resp.data.items) || [] |
87 | | - const filteredResources = resources.filter( |
88 | | - (item) => |
89 | | - item.spec?.elasticsearch?.databaseRef?.name === dbName && |
90 | | - item.spec?.elasticsearch?.databaseRef?.namespace === dbNamespace, |
91 | | - ) |
92 | | - filteredResources.map((item) => { |
93 | | - const name = (item.metadata && item.metadata.name) || '' |
94 | | - item.text = name |
95 | | - item.value = name |
96 | | - return true |
97 | | - }) |
98 | | - return filteredResources |
99 | | - } catch (e) { |
100 | | - console.log(e) |
101 | | - return [] |
| 72 | + function getDbNamespace() { |
| 73 | + if (isConsole()) { |
| 74 | + // watchDependency('discriminator#/database') |
| 75 | + const data = getValue(discriminator, '/database') || {} |
| 76 | + return (data && data.namespace) || '' |
| 77 | + } else { |
| 78 | + const namespace = storeGet('/route/query/namespace') || '' |
| 79 | + return namespace |
102 | 80 | } |
103 | | - } else return [] |
104 | | -} |
| 81 | + } |
105 | 82 |
|
106 | | -return { |
107 | | - isConsole, |
108 | | - getDatabases, |
109 | | - isDbSelected, |
110 | | - setRoleName, |
111 | | - getDbNamespace, |
112 | | - getEngines, |
| 83 | + async function getEngines(group, version, resource) { |
| 84 | + const owner = storeGet('/route/params/user') || '' |
| 85 | + const cluster = storeGet('/route/params/cluster') || '' |
| 86 | + const dbValue = getValue(discriminator, '/database') || {} |
| 87 | + const dbName = storeGet('/route/params/name') || dbValue.name || '' |
| 88 | + const dbNamespace = storeGet('/route/query/namespace') || dbValue.namespace || '' |
| 89 | + if (dbName) { |
| 90 | + try { |
| 91 | + const resp = await axios.get( |
| 92 | + `/clusters/${owner}/${cluster}/proxy/${group}/${version}/${resource}`, |
| 93 | + ) |
| 94 | + |
| 95 | + const resources = (resp && resp.data && resp.data.items) || [] |
| 96 | + const filteredResources = resources.filter( |
| 97 | + (item) => |
| 98 | + item.spec?.elasticsearch?.databaseRef?.name === dbName && |
| 99 | + item.spec?.elasticsearch?.databaseRef?.namespace === dbNamespace, |
| 100 | + ) |
| 101 | + filteredResources.map((item) => { |
| 102 | + const name = (item.metadata && item.metadata.name) || '' |
| 103 | + item.text = name |
| 104 | + item.value = name |
| 105 | + return true |
| 106 | + }) |
| 107 | + return filteredResources |
| 108 | + } catch (e) { |
| 109 | + console.log(e) |
| 110 | + return [] |
| 111 | + } |
| 112 | + } else return [] |
| 113 | + } |
| 114 | + |
| 115 | + return { |
| 116 | + isConsole, |
| 117 | + getDatabases, |
| 118 | + isDbSelected, |
| 119 | + setRoleName, |
| 120 | + getDbNamespace, |
| 121 | + getEngines, |
| 122 | + } |
113 | 123 | } |
0 commit comments