Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from idb-project/feature/cloudproviders
Feature cloudproviders
- Loading branch information
Showing
26 changed files
with
697 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
module V2 | ||
class API < Grape::API | ||
mount V2::Machines | ||
mount V2::CloudProviders | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
module V2 | ||
class CloudProviders < Grape::API | ||
helpers do | ||
def get_token | ||
if params[:idb_api_token] | ||
return params[:idb_api_token] | ||
elsif request.headers["X-Idb-Api-Token"] | ||
return request.headers["X-Idb-Api-Token"] | ||
else | ||
error!("Unauthorized.", 401) | ||
end | ||
end | ||
|
||
def authenticate! | ||
token = params[:idb_api_token] ? params[:idb_api_token] : request.headers["X-Idb-Api-Token"] | ||
if ApiToken.where("token = ?", token).empty? | ||
error!("Unauthorized.", 401) | ||
end | ||
end | ||
|
||
def can_read! | ||
token = params[:idb_api_token] ? params[:idb_api_token] : request.headers["X-Idb-Api-Token"] | ||
unless ApiToken.where("token = ?", token).first.read | ||
error!("Unauthorized.", 401) | ||
end | ||
end | ||
|
||
def can_write! | ||
token = params[:idb_api_token] ? params[:idb_api_token] : request.headers["X-Idb-Api-Token"] | ||
unless ApiToken.where("token = ?", token).first.write | ||
error!("Unauthorized.", 401) | ||
end | ||
end | ||
end | ||
|
||
version 'v2' | ||
format :json | ||
|
||
resource :cloud_providers do | ||
desc "Return a list of all cloud providers" | ||
get do | ||
authenticate! | ||
can_read! | ||
unless IDB.config.modules.api.v2_enabled | ||
status 501 | ||
return {} | ||
end | ||
|
||
p = params.to_hash | ||
|
||
unless p["owner"].to_i != 0 | ||
CloudProvider.all | ||
else | ||
m = CloudProvider.where("owner_id = ?", p["owner"].to_i) | ||
if m.empty? | ||
status 404 | ||
{} | ||
else | ||
m | ||
end | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
class CloudProvidersController < ApplicationController | ||
def index | ||
@cloud_providers = CloudProvider.all | ||
end | ||
|
||
def new | ||
@cloud_provider = CloudProvider.new | ||
@cloud_providers = {} | ||
@all_cloud_providers = CloudProvider.all | ||
@owners = Owner.all | ||
end | ||
|
||
def create | ||
@cloud_provider = CloudProvider.new(params.require(:cloud_provider).permit(:name, :description, :owner_id, :config, :apidocs)) | ||
@owners = Owner.all | ||
|
||
if params["check"] == "json" | ||
begin | ||
JSON.parse(@cloud_provider.config) | ||
rescue JSON::ParserError | ||
flash.alert = "JSON validation failed, check the config for errors!" | ||
render :new | ||
return | ||
end | ||
end | ||
|
||
if @cloud_provider.save | ||
flash.notice = "Cloud provider created" | ||
redirect_to cloud_providers_path | ||
else | ||
render :new | ||
end | ||
end | ||
|
||
def show | ||
@cloud_provider = CloudProvider.find(params[:id]) | ||
@all_cloud_providers = CloudProvider.all | ||
end | ||
|
||
def edit | ||
@cloud_provider = CloudProvider.find(params[:id]) | ||
@cloud_providers = {} | ||
@all_cloud_providers = CloudProvider.all | ||
@owners = Owner.all | ||
end | ||
|
||
def update | ||
@cloud_provider = CloudProvider.find(params[:id]) | ||
@owners = Owner.all | ||
|
||
if params["check"] == "json" | ||
begin | ||
JSON.parse(params[:cloud_provider][:config]) | ||
rescue JSON::ParserError | ||
flash.alert = "JSON validation failed, check the config for errors!" | ||
render :new | ||
return | ||
end | ||
end | ||
|
||
if @cloud_provider.update(params.require(:cloud_provider).permit(:name, :description, :owner_id, :config, :apidocs)) | ||
redirect_to cloud_providers_path, notice: 'Cloud Provider updated.' | ||
else | ||
render :edit | ||
end | ||
end | ||
|
||
def destroy | ||
@cloud_provider = CloudProvider.find(params[:id]) | ||
@cloud_provider.destroy | ||
|
||
redirect_to cloud_providers_path, notice: 'Cloud Provider deleted!' | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
class CloudProvider < ActiveRecord::Base | ||
validates :name, presence: true, uniqueness: true | ||
belongs_to :owner | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
|
||
class CloudProviderPresenter < Keynote::Presenter | ||
presents :cloud_provider | ||
|
||
delegate :id, :name, :owner, :description, :config, | ||
to: :cloud_provider | ||
|
||
def name_link | ||
link_to(cloud_provider.name, cloud_provider) | ||
end | ||
|
||
def apidocs_link | ||
link_to(cloud_provider.apidocs, cloud_provider.apidocs).html_safe | ||
end | ||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<div id="cloud-provider-form"> | ||
|
||
<div class="row-fluid"> | ||
<div class="span12"> | ||
<div class="navbar"> | ||
<div class="navbar-inner"> | ||
<div class="container-fluid"> | ||
<%- if @cloud_provider.new_record? -%> | ||
<a class="brand" href="<%= new_cloud_provider_path %>">New Cloud Provider</a> | ||
<%- else-%> | ||
<a class="brand" href="<%= edit_cloud_provider_path(@cloud_provider) %>"><%= @cloud_provider.name %></a> | ||
<%- end -%> | ||
<ul class="nav pull-right"> | ||
<%- unless @cloud_provider.new_record? -%> | ||
<li><%= link_to '<i class="icon-remove-sign"></i> Delete'.html_safe, cloud_providers_path(@cloud_provider), class: 'cloud-provider-delete', method: 'delete' %></li> | ||
<li><%= link_to '<i class="icon-level-up"></i> Cancel'.html_safe, cloud_providers_path(@cloud_provider) %></li> | ||
<%- else -%> | ||
<li><%= link_to '<i class="icon-level-up"></i> Cancel'.html_safe, cloud_providers_path(@cloud_provider) %></li> | ||
<%- end -%> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="tab-content"> | ||
<div class="tab-pane active" id="base"> | ||
<div class="row-fluid"> | ||
<div class="span6"> | ||
<%= simple_form_for(@cloud_provider, html: {class: 'form-horizontal', multipart: true }, | ||
defaults: {input_html: {class: 'span6'}}) do |f| %> | ||
<%= f.input :name %> | ||
<%= f.input :owner_id, collection: @owners.order(:name), include_blank: false, label: "Owner" %> | ||
<%= f.input :description %> | ||
<%= f.input :apidocs, label: "API Documentation" %> | ||
<%= f.input :config %> | ||
<div class="control-group text optional cloud_provider_check"> | ||
<label class="text optional control-label" for="cloud_provider_check">Validate JSON config</label> | ||
<div class="controls"> | ||
<%= check_box_tag 'check', 'json', true %> | ||
</div> | ||
</div> | ||
<%= f.button :submit %> | ||
<%- end -%> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<table id="cloud-provider-table" class="table tablesorter custom-popup"> | ||
<thead> | ||
<tr> | ||
<th>Name</th> | ||
<th>Owner</th> | ||
<th>API Documentation</th> | ||
<th>Description</th> | ||
</tr> | ||
</thead> | ||
<tbody> | ||
<%- cloud_providers.each do |cloud_provider| -%> | ||
<%- owner = k(:owner, cloud_provider.owner) -%> | ||
<%- cloud_provider = k(:cloud_provider, cloud_provider) -%> | ||
<tr id="#cloud-provider-<%= cloud_provider.name %>"> | ||
<td><%= cloud_provider.name_link %></td> | ||
<td><%= owner.name_link %></td> | ||
<td><%= cloud_provider.apidocs_link %></td> | ||
<td><%= cloud_provider.description %></td> | ||
</tr> | ||
<%- end -%> | ||
</tbody> | ||
</table> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render 'form' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<div id="cloud_providers-index"> | ||
|
||
<div class="row-fluid"> | ||
<div class="span12"> | ||
<div class="navbar"> | ||
<div class="navbar-inner"> | ||
<div class="container-fluid"> | ||
<a class="brand" href="<%= cloud_providers_path %>">Cloud Providers</a> | ||
<ul class="nav"> | ||
<li> | ||
<div class="columnSelectorWrapper"> | ||
<input id="colSelect1" type="checkbox" class="hidden"> | ||
<label style="margin-bottom: 0px; color: #777777; margin-right: 15px;" class="columnSelectorButton" for="colSelect1"><i class="icon-cog"> </i>Columns</label> | ||
<div id="columnSelector" class="columnSelector"></div> | ||
</div> | ||
</li> | ||
<li><%= link_to '<i class="icon-plus"></i> New Cloud Provider'.html_safe, new_cloud_provider_path %></li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="row-fluid"> | ||
<div class="span12"> | ||
<%= render partial: 'table', locals: {cloud_providers: @cloud_providers} %> | ||
</div> | ||
</div> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<%= render 'form' %> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
<div id="cloud-provider-show"> | ||
|
||
<div class="row-fluid tabbable"> | ||
<div class="span12"> | ||
<div class="navbar"> | ||
<div class="navbar-inner"> | ||
<div class="container-fluid"> | ||
<span class="brand"><%= @cloud_provider.name %></span> | ||
<ul class="nav pull-right"> | ||
<li><%= link_to '<i class="icon-edit"></i> Edit'.html_safe, edit_cloud_provider_path(@cloud_provider) %></li> | ||
<li><%= link_to '<i class="icon-remove-sign"></i> Delete'.html_safe, cloud_provider_path(@cloud_provider), class: 'cloud-provider-delete', method: 'delete' %></li> | ||
<li><%= link_to '<i class="icon-level-up"></i> Cancel'.html_safe, cloud_providers_path %></li> | ||
</ul> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<div class="row-fluid"> | ||
<div class="span6 tab-content"> | ||
<div class="tab-pane active" id="data"> | ||
<table class="table table-condensed"> | ||
<tr> | ||
<th>Name</th> | ||
<th>Owner</th> | ||
<th>API Documentation</th> | ||
<th>Description</th> | ||
</tr> | ||
<tr> | ||
<%- owner = k(:owner, @cloud_provider.owner) -%> | ||
<%- cloud_provider = k(:cloud_provider, @cloud_provider) -%> | ||
<td><%= @cloud_provider.name %></td> | ||
<td><%= owner.name_link %></td> | ||
<td><%= cloud_provider.apidocs_link %></td> | ||
<td><%= @cloud_provider.description %></td> | ||
</tr> | ||
</table> | ||
</div> | ||
<h5>Configuration</h5> | ||
<pre><code><%= @cloud_provider.config %></code></pre> | ||
</div> | ||
</div> | ||
|
||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.