Esempio n. 1
0
def createsecuritygroup(projectid, authorization):
    command = 'createSecurityGroup'
    res = json.loads(request.data)
    args = {'name': res['name'],
            'description': res['description']}
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )

    cloudstack_response = cloudstack_response

    app.logger.debug(
        'Processing request for creating a Firewall \n'
        'Project: ' + projectid + '\n' +
        'Firewall: ' + res['name'] + '\n' +
        json.dumps(cloudstack_response, indent=4, separators=(',', ': '))
    )

    net_protocol_codes = {'1': 'icmp', '6': 'tcp', '17': 'udp'}

    rules = res['allowed']
    if rules is not []:
        for rule in rules:
            command = 'authorizeSecurityGroupIngress'
            args = {'securitygroupname': res['name'],
                    'protocol': net_protocol_codes[str(rule['IPProtocol'])],
                    'startport': rule['ports'][0],
                    'endport': rule['ports'][0],
                    'cidrlist': ','.join([cidr for cidr in
                                          res['sourceRanges']])}
            cloudstack_response = requester.make_request(
                command,
                args,
                authorization.client_id,
                authorization.client_secret
            )

            cloudstack_response = cloudstack_response

            app.logger.debug(
                'Processing request for adding a rule to a Firewall \n'
                'Project: ' + projectid + '\n' +
                'Firewall: ' + res['name'] + '\n' +
                json.dumps(cloudstack_response,
                           indent=4, separators=(',', ': '))
            )

    # return Global Operations
    populated_response = {}
    res = jsonify(populated_response)
    res.status_code = 200
    return res
Esempio n. 2
0
def _add_sshkey_metadata_segment(authorization, keyname, value, instanceid):
    command = 'createTags'
    args = {
        'tags[0].key': keyname,
        'tags[0].value': value,
        'resourceids': instanceid,
        'resourcetype': 'UserVm'
    }

    requester.make_request(command, args, authorization.client_id,
                           authorization.client_secret)
Esempio n. 3
0
def _add_sshkey_metadata_segment(authorization, keyname, value, instanceid):
    command = 'createTags'
    args = {
        'tags[0].key': keyname,
        'tags[0].value': value,
        'resourceids': instanceid,
        'resourcetype': 'UserVm'
    }

    requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )
Esempio n. 4
0
def _deploy_virtual_machine(authorization, args, projectid):
    command = 'deployVirtualMachine'

    converted_args = {}
    template = images.get_template_by_name(authorization=authorization,
                                           image=args['template'])
    converted_args['templateid'] = template['id']

    zone = zones.get_zone_by_name(authorization=authorization,
                                  zone=args['zone'])
    converted_args['zoneid'] = zone['id']

    serviceoffering = machine_type.get_machinetype_by_name(
        authorization=authorization, machinetype=args['serviceoffering'])
    converted_args['serviceofferingid'] = serviceoffering['id']

    if 'network' in args:
        network = networks.get_network_by_name(authorization=authorization,
                                               network=args['network'])
        converted_args['securitygroupids'] = network['id']

    converted_args['displayname'] = args['name']
    converted_args['name'] = args['name']
    converted_args['keypair'] = projectid

    cloudstack_response = requester.make_request(command, converted_args,
                                                 authorization.client_id,
                                                 authorization.client_secret)

    return cloudstack_response
    def validate_client_secret(self, client_id, client_secret):
        command = 'listCapabilities'
        args = {}
        cloudstack_response = requester.make_request(
            command,
            args,
            client_id,
            client_secret
        )

        if cloudstack_response:
            existing_client = Client.query.get(client_id)

            if existing_client is not None:
                existing_client.client_secret = client_secret
            else:
                client = Client(
                    client_id,
                    client_secret
                )
                db.session.add(client)

            db.session.commit()
            return True
        else:
            return False
Esempio n. 6
0
def getsecuritygroup(projectid, authorization, firewall):
    command = 'listSecurityGroups'
    args = {
        'securitygroupname': firewall
    }
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )
    cloudstack_response = cloudstack_response

    if cloudstack_response['listsecuritygroupsresponse']['securitygroup']:
        response_item = cloudstack_response[
            'listsecuritygroupsresponse']['securitygroup'][0]
        firewall = _cloudstack_securitygroup_to_gce(response_item)
        res = jsonify(firewall)
        res.status_code = 200

    else:
        func_route = url_for('getsecuritygroup', projectid=projectid,
                             firewall=firewall)
        res = errors.resource_not_found(func_route)

    return res
