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
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)
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 )
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
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
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 _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 _get_zones(authorization): command = 'listZones' args = {} cloudstack_response = requester.make_request(command, args, authorization.client_id, authorization.client_secret) return cloudstack_response
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
def _get_async_result(authorization, args): command = 'queryAsyncJobResult' cloudstack_response = requester.make_request( command, args, authorization.client_id, authorization.client_secret ) return cloudstack_response
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
def _get_zones(authorization): command = 'listZones' args = {} cloudstack_response = requester.make_request( command, args, authorization.client_id, authorization.client_secret ) return cloudstack_response
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
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
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
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
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
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
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
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
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 )
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 )
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))
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 ))
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
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 )
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
def _get_async_result(authorization, args): command = 'queryAsyncJobResult' cloudstack_response = requester.make_request(command, args, authorization.client_id, authorization.client_secret) return cloudstack_response