from MyRequestsClass import MyRequests
import json
from time import sleep

def list_machines(uri, backend_id, cookie=None):
    req = MyRequests(uri=uri+'/backends/'+backend_id+"/machines", cookie=cookie)
    response = req.get()
    assert response.ok, u'\nGot %d Response Status: %s \n%s' % (response.status_code, response.reason, response.text)
    try:
        params = response.json()
        return params
    except Exception as e:
        assert False, u'Exception: %s' % e

#! disk and image_extra are required only for Linode
def create_machine(uri, backend_id, key_id, name, location, image, size, script=None, disk=None, image_extra=None, cookie=None):
    payload = {
        #'backend': backend_id,
        'key': key_id,
        'name': name,
        'location': location,
        'image': image,
        'size': size,
        'script': script,
        'disk': disk,
        'image_extra': image_extra
    }
    req = MyRequests(uri=uri+"/backends/"+backend_id+"/machines", cookie=cookie, data=json.dumps(payload), timeout=600)
    response = req.post()
    assert response.ok, u'\nGot %d Response Status: %s \n%s' % (response.status_code, response.reason, response.text)

    try:
        params = response.json()
        print "Created machine with Name: %s and id %s a" % (params['name'], params['id'])
        return params
    except Exception as e:
        assert False, u'Exception: %s' % e


def destroy_machine(uri, backend_id, machine_id, cookie=None):
    payload = {
        'action': 'destroy'
    }
    req = MyRequests(uri=uri+"/backends/"+backend_id+"/machines/"+machine_id, data=json.dumps(payload), cookie=cookie)
    response = req.post()
    assert response.ok, u'\nGot %d Response Status: %s \n%s' % (response.status_code, response.reason, response.text)
    print "Destroyed machine with id: %s" % machine_id


def reboot_machine(uri, backend_id, machine_id, cookie=None):
    payload = {
        'action': 'reboot'
    }
    req = MyRequests(uri=uri+"/backends/"+backend_id+"/machines/"+machine_id, cookie=cookie, data=json.dumps(payload))
    response = req.post()
    assert response.ok, u'\nGot %d Response Status: %s \n%s' % (response.status_code, response.reason, response.text)
    print "Rebooted machine with id: %s" % machine_id


def stop_machine(uri, backend_id, machine_id, cookie=None):
    payload = {
        'action': 'stop'
    }
    req = MyRequests(uri=uri+"/backends/"+backend_id+"/machines/"+machine_id, data=json.dumps(payload), cookie=cookie)
    response = req.post()
    assert response.ok, u'\nGot %d Response Status: %s \n%s' % (response.status_code, response.reason, response.text)
    print "Stopped machine with id: %s" % machine_id


def start_machine(uri, backend_id, machine_id, cookie=None):
    sleep(30)
    payload = {
        'action': 'start'
    }
    req = MyRequests(uri=uri+"/backends/"+backend_id+"/machines/"+machine_id, data=json.dumps(payload), cookie=cookie)
    response = req.post()
    if response.status_code == 503:
        print "Machine state is not yet ready to be started"
        return
    assert response.ok, u'\nGot %d Response Status: %s \n%s' % (response.status_code, response.reason, response.text)
    print "Started machine with id: %s" % machine_id

@@ -110,24 +110,35 @@
  cookie=None
+, csrf=None
 ):%0A    req =
@@ -185,32 +185,53 @@
 _id+%22/machines%22,
+%0A                    
  cookie=cookie)%0A
@@ -220,32 +220,43 @@
    cookie=cookie
+, csrf=csrf
 )%0A    response =
@@ -644,16 +644,35 @@
 e, size,
+%0A                  
  script=
@@ -709,32 +709,62 @@
 one, cookie=None
+,%0A                   csrf=None
 ):%0A    payload =
@@ -1073,16 +1073,37 @@
 chines%22,
+%0A                    
  cookie=
@@ -1147,16 +1147,48 @@
 eout=600
+,%0A                     csrf=csrf
 )%0A    re
@@ -1605,32 +1605,43 @@
 _id, cookie=None
+, csrf=None
 ):%0A    payload =
@@ -1746,32 +1746,53 @@
 es/%22+machine_id,
+%0A                    
  data=json.dumps
@@ -1807,32 +1807,43 @@
 ), cookie=cookie
+, csrf=csrf
 )%0A    response =
@@ -2080,32 +2080,43 @@
 _id, cookie=None
+, csrf=None
 ):%0A    payload =
@@ -2220,32 +2220,53 @@
 es/%22+machine_id,
+%0A                    
  cookie=cookie, 
@@ -2289,16 +2289,27 @@
 payload)
+, csrf=csrf
 )%0A    re
@@ -2555,24 +2555,35 @@
  cookie=None
+, csrf=None
 ):%0A    paylo
@@ -2689,32 +2689,53 @@
 es/%22+machine_id,
+%0A                    
  data=json.dumps
@@ -2750,32 +2750,43 @@
 ), cookie=cookie
+, csrf=csrf
 )%0A    response =
@@ -3024,24 +3024,35 @@
  cookie=None
+, csrf=None
 ):%0A    sleep
@@ -3181,16 +3181,37 @@
 hine_id,
+%0A                    
  data=js
@@ -3242,16 +3242,27 @@
 e=cookie
+, csrf=csrf
 )%0A    re

Add csrf parameter in machines requests