Skip to content

Commit

Permalink
marklogic-community#593 Change deployment of REST options to use REST…
Browse files Browse the repository at this point in the history
… API
  • Loading branch information
dmcassel committed Apr 7, 2016
1 parent 3e444ed commit 7a8928b
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 41 deletions.
87 changes: 59 additions & 28 deletions deploy/lib/ml_rest.rb
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,37 @@ def install_properties(path)
end
end

def install_options(path)
@logger.info("Loading REST options in #{path}")
if (File.exists?(path))
Dir.foreach(path) do |item|
next if item == '.' or item == '..'

file = open("#{path}/#{item}", "rb")
ext = File.extname(item)
basename = File.basename(item, ext)

headers = {}
if (ext == '.xml')
headers['Content-Type'] = 'application/xml'
elsif (ext == '.json')
headers['Content-Type'] = 'application/json'
else
@logger.error("Unrecognized REST options format: #{item}")
end

contents = file.read

r = go("http://#{@hostname}:#{@port}/v1/config/query/#{basename}", "put", headers, nil, contents)
if (r.code.to_i < 200 && r.code.to_i > 206)
@logger.error("code: #{r.code.to_i} body:#{r.body}")
end
end
else
@logger.error "#{path} does not exist"
end
end

def install_extensions(path)
if (File.exists?(path))

Expand All @@ -82,35 +113,35 @@ def install_extensions(path)
data.each_with_index do |d, i|
file = open(d, "rb")
contents = file.read

file_ext = File.extname(d)[1..-1]
file_name = File.basename(d, ".*")

is_sjs = (file_ext == "sjs")
is_xsl = file_ext.include?("xsl")
next if is_xsl # XSLT rest extension not supported

@logger.debug "Deploying #{File.basename(d)}"

headers = {
'Content-Type' => (is_sjs ? 'application/vnd.marklogic-javascript' : 'application/xquery')
}
params = []

extensionName = file_name

if (is_sjs)

contents.scan(/@name\s+(\b\w*\b)/).each do |m|
if (!m[0].nil? || !m[0].to_s.empty?)
extensionName = m[0]
end
end

contents.scan(/exports+[.]+(GET|PUT|POST|DELETE)/).each do |m|
params << "method=#{m[0].downcase}"
end

# look for annotations of this form:
# /**
# * @param {string} myString The string
Expand All @@ -123,22 +154,22 @@ def install_extensions(path)
args << ":#{m[2]}=xs:#{m[1]}"
end
if (!m[0].nil? || !m[0].to_s.empty?)
args.each do |arg|
args.each do |arg|
params << "#{m[0].downcase}#{arg}"
end
args = []
end
end

else
# XQuery

extensionName = $1 if contents =~ /module\s*namespace\s*[\w\-]+\s*=\s*"http:\/\/marklogic.com\/rest-api\/resource\/([^"]+)"/

contents.scan(/function\s+[^:]+:(get|put|post|delete)/).each do |m|
params << "method=#{m[0]}"
end

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/declare\s+(\%\w+:\w+\(([\"\w\-\=\,\s:?*+]*)\))*\s*function\s+[^:]+:(get|put|post|delete)/m).each do |m|
Expand All @@ -158,19 +189,19 @@ def install_extensions(path)
end
end
end

end

@logger.debug "extensionName: #{extensionName}"
@logger.debug "headers: #{headers}"
@logger.debug "params: #{params}"

url = "http://#{@hostname}:#{@port}/v1/config/resources/#{extensionName}"
if (params.length > 0)
url << "?" << params.join("&")
end
@logger.debug "loading: #{d}"

r = go(url, "put", headers, nil, contents)
if (r.code.to_i < 200 && r.code.to_i > 206)
@logger.error("code: #{r.code.to_i} body:#{r.body}")
Expand All @@ -189,26 +220,26 @@ def install_transforms(path)

data.each_with_index do |d, i|
@logger.debug "Deploying #{File.basename(d)}"

file = open(d, "rb")
contents = file.read

file_ext = File.extname(d)[1..-1]
file_name = File.basename(d, ".*")

is_sjs = (file_ext == "sjs")
is_xsl = file_ext.include?("xsl")
is_xqy = file_ext.include?("xq")

headers = {
'Content-Type' => (is_sjs ? 'application/vnd.marklogic-javascript' : (is_xsl ? 'application/xslt+xml': 'application/xquery'))
}

transformName = file_name
params = []

if (is_sjs)

# look for annotations of this form:
# /**
# * @param {string} myString The string
Expand All @@ -221,15 +252,15 @@ def install_transforms(path)
args << ":#{m[2]}=xs:#{m[1]}"
end
if (!m[0].nil? || !m[0].to_s.empty?)
args.each do |arg|
args.each do |arg|
params << "#{m[0].downcase}#{arg}"
end
args = []
end
end

elsif (is_xsl)

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/<!--\s*(\%\w+:\w+\(([\"\w\-\=\,\s:?*+]*)\))*\s*-->/m).each do |m|
Expand All @@ -250,7 +281,7 @@ def install_transforms(path)
end

else # XQuery

# look for annotations of this form:
# %roxy:params("argname=type", "anotherarg=type")
contents.scan(/declare\s+(\%\w+:\w+\(([\"\w\-\=\,\s:?*+]*)\))*\s*function/m).each do |m|
Expand Down
32 changes: 19 additions & 13 deletions deploy/lib/server_config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1752,24 +1752,30 @@ def deploy_rest
return
end

if (@properties.has_key?('ml.rest-options.dir') && File.exist?(@properties['ml.rest-options.dir']))
prop_path = "#{@properties['ml.rest-options.dir']}/properties.xml"
if (File.exist?(prop_path))
mlRest.install_properties(ServerConfig.expand_path(prop_path))
end
load_data "#{@properties['ml.rest-options.dir']}/options",
:add_prefix => "/#{@properties['ml.group']}/#{@properties['ml.app-name']}/rest-api",
:remove_prefix => @properties['ml.rest-options.dir'],
:db => rest_modules_db
else
logger.info "\nNo REST API options found in: #{@properties['ml.rest-options.dir']}";
end

deploy_rest_config(rest_modules_db)
deploy_ext()
deploy_transform()
end
end

def deploy_rest_config (rest_modules_db)
if (@properties.has_key?('ml.rest-options.dir') && File.exist?(@properties['ml.rest-options.dir']))

prop_path = "#{@properties['ml.rest-options.dir']}/properties.xml"
if (File.exist?(prop_path))
mlRest.install_properties(ServerConfig.expand_path(prop_path))
end

options_path = "#{@properties['ml.rest-options.dir']}/options"
if (File.exist?(options_path))
mlRest.install_options(ServerConfig.expand_path(options_path))
end

else
logger.info "\nNo REST API options found in: #{@properties['ml.rest-options.dir']}";
end
end

def deploy_ext
extension = find_arg(['--file'])
path = @properties['ml.rest-ext.dir']
Expand Down

0 comments on commit 7a8928b

Please sign in to comment.