Esempio n. 7
0
def _list_ssh_keys(authorization):
    command = 'listTags'
    args = {
        'resourcetype': 'UserVm',
        'keyword': 'sshkey-segment'
    }

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

    resources = {}
    sshkeys = set()

    if cloudstack_response['listtagsresponse']:
        for tag in cloudstack_response['listtagsresponse']['tag']:
            if tag['resourceid'] not in resources:
                resources[tag['resourceid']] = {}
            resources[tag['resourceid']][tag['key']] = tag['value']
        for resource in resources:
            sorted_resource = collections.OrderedDict(
                sorted(
                    resources[resource].items()))
            sshkey = ''
            for keychunk in sorted_resource:
                sshkey = sshkey + sorted_resource[keychunk]
            sshkeys.add(sshkey)

    sshkeys = '\n'.join(sshkeys)

    return sshkeys
Esempio n. 8
0
def _list_ssh_keys(authorization):
    command = 'listTags'
    args = {'resourcetype': 'UserVm', 'keyword': 'sshkey-segment'}

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

    resources = {}
    sshkeys = set()

    if cloudstack_response['listtagsresponse']:
        for tag in cloudstack_response['listtagsresponse']['tag']:
            if tag['resourceid'] not in resources:
                resources[tag['resourceid']] = {}
            resources[tag['resourceid']][tag['key']] = tag['value']
        for resource in resources:
            sorted_resource = collections.OrderedDict(
                sorted(resources[resource].items()))
            sshkey = ''
            for keychunk in sorted_resource:
                sshkey = sshkey + sorted_resource[keychunk]
            sshkeys.add(sshkey)

    sshkeys = '\n'.join(sshkeys)

    return sshkeys
    def validate_client_secret(self, client_id, client_secret):
        command = 'listCapabilities'
        args = {}
        cloudstack_response = requester.make_request(
            command,
            args,
            client_id,
            client_secret
        )

        if cloudstack_response:
            existing_client = Client.query.get(client_id)

            if existing_client is not None:
                existing_client.client_secret = client_secret
            else:
                client = Client(
                    client_id,
                    client_secret
                )
                db.session.add(client)

            db.session.commit()
            return True
        else:
            return False
Esempio n. 10
0
def _get_zones(authorization):
    command = 'listZones'
    args = {}
    cloudstack_response = requester.make_request(command, args,
                                                 authorization.client_id,
                                                 authorization.client_secret)

    return cloudstack_response
Esempio n. 11
0
def _get_items(authorization, args=None):
    args['listAll'] = 'true'

    response = requester.make_request(args['command'], args,
                                      authorization.client_id,
                                      authorization.client_secret)
    response = response[response.keys()[0]]

    return response
Esempio n. 12
0
def _get_async_result(authorization, args):
    command = 'queryAsyncJobResult'
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )
    return cloudstack_response
Esempio n. 13
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
Esempio n. 14
0
def _get_zones(authorization):
    command = 'listZones'
    args = {}
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )

    return cloudstack_response
Esempio n. 15
0
def _get_network(authorization, args=None):
    command = 'createSecurityGroup'
    if not args:
        args = {}
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )

    return cloudstack_response
Esempio n. 16
0
def _get_items(authorization, args=None):
    args['listAll'] = 'true'

    response = requester.make_request(
        args['command'],
        args,
        authorization.client_id,
        authorization.client_secret
    )
    response = response[response.keys()[0]]

    return response
Esempio n. 17
0
def _get_machinetypes(authorization, args=None):
    command = 'listServiceOfferings'
    if not args:
        args = {}

    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )
    return cloudstack_response
Esempio n. 18
0
def _get_disks(authorization, args=None):
    command = 'listVolumes'
    if not args:
        args = {}
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )

    return cloudstack_response
Esempio n. 19
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(helpers.get_root_url() +
                            url_for('getproject', projectid=projectid)),
        'status':
        'PENDING',
        'progress':
        0
    })
    res.status_code = 200
    return res
Esempio n. 20
0
def _get_virtual_machines(authorization, args=None):
    command = 'listVirtualMachines'
    if not args:
        args = {}

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

    return cloudstack_response
Esempio n. 21
0
def _add_network(authorization, args=None):
    command = 'createSecurityGroup'
    if not args:
        args = {}

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

    return cloudstack_response
Esempio n. 22
0
def _get_templates(authorization, args=None):
    command = 'listTemplates'
    if not args:
        args = {}

    if 'templatefilter' not in args:
        args['templatefilter'] = 'executable'

    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )
    return cloudstack_response
