#!/usr/libexec/platform-python
"""
OpenNebula Driver for Linstor
Copyright 2018 LINBIT USA LLC

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.
"""


import sys
from one import util, vm
from one.datastore import Datastore
from one.extender import get_rsc_name, make_resource, resize_disk


SRC = sys.argv[1]
SIZE = sys.argv[2]
VM_ID = int(sys.argv[3])
DATASTORE_ID = sys.argv[4]


def main():
    disk_id = SRC.split(".")[-1].strip()

    target_vm = vm.Vm(util.show_vm(VM_ID))
    datastore = Datastore(util.show_ds(DATASTORE_ID))

    res_name = get_rsc_name(target_vm, disk_id)

    util.log_info("Entering resize: Set '{r}' to size '{s}'.".format(r=res_name, s=SIZE))
    resource = make_resource(res_name, datastore.linstor_controllers, datastore.linstor_controller_auth_token)
    resize_disk(resource, target_vm, disk_id, int(SIZE))

    util.log_info("Leaving resize successfully.")


if __name__ == "__main__":
    util.run_main(main)
