Esempio n. 1
0
def RunBaseCreateCommand(args, release_track):
  """Creates a user in a given instance.

  Args:
    args: argparse.Namespace, The arguments that this command was invoked with.
    release_track: base.ReleaseTrack, the release track that this was run under.

  Returns:
    SQL user resource iterator.
  """
  client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
  sql_client = client.sql_client
  sql_messages = client.sql_messages

  instance_ref = client.resource_parser.Parse(
      args.instance,
      params={'project': properties.VALUES.core.project.GetOrFail},
      collection='sql.instances')
  operation_ref = None

  host = users.GetHostValue(args)
  if release_track == base.ReleaseTrack.ALPHA:
    user_type = users.ParseUserType(sql_messages, args)
    new_user = sql_messages.User(
        kind='sql#user',
        project=instance_ref.project,
        instance=args.instance,
        name=args.username,
        host=host,
        password=args.password,
        type=user_type)
  else:
    new_user = sql_messages.User(
        kind='sql#user',
        project=instance_ref.project,
        instance=args.instance,
        name=args.username,
        host=host,
        password=args.password)

  result_operation = sql_client.users.Insert(new_user)
  operation_ref = client.resource_parser.Create(
      'sql.operations',
      operation=result_operation.name,
      project=instance_ref.project)
  if args.async_:
    result = sql_client.operations.Get(
        sql_messages.SqlOperationsGetRequest(
            project=operation_ref.project, operation=operation_ref.operation))
  else:
    operations.OperationsV1Beta4.WaitForOperation(sql_client, operation_ref,
                                                  'Creating Cloud SQL user')
    result = new_user
    result.kind = None

  log.CreatedResource(args.username, kind='user', is_async=args.async_)

  return result
    def Run(self, args):
        """Changes a user's password in a given instance.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      SQL user resource iterator.
    """
        client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
        sql_client = client.sql_client
        sql_messages = client.sql_messages

        if args.prompt_for_password:
            args.password = getpass.getpass('Instance Password: '******'project': properties.VALUES.core.project.GetOrFail},
            collection='sql.instances')
        operation_ref = None

        host = users.GetHostValue(args)
        result_operation = sql_client.users.Update(
            sql_messages.SqlUsersUpdateRequest(
                project=instance_ref.project,
                instance=args.instance,
                name=args.username,
                host=host,
                user=sql_messages.User(project=instance_ref.project,
                                       instance=args.instance,
                                       name=args.username,
                                       host=host,
                                       password=args.password)))
        operation_ref = client.resource_parser.Create(
            'sql.operations',
            operation=result_operation.name,
            project=instance_ref.project)
        if args. async:
            return sql_client.operations.Get(
                sql_messages.SqlOperationsGetRequest(
                    project=operation_ref.project,
                    operation=operation_ref.operation))
        operations.OperationsV1Beta4.WaitForOperation(
            sql_client, operation_ref, 'Updating Cloud SQL user')
Esempio n. 3
0
  def Run(self, args):
    """Lists Cloud SQL users in a given instance.

    Args:
      args: argparse.Namespace, The arguments that this command was invoked
          with.

    Returns:
      SQL user resource iterator.
    """
    client = api_util.SqlClient(api_util.API_VERSION_DEFAULT)
    sql_client = client.sql_client
    sql_messages = client.sql_messages

    instance_ref = client.resource_parser.Parse(
        args.instance,
        params={'project': properties.VALUES.core.project.GetOrFail},
        collection='sql.instances')
    operation_ref = None

    host = users.GetHostValue(args)

    console_io.PromptContinue(
        message='{0}@{1} will be deleted. New connections can no longer be '
        'made using this user. Existing connections are not affected.'.format(
            args.username, host),
        default=True,
        cancel_on_no=True)

    result_operation = sql_client.users.Delete(
        sql_messages.SqlUsersDeleteRequest(
            project=instance_ref.project,
            instance=instance_ref.Name(),
            name=args.username,
            host=host))
    operation_ref = client.resource_parser.Create(
        'sql.operations',
        operation=result_operation.name,
        project=instance_ref.project)
    if args.async:
      return sql_client.operations.Get(
          sql_messages.SqlOperationsGetRequest(
              project=operation_ref.project, operation=operation_ref.operation))
    operations.OperationsV1Beta4.WaitForOperation(sql_client, operation_ref,
                                                  'Deleting Cloud SQL user')