#!/bin/bash
#
# Copyright (c) 2015 Mirantis Inc.
#
# 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.

export NETWORK=${1:-neutron}
export PLUGIN=${2:-fake}
export IS_TRANSIENT=true
export AUTO_SECURITY_GROUP=true
# Normalize network name
if [[ $NETWORK == nova* ]]; then
    export NETWORK="nova-network"
    export IS_TRANSIENT=false
    export AUTO_SECURITY_GROUP=false
fi

export DEST=${DEST:-$BASE/new}
export DEVSTACK_DIR=${DEVSTACK_DIR:-$DEST/devstack}
export SAHARA_DIR=${SAHARA_DIR:-$DEST/sahara}
export SAHARA_TESTS_DIR=${SAHARA_TESTS_DIR:-$DEST/sahara-tests}
export SAHARA_IMAGE_ELEMENTS_DIR=${SAHARA_IMAGE_ELEMENTS_DIR:-$DEST/sahara-image-elements}

source settings

export LOCALCONF_PATH=$DEVSTACK_DIR/local.conf

function sahara_build_image {
    cd $SAHARA_IMAGE_ELEMENTS_DIR

    sudo -E chown -R jenkins:stack $SAHARA_IMAGE_ELEMENTS_DIR

    ./tools/gate/build-images $PLUGIN
}

function sahara_register_image {
    openstack dataprocessing image register --username $SAHARA_IMAGE_USERNAME \
        $SAHARA_IMAGE_NAME
    openstack dataprocessing image tags add $SAHARA_IMAGE_NAME --tags \
        $SAHARA_PLUGIN_VERSION $SAHARA_PLUGIN_NAME
}

function sahara_register_flavor {
    nova flavor-create $SAHARA_FLAVOR_NAME $SAHARA_FLAVOR_ID $SAHARA_FLAVOR_RAM \
        $SAHARA_FLAVOR_DISK $SAHARA_FLAVOR_VCPUS
}
