#!/usr/bin/env ruby

# -------------------------------------------------------------------------- #
# Copyright 2002-2025, 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 unregister a new IP network in the IPAM.
#
# STDIN Input:
#   - Base64 encoded XML with AR request
#
################################################################################
ONE_LOCATION = ENV['ONE_LOCATION'] unless defined?(ONE_LOCATION)

if !ONE_LOCATION
    LIB_LOCATION      ||= '/usr/lib/one'
    RUBY_LIB_LOCATION ||= '/usr/lib/one/ruby'
    GEMS_LOCATION     ||= '/usr/share/one/gems'
else
    LIB_LOCATION      ||= ONE_LOCATION + '/lib'
    RUBY_LIB_LOCATION ||= ONE_LOCATION + '/lib/ruby'
    GEMS_LOCATION     ||= ONE_LOCATION + '/share/gems'
end

# %%RUBYGEMS_SETUP_BEGIN%%
require 'load_opennebula_paths'
# %%RUBYGEMS_SETUP_END%%

$LOAD_PATH << RUBY_LIB_LOCATION
$LOAD_PATH << LIB_LOCATION + '/oneprovision/lib'

require 'net/http'
require 'uri'
require 'json'
require 'scaleway'
require 'base64'
require 'nokogiri'
require 'opennebula'
require 'oneprovision'
require 'ipaddr'
require 'digest'

begin
    data = Nokogiri::XML(Base64.decode64(STDIN.read))

    # --------------------------------------------------------------------------
    # Get connection details for the provider
    # --------------------------------------------------------------------------
    provision_id = data.xpath('//AR/PROVISION_ID').text

    if provision_id.empty?
        STDERR.puts 'Missing provision id in address range'
        exit(-1)
    end

    one       = OpenNebula::Client.new
    provision = OneProvision::Provision.new_with_id(provision_id, one)
    rc        = provision.info

    if OpenNebula.is_error?(rc)
        STDERR.puts rc.message
        exit(-1)
    end

    provider = provision.provider
    connect  = provider.body['connection']

    sw_secret_key = connect['secret_key']
    sw_zone       = connect['zone']

    # --------------------------------------------------------------------------
    # Connect to Scaleway and delete the Flexible IP
    # --------------------------------------------------------------------------

    scaleway_id = data.xpath('//AR/SCALEWAY_IP_ID').text.to_s

    if scaleway_id.empty?
        STDERR.puts 'Missing Scaleway range ID'
        exit(-1)
    end

    sw = Scaleway.new(sw_secret_key)
    resp = sw.api_call("/flexible-ip/v1alpha1/zones/#{sw_zone}/fips/#{scaleway_id}",
                       Net::HTTP::Delete)

    unless resp.code == '204'
        STDERR.puts "Scaleway API failure, HTTP #{resp.code}, #{resp.message}, #{resp.body}"
        exit(-1)
    end

    exit(0)
rescue StandardError => e
    STDERR.puts e.to_s
    exit(-1)
end
