-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathNCM_APIv3_create_users.py
More file actions
104 lines (87 loc) · 3.81 KB
/
NCM_APIv3_create_users.py
File metadata and controls
104 lines (87 loc) · 3.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# This script creates and updates users in NCM API v3 using the NCM library (pip install ncm)
# It reads a CSV file with user details and updates the users in NCM
# The CSV file must have the following columns named on row 1:
# first name, last name, email, role
# Possible values for role are: no_access, read_only_user, full_access_user, administrator
# Usage: python "NCM_APIv3_create_users.py" <csv_file_path>
# Token can be set via TOKEN environment variable
import csv
import os
import sys
from ncm import ncm
# Get CSV filename from command-line argument
if len(sys.argv) < 2:
print("Error: CSV filename required as command-line argument")
print(f"Usage: {sys.argv[0]} <csv_file_path>")
sys.exit(1)
csv_filename = sys.argv[1]
# Get token from environment variable
token = os.environ.get('TOKEN') or os.environ.get('NCM_API_TOKEN')
if not token:
print("Error: Please set your NCM API v3 token as TOKEN environment variable")
print("Error: Please set your NCM API v3 token as TOKEN environment variable")
sys.exit(1)
# Initialize the NCM client
ncm_client = ncm.NcmClientv3(api_key=token, log_events=True)
def create_and_update_user(first_name, last_name, email, role):
try:
# Create the user first
response = ncm_client.create_user(first_name=first_name, last_name=last_name, email=email)
if response.startswith('ERROR'):
print(f"Error creating user {email} - does it already exist? {response}")
else:
print(f"User {email} created successfully")
# Then update their role
response = ncm_client.update_user_role(email, role)
if response.startswith('ERROR'):
print(f"Error updating user {email} - {response}")
else:
print(f"User {email} updated role to {role} successfully")
except Exception as e:
print(f"Error processing {email}: {str(e)}")
# Check if CSV file exists
if not os.path.exists(csv_filename):
print(f"Error: CSV file not found: {csv_filename}")
sys.exit(1)
# Read and process CSV file
print(f"Processing CSV file: {csv_filename}")
try:
with open(csv_filename, 'r', encoding='utf-8') as file:
csv_reader = csv.DictReader(file)
# Check for required columns
required_columns = ['first name', 'last name', 'email', 'role']
csv_columns = [col.lower().strip() for col in csv_reader.fieldnames] if csv_reader.fieldnames else []
missing_columns = []
for req_col in required_columns:
if req_col.lower() not in csv_columns:
missing_columns.append(req_col)
if missing_columns:
print(f"Error: Missing required columns: {', '.join(missing_columns)}")
print(f"Found columns: {', '.join(csv_reader.fieldnames)}")
sys.exit(1)
# Find the actual column names (case-insensitive)
column_map = {}
for req_col in required_columns:
for csv_col in csv_reader.fieldnames:
if csv_col.lower().strip() == req_col.lower():
column_map[req_col] = csv_col
break
row_count = 0
for row in csv_reader:
row_count += 1
create_and_update_user(
first_name=row[column_map['first name']].strip(),
last_name=row[column_map['last name']].strip(),
email=row[column_map['email']].strip(),
role=row[column_map['role']].strip()
)
print(f"\nProcessed {row_count} user(s) from CSV file")
except FileNotFoundError:
print(f"Error: CSV file not found: {csv_filename}")
sys.exit(1)
except KeyError as e:
print(f"Error: Column '{e}' not found in CSV file")
sys.exit(1)
except Exception as e:
print(f"Error reading CSV file: {e}")
sys.exit(1)