Skip to content

Commit

Permalink
Create volume and inventory api
Browse files Browse the repository at this point in the history
  • Loading branch information
anivargi authored Nov 17, 2016
1 parent c685780 commit d698b61
Show file tree
Hide file tree
Showing 10 changed files with 472 additions and 62 deletions.
8 changes: 8 additions & 0 deletions base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ class Base < Sinatra::Base
)
}

get '/ping' do
'pong'
end

get '/GetJobList' do

end

protected

def etcd
Expand Down
146 changes: 110 additions & 36 deletions cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,132 @@

class Cluster < Base

get '/ping' do
'pong'
get '/GetClusterList' do
clusters = []
etcd.get('/clusters', recursive: true).children.each do |cluster|
cluster_attrs = {}
tendrl_context = "#{cluster.key}/Tendrl_context"
clusters << recurse(etcd.get(tendrl_context))
end
respond_to do |f|
f.json { clusters.to_json }
end
end

get '/cluster/:cluster_id/:object_type/attributes' do
cluster = JSON.parse(etcd.get("/clusters/#{params[:cluster_id]}").value)
component = Tendrl::Component.new(cluster['sds_version'],
params[:object_type])

get '/:cluster_id/Flows' do
halt 404 if cluster(params[:cluster_id]).nil?
flows = Tendrl::Flow.find_all
respond_to do |f|
f.json { component.attributes.to_json }
f.json { flows.to_json }
end
end

get '/cluster/:cluster_id/:object_type/actions' do
cluster = JSON.parse(etcd.get("/clusters/#{params[:cluster_id]}").value)
component = Tendrl::Component.new(cluster['sds_version'],
params[:object_type])

get %r{\/([a-zA-Z0-9-]+)\/Get(\w+)List} do |cluster_id, object_name|
halt 404 if cluster(cluster_id).nil?
objects = []
etcd.get(
"#{@cluster.key}/#{object_name.pluralize}", recursive: true
).children.each do |node|
objects << recurse(node)
end
respond_to do |f|
f.json { component.actions.to_json }
f.json { objects.to_json }
end
end

post '/cluster/:cluster_id/:object_type/:action' do
cluster = JSON.parse(etcd.get("/clusters/#{params[:cluster_id]}").value)
component = Tendrl::Component.new(cluster['sds_version'],
params[:object_type])
post '/:cluster_id/:flow' do
halt 404 if cluster(params[:cluster_id]).nil?
flow = Tendrl::Flow.find_by_external_name_and_type(
params[:flow], 'cluster'
)
raise Sinatra::NotFound if flow.nil?
body = JSON.parse(request.body.read)
job_id = SecureRandom.uuid
etcd.set("/queue/#{job_id}", value: {
cluster_id: params[:cluster_id],
sds_nvr: cluster['sds_version'],
action: params[:action],
object_type: params[:object_type],
status: 'processing',
attributes: body.slice(*component.attributes.keys)
}.to_json)

job = {
job_id: job_id,
status: 'processing',
sds_nvr: cluster['sds_version'],
action: params[:action],
object_type: params[:object_type]
}
job_id = SecureRandom.hex
tendrl_context = context(params[:cluster_id])
job = etcd.set(
"/queue/#{job_id}",
value: {
cluster_id: params[:cluster_id],
status: 'new',
parameters: body.merge(tendrl_context),
run: flow.run,
type: 'sds',
created_from: 'API'
}.
to_json
)
respond_to do |f|
status 202
f.json { { job_id: job_id }.to_json }
end

end

delete '/:cluster_id/:flow' do
status 404 if cluster(params[:cluster_id]).nil?
flow = Tendrl::Flow.find_by_external_name_and_type(
params[:flow], 'cluster'
)
status 404 if flow.nil?
body = JSON.parse(request.body.read)
job_id = SecureRandom.hex
tendrl_context = context(params[:cluster_id])
job = etcd.set(
"/queue/#{job_id}",
value: {
cluster_id: params[:cluster_id],
status: 'new',
parameters: body.merge(tendrl_context),
run: flow.run,
type: 'sds',
created_from: 'API'
}.
to_json
)
respond_to do |f|
status 202
f.json { job.to_json }
f.json { { job_id: job_id }.to_json }
end

end

private

def recurse(node, attrs={})
node.children.each do |child|
if child.dir
recurse(child, attrs)
else
attrs[child.key.split('/')[-1]] = child.value
end
end
attrs
end

def context(cluster_id)
tendrl_context = "#{@cluster.key}/Tendrl_context"
attrs = recurse(etcd.get(tendrl_context))
{
'Tendrl_context.sds_name' => attrs['sds_name'],
'Tendrl_context.sds_version' => attrs['sds_version'],
'Tendrl_context.cluster_id' => attrs['cluster_id']
}
end

def cluster(cluster_id)
begin
@cluster ||= etcd.get("/clusters/#{cluster_id}")
load_definitions(cluster_id)
rescue Etcd::KeyNotFound
nil
end
end

def load_definitions(cluster_id)
definitions = etcd.get(
"/clusters/#{cluster_id}/definitions/data"
).value
Tendrl.cluster_definitions = YAML.load(definitions)
end

end
6 changes: 6 additions & 0 deletions lib/tendrl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,16 @@

