def post(self, id): """ Create snapshot """ data = request.get_json()['data'] current_identity = import_user() container = Container.query.get(id) # If exists client = lgw.lxd_client() c = client.containers.get(container.name) if c.name and (id in current_identity.containers or current_identity.admin): snapname = data['attributes']['name'] if not c.snapshots.create(snapname): api.abort(code=409, message='Snapshot name already exists') else: time.sleep(20) snap = c.snapshots.get(snapname) snapshot_json = { 'attributes': { 'name': snap.name, 'created_at': snap.created_at, 'stateful': snap.stateful } } return {'data': snapshot_json} api.abort(code=500, message='Can\'t create container')
def get(self, id, name): """ Get snapshot """ current_identity = import_user() container = Container.query.get(id) #If exists client = lgw.lxd_client() c = client.containers.get(container.name) if c.name and (id in current_identity.containers or current_identity.admin): try: snap = c.snapshots.get(name) snapshot_json = { 'attributes': { 'name': snap.name, 'created_at': snap.created_at, 'stateful': snap.stateful } } except: api.abort(code=404, message='Snapshot doesn\'t exists') return {'data': snapshot_json} api.abort(code=404, message='Container doesn\'t exists')
def get(self, id): """ Get snapshot list """ current_identity = import_user() container = Container.query.get(id) snapshots = [] #If exists client = lgw.lxd_client() c = client.containers.get(container.name) if c.name and (id in current_identity.containers or current_identity.admin): all = c.snapshots.all() for snap in all: snapshot_json = { 'attributes': { 'name': snap.name, 'created_at': snap.created_at, 'stateful': snap.stateful } } snapshots.append(snapshot_json) return {'data': snapshots}
def post(self, id, name): """ Restore snapshot """ current_identity = import_user() container = Container.query.get(id) client = lgw.lxd_client() c = client.containers.get(container.name) if c.name and (id in current_identity.containers or current_identity.admin): try: if name == c.snapshots.get(name).name: try: c.raw_put({'restore': name}) time.sleep(20) return {}, 204 except: api.abort(code=500, message='Error restoring snapshot') except: api.abort(code=404, message='Snapshot doesn\'t exists') api.abort(code=404, message='Container doesn\'t exists')
def put(self, id, name, d=None): """ Update snapshot """ client = lgw.lxd_client() if d: data = d else: data = request.get_json()['data'] current_identity = import_user() container = Container.query.get(id) c = client.containers.get(container.name) if c.name and (id in current_identity.containers or current_identity.admin): if 'name' in data['attributes']: try: snap = c.snapshots.get(name) except: api.abort(code=404, message='Snapshot doesn\'t exists') if name == snap.name: snap.rename(data['attributes']['name']) else: api.abort(code=500, message='Error while rename snapshot') snapshot_json = { 'attributes': { 'name': snap.name, 'created_at': snap.created_at, 'stateful': snap.stateful } } return {'data': snapshot_json} api.abort(code=404, message='Container doesn\'t exists')