#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2016, OpenNebula Project, OpenNebula Systems                #
#                                                                            #
# Licensed under the Apache License, Version 2.0 (the "License"); you may    #
# not use this file except in compliance with the License. You may obtain    #
# a copy of the License at                                                   #
#                                                                            #
# http://www.apache.org/licenses/LICENSE-2.0                                 #
#                                                                            #
# Unless required by applicable law or agreed to in writing, software        #
# distributed under the License is distributed on an "AS IS" BASIS,          #
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
# See the License for the specific language governing permissions and        #
# limitations under the License.                                             #
# -------------------------------------------------------------------------- #

###############################################################################
# This script is used to import a file into the marketplace. The source file
# is an opaque representation of an OpenNebula object, like a image file or a
# tar.gz with several vm template or flow disk images
###############################################################################

ONE_LOCATION = ENV["ONE_LOCATION"]

if !ONE_LOCATION
    RUBY_LIB_LOCATION = "/usr/lib/one/ruby"
else
    RUBY_LIB_LOCATION = ONE_LOCATION + "/lib/ruby"
end

UTILS_PATH = File.join(File.dirname(__FILE__), '../../datastore')

$: << RUBY_LIB_LOCATION
$: << RUBY_LIB_LOCATION+"/cli"
$: << File.dirname(__FILE__)

TOTAL_MB_DEFAULT = 1048576 # Default maximum 1TB

require 'base64'
require 'rexml/document'
require 'getoptlong'
require 'pp'

require 'S3'

def xpath(xml, xpath)
    xml.elements[xpath].text.to_s rescue nil
end

xml = REXML::Document.new(Base64::decode64(ARGV[0])).root

# required
access_key_id     = xpath(xml, 'MARKETPLACE/TEMPLATE/ACCESS_KEY_ID')
secret_access_key = xpath(xml, 'MARKETPLACE/TEMPLATE/SECRET_ACCESS_KEY')
bucket            = xpath(xml, 'MARKETPLACE/TEMPLATE/BUCKET')
region            = xpath(xml, 'MARKETPLACE/TEMPLATE/REGION')
total_mb          = xpath(xml, 'MARKETPLACE/TEMPLATE/TOTAL_MB') || TOTAL_MB_DEFAULT

# optional
signature_version = xpath(xml, 'MARKETPLACE/TEMPLATE/SIGNATURE_VERSION')
endpoint          = xpath(xml, 'MARKETPLACE/TEMPLATE/ENDPOINT')
force_path_style  = xpath(xml, 'MARKETPLACE/TEMPLATE/FORCE_PATH_STYLE')

s3_config = {
    :region            => region,
    :access_key_id     => access_key_id,
    :secret_access_key => secret_access_key
}

s3_config[:signature_version] = signature_version if !signature_version.to_s.empty?
s3_config[:endpoint]          = endpoint if !endpoint.to_s.empty?
s3_config[:force_path_style]  = true if force_path_style.to_s.downcase == "yes"

s3 = S3.new(s3_config)

s3.bucket = bucket

used_mb = (s3.get_bucket_size.to_f/1024/1024).ceil
free_mb = total_mb - used_mb

puts <<EOF
USED_MB="#{used_mb}"
FREE_MB="#{free_mb}"
TOTAL_MB="#{total_mb}"
EOF
