Refactor modes and adding edit action to Gatekeeper

This commit is contained in:
Westly Wright 2020-03-10 15:41:56 -07:00
parent 4c877c47b8
commit a2741cb37f
No known key found for this signature in database
GPG Key ID: 4FAB3D8673DC54A3
3 changed files with 105 additions and 13 deletions

View File

@ -3,7 +3,7 @@ import CodeMirror from './CodeMirror';
import AsyncButton from '@/components/AsyncButton'; import AsyncButton from '@/components/AsyncButton';
import Footer from '@/components/form/Footer'; import Footer from '@/components/form/Footer';
import { NAMESPACE } from '@/config/types'; import { NAMESPACE } from '@/config/types';
import { _VIEW } from '@/config/query-params'; import { _VIEW, _EDIT } from '@/config/query-params';
import GatekeeperTables from '@/components/GatekeeperTables'; import GatekeeperTables from '@/components/GatekeeperTables';
import { findBy } from '@/utils/array'; import { findBy } from '@/utils/array';
@ -55,7 +55,8 @@ export default {
return { return {
gatekeeperEnabled, gatekeeperEnabled,
showYamlEditor: false, showYamlEditor: false,
errors: null, errors: [],
saving: false,
}; };
}, },
@ -98,6 +99,16 @@ export default {
}, },
}, },
watch: {
mode() {
if (this.mode === _EDIT) {
this.showYamlEditor = true;
} else {
this.showYamlEditor = false;
}
},
},
methods: { methods: {
async ensureNamespace() { async ensureNamespace() {
if ( findBy(this.namespaces, 'metadata.name', 'gatekeeper-system') ) { if ( findBy(this.namespaces, 'metadata.name', 'gatekeeper-system') ) {
@ -118,6 +129,12 @@ export default {
// await newSystemNs.waitForState('active'); // await newSystemNs.waitForState('active');
}, },
showActions() {
this.$store.commit('actionMenu/show', {
resources: this.config,
elem: this.$refs.actions,
});
},
/** /**
* Gets called when the user clicks on the button * Gets called when the user clicks on the button
* Checks for the system namespace and creates that first if it does not exist * Checks for the system namespace and creates that first if it does not exist
@ -142,6 +159,8 @@ export default {
} }
buttonCb(false); buttonCb(false);
} }
this.saving = false;
}, },
/** /**
@ -150,7 +169,11 @@ export default {
*/ */
openYamlEditor() { openYamlEditor() {
if (this.showYamlEditor) { if (this.showYamlEditor) {
this.showYamlEditor = false; if (this.mode === _EDIT) {
this.$router.push({ name: this.$route.name });
} else {
this.showYamlEditor = false;
}
} else { } else {
this.showYamlEditor = true; this.showYamlEditor = true;
} }
@ -271,10 +294,13 @@ export default {
v-bind="$attrs" v-bind="$attrs"
@click="disable" @click="disable"
/> />
<button ref="actions" type="button" class="btn btn-sm role-multi-action actions" @click="showActions">
<i class="icon icon-actions" />
</button>
</div> </div>
</header> </header>
<div v-if="gatekeeperEnabled" class="mt-20 text-center"> <div v-if="gatekeeperEnabled" class="mt-20">
<GatekeeperTables /> <GatekeeperTables v-if="gatekeeperEnabled && !showYamlEditor" />
</div> </div>
<div v-else class="mt-20 mb-20"> <div v-else class="mt-20 mb-20">
<hr /> <hr />
@ -313,6 +339,7 @@ export default {
<button <button
type="button" type="button"
class="btn bg-primary" class="btn bg-primary"
:disable="saving"
@click="openYamlEditor" @click="openYamlEditor"
> >
Customize Configuration Customize Configuration
@ -333,7 +360,7 @@ export default {
@onChanges="onChanges" @onChanges="onChanges"
/> />
<Footer <Footer
mode="create" :mode="mode"
@errors="errors" @errors="errors"
@save="enable" @save="enable"
@done="openYamlEditor" @done="openYamlEditor"

View File

@ -0,0 +1,47 @@
import { addParams } from '@/utils/url';
import { MODE, _EDIT } from '@/config/query-params';
const GATEKEEPER_DEFAULT_ID = 'rancher-gatekeeper-operator';
export default {
isGateKeeper() {
return this.id.includes(GATEKEEPER_DEFAULT_ID);
},
availableActions() {
const { isGateKeeper } = this;
let out = this._standardActions.slice();
if (isGateKeeper) {
const toFilter = ['cloneYaml'];
out = out.filter((action) => {
if (!toFilter.includes(action.action)) {
return action;
}
});
}
return out;
},
appEditUrl() {
const { isGateKeeper } = this;
const router = this.currentRouter();
if (isGateKeeper) {
const url = router.resolve({ name: router.currentRoute.name }).href;
return url;
} else {
return this.detailUrl();
}
},
goToEdit() {
return (moreQuery = {}) => {
const url = addParams(this.appEditUrl, { [MODE]: _EDIT, ...moreQuery });
this.currentRouter().push({ path: url });
};
},
};

View File

@ -1,7 +1,7 @@
<script> <script>
import { NAMESPACE, MANAGEMENT, EXTERNAL } from '@/config/types'; import { NAMESPACE, MANAGEMENT, EXTERNAL } from '@/config/types';
import GatekeeperConfig from '@/components/GatekeeperConfig'; import GatekeeperConfig from '@/components/GatekeeperConfig';
import { _CREATE, _VIEW } from '@/config/query-params'; import { _CREATE, _EDIT, _VIEW } from '@/config/query-params';
const TEMPLATE_ID = 'cattle-global-data/system-library-rancher-gatekeeper-operator'; const TEMPLATE_ID = 'cattle-global-data/system-library-rancher-gatekeeper-operator';
const APP_ID = 'rancher-gatekeeper-operator'; const APP_ID = 'rancher-gatekeeper-operator';
@ -33,11 +33,14 @@ export default {
components: { GatekeeperConfig }, components: { GatekeeperConfig },
data() { data() {
return { gateKeeperUnAvailable: false }; return {
gateKeeperUnavailable: false,
mode: _VIEW,
};
}, },
async asyncData({ store }) { async asyncData({ store, route }) {
let mode = _VIEW; let mode = route?.query?.mode ? route.query.mode : _VIEW;
try { try {
const template = await store.dispatch('management/find', { const template = await store.dispatch('management/find', {
@ -46,7 +49,10 @@ export default {
}); });
if (!template?.id ) { if (!template?.id ) {
return { gateKeeperUnAvailable: true }; return {
gateKeeperUnavailable: true,
mode: _VIEW,
};
} }
// clusterID is on router.state.clusterid and find // clusterID is on router.state.clusterid and find
@ -102,15 +108,27 @@ export default {
} catch (e) { } catch (e) {
console.error(e); console.error(e);
return { gateKeeperUnAvailable: true }; return {
gateKeeperUnavailable: true,
mode: _VIEW,
};
} }
}, },
beforeRouteUpdate(to, from, next) {
if (to.query?.mode === _EDIT) {
this.mode = _EDIT;
} else if (from.query?.mode === _EDIT && !to.query?.mode) {
this.mode = _VIEW;
}
next();
},
}; };
</script> </script>
<template> <template>
<div> <div>
<div v-if="gateKeeperUnAvailable"> <div v-if="gateKeeperUnavailable">
<h2 class="text-center mt-50"> <h2 class="text-center mt-50">
OPA + Gatekeeper is not available in the system-charts catalog. OPA + Gatekeeper is not available in the system-charts catalog.
</h2> </h2>