Skip to content

Commit

Permalink
#127,#241 added possibility to create new script configs
Browse files Browse the repository at this point in the history
  • Loading branch information
bugy committed Nov 10, 2019
1 parent 325a697 commit b224490
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 12 deletions.
7 changes: 6 additions & 1 deletion src/config/config_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,14 @@ def _script_name_to_file_name(script_name):
def _preprocess_incoming_config(config):
name = config.get('name')
if is_blank(name):
raise InvalidConfigException('Name is a required parameter for script')
raise InvalidConfigException('Script name is required')
config['name'] = name.strip()

script_path = config.get('script_path')
if is_blank(script_path):
raise InvalidConfigException('Script path is required')
config['script_path'] = script_path.strip()


class ConfigService:
def __init__(self, authorizer, conf_folder) -> None:
Expand Down
32 changes: 31 additions & 1 deletion src/tests/config_service_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,21 @@ def test_strip_name(self):
config['name'] = 'conf1'
_validate_config(self, 'conf1.json', config)

def test_blank_script_path(self):
config = _prepare_script_config_object('Conf X', description='My wonderful test config')
config['script_path'] = ' '

self.assertRaises(InvalidConfigException, self.config_service.create_config,
self.admin_user, config)

def test_strip_script_path(self):
config = _prepare_script_config_object('Conf X', description='My wonderful test config')
config['script_path'] = ' my_script.sh\t \t'

self.config_service.create_config(self.admin_user, config)
config['script_path'] = 'my_script.sh'
_validate_config(self, 'Conf_X.json', config)

def test_name_already_exists(self):
_create_script_config_file('confX', name='confX')
config = _prepare_script_config_object('confX', description='My wonderful test config')
Expand Down Expand Up @@ -261,6 +276,21 @@ def test_strip_name(self):
config['name'] = 'Conf X'
_validate_config(self, 'confX.json', config)

def test_blank_script_path(self):
config = _prepare_script_config_object('Conf X', description='My wonderful test config')
config['script_path'] = ' '

self.assertRaises(InvalidConfigException, self.config_service.update_config,
self.admin_user, config, 'confX.json')

def test_strip_script_path(self):
config = _prepare_script_config_object('Conf X', description='My wonderful test config')
config['script_path'] = ' my_script.sh\t \t'

self.config_service.update_config(self.admin_user, config, 'confX.json')
config['script_path'] = 'my_script.sh'
_validate_config(self, 'confX.json', config)

def test_name_already_exists(self):
config = _prepare_script_config_object('Conf Y', description='My wonderful test config')

Expand Down Expand Up @@ -370,7 +400,7 @@ def _validate_config(test_case, expected_filename, expected_body):


def _prepare_script_config_object(name, **kwargs):
config = {'name': name}
config = {'name': name, 'script_path': name + '.sh'}

if kwargs:
config.update(kwargs)
Expand Down
2 changes: 1 addition & 1 deletion src/web/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ def post(self, user):
try:
self.application.config_service.create_config(user, config)
except (InvalidConfigException) as e:
raise tornado.web.HTTPError(422, str(e))
raise tornado.web.HTTPError(422, reason=str(e))

@requires_admin_rights
@inject_user
Expand Down
10 changes: 8 additions & 2 deletions web-src/js/admin/components/PromisableButton.vue
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
</template>

<script>
import {isEmptyString} from '../../common';
export default {
name: 'PromisableButton',
props: {
Expand All @@ -47,8 +49,12 @@
this.error = null;
this.inProgress = false;
})
.catch(() => {
this.error = 'Failed to ' + this.title;
.catch((e) => {
if (!isEmptyString(e.userMessage)) {
this.error = e.userMessage;
} else {
this.error = 'Failed to ' + this.title;
}
this.inProgress = false;
})
}
Expand Down
29 changes: 24 additions & 5 deletions web-src/js/admin/scripts-config/ScriptsList.vue
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
<template>
<div class="collection container scripts-list">
<div class="container scripts-list">
<PageProgress v-if="loading"/>
<router-link :key="script" :to="script" append class="collection-item" v-else
v-for="script in scripts">
{{script}}
</router-link>
<div v-else>
<router-link :to="newScriptPath" append class="waves-effect waves-light btn add-script-btn">
<i class="material-icons left">add</i>
Add
</router-link>
<div class="collection">
<router-link :key="script" :to="script" append class="collection-item"
v-for="script in scripts">
{{script}}
</router-link>
</div>
</div>
</div>
</template>

<script>
import {mapActions, mapState} from 'vuex';
import PageProgress from '../components/PageProgress';
import {NEW_SCRIPT} from './script-config-module';
export default {
name: 'ScriptsList',
Expand All @@ -19,6 +28,12 @@
this.init();
},
data() {
return {
newScriptPath: NEW_SCRIPT
}
},
computed: {
...mapState('scripts', {
scripts: 'scripts',
Expand All @@ -41,4 +56,8 @@
margin-top: 1.5em;
margin-bottom: 1em;
}
.add-script-btn {
margin-bottom: 1em;
}
</style>
29 changes: 27 additions & 2 deletions web-src/js/admin/scripts-config/script-config-module.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,23 @@ function removeEmptyValues(config) {
}
}

export const NEW_SCRIPT = '_new';

export default {
state: {
scriptName: null,
scriptConfig: null,
scriptFilename: null,
error: null
error: null,
new: false
},
namespaced: true,
actions: {
init({commit, state}, scriptName) {
if (scriptName === NEW_SCRIPT) {
commit('INIT_NEW_SCRIPT');
return;
}
commit('RESET', scriptName);

axios.get('admin/scripts/' + scriptName)
Expand All @@ -48,7 +55,9 @@ export default {

removeEmptyValues(config);

return axios.put('admin/scripts', {
const axiosAction = state.new ? axios.post : axios.put;

return axiosAction('admin/scripts', {
config,
filename: state.scriptFilename
})
Expand All @@ -62,6 +71,12 @@ export default {
path: `/scripts/${newName}`
});
}
})
.catch(e => {
if (e.response.status === 422) {
e.userMessage = e.response.data;
}
throw e;
});
}
},
Expand All @@ -71,18 +86,28 @@ export default {
state.scriptConfig = null;
state.scriptFilename = null;
state.error = null;
state.new = false;
},

SET_SCRIPT_CONFIG(state, {config, filename}) {
state.error = null;
state.scriptConfig = config;
state.scriptFilename = filename;
state.new = false;
},

SET_LOAD_ERROR(state, error) {
state.error = error;
state.scriptConfig = null;
state.scriptFilename = null;
},

INIT_NEW_SCRIPT(state) {
state.scriptName = null;
state.scriptConfig = {parameters: []};
state.new = true;
state.scriptFilename = null;
state.error = null;
}
}
}

0 comments on commit b224490

Please sign in to comment.