import logging
from lftools import config
+from lftools import helpers
import lftools.api.client as client
log = logging.getLogger(__name__)
else:
return "Failed to create tag {}".format(name)
+ def create_user(self, username, first_name, last_name, email_address, roles, password=None):
+ """Create a new user.
+
+ @param username:
+ @param first_name:
+ @param last_name:
+ @param email:
+ @param status:
+ @param roles:
+ @param password:
+ """
+ list_of_roles = roles.split(",")
+ data = {
+ "userId": username,
+ "firstName": first_name,
+ "lastName": last_name,
+ "emailAddress": email_address,
+ "status": "active",
+ "roles": list_of_roles,
+ }
+
+ if password:
+ data["password"] = password
+ else:
+ data["password"] = helpers.generate_password()
+
+ json_data = json.dumps(data)
+ result = self.post("beta/security/users", data=json_data)[0]
+
+ if result.status_code == 200:
+ return "User {} successfully created with password {}".format(username, data["password"])
+ else:
+ log.error("Failed to create user {}".format(username))
+
def delete_script(self, name):
"""Delete a script from the server.
else:
return "Failed to delete tag {}.".format(name)
+ def delete_user(self, username):
+ """Delete a user.
+
+ @param username:
+ """
+ result = self.delete("beta/security/users/{}".format(username))
+
+ if hasattr(result, "status_code"):
+ if result.status_code == 204:
+ return "Successfully deleted user {}".format(username)
+ else:
+ return "Failed to delete user {} with error: {}".format(username, result[1])
+
def list_assets(self, repository, **kwargs):
"""List the assets of a given repo.
r = ctx.obj["nexus3"]
data = r.list_user(username)
log.info(tabulate(data, headers=["User ID", "First Name", "Last Name", "Email Address", "Status", "Roles",],))
+
+
+@user.command(name="create")
+@click.argument("username")
+@click.argument("first_name")
+@click.argument("last_name")
+@click.argument("email_address")
+@click.argument("roles")
+@click.argument("password", required=False)
+@click.pass_context
+def user_create(ctx, username, first_name, last_name, email_address, roles, password):
+ """Create a new user account."""
+ r = ctx.obj["nexus3"]
+ data = r.create_user(username, first_name, last_name, email_address, roles, password)
+ log.info(data)
+
+
+@user.command(name="delete")
+@click.argument("username")
+@click.pass_context
+def user_delete(ctx, username):
+ """Delete a user account."""
+ r = ctx.obj["nexus3"]
+ data = r.delete_user(username)
+ log.info(data)
--- /dev/null
+# SPDX-License-Identifier: EPL-1.0
+##############################################################################
+# Copyright (c) 2020 The Linux Foundation and others.
+#
+# All rights reserved. This program and the accompanying materials
+# are made available under the terms of the Eclipse Public License v1.0
+# which accompanies this distribution, and is available at
+# http://www.eclipse.org/legal/epl-v10.html
+##############################################################################
+
+"""Nexus3 REST API interface."""
+
+import random
+import string
+
+
+def generate_password(length=12):
+ punctuation = "!#$%&()*+,-.:;<=>?@[]^_{|}~"
+ password_characters = string.ascii_letters + string.digits + punctuation
+ return "".join(random.choice(password_characters) for _ in range(length))