Skip to content
Permalink
Browse files
Add support for instance deploying, using ga api
  • Loading branch information
brogand93 committed May 27, 2014
1 parent 8ce7b03 commit 0b3dd84ea48c9d462bf9b31f54bc4cf5aa7d00f7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 55 deletions.
@@ -38,6 +38,7 @@ def _load_config_file():
app = Flask(__name__)
db = SQLAlchemy(app)
publickey_storage = {}
jobid_storage = {}


basedir = os.path.abspath(os.path.dirname(__file__))
@@ -118,18 +118,19 @@ def _cloudstack_virtual_machine_to_gce(cloudstack_response, zone, projectid):
response['disks'] = []

networking = {}
networking['network'] = cloudstack_response['securitygroup'][0]['name']
networking['networkIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['name'] = cloudstack_response['nic'][0]['id']
networking['accessConfigs'] = []
if cloudstack_response['securitygroup']:
networking['network'] = cloudstack_response['securitygroup'][0]['name']
networking['networkIP'] = cloudstack_response['nic'][0]['ipaddress']
networking['name'] = cloudstack_response['nic'][0]['id']
networking['accessConfigs'] = []

accessconfig = {}
accessconfig['kind'] = 'compute#accessConfig'
accessconfig['type'] = 'ONE_TO_ONE_NAT'
accessconfig['name'] = 'External NAT'
accessconfig['natIP'] = cloudstack_response['nic'][0]['ipaddress']

networking['accessConfigs'].append(accessconfig)
networking['accessConfigs'] = accessconfig

response['networkInterfaces'].append(networking)

@@ -163,11 +164,7 @@ def _get_virtual_machine_by_name(authorization, instance):
return None


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/aggregated/instances',
methods=['GET'])
@app.route('/' + app.config['PATH'] + '<projectid>/aggregated/instances', methods=['GET'])
@authentication.required
def aggregatedlistinstances(authorization, projectid):
zone_list = zones.get_zone_names(authorization=authorization)
@@ -199,11 +196,7 @@ def aggregatedlistinstances(authorization, projectid):
return helper.create_response(data=populated_response)


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/zones/<zone>/instances',
methods=['GET'])
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances', methods=['GET'])
@authentication.required
def listinstances(authorization, projectid, zone):
instance = None
@@ -250,11 +243,7 @@ def listinstances(authorization, projectid, zone):
return helper.create_response(data=populated_response)


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/zones/<zone>/instances/<instance>',
methods=['GET'])
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances/<instance>', methods=['GET'])
@authentication.required
def getinstance(projectid, authorization, zone, instance):
response = _get_virtual_machine_by_name(
@@ -278,18 +267,15 @@ def getinstance(projectid, authorization, zone, instance):
return errors.resource_not_found(function_route)


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/zones/<zone>/instances',
methods=['POST'])
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances', methods=['POST'])
@authentication.required
def addinstance(authorization, projectid, zone):
data = json.loads(request.data)
print data
args = {}
args['name'] = data['name']
args['serviceoffering'] = data['machineType'].rsplit('/', 1)[1]
args['template'] = data['image'].rsplit('/', 1)[1]
args['template'] = data['disks'][0]['initializeParams']['sourceImage'].rsplit('/', 1)[1]
args['zone'] = zone

network = data['networkInterfaces'][0]['network'].rsplit('/', 1)[1]
@@ -324,11 +310,7 @@ def addinstance(authorization, projectid, zone):
return helper.create_response(data=populated_response)


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/zones/<zone>/instances/<instance>',
methods=['DELETE'])
@app.route('/' + app.config['PATH'] + '<projectid>/zones/<zone>/instances/<instance>', methods=['DELETE'])
@authentication.required
def deleteinstance(projectid, authorization, zone, instance):
deletion_result = _destroy_virtual_machine(authorization, instance)
@@ -18,7 +18,7 @@
# under the License.

