Skip to content
This repository has been archived by the owner on Jan 7, 2018. It is now read-only.

Commit

Permalink
Merge pull request #10 from NREL/website-backends
Browse files Browse the repository at this point in the history
Add admin interface for managing website backend configurations
  • Loading branch information
GUI committed Mar 29, 2015
2 parents 600f82a + eb7895c commit 9f6368f
Show file tree
Hide file tree
Showing 28 changed files with 517 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@ Admin.ConfigPublishController = Ember.Controller.extend({
var newApis = this.get('model.config.apis.new');
var modifiedApis = this.get('model.config.apis.modified');
var deletedApis = this.get('model.config.apis.deleted');
if(newApis.length > 0 || modifiedApis.length > 0 || deletedApis.length > 0) {
var newWebsiteBackends = this.get('model.config.website_backends.new');
var modifiedWebsiteBackends = this.get('model.config.website_backends.modified');
var deletedWebsiteBackends = this.get('model.config.website_backends.deleted');

if(newApis.length > 0 || modifiedApis.length > 0 || deletedApis.length > 0 || newWebsiteBackends.length > 0 || modifiedWebsiteBackends.length > 0 || deletedWebsiteBackends.length > 0) {
return true;
} else {
return false;
}
}.property('model.config.apis.new.@each', 'model.config.apis.modified.@each', 'model.config.apis.deleted.@each'),
}.property('model.config.apis.new.@each', 'model.config.apis.modified.@each', 'model.config.apis.deleted.@each', 'model.config.website_backends.new.@each', 'model.config.website_backends.modified.@each', 'model.config.website_backends.deleted.@each'),
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
Admin.WebsiteBackendsFormController = Ember.ObjectController.extend(Admin.Save, {
backendProtocolOptions: [
{ id: 'http', name: 'http' },
{ id: 'https', name: 'https' },
],

changeDefaultPort: function() {
var protocol = this.get('model.backendProtocol');
var port = parseInt(this.get('model.serverPort'), 10);
if(protocol === 'https') {
if(!port || port === 80) {
this.set('model.serverPort', 443);
}
} else {
if(!port || port === 443) {
this.set('model.serverPort', 80);
}
}
}.observes('model.backendProtocol'),

actions: {
submit: function() {
this.save({
transitionToRoute: 'website_backends',
message: 'Successfully saved the "' + this.get('model.frontendHost') + '" website backend<br><strong>Note:</strong> Your changes are not yet live. <a href="/admin/#/config/publish">Publish Changes</a> to send your updates live.',
});
},

delete: function() {
bootbox.confirm('Are you sure you want to delete this website backend?', _.bind(function(result) {
if(result) {
this.get('model').deleteRecord();
this.transitionToRoute('website_backends');
}
}, this));
},
},
});

Admin.WebsiteBackendsEditController = Admin.WebsiteBackendsFormController.extend();
Admin.WebsiteBackendsNewController = Admin.WebsiteBackendsFormController.extend();
38 changes: 38 additions & 0 deletions app/assets/javascripts/admin/models/website_backend.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
Admin.WebsiteBackend = Ember.Model.extend(Ember.Validations.Mixin, {
id: Ember.attr(),
frontendHost: Ember.attr(),
backendProtocol: Ember.attr(),
serverHost: Ember.attr(),
serverPort: Ember.attr(Number),

validations: {
frontendHost: {
presence: true,
format: {
with: CommonValidations.frontend_host_format,
message: polyglot.t('errors.messages.invalid_host_format'),
},
},
backendProtocol: {
presence: true,
},
serverHost: {
presence: true,
format: {
with: CommonValidations.frontend_host_format,
message: polyglot.t('errors.messages.invalid_host_format'),
},
},
serverPort: {
presence: true,
numericality: true,
},
},
});

Admin.WebsiteBackend.url = '/api-umbrella/v1/website_backends';
Admin.WebsiteBackend.rootKey = 'website_backend';
Admin.WebsiteBackend.collectionKey = 'data';
Admin.WebsiteBackend.primaryKey = 'id';
Admin.WebsiteBackend.camelizeKeys = true;
Admin.WebsiteBackend.adapter = Admin.APIUmbrellaRESTAdapter.create();
5 changes: 5 additions & 0 deletions app/assets/javascripts/admin/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,9 @@ Admin.Router.map(function() {
this.route('map', { path: '/map/*query' });
this.route('mapDefault', { path: '/map' });
});

this.resource('website_backends', { path: '/website_backends' }, function() {
this.route('new');
this.route('edit', { path: '/:websiteBackendId/edit' });
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Admin.WebsiteBackendsBaseRoute = Ember.Route.extend({
setupController: function(controller, model) {
controller.set('model', model);

$('ul.nav li').removeClass('active');
$('ul.nav li.nav-config').addClass('active');
},
});
10 changes: 10 additions & 0 deletions app/assets/javascripts/admin/routes/website_backends/edit_route.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
Admin.WebsiteBackendsEditRoute = Admin.WebsiteBackendsBaseRoute.extend({
model: function(params) {
// Clear the record cache, so this is always fetched from the server (to
// account for two users simultaneously editing the same record).
Admin.WebsiteBackend.clearCache();

return Admin.WebsiteBackend.find(params.websiteBackendId);
},
});

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Admin.WebsiteBackendsIndexRoute = Admin.WebsiteBackendsBaseRoute.extend({
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
Admin.WebsiteBackendsNewRoute = Admin.WebsiteBackendsBaseRoute.extend({
model: function() {
return Admin.WebsiteBackend.create({
serverPort: 80,
});
},
});

27 changes: 24 additions & 3 deletions app/assets/javascripts/admin/templates/config/publish.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,42 @@
{{#if model.config.apis.new}}
<fieldset>
<legend>{{model.config.apis.new.length}} New API Backends</legend>
{{render "config/publish_api" model.config.apis.new}}
{{render "config/publish_record" model.config.apis.new category="apis"}}
</fieldset>
{{/if}}

{{#if model.config.website_backends.new}}
<fieldset>
<legend>{{model.config.website_backends.new.length}} New Website Backends</legend>
{{render "config/publish_record" model.config.website_backends.new category="website_backends"}}
</fieldset>
{{/if}}

{{#if model.config.apis.modified}}
<fieldset>
<legend>{{model.config.apis.modified.length}} Modified API Backends</legend>
{{render "config/publish_api" model.config.apis.modified}}
{{render "config/publish_record" model.config.apis.modified category="apis"}}
</fieldset>
{{/if}}

{{#if model.config.website_backends.modified}}
<fieldset>
<legend>{{model.config.website_backends.modified.length}} Modified Website Backends</legend>
{{render "config/publish_record" model.config.website_backends.modified category="website_backends"}}
</fieldset>
{{/if}}

{{#if model.config.apis.deleted}}
<fieldset>
<legend>{{model.config.apis.deleted.length}} Deleted API Backends</legend>
{{render "config/publish_api" model.config.apis.deleted}}
{{render "config/publish_record" model.config.apis.deleted category="apis"}}
</fieldset>
{{/if}}

{{#if model.config.website_backends.deleted}}
<fieldset>
<legend>{{model.config.website_backends.deleted.length}} Deleted Website Backends</legend>
{{render "config/publish_record" model.config.website_backends.deleted category="website_backends"}}
</fieldset>
{{/if}}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@
{{#each model}}
<tr>
<td class="text-center">
<input type="hidden" name="config[apis][{{unbound id}}][mode]" {{bind-attr value='mode'}} />
<input type="hidden" name="config[apis][{{unbound id}}][active_version]" {{bind-attr value='active.version'}} />
<input type="hidden" name="config[apis][{{unbound id}}][pending_version]" {{bind-attr value='pending.version'}} />
<input type="hidden" name="config[apis][{{unbound id}}][publish]" value="0" />
<input type="checkbox" name="config[apis][{{unbound id}}][publish]" value="1" />
<input type="hidden" name="config[{{unbound view.category}}][{{unbound id}}][mode]" {{bind-attr value='mode'}} />
<input type="hidden" name="config[{{unbound view.category}}][{{unbound id}}][active_version]" {{bind-attr value='active.version'}} />
<input type="hidden" name="config[{{unbound view.category}}][{{unbound id}}][pending_version]" {{bind-attr value='pending.version'}} />
<input type="hidden" name="config[{{unbound view.category}}][{{unbound id}}][publish]" value="0" />
<input type="checkbox" name="config[{{unbound view.category}}][{{unbound id}}][publish]" value="1" />
</td>
<td>{{name}}<br><small><a href="#" {{action 'toggleConfigDiff' id target='view'}}>View Config Differences</a></small></td>
</tr>
Expand Down
38 changes: 38 additions & 0 deletions app/assets/javascripts/admin/templates/website_backends/_form.hbs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{{error-messages model=model}}

{{#form-for model}}
<fieldset class="form-horizontal info">
{{input frontendHost
label='Frontend Host'
placeholder='example.com'
inputConfig='class:span6'}}

{{input backendProtocol as='select'
label=(t 'mongoid.attributes.api.backend_protocol')
value='backendProtocol'
collection='backendProtocolOptions'
optionValuePath='content.id'
optionLabelPath='content.name'}}

{{input serverHost
label='Backend Server'
placeholder='example.github.io'
inputConfig='class:span6'}}

{{input serverPort
label='Backend Port'
inputConfig='class:span2'}}
</fieldset>

<div class="row-fluid">
<div class="span6">
<button type="submit" id="save_button" class="btn btn-large btn-primary" data-loading-text="<i class='fa fa-refresh fa-spin'></i> Saving...">Save</button>
</div>
<div class="span6 record-details">
{{#if id}}
Created: {{formatDate createdAt}} by {{creator.username}}<br>
Last Updated: {{formatDate updatedAt}} by {{updater.username}}<br>
{{/if}}
</div>
</div>
{{/form-for}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Edit Website Backend</h1>
{{partial "website_backends/form"}}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<h1>Website Backends</h1>

<div class="button-actions button-actions-down">
<a href="#/website_backends/new" class="btn btn-primary"><i class="fa fa-plus"></i> Add Website Backend</a>
</div>

<div id="results_table searchable-table-with-add">
{{view Admin.WebsiteBackendsTableView}}
</div>
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<h1>Add Website Backend</h1>
{{partial "website_backends/form"}}
7 changes: 0 additions & 7 deletions app/assets/javascripts/admin/views/config/publish_api_view.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Admin.ConfigPublishRecordView = Ember.View.extend({
actions: {
toggleConfigDiff: function(id) {
$('[data-diff-id=' + id + ']').toggle();
}
}
});
31 changes: 31 additions & 0 deletions app/assets/javascripts/admin/views/website_backends/table_view.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
Admin.WebsiteBackendsTableView = Ember.View.extend({
tagName: 'table',
classNames: ['table', 'table-striped', 'table-bordered', 'table-condensed'],

didInsertElement: function() {
this.set('table', this.$().DataTable({
serverSide: true,
ajax: '/api-umbrella/v1/website_backends.json',
pageLength: 50,
rowCallback: function(row, data) {
$(row).data('id', data.id);
},
order: [[0, 'asc']],
columns: [
{
data: 'frontend_host',
title: 'Host',
defaultContent: '-',
render: _.bind(function(name, type, data) {
if(type === 'display' && name && name !== '-') {
var link = '#/website_backends/' + data.id + '/edit';
return '<a href="' + link + '">' + _.escape(name) + '</a>';
}

return name;
}, this),
},
]
}));
},
});
31 changes: 22 additions & 9 deletions app/controllers/api/v1/config_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,36 @@ def pending_changes
def publish
active_config = ConfigVersion.active_config || {}
new_config = active_config.deep_dup
new_config["apis"] ||= []

params[:config][:apis].each do |api_id, api_params|
next unless(api_params[:publish].to_s == "1")
["apis", "website_backends"].each do |category|
new_config[category] ||= []
next unless(params[:config].present? && params[:config][category].present?)

api = Api.unscoped.find(api_id)
authorize(api, :publish?)
params[:config][category].each do |record_id, record_params|
next unless(record_params[:publish].to_s == "1")

api.handle_transition_https_on_publish!
record = case(category)
when "apis"
Api.unscoped.find(record_id)
when "website_backends"
WebsiteBackend.unscoped.find(record_id)
end

new_config["apis"].reject! { |data| data["_id"] == api_id }
unless api.deleted_at?
new_config["apis"] << api.attributes_hash
authorize(record, :publish?)

if(record.kind_of?(Api))
record.handle_transition_https_on_publish!
end

new_config[category].reject! { |data| data["_id"] == record_id }
unless record.deleted_at?
new_config[category] << record.attributes_hash
end
end
end

new_config["apis"].sort_by! { |data| data["sort_order"].to_i }
new_config["website_backends"].sort_by! { |data| data["frontend_host"].to_i }

@config_version = ConfigVersion.publish!(new_config)
respond_with(:api_v1, @config_version, :root => "config_version", :location => api_v1_config_pending_changes_url)
Expand Down
Loading

0 comments on commit 9f6368f

Please sign in to comment.