initial commit
This commit is contained in:
@@ -0,0 +1,46 @@
|
||||
# Exercise 2: Authentication
|
||||
|
||||
## Overview
|
||||
In this exercise, you'll learn how to authenticate with the Zerto API using Keycloak. You'll create a client and establish a connection to your ZVM.
|
||||
|
||||
## Objectives
|
||||
- Create a Keycloak client
|
||||
- Configure authentication parameters
|
||||
- Test the connection to ZVM
|
||||
- Handle authentication errors
|
||||
|
||||
## Time
|
||||
10 minutes
|
||||
|
||||
## Prerequisites
|
||||
- Completed Exercise 1
|
||||
- Valid ZVM credentials
|
||||
- Client ID and secret
|
||||
|
||||
## Exercise Steps
|
||||
1. Set up your configuration
|
||||
2. Create the Keycloak client
|
||||
3. Test the connection
|
||||
4. Handle authentication errors
|
||||
|
||||
## Working Directory
|
||||
The `working` directory contains:
|
||||
- `auth.py` - Template to complete
|
||||
|
||||
## Solution
|
||||
The `solution` directory contains:
|
||||
- `auth.py` - Complete working example
|
||||
|
||||
## Key Concepts
|
||||
- Keycloak authentication
|
||||
- Client credentials flow
|
||||
- Error handling
|
||||
- Connection management
|
||||
|
||||
## Common Issues
|
||||
- Invalid credentials
|
||||
- SSL certificate issues
|
||||
- Network connectivity problems
|
||||
|
||||
## Next Steps
|
||||
Proceed to Exercise 3: Site Discovery to start working with Zerto sites.
|
||||
@@ -0,0 +1,88 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Exercise 2: Authentication - Solution
|
||||
This script demonstrates how to authenticate with Zerto API using Keycloak.
|
||||
|
||||
Prerequisites:
|
||||
1. Install the zvml package in development mode:
|
||||
cd /path/to/zvml-python-sdk
|
||||
pip install -e .
|
||||
2. Update prerequisites/config.py with your ZVM details
|
||||
|
||||
This solution demonstrates:
|
||||
- ZVMLClient initialization with Keycloak authentication
|
||||
- Connection testing using local site information
|
||||
- Proper error handling and logging
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
# Add prerequisites to Python path
|
||||
prerequisites_path = Path(__file__).parent.parent.parent.parent / "prerequisites"
|
||||
sys.path.append(str(prerequisites_path))
|
||||
|
||||
# Import the SDK modules
|
||||
from zvml import ZVMLClient
|
||||
|
||||
# Import configuration
|
||||
try:
|
||||
from config import (
|
||||
ZVM_HOST,
|
||||
ZVM_PORT,
|
||||
ZVM_SSL_VERIFY,
|
||||
CLIENT_ID,
|
||||
CLIENT_SECRET
|
||||
)
|
||||
except ImportError:
|
||||
print("Error: Please copy config.example.py to config.py and update with your values")
|
||||
print("Expected path:", prerequisites_path / "config.py")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
"""
|
||||
Main function to demonstrate Zerto authentication.
|
||||
Shows how to:
|
||||
1. Initialize ZVMLClient with Keycloak credentials
|
||||
2. Test connection by retrieving local site info
|
||||
3. Handle authentication and connection errors
|
||||
"""
|
||||
# Set up logging with timestamp
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
try:
|
||||
# Step 1: Create a ZVMLClient instance
|
||||
logging.info(f"Initializing ZVMLClient for ZVM at {ZVM_HOST}")
|
||||
client = ZVMLClient(
|
||||
zvm_address=ZVM_HOST,
|
||||
client_id=CLIENT_ID,
|
||||
client_secret=CLIENT_SECRET,
|
||||
verify_certificate=ZVM_SSL_VERIFY
|
||||
)
|
||||
|
||||
# Step 2: Test the connection by getting local site info
|
||||
logging.info("Testing connection by retrieving local site information...")
|
||||
local_site = client.localsite.get_local_site()
|
||||
|
||||
# Extract and log version information
|
||||
version = local_site.get('Version')
|
||||
logging.info(f"Successfully connected to ZVM version: {version}")
|
||||
|
||||
# Optional: Log additional site details if needed
|
||||
# logging.debug(f"Full site details: {json.dumps(local_site, indent=2)}")
|
||||
|
||||
# Step 3: Print final connection status
|
||||
logging.info("Connection successful!")
|
||||
|
||||
except Exception as e:
|
||||
logging.error(f"Authentication failed: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
@@ -0,0 +1,85 @@
|
||||
#!/usr/bin/env python3
|
||||
"""
|
||||
Exercise 2: Authentication
|
||||
This script demonstrates how to authenticate with Zerto API using Keycloak.
|
||||
|
||||
Prerequisites:
|
||||
1. Install the zvml package in development mode:
|
||||
cd /path/to/zvml-python-sdk
|
||||
pip install -e .
|
||||
2. Update prerequisites/config.py with your ZVM details
|
||||
|
||||
Your task:
|
||||
1. Initialize ZVMLClient with Keycloak credentials
|
||||
2. Test connection by retrieving local site information
|
||||
3. Handle authentication and connection errors
|
||||
|
||||
If you need help, check the solution in the solution directory.
|
||||
"""
|
||||
|
||||
import sys
|
||||
import os
|
||||
import logging
|
||||
import json
|
||||
from pathlib import Path
|
||||
|
||||
# Add prerequisites to Python path
|
||||
prerequisites_path = Path(__file__).parent.parent.parent.parent / "prerequisites"
|
||||
sys.path.append(str(prerequisites_path))
|
||||
|
||||
# Import the SDK modules
|
||||
from zvml import ZVMLClient
|
||||
|
||||
# Import configuration
|
||||
try:
|
||||
from config import (
|
||||
ZVM_HOST,
|
||||
ZVM_PORT,
|
||||
ZVM_SSL_VERIFY,
|
||||
CLIENT_ID,
|
||||
CLIENT_SECRET
|
||||
)
|
||||
except ImportError:
|
||||
print("Error: Please copy config.example.py to config.py and update with your values")
|
||||
print("Expected path:", prerequisites_path / "config.py")
|
||||
sys.exit(1)
|
||||
|
||||
def main():
|
||||
"""
|
||||
Main function to demonstrate Zerto authentication.
|
||||
Complete the following steps:
|
||||
1. Initialize ZVMLClient with Keycloak credentials
|
||||
2. Test connection by retrieving local site info
|
||||
3. Handle authentication and connection errors
|
||||
"""
|
||||
# Set up logging with timestamp
|
||||
logging.basicConfig(
|
||||
level=logging.INFO,
|
||||
format='%(asctime)s - %(levelname)s - %(message)s'
|
||||
)
|
||||
|
||||
try:
|
||||
# Step 1: Create a ZVMLClient instance
|
||||
# TODO: Initialize the ZVMLClient with your ZVM host and credentials
|
||||
# Hint: Use ZVMLClient(zvm_address=ZVM_HOST, client_id=CLIENT_ID,
|
||||
# client_secret=CLIENT_SECRET, verify_certificate=ZVM_SSL_VERIFY)
|
||||
client = None # Replace with actual client initialization
|
||||
|
||||
# Step 2: Test the connection
|
||||
# TODO: Try to get local site information to verify the connection
|
||||
# Hint: Use client.localsite.get_local_site() and extract the Version
|
||||
# Hint: Log the version using logging.info()
|
||||
pass # Replace with actual connection test
|
||||
|
||||
# Step 3: Print connection status
|
||||
# TODO: Display whether the connection was successful
|
||||
# Hint: Use logging.info() to show the status
|
||||
|
||||
except Exception as e:
|
||||
# TODO: Handle any authentication or connection errors
|
||||
# Hint: Use logging.error() to log the error message
|
||||
logging.error(f"Authentication failed: {str(e)}")
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user