{"id":11340,"date":"2025-01-08T13:00:19","date_gmt":"2025-01-08T18:00:19","guid":{"rendered":"https:\/\/www.rushworth.us\/lisa\/?p=11340"},"modified":"2025-01-08T13:00:20","modified_gmt":"2025-01-08T18:00:20","slug":"sumo-logic-creating-roles-via-api","status":"publish","type":"post","link":"https:\/\/www.rushworth.us\/lisa\/?p=11340","title":{"rendered":"Sumo Logic: Creating Roles via API"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">This script creates very basic roles with no extra capabilities and restricts the role to viewing only the indicated source category&#8217;s data. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n################################################################################\n# This script reads an Excel file containing role data, then uses the Sumo Logic\n# API to create roles based on the data. It checks each row for a role name and\n# uses the source category to set data filters. The script requires a config.py\n# file with access credentials.\n################################################################################\nimport pandas as pd\nimport requests\nimport json\nfrom config import access_id, access_key  # Import credentials from config.py\n\n# Path to Excel file\nexcel_file_path = &#039;NewRoles.xlsx&#039;\n\n# Base URL for Sumo Logic API\nbase_url = &#039;https:\/\/api.sumologic.com\/api\/v1&#039;\n\n################################################################################\n# Function to create a new role using the Sumo Logic API.\n# \n# Args:\n#     role_name (str): The name of the role to create.\n#     role_description (str): The description of the role.\n#     source_category (str): The source category to restrict the role to.\n# \n# Returns:\n#     None. Prints the status of the API call.\n################################################################################\ndef create_role(role_name, role_description, source_category):\n    \n    url = f&#039;{base_url}\/roles&#039;\n\n    # Role payload\n    data_filter = f&#039;_sourceCategory={source_category}&#039;\n    payload = {\n        &#039;name&#039;: role_name,\n        &#039;description&#039;: role_description,\n        &#039;logAnalyticsDataFilter&#039;: data_filter,\n        &#039;auditDataFilter&#039;: data_filter,\n        &#039;securityDataFilter&#039;: data_filter\n    }\n\n    # Headers for the request\n    headers = {\n        &#039;Content-Type&#039;: &#039;application\/json&#039;,\n        &#039;Accept&#039;: &#039;application\/json&#039;\n    }\n\n    # Debugging line\n    print(f&quot;Attempting to create role: &#039;{role_name}&#039; with description: &#039;{role_description}&#039; and filter: &#039;{data_filter}&#039;&quot;)\n\n    # Make the POST request to create a new role\n    response = requests.post(url, auth=(access_id, access_key), headers=headers, data=json.dumps(payload))\n\n    # Check the response\n    if response.status_code == 201:\n        print(f&#039;Role {role_name} created successfully.&#039;)\n    else:\n        print(f&#039;Failed to create role {role_name}. Status Code: {response.status_code}&#039;)\n        print(&#039;Response:&#039;, response.json())\n\n################################################################################\n# Reads an Excel file and processes each row to extract role information and\n# create roles using the Sumo Logic API.\n# \n# Args:\n#     file_path (str): The path to the Excel file containing role data.\n# \n# Returns:\n#     None. Processes the file and attempts to create roles based on the data.\n################################################################################\ndef process_excel(file_path):\n    # Load the spreadsheet\n    df = pd.read_excel(file_path, engine=&#039;openpyxl&#039;)\n\n    # Print column names to help debug and find correct ones\n    print(&quot;Columns found in Excel:&quot;, df.columns)\n\n    # Iterate over each row in the DataFrame\n    for index, row in df.iterrows():\n        role_name = row&#x5B;&#039;Role Name&#039;]  # Correct column name for role name\n        source_category = row&#x5B;&#039;Source Category&#039;]  # Correct column name for source category to which role is restricted\n\n        # Only create a role if the role name is not null\n        if pd.notnull(role_name):\n            role_description = f&#039;Provides access to source category {source_category}&#039;\n            create_role(role_name, role_description, source_category)\n\n\n# Process the Excel file\nprocess_excel(excel_file_path)\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>This script creates very basic roles with no extra capabilities and restricts the role to viewing only the indicated source category&#8217;s data.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2076],"tags":[664,2074,2075],"class_list":["post-11340","post","type-post","status-publish","format-standard","hentry","category-sumo-logic","tag-python","tag-sumo-logic","tag-sumo-logic-api"],"_links":{"self":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11340","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=11340"}],"version-history":[{"count":1,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11340\/revisions"}],"predecessor-version":[{"id":11341,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=\/wp\/v2\/posts\/11340\/revisions\/11341"}],"wp:attachment":[{"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=11340"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=11340"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.rushworth.us\/lisa\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=11340"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}