summaryrefslogtreecommitdiff
path: root/opendc/api/v2/paths/pathId/branches/endpoint.py
blob: 6b0eebbd5e96f4802d91c2047468606dde213e56 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
from datetime import datetime

from opendc.models_old.datacenter import Datacenter
from opendc.models_old.machine import Machine
from opendc.models_old.object import Object
from opendc.models_old.path import Path
from opendc.models_old.rack import Rack
from opendc.models_old.room import Room
from opendc.models_old.section import Section
from opendc.models_old.tile import Tile
from opendc.util import database, exceptions
from opendc.util.rest import Request, Response


def POST(request):
    """Create a new Path that branches off of this Path at the specified tick."""

    # Make sure required parameters are there

    try:
        request.check_required_parameters(path={'pathId': 'int'}, body={'section': {'startTick': 'int'}})

    except exceptions.ParameterError as e:
        return Response(400, str(e))

    # Instantiate the current Path from the database

    current_path = Path.from_primary_key((request.params_path['pathId'], ))

    # Make sure the current Path exists

    if not current_path.exists():
        return Response(404, '{} not found.'.format(current_path))

    # Make sure this user is authorized to branch off the current Path

    if not current_path.google_id_has_at_least(request.google_id, 'EDIT'):
        return Response(403, 'Forbidden from branching off {}.'.format(current_path))

    # Create the new Path

    new_path = Path(simulation_id=current_path.simulation_id,
                    datetime_created=database.datetime_to_string(datetime.now()))

    new_path.insert()

    # Get the current Path's sections and add them to the new Path if they're before the branch

    current_sections = Section.query('path_id', current_path.id)
    last_section = None

    for current_section in current_sections:

        if current_section.start_tick < request.params_body['section']['startTick'] or current_section.start_tick == 0:
            new_section = Section(path_id=new_path.id,
                                  datacenter_id=current_section.datacenter_id,
                                  start_tick=current_section.start_tick)

            new_section.insert()

            last_section = current_section

    # Make a deep copy of the last section's datacenter, its rooms, their tiles, etc.

    path_parameters = {'simulationId': new_path.simulation_id}

    # Copy the Datacenter

    old_datacenter = Datacenter.from_primary_key((last_section.datacenter_id, ))

    message = old_datacenter.generate_api_call(path_parameters, request.token)
    response = Request(message).process()

    path_parameters['datacenterId'] = response.content['id']

    # Create the new last Section, with the IDs of the new Path and new Datacenter

    if last_section.start_tick != 0:
        new_section = Section(path_id=new_path.id,
                              datacenter_id=path_parameters['datacenterId'],
                              start_tick=request.params_body['section']['startTick'])

        new_section.insert()

    else:
        last_section.datacenter_id = path_parameters['datacenterId']
        last_section.update()

    # Copy the rest of the Datacenter, starting with the Rooms...

    old_rooms = Room.query('datacenter_id', old_datacenter.id)

    for old_room in old_rooms:

        old_room.datacenter_id = path_parameters['datacenterId']

        if old_room.topology_id is None:
            old_room.topology_id = old_room.id

        message = old_room.generate_api_call(path_parameters, request.token)
        response = Request(message).process()

        path_parameters['roomId'] = response.content['id']

        # ... then the Tiles, ...

        old_tiles = Tile.query('room_id', old_room.id)

        for old_tile in old_tiles:

            old_tile.room_id = path_parameters['roomId']

            if old_tile.topology_id is None:
                old_tile.topology_id = old_tile.id

            message = old_tile.generate_api_call(path_parameters, request.token)
            response = Request(message).process()

            path_parameters['tileId'] = response.content['id']

            old_objects = Object.query('id', old_tile.object_id)

            # ... then the Tile's Rack, ...

            if len(old_objects) == 1 and old_objects[0].type == 'RACK':

                old_rack = Rack.query('id', old_objects[0].id)[0]

                if old_rack.topology_id is None:
                    old_rack.topology_id = old_rack.id

                message = old_rack.generate_api_call(path_parameters, request.token)
                response = Request(message).process()

                path_parameters['rackId'] = response.content['id']

                # ... then the Rack's Machines ...

                old_machines = Machine.query('rack_id', old_rack.id)

                for old_machine in old_machines:
                    old_machine.read()
                    old_machine.rack_id = path_parameters['rackId']

                    if old_machine.topology_id is None:
                        old_machine.topology_id = old_machine.id

                    message = old_machine.generate_api_call(path_parameters, request.token)
                    response = Request(message).process()

                    path_parameters['machineId'] = response.content['id']

    # Return the new Path

    return Response(200, 'Successfully created {}.'.format(new_path), new_path.to_JSON())