<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright &copy; 2014-2016 NetApp, Inc. All Rights Reserved.

from __future__ import unicode_literals

import click

import os
import csv
from element.cli.cli import pass_context
from pkg_resources import Requirement, resource_filename
import simplejson
from element.cli import utils as cli_utils
from solidfire.factory import ElementFactory

@click.group()
@pass_context
def cli(ctx):
    """Connection management"""

@cli.command('push', short_help="Pushes the connection onto connections.csv to save for later use. This is located at "+resource_filename(Requirement.parse("solidfire-cli"), "connections.csv")
)
@click.option('--mvip', '-m',
              default=None,
              help="SolidFire MVIP",
              required=False)
@click.option('--username', '-u',
              default=None,
              help="SolidFire cluster username",
              required=False)
@click.option('--password', '-p',
              default=None,
              help="SolidFire cluster password",
              required=False)
@click.option('--version', '-v',
              default = None,
              help='The version you would like to connect on',
              required=False)
@click.option('--name', '-n',
              default = None,
              help="The name you want to associate with the connection'.",
              required=True,
              prompt=True)
@click.option('--port', '-q',
              default = None,
              help="The port you wish to connect on",
              required=False)
@click.option('--verifyssl', '-s',
              default = None,
              help="Enable this to check ssl connection for errors especially when using a hostname. It is invalid to set this to true when using an IP address in the target.",
              required=False,
              is_flag=True)
@pass_context
def push(ctx, mvip, username, password, version, port, name, verifyssl):
    # First, attempt to establish the connection. If that's not possible,
    # throw the error.

    if mvip and ctx.mvip:
        ctx.logger.error("Please only provide the mvip once.")
    if username and ctx.username:
        ctx.logger.error("Please only provide the username once.")
    if password and ctx.password:
        ctx.logger.error("Please only provide the password once.")
    if name and ctx.name:
        ctx.logger.error("Please only provide the name once.")

    if ctx.mvip is None and mvip is None:
        ctx.logger.error("Please provide the mvip. It is a required parameter.")

    if ctx.mvip is None:
        ctx.mvip = mvip
    if ctx.username is None:
        ctx.username = username
    if ctx.password is None:
        ctx.password = password
    if version is not None:
        ctx.version = str(float(version))
    if port is not None:
        ctx.port = port
    ctx.verifyssl = ctx.verifyssl or verifyssl

    # Verify that the connection exists or get the extra info.
    cli_utils.establish_connection(ctx)

    connections = cli_utils.get_connections()
    # First, ensure that no other connections have the same name:
    sameName = [connection for connection in connections if connection["name"]==name]
    if sameName != []:
        ctx.logger.error("A connection with that name already exists. Please try another.")
        exit(1)

    if(ctx.username is not None and ctx.password is not None):
        ctx.username = cli_utils.encrypt(ctx.username)
        ctx.password = cli_utils.encrypt(ctx.password)

    connections = connections + [{'mvip': ctx.mvip,
                                  'username': "b'"+ctx.username.decode('utf-8')+"'",
                                  'password': "b'"+ctx.password.decode('utf-8')+"'",
                                  'port': ctx.port,
                                  'url': 'https://%s:%s' % (ctx.mvip, ctx.port),
                                  'version': ctx.version,
                                  'name': name,
                                  'verifyssl': verifyssl}]
    try:
        cli_utils.write_connections(connections)
    except PermissionError as e:
        ctx.logger.error(e.args[1]+" Please verify you have write permissions for "+resource_filename(Requirement.parse("solidfire-cli"), "connections.csv"))

@cli.command('remove', short_help="Removes a given connection")
@click.option('--name', '-n',<commit_msg>Fixes error handling<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright &copy; 2014-2016 NetApp, Inc. All Rights Reserved.

from __future__ import unicode_literals

import click

import os
import csv
from element.cli.cli import pass_context
from pkg_resources import Requirement, resource_filename
import simplejson
from element.cli import utils as cli_utils
from solidfire.factory import ElementFactory

@click.group()
@pass_context
def cli(ctx):
    """Connection management"""

@cli.command('push', short_help="Pushes the connection onto connections.csv to save for later use. This is located at "+resource_filename(Requirement.parse("solidfire-cli"), "connections.csv")
)
@click.option('--mvip', '-m',
              default=None,
              help="SolidFire MVIP",
              required=False)
@click.option('--username', '-u',
              default=None,
              help="SolidFire cluster username",
              required=False)
@click.option('--password', '-p',
              default=None,
              help="SolidFire cluster password",
              required=False)
@click.option('--version', '-v',
              default = None,
              help='The version you would like to connect on',
              required=False)
@click.option('--name', '-n',
              default = None,
              help="The name you want to associate with the connection'.",
              required=True,
              prompt=True)
@click.option('--port', '-q',
              default = None,
              help="The port you wish to connect on",
              required=False)
@click.option('--verifyssl', '-s',
              default = None,
              help="Enable this to check ssl connection for errors especially when using a hostname. It is invalid to set this to true when using an IP address in the target.",
              required=False,
              is_flag=True)
@pass_context
def push(ctx, mvip, username, password, version, port, name, verifyssl):
    # First, attempt to establish the connection. If that's not possible,
    # throw the error.

    if mvip and ctx.mvip:
        ctx.logger.error("Please only provide the mvip once.")
    if username and ctx.username:
        ctx.logger.error("Please only provide the username once.")
    if password and ctx.password:
        ctx.logger.error("Please only provide the password once.")
    if name and ctx.name:
        ctx.logger.error("Please only provide the name once.")

    if ctx.mvip is None and mvip is None:
        ctx.logger.error("Please provide the mvip. It is a required parameter.")

    if ctx.mvip is None:
        ctx.mvip = mvip
    if ctx.username is None:
        ctx.username = username
    if ctx.password is None:
        ctx.password = password
    if version is not None:
        ctx.version = str(float(version))
    if port is not None:
        ctx.port = port
    ctx.verifyssl = ctx.verifyssl or verifyssl

    # Verify that the connection exists or get the extra info.
    cli_utils.establish_connection(ctx)

    connections = cli_utils.get_connections()
    # First, ensure that no other connections have the same name:
    sameName = [connection for connection in connections if connection["name"]==name]
    if sameName != []:
        ctx.logger.error("A connection with that name already exists. Please try another.")
        exit(1)

    if(ctx.username is not None and ctx.password is not None):
        ctx.username = cli_utils.encrypt(ctx.username)
        ctx.password = cli_utils.encrypt(ctx.password)

    connections = connections + [{'mvip': ctx.mvip,
                                  'username': "b'"+ctx.username.decode('utf-8')+"'",
                                  'password': "b'"+ctx.password.decode('utf-8')+"'",
                                  'port': ctx.port,
                                  'url': 'https://%s:%s' % (ctx.mvip, ctx.port),
                                  'version': ctx.version,
                                  'name': name,
                                  'verifyssl': verifyssl}]
    try:
        cli_utils.write_connections(connections)
    except Exception as e:
        ctx.logger.error(e.args)

@cli.command('remove', short_help="Removes a given connection")
@click.option('--name', '-n',
{'fraction_non_alphanumeric': 0.11198519204072188, 'fraction_numerical': 0.0028921795465062473, 'mean_word_length': 2.268431001890359, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, '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': '4638554', 'n_tokens_mistral': 2353, 'n_tokens_neox': 2312, 'n_words': 744}