Commit a186e033 authored by Christos Stavrakakis's avatar Christos Stavrakakis
Browse files

cyclades: Management commands for volume types

Create 'snf-manage' commands to handle volumes types. This commit adds
the following commands:

* volume-type-create
* volume-type-list
* volume-type-show
* volume-type-modify

Also, update 'volume-create' command to take as option the volume's
type.
parent 3744ed2b
......@@ -59,6 +59,7 @@ class Command(ListCommand):
user_uuid_field = "userid"
astakos_auth_url = ASTAKOS_AUTH_URL
astakos_token = ASTAKOS_TOKEN
select_related = ["flavor.volume_type"]
def get_ips(version, vm):
ips = []
......
......@@ -17,7 +17,7 @@ from django.core.management import CommandError
from synnefo.db.models import (Backend, VirtualMachine, Network,
Flavor, IPAddress, Subnet,
BridgePoolTable, MacPrefixPoolTable,
NetworkInterface, Volume)
NetworkInterface, Volume, VolumeType)
from functools import wraps
from django.conf import settings
......@@ -47,7 +47,8 @@ RESOURCE_MAP = {
"network": Network.objects,
"subnet": Subnet.objects,
"port": NetworkInterface.objects,
"floating-ip": IPAddress.objects.filter(floating_ip=True)}
"floating-ip": IPAddress.objects.filter(floating_ip=True),
"volume-type": VolumeType.objects}
def get_resource(name, value, for_update=False):
......
......@@ -459,3 +459,18 @@ def pprint_volume_in_ganeti(volume, stdout=None, title=None):
title=title)
vm.put_client(client)
def pprint_volume_type(volume_type, stdout=None, title=None):
if stdout is None:
stdout = sys.stdout
if title is None:
title = "Volume Type %s" % volume_type.id
vtype_info = OrderedDict([
("name", volume_type.name),
("disk template", volume_type.disk_template),
("deleted", volume_type.deleted),
])
pprint_table(stdout, vtype_info.items(), separator=" | ", title=title)
......@@ -61,6 +61,13 @@ class Command(SynnefoCommand):
dest="server_id",
default=None,
help="The ID of the server that the volume will be connected to."),
make_option(
"--volume-type",
dest="volume_type_id",
default=None,
help="The ID of the volume's type. If the volume will be attached"
" to a server, the volume's and the server's volume type"
" must match."),
make_option(
"--wait",
dest="wait",
......@@ -78,6 +85,7 @@ class Command(SynnefoCommand):
size = options.get("size")
user_id = options.get("user_id")
server_id = options.get("server_id")
volume_type_id = options.get("volume_type_id")
wait = parse_bool(options["wait"])
display_name = options.get("name", "")
......@@ -95,6 +103,8 @@ class Command(SynnefoCommand):
if user_id is None:
user_id = vm.userid
vtype = common.get_resource("volume-type", volume_type_id)
source_image_id = source_volume_id = source_snapshot_id = None
source = options.get("source")
if source is not None:
......@@ -119,6 +129,7 @@ class Command(SynnefoCommand):
source_image_id=source_image_id,
source_snapshot_id=source_snapshot_id,
source_volume_id=source_volume_id,
volume_type_id=vtype.id,
metadata={})
self.stdout.write("Created volume '%s' in DB:\n" % volume.id)
......
# Copyright (C) 2010-2014 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from optparse import make_option
from django.db import IntegrityError
from snf_django.management.commands import SynnefoCommand, CommandError
from synnefo.management import pprint, common
from synnefo.db.models import VolumeType
HELP_MSG = """Create a new Volume Type."""
class Command(SynnefoCommand):
help = HELP_MSG
option_list = SynnefoCommand.option_list + (
make_option(
"--name",
dest="name",
default=None,
help="The display name of the volume type."),
make_option(
"--disk-template",
dest="disk_template",
default=None,
help="The disk template of the volume type"),
)
@common.convert_api_faults
def handle(self, *args, **options):
if args:
raise CommandError("Command doesn't accept any arguments")
name = options.get("name")
disk_template = options.get("disk_template")
if name is None:
raise CommandError("Please specify the name of the volume type.")
if disk_template is None:
raise CommandError("Please specify the disk template of the volume"
" type.")
try:
vtype = VolumeType.objects.create(name=name,
disk_template=disk_template)
except IntegrityError as e:
raise CommandError("Failed to create volume type: %s" % e)
self.stdout.write("Created volume Type '%s' in DB:\n" % vtype.id)
pprint.pprint_volume_type(vtype, stdout=self.stdout)
# Copyright (C) 2010-2014 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#from optparse import make_option
from snf_django.management.commands import ListCommand
from synnefo.db.models import VolumeType
from logging import getLogger
log = getLogger(__name__)
def get_flavors(vtype):
return vtype.flavors.count()
def get_volumes(vtype):
return vtype.volumes.count()
def get_servers(vtype):
return vtype.servers.count()
class Command(ListCommand):
help = "List Volume Types"
option_list = ListCommand.option_list
object_class = VolumeType
deleted_field = "deleted"
select_related = ["volume_type"]
prefetch_related = ["flavors", "volumes"]
FIELDS = {
"id": ("id", "ID"),
"name": ("name", "Name"),
"disk_template": ("disk_template", "Disk template"),
"flavors": (get_flavors, "Number of flavors using this volume type"),
"volumes": (get_volumes, "Number of volumes using this volume type"),
"deleted": ("deleted", "Whether volume type is deleted or not"),
}
fields = ["id", "name", "disk_template", "flavors", "volumes", "deleted"]
# Copyright (C) 2010-2014 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from optparse import make_option
from django.core.management.base import CommandError
from django.db import transaction
from snf_django.management.commands import SynnefoCommand
from synnefo.management.common import get_resource
from snf_django.management.utils import parse_bool
from logging import getLogger
log = getLogger(__name__)
class Command(SynnefoCommand):
args = "<volume_type_id>"
help = "Modify a Volume Type"
option_list = SynnefoCommand.option_list + (
make_option(
"--deleted",
dest="deleted",
metavar="True|False",
choices=["True", "False"],
default=None,
help="Mark/unmark a volume type as deleted. Deleted volume types"
" cannot be used for creation of new volumes. All related"
" flavors will also be updated."),
)
@transaction.commit_on_success
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a volume type ID")
vtype = get_resource("volume-type", args[0], for_update=True)
deleted = options['deleted']
if deleted:
deleted = parse_bool(deleted)
self.stdout.write("Marking volume type '%s' and all related"
" flavors as deleted=%s\n" % (vtype.id, deleted))
vtype.deleted = deleted
vtype.save()
vtype.flavors.update(deleted=deleted)
# Copyright (C) 2010-2014 GRNET S.A.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from snf_django.management.commands import SynnefoCommand, CommandError
from synnefo.management.common import convert_api_faults
from synnefo.management import pprint, common
class Command(SynnefoCommand):
help = "Show Volume Type information"
args = "<VolumeType ID>"
@convert_api_faults
def handle(self, *args, **options):
if len(args) != 1:
raise CommandError("Please provide a volume ID")
volume_type = common.get_resource("volume-type", args[0])
pprint.pprint_volume_type(volume_type, stdout=self.stdout)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment