Revision d05e5324 snf-cyclades-app/synnefo/logic/server_attachments.py

b/snf-cyclades-app/synnefo/logic/server_attachments.py
36 36
log = logging.getLogger(__name__)
37 37

  
38 38

  
39
@commands.server_command("ATTACH_VOLUME")
40 39
def attach_volume(vm, volume):
41 40
    """Attach a volume to a server.
42 41

  
......
60 59
        raise faults.BadRequest(msg)
61 60

  
62 61
    # Check maximum disk per instance hard limit
63
    if vm.volumes.filter(deleted=False).count() == settings.GANETI_MAX_DISKS_PER_INSTANCE:
62
    vm_volumes_num = vm.volumes.filter(deleted=False).count()
63
    if vm_volumes_num == settings.GANETI_MAX_DISKS_PER_INSTANCE:
64 64
        raise faults.BadRequest("Maximum volumes per server limit reached")
65 65

  
66
    jobid = backend.attach_volume(vm, volume)
66
    if volume.status == "CREATING":
67
        action_fields = {"disks": [("add", volume, {})]}
68
    comm = commands.server_command("ATTACH_VOLUME",
69
                                   action_fields=action_fields)
70
    return comm(_attach_volume)(vm, volume)
71

  
67 72

  
73
def _attach_volume(vm, volume):
74
    """Attach a Volume to a VM and update the Volume's status."""
75
    jobid = backend.attach_volume(vm, volume)
68 76
    log.info("Attached volume '%s' to server '%s'. JobID: '%s'", volume.id,
69 77
             volume.machine_id, jobid)
70

  
71 78
    volume.backendjobid = jobid
72 79
    volume.machine = vm
73
    volume.status = "ATTACHING"
80
    if volume.status == "AVAILALBE":
81
        volume.status = "ATTACHING"
82
    else:
83
        volume.status = "CREATING"
74 84
    volume.save()
75 85
    return jobid
76 86

  
77 87

  
78
@commands.server_command("DETACH_VOLUME")
79 88
def detach_volume(vm, volume):
80
    """Detach a volume to a server.
89
    """Detach a Volume from a VM
81 90

  
82 91
    The volume must be in 'IN_USE' status in order to be detached. Also,
83 92
    the root volume of the instance (index=0) can not be detached. This
......
87 96
    """
88 97

  
89 98
    _check_attachment(vm, volume)
90
    if volume.status != "IN_USE":
91
        #TODO: Maybe allow other statuses as well ?
99
    if volume.status not in ["IN_USE", "ERROR"]:
92 100
        raise faults.BadRequest("Cannot detach volume while volume is in"
93 101
                                " '%s' status." % volume.status)
94 102
    if volume.index == 0:
95 103
        raise faults.BadRequest("Cannot detach the root volume of a server")
104

  
105
    action_fields = {"disks": [("remove", volume, {})]}
106
    comm = commands.server_command("DETACH_VOLUME",
107
                                   action_fields=action_fields)
108
    return comm(_detach_volume)(vm, volume)
109

  
110

  
111
def _detach_volume(vm, volume):
112
    """Detach a Volume from a VM and update the Volume's status"""
96 113
    jobid = backend.detach_volume(vm, volume)
97 114
    log.info("Detached volume '%s' from server '%s'. JobID: '%s'", volume.id,
98 115
             volume.machine_id, jobid)
99 116
    volume.backendjobid = jobid
100
    volume.status = "DETACHING"
117
    if volume.delete_on_termination:
118
        volume.status = "DELETING"
119
    else:
120
        volume.status = "DETACHING"
101 121
    volume.save()
102 122
    return jobid
103 123

  
104 124

  
105 125
def _check_attachment(vm, volume):
106
    """Check that volume is attached to vm."""
126
    """Check that the Volume is attached to the VM"""
107 127
    if volume.machine_id != vm.id:
108 128
        raise faults.BadRequest("Volume '%s' is not attached to server '%s'"
109 129
                                % volume.id, vm.id)

Also available in: Unified diff