module Tendrl

def self.current_definitions
@cluster_definitions || @node_definitions
end

def self.cluster_definitions
@cluster_definitions
end

def self.cluster_definitions=(definitions)
@node_definitions = nil
@cluster_definitions = definitions
end

Expand All @@ -24,6 +29,7 @@ def self.node_definitions
end

def self.node_definitions=(definitions)
@cluster_definitions = nil
@node_definitions = definitions
end

Expand Down
10 changes: 10 additions & 0 deletions lib/tendrl/attribute.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,15 @@ def initialize(object_type, name, attributes)
@required = nil
end

def to_hash
{
name: "#{@object_type}.#{@name}",
help: @help,
type: @type,
default: @default,
required: @required
}
end

end
end
24 changes: 15 additions & 9 deletions lib/tendrl/flow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ module Tendrl
class Flow

METHOD_MAPPING = { 'create' => 'POST', 'update' => 'PUT', 'delete' =>
'DELETE' }
'DELETE', 'action' => 'GET' }

attr_reader :namespace, :flow_name

def initialize(namespace, flow_name)
@instance = Tendrl.node_definitions
@instance = Tendrl.current_definitions
@namespace = namespace
@flow_name = flow_name
@flow = @instance[namespace]['flows'][flow_name]
Expand Down Expand Up @@ -79,8 +79,11 @@ def optional_attributes
optional_attributes.each do |ma|
next if ma.end_with?('cluster_id')
if ma.end_with?('[]')
flow_attributes << { name: ma, type: 'List',
required: false }
flow_attributes << {
name: ma,
type: 'List',
required: false
}
else
attribute = Object.find_by_attribute(ma)
attribute[:required] = false
Expand All @@ -93,12 +96,15 @@ def optional_attributes

def self.find_all
flows = []
Tendrl.node_definitions.keys.map do |key|
Tendrl.current_definitions.keys.map do |key|
if key.end_with?('_integration')
Tendrl.node_definitions[key]['flows'].keys.each do |fk|
Tendrl.current_definitions[key]['flows'].keys.each do |fk|
flow = Tendrl::Flow.new(key, fk)
flows << { name: flow.name, method: flow.method, attributes:
flow.attributes }
flows << {
name: flow.name,
method: flow.method,
attributes: flow.attributes
}
end
end
end
Expand All @@ -109,7 +115,7 @@ def self.find_by_external_name_and_type(external_name, type)
if type == 'node'
partial_namespace = 'namespace.tendrl.node_agent'
elsif type == 'cluster'
partial_namespace = 'namespace.tendrl.cluster_agent'
partial_namespace = 'namespace.tendrl'
end
sds_name, operation, object = external_name.underscore.split('_')
namespace = "#{partial_namespace}.#{sds_name}_integration"
Expand Down
24 changes: 18 additions & 6 deletions lib/tendrl/object.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Tendrl
class Object
def initialize(namespace, type)
@config = Tendrl.node_definitions
@config = Tendrl.current_definitions
@type = type
@namespace = namespace
@object = @config[namespace]['objects'][type]
Expand All @@ -19,12 +19,24 @@ def atoms
end
end

def self.find_by_object_name(object_name)
object = nil
Tendrl.current_definitions.keys.each do |key|
next if key == 'tendrl_schema_version'
objects = Tendrl.current_definitions[key]['objects'].keys
if objects.include?(object_name)
object = Object.new(key, object_name)
break
end
end
object
end

def self.find_by_attribute(attribute)
object, attribute = attribute.split('.')
config = Tendrl.node_definitions
objects = config['namespace.tendrl.node_agent']['objects']
attrs = objects[object]['attrs']
attrs[attribute].merge(name: "#{object}.#{attribute}")
object_name, attribute = attribute.split('.')
object = find_by_object_name(object_name)
attribute = object.attributes.find{|a| a.name == attribute }
attribute.to_hash
end

end
Expand Down
17 changes: 7 additions & 10 deletions node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ class Node < Base
get '/GetNodeList' do
nodes = []
etcd.get('/nodes').children.each do |node|
begin
hash = {}
etcd.get("#{node.key}/node_context").children.each do |child|
hash[child.key.split("#{node.key}/node_context/")[1]] = child.value
end
nodes << hash
rescue Etcd::KeyNotFound

node_attrs = {}
etcd.get("#{node.key}/Node_context").children.each do |child|
node_attrs[child.key.split("#{node.key}/Node_context/")[1]] = child.value
end
nodes << node_attrs
end
respond_to do |f|
f.json { nodes.to_json }
Expand All @@ -35,12 +31,13 @@ class Node < Base
flow = Tendrl::Flow.find_by_external_name_and_type(params[:flow], 'node')
raise Sinatra::NotFound if flow.nil?
body = JSON.parse(request.body.read)
body['Tendrl_context.cluster_id'] = SecureRandom.uuid
job_id = SecureRandom.hex
job = etcd.set(
"/queue/#{job_id}",
value: {
cluster_id: SecureRandom.uuid,
status: 'processing',
cluster_id: body['Tendrl_context.cluster_id'],
status: 'new',
parameters: body,
run: flow.run,
type: 'node',
Expand Down
Loading

0 comments on commit d698b61

Please sign in to comment.