<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright 2015 Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

import cherrypy
import json
from jsonpath_rw import parse

import cumulus
import cumulus.aws.ec2.tasks.key

from cumulus.common.girder import get_task_token
from cumulus.aws.ec2 import get_ec2_client

from girder.api import access
from girder.api.describe import Description
from girder.constants import AccessType
from girder.api.docs import addModel
from girder.api.rest import RestException, getBodyJson, ModelImporter,\
    getCurrentUser
from girder.api.rest import loadmodel


def requireParams(required, params):
    for param in required:
        if param not in params:
            raise RestException("Parameter '%s' is required." % param)


def _filter(profile):
    del profile['access']
    profile = json.loads(json.dumps(profile, default=str))

    return profile


@access.user
@loadmodel(model='user', level=AccessType.WRITE)
def create_profile(user, params):
    body = getBodyJson()
    requireParams(['name', 'accessKeyId', 'secretAccessKey', 'regionName',
                   'availabilityZone'], body)

    profile_type = 'ec2' if 'cloud-provider' not in body.keys() else body['cloud-provider']

    model = ModelImporter.model('aws', 'cumulus')
    profile = model.create_profile(user['_id'], body['name'],
                                   profile_type,
                                   body['accessKeyId'],
                                   body['secretAccessKey'], body['regionName'],
                                   body['availabilityZone'],
                                   body.get('publicIPs', False))

    # Now fire of a task to create a key pair for this profile
    try:
        cumulus.aws.ec2.tasks.key.generate_key_pair.delay(
            _filter(profile), get_task_token()['_id'])

        cherrypy.response.status = 201
        cherrypy.response.headers['Location'] \
            = '/user/%s/aws/profile/%s' % (str(user['_id']),
                                           str(profile['_id']))<commit_msg>Fix static analysis error<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

###############################################################################
#  Copyright 2015 Kitware Inc.
#
#  Licensed under the Apache License, Version 2.0 ( the "License" );
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.
###############################################################################

import cherrypy
import json
from jsonpath_rw import parse

import cumulus
import cumulus.aws.ec2.tasks.key

from cumulus.common.girder import get_task_token
from cumulus.aws.ec2 import get_ec2_client

from girder.api import access
from girder.api.describe import Description
from girder.constants import AccessType
from girder.api.docs import addModel
from girder.api.rest import RestException, getBodyJson, ModelImporter,\
    getCurrentUser
from girder.api.rest import loadmodel


def requireParams(required, params):
    for param in required:
        if param not in params:
            raise RestException("Parameter '%s' is required." % param)


def _filter(profile):
    del profile['access']
    profile = json.loads(json.dumps(profile, default=str))

    return profile


@access.user
@loadmodel(model='user', level=AccessType.WRITE)
def create_profile(user, params):
    body = getBodyJson()
    requireParams(['name', 'accessKeyId', 'secretAccessKey', 'regionName',
                   'availabilityZone'], body)

    profile_type = 'ec2' if 'cloud-provider' not in body.keys() \
                   else body['cloud-provider']

    model = ModelImporter.model('aws', 'cumulus')
    profile = model.create_profile(user['_id'], body['name'],
                                   profile_type,
                                   body['accessKeyId'],
                                   body['secretAccessKey'], body['regionName'],
                                   body['availabilityZone'],
                                   body.get('publicIPs', False))

    # Now fire of a task to create a key pair for this profile
    try:
        cumulus.aws.ec2.tasks.key.generate_key_pair.delay(
            _filter(profile), get_task_token()['_id'])

        cherrypy.response.status = 201
        cherrypy.response.headers['Location'] \
            = '/user/%s/aws/profile/%s' % (str(user['_id']),
                                           str(profile['_id']))
{'fraction_non_alphanumeric': 0.15216999819917162, 'fraction_numerical': 0.0061228164955879705, 'mean_word_length': 2.9140239605355887, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 14, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6691250', 'n_tokens_mistral': 1486, 'n_tokens_neox': 1430, 'n_words': 440}