Exemple #1
0
def discovery():
    with open(app.config['DATA'] + '/v1.json') as template:
        discovery_template = json.loads(template.read())

    discovery_template[
        'baseUrl'] = helper.get_root_url() + '/' + app.config['PATH']
    discovery_template['basePath'] = '/' + app.config['PATH']
    discovery_template['rootUrl'] = helper.get_root_url() + '/'
    discovery_template['servicePath'] = app.config['PATH']

    return helper.create_response(data=discovery_template)
Exemple #2
0
def _create_instance_response(async_result, projectid, authorization):
    populated_response = {
        'kind': 'compute#operation',
        'id': async_result['jobid'],
        'operationType': 'insert',
        'name': async_result['jobid'],
        'user': async_result['userid'],
        'insertTime': async_result['created'],
        'startTime': async_result['created'],
        'selfLink': urllib.unquote_plus(helper.get_root_url() + url_for(
            'getoperations',
            projectid=projectid,
            operationid=async_result['jobid']
        ))
    }

    if async_result['jobstatus'] is 0:
        # handle pending case
        populated_response['targetLink'] = ''
        populated_response['status'] = 'PENDING'
        populated_response['progress'] = 0
    elif async_result['jobstatus'] is 1:
        # handle successful case
        populated_response['status'] = 'DONE'
        populated_response['id'] = async_result['jobid']
        populated_response['zone'] = urllib.unquote_plus(
            helper.get_root_url() +
            url_for(
                'getzone',
                projectid=projectid,
                zone=async_result['jobresult']['virtualmachine']['zonename'],
            ))
        populated_response['targetLink'] = urllib.unquote_plus(
            helper.get_root_url() +
            url_for(
                'getinstance',
                projectid=projectid,
                zone=async_result['jobresult']['virtualmachine']['zonename'],
                instance=async_result['jobresult']['virtualmachine']['displayname']))
        _add_sshkey_metadata(
            authorization=authorization,
            publickey=publickey_storage[projectid],
            instanceid=async_result['jobresult']['virtualmachine']['id']
        )

    return populated_response
Exemple #3
0
def _delete_instance_response(async_result, projectid):
    populated_response = {
        'kind': 'compute#operation',
        'insertTime': async_result['created'],
        'operationType': 'delete',
        'name': async_result['jobid'],
        'startTime': async_result['created'],
        'selfLink': urllib.unquote_plus(helper.get_root_url() + url_for(
            'getoperations',
            projectid=projectid,
            operationid=async_result['jobid']
        ))
    }

    if async_result['jobstatus'] is 0:
        # handle pending case
        populated_response['targetLink'] = ''
        populated_response['status'] = 'PENDING'
        populated_response['progress'] = 0
    elif async_result['jobstatus'] is 1:
        # handle successful case
        populated_response['status'] = 'DONE'
        populated_response['zone'] = urllib.unquote_plus(
            helper.get_root_url() +
            url_for(
                'getzone',
                projectid=projectid,
                zone=async_result['jobresult']['virtualmachine']['zonename'],
            ))
        populated_response['targetLink'] = urllib.unquote_plus(
            helper.get_root_url() +
            url_for(
                'getinstance',
                projectid=projectid,
                zone=async_result['jobresult']['virtualmachine']['zonename'],
                instance=async_result['jobresult']['virtualmachine']['displayname']))

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

    return populated_response
Exemple #4
0
def _cloudstack_machinetype_to_gce(cloudstack_response, projectid, zone):
    response = {}
    response['kind'] = 'compute#machineType'
    response['name'] = cloudstack_response['name']
    response['id'] = cloudstack_response['id']
    response['description'] = cloudstack_response['displaytext']
    response['creationTimestamp'] = cloudstack_response['created']
    response['guestCpus'] = cloudstack_response['cpunumber']
    response['memoryMb'] = cloudstack_response['memory']

    response['selfLink'] = urllib.unquote_plus(helper.get_root_url() + url_for(
        'getmachinetype',
        projectid=projectid,
        machinetype=cloudstack_response['name'],
        zone=zone
    ))
    response['zone'] = zone

    return response
Exemple #5
0
def setglobalmetadata(projectid, authorization):
    data = json.loads(request.data)
    publickey_storage[projectid] = data['items'][0]['value']
    data = data['items'][0]['value'].split(':')[1]

    command = 'deleteSSHKeyPair'
    args = {
        'name': projectid
    }

    requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret,
    )

    command = 'registerSSHKeyPair'
    args = {
        'name': projectid,
        'publickey': data
    }

    requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret,
    )

    res = jsonify({
        "kind": "compute#operation",
        'operationType': 'setMetadata',
        'targetLink': urllib.unquote_plus(helper.get_root_url() + url_for(
            'getproject',
            projectid=projectid
        )),
        'status': 'PENDING',
        'progress': 0
    })
    res.status_code = 200
    return res
Exemple #6
0
def _cloudstack_virtual_machine_to_gce(cloudstack_response, zone, projectid):
    response = {}
    response['kind'] = 'compute#instance'
    response['id'] = cloudstack_response['id']
    response['creationTimestamp'] = cloudstack_response['created']
    response['status'] = cloudstack_response['state'].upper()
    response['name'] = cloudstack_response['name']
    response['description'] = cloudstack_response['name']
    response['machineType'] = cloudstack_response['serviceofferingname']
    response['image'] = cloudstack_response['templatename']
    response['canIpForward'] = 'true'
    response['networkInterfaces'] = []
    response['disks'] = []

    networking = {}
    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'] = accessconfig

    response['networkInterfaces'].append(networking)

    response['selfLink'] = urllib.unquote_plus(helper.get_root_url() + url_for(
        'getinstance',
        projectid=projectid,
        instance=cloudstack_response['name'],
        zone=zone
    ))
    response['zone'] = zone

    return response
Exemple #7
0
def addnetwork(authorization, projectid):
    data = json.loads(request.data)
    args = {}
    args['name'] = data['name']
    args['description'] = data['description']

    network_result = _get_network(authorization, args)

    if 'errortext' in network_result['createsecuritygroupresponse']:
        populated_response = {
            'kind': 'compute#operation',
            'operationType': 'insert',
            'targetLink': '',
            'status': 'DONE',
            'progress': 100,
            'error': {
                'errors': [{
                    'code': 'RESOURCE_ALREADY_EXISTS',
                    'message': 'the resource \'projects/\'' + projectid + '/global/networks/' + args['name']
                }]
            }
        }
    else:
        populated_response = {
            'kind': 'compute#operation',
            'operationType': 'insert',
            'targetLink': urllib.unquote_plus(
                helper.get_root_url() + url_for(
                    'getnetwork',
                    projectid=projectid,
                    network=data['name']
                )
            ),
            'status': 'DONE',
            'progress': 100
        }

    return helper.create_response(data=populated_response)
Exemple #8
0
def _cloudstack_volume_to_gce(cloudstack_response, projectid, zone):
    response = {}
    response['kind'] = 'compute#disk'
    response['id'] = cloudstack_response['id']
    response['creationTimestamp'] = cloudstack_response['created']
    response['status'] = cloudstack_response['state'].upper()
    response['name'] = cloudstack_response['name']
    response['description'] = cloudstack_response['name']
    response['sizeGb'] = cloudstack_response['size']

    response['selfLink'] = urllib.unquote_plus(helper.get_root_url() + url_for(
        'getmachinetype',
        projectid=projectid,
        machinetype=cloudstack_response['name'],
        zone=zone
    ))

    if not zone:
        response['zone'] = cloudstack_response['zonename']
    else:
        response['zone'] = zone

    return response