Esempio n. 23
0
def _destroy_virtual_machine(authorization, instance):
    virtual_machine_id = _get_virtual_machine_by_name(
        authorization,
        instance)['id']

    if virtual_machine_id is None:
        func_route = url_for('_destroy_virtual_machine', instance=instance)
        return errors.resource_not_found(func_route)

    args = {
        'id': virtual_machine_id
    }
    return requester.make_request(
        'destroyVirtualMachine',
        args,
        authorization.client_id,
        authorization.client_secret
    )
Esempio n. 24
0
def _delete_network(authorization, projectid, network):
    args = {'command': 'listSecurityGroups'}
    network_response = controllers.get_item_with_name(authorization, network, args, 'securitygroup')
    if not network_response:
        return None

    securitygroup_id = network_response['id']

    args = {
        'id': securitygroup_id
    }

    return requester.make_request(
        'deleteSecurityGroup',
        args,
        authorization.client_id,
        authorization.client_secret
    )
Esempio n. 25
0
def deleteinstance(projectid, authorization, zone, instance):
    args = {'command': 'listVirtualMachines'}
    virtual_machine = controllers.get_item_with_name(authorization, instance,
                                                     args, 'virtualmachine')

    virtual_machine_id = virtual_machine['id']
    args = {'id': virtual_machine_id}

    deletion_result = requester.make_request('destroyVirtualMachine', args,
                                             authorization.client_id,
                                             authorization.client_secret)

    return helpers.create_response(
        operations.create_async_response(
            projectid=projectid,
            operationid=deletion_result['destroyvirtualmachineresponse']
            ['jobid'],
            authorization=authorization))
Esempio n. 26
0
def deleteinstance(projectid, authorization, zone, instance):
    args = {'command': 'listVirtualMachines'}
    virtual_machine = controllers.get_item_with_name(authorization, instance, args, 'virtualmachine')

    virtual_machine_id = virtual_machine['id']
    args = {'id': virtual_machine_id}

    deletion_result = requester.make_request(
        'destroyVirtualMachine',
        args,
        authorization.client_id,
        authorization.client_secret
    )

    return helpers.create_response(operations.create_async_response(
        projectid=projectid,
        operationid=deletion_result['destroyvirtualmachineresponse']['jobid'],
        authorization=authorization
    ))
Esempio n. 27
0
def _deploy_virtual_machine(authorization, args, projectid):
    command = 'deployVirtualMachine'

    converted_args = {}
    template = images.get_template_by_name(
        authorization=authorization,
        image=args['template']
    )
    converted_args['templateid'] = template['id']

    zone = zones.get_zone_by_name(
        authorization=authorization,
        zone=args['zone']
    )
    converted_args['zoneid'] = zone['id']

    serviceoffering = machine_type.get_machinetype_by_name(
        authorization=authorization,
        machinetype=args['serviceoffering']
    )
    converted_args['serviceofferingid'] = serviceoffering['id']

    if 'network' in args:
        network = networks.get_network_by_name(
            authorization=authorization,
            securitygroup=args['network']
        )
        converted_args['securitygroupids'] = network['id']

    converted_args['displayname'] = args['name']
    converted_args['name'] = args['name']
    converted_args['keypair'] = projectid

    cloudstack_response = requester.make_request(
        command,
        converted_args,
        authorization.client_id,
        authorization.client_secret
    )

    return cloudstack_response
Esempio n. 28
0
def _delete_network(authorization, projectid, network):
    securitygroup_id = get_network_by_name(authorization, network)['id']

    if securitygroup_id is None:
        func_route = url_for(
            'getnetwork',
            projectid=projectid,
            network=network
        )

        return errors.resource_not_found(func_route)

    args = {
        'id': securitygroup_id
    }

    return requester.make_request(
        'deleteSecurityGroup',
        args,
        authorization.client_id,
        authorization.client_secret
    )
Esempio n. 29
0
def deletesecuritygroup(projectid, authorization, firewall):
    command = 'deleteSecurityGroup'
    args = {'name': firewall}
    cloudstack_response = requester.make_request(
        command,
        args,
        authorization.client_id,
        authorization.client_secret
    )

    cloudstack_response = cloudstack_response

    app.logger.debug(
        'Processing request for deleting a Firewall \n'
        'Project: ' + projectid + '\n' +
        'Firewall: ' + firewall + '\n' +
        json.dumps(cloudstack_response, indent=4, separators=(',', ': '))
    )

    populated_response = {}

    res = jsonify(populated_response)
    res.status_code = 200
    return res
Esempio n. 30
0
def _get_async_result(authorization, args):
    command = 'queryAsyncJobResult'
    cloudstack_response = requester.make_request(command, args,
                                                 authorization.client_id,
                                                 authorization.client_secret)
    return cloudstack_response