Test first script

 To test the first script for generating a REQ in Visual Studio Code (VS Code), here is a detailed step-by-step guide, including setting up the environment and installing required libraries.

Step-by-Step Guide for Testing the REQ Generation Script in VS Code


1. Install Python and VS Code Extensions

If you haven’t already done so:

  • Python Installation:
    • Download and install Python from here.
    • Make sure to check the option "Add Python to PATH" during installation.
  • Install Python Extension in VS Code:
    • Open VS Code.
    • Go to the Extensions view by clicking on the square icon in the sidebar (or press Ctrl+Shift+X).
    • Search for "Python" and install the official extension by Microsoft.

2. Set Up a Virtual Environment

It's good practice to create a virtual environment to manage dependencies.

  • Open a terminal in VS Code (Ctrl+ backtick).

  • Run the following commands to create and activate a virtual environment:

    bash
    # Create a virtual environment (replace `venv` with your desired name) python -m venv venv # Activate the virtual environment # On Windows: .\venv\Scripts\activate # On macOS/Linux: source venv/bin/activate

3. Install Required Python Libraries

You need the following libraries to run the script:

  • requests for making HTTP API calls.
  • json (part of Python’s standard library).

In the terminal (with your virtual environment activated), run:

bash
pip install requests

The json library is part of the standard Python library, so no need to install it separately.

4. Prepare Your Script

  • In VS Code, create a new Python file (.py) and name it something like generate_req.py.

  • Copy and paste the first script (REQ generation) into this file:

    python
    import requests import json import os # Replace environment variables with actual values or securely store them in Azure Key Vault user_name = os.environ.get('USER_NAME', 'your_user_name_here') pwd = os.environ.get('PASSWORD', 'your_password_here') api_root = os.environ.get('API_ROOT', 'https://atlas-api.ubsdev.net/api/') def generate_req(): headers = { 'Content-Type': 'application/json', 'Accept': 'application/json' } cat_item_sys_id = '4516dddd87cf06d00dc141d30cbb358b' api_endpoint = f'sn_sc/servicecatalog/items/{cat_item_sys_id}/order_now' data = { 'sysparm_requested_for': '3792a9cf4799b1d050d346f8736d4334', 'sysparm_quantity': 1, 'variables': {'hostname': 'ZU322K10'} } response = requests.post( f'{api_root}{api_endpoint}', auth=(user_name, pwd), data=json.dumps(data), headers=headers, verify=False ) if response.status_code == 200: response_data = response.json() req_number = response_data.get('result', {}).get('display_value') print(f"REQ generated successfully: {req_number}") return req_number else: print(f"Failed to generate REQ. Status Code: {response.status_code}") print(response.text) return None if __name__ == "__main__": generate_req()

5. Configure Environment Variables (Optional)

You can either replace the os.environ.get variables directly in the code (for testing) or create a .env file to store your credentials securely:

  • In the root directory of your project, create a .env file and add:

    makefile
    USER_NAME=your_user_name PASSWORD=your_password API_ROOT=https://atlas-api.ubsdev.net/api/
  • Install the python-dotenv library to load environment variables:

    bash
    pip install python-dotenv
  • Modify the script to load .env variables:

    python
    from dotenv import load_dotenv # Load environment variables from .env file load_dotenv()

6. Run the Script in VS Code

  • Make sure your virtual environment is activated.

  • Open the terminal in VS Code.

  • Run the script:

    bash
    python generate_req.py
  • If everything is correct, it should either print the REQ number generated or give you an error response from the ServiceNow API.

7. Check for Errors

If you encounter any issues, check the following:

  • Network connectivity: Ensure you can connect to the ServiceNow API (check the API root URL).
  • Authentication: Double-check the credentials you're using and ensure they have the necessary permissions.
  • API endpoint correctness: Ensure the cat_item_sys_id and other values are correct and valid for the API you're working with.
  • SSL verification: If you are facing issues with SSL verification, you might need to set verify=True or install missing certificates.

8. Logging for Debugging (Optional)

Add logging to your script to capture important details:

python
import logging logging.basicConfig(level=logging.INFO) def generate_req(): logging.info("Generating REQ...") # Rest of the code

9. Next Steps

Once the script works successfully:

  • You can move to testing the extraction of the REQ details using the second script.
  • Eventually, you can combine both scripts as described earlier.

Let me know if you need further assistance with any of the steps!

No comments

Theme images by tjasam. Powered by Blogger.