import urllib
from gstack import app, publickey_storage
from gstack import app, publickey_storage, jobid_storage
from gstack import authentication
from gstack.controllers import helper
from gstack.services import requester
@@ -39,10 +39,6 @@ def _get_async_result(authorization, args):
def _delete_instance_response(async_result, projectid):
populated_response = {
'kind': 'compute#operation',
'id': async_result['jobid'],
'name': async_result['jobid'],
'operationType': 'delete',
'user': async_result['userid'],
'insertTime': async_result['created'],
'startTime': async_result['created'],
'selfLink': urllib.unquote_plus(helper.get_root_url() + url_for(
@@ -54,11 +50,15 @@ def _delete_instance_response(async_result, projectid):

if async_result['jobstatus'] is 0:
# handle pending case
jobid_storage['jobid'] = async_result['jobid']
populated_response['name'] = 'cloudstackasyncrequest'
populated_response['targetLink'] = ''
populated_response['status'] = 'PENDING'
populated_response['progress'] = 0
elif async_result['jobstatus'] is 1:
# handle successful case
del jobid_storage['jobid']
populated_response['name'] = async_result['jobid'],
populated_response['status'] = 'DONE'
populated_response['zone'] = urllib.unquote_plus(
helper.get_root_url() +
@@ -88,11 +88,7 @@ def _add_sshkey_metadata(authorization, publickey, instanceid):
for datasegment in split_publickey:
print datasegment
_add_sshkey_metadata_segment(
authorization,
str(i) +
'-sshkey-segment',
datasegment,
instanceid)
authorization, str(i) + '-sshkey-segment', datasegment, instanceid)
i = i + 1


@@ -117,7 +113,6 @@ def _create_instance_response(async_result, projectid, authorization):
populated_response = {
'kind': 'compute#operation',
'id': async_result['jobid'],
'name': async_result['jobid'],
'operationType': 'insert',
'user': async_result['userid'],
'insertTime': async_result['created'],
@@ -131,12 +126,17 @@ def _create_instance_response(async_result, projectid, authorization):

if async_result['jobstatus'] is 0:
# handle pending case
jobid_storage['jobid'] = async_result['jobid']
populated_response['name'] = 'cloudstackasyncrequest'
populated_response['targetLink'] = ''
populated_response['status'] = 'PENDING'
populated_response['progress'] = 0
elif async_result['jobstatus'] is 1:
# handle successful case
del jobid_storage['jobid']
populated_response['name'] = async_result['jobid']
populated_response['status'] = 'DONE'
populated_response['id'] = async_result['jobid']
populated_response['zone'] = urllib.unquote_plus(
helper.get_root_url() +
url_for(
@@ -157,15 +157,16 @@ def _create_instance_response(async_result, projectid, authorization):
instanceid=async_result['jobresult']['virtualmachine']['id']
)

# need to add a case here for error handling, its job status 2

return populated_response


def create_response(authorization, projectid, operationid):
jobid = operationid
if 'jobid' in jobid_storage:
jobid = jobid_storage['jobid']
async_result = _get_async_result(
authorization=authorization,
args={'jobId': operationid}
args={'jobId': jobid}
)

command_name = None
@@ -190,11 +191,7 @@ def create_response(authorization, projectid, operationid):
return populated_response


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/global/operations/<operationid>',
methods=['GET'])
@app.route('/' + app.config['PATH'] + '<projectid>/global/operations/<operationid>', methods=['GET'])
@authentication.required
def getoperations(authorization, operationid, projectid):
return helper.create_response(create_response(
@@ -164,6 +164,7 @@ def getproject(authorization, projectid):
if project:
metadata = {}
metadata['sshKeys'] = _list_ssh_keys(authorization)
publickey_storage[projectid] = metadata['sshKeys']

res = jsonify(_cloudstack_project_to_gce(project, metadata))
res.status_code = 200
@@ -177,11 +178,7 @@ def getproject(authorization, projectid):
return res


@app.route(
'/' +
app.config['PATH'] +
'<projectid>/setCommonInstanceMetadata',
methods=['POST'])
@app.route('/' + app.config['PATH'] + '<projectid>/setCommonInstanceMetadata', methods=['POST'])
@authentication.required
def setglobalmetadata(projectid, authorization):
data = json.loads(request.data)

0 comments on commit 0b3dd84

Please sign in to comment.