0
# Core Driver System
1
2
The core driver system provides the foundation for Libcloud's unified interface across all cloud providers. It implements a factory pattern that allows you to obtain provider-specific drivers through a consistent API.
3
4
## Driver Types
5
6
```python { .api }
7
class DriverType:
8
"""Enumeration of supported service types"""
9
BACKUP = BackupProvider
10
COMPUTE = ComputeProvider
11
CONTAINER = ContainerProvider
12
DNS = DnsProvider
13
LOADBALANCER = LoadBalancerProvider
14
STORAGE = StorageProvider
15
```
16
17
## Driver Factory Functions
18
19
### Universal Driver Factory
20
21
```python { .api }
22
def get_driver(type: DriverType, provider: str) -> BaseDriver
23
```
24
25
Get a driver instance for the specified type and provider.
26
27
**Parameters:**
28
- `type`: The service type from DriverType enum
29
- `provider`: The provider identifier string
30
31
**Returns:**
32
- Driver instance for the specified provider
33
34
**Raises:**
35
- `DriverTypeNotFoundError`: If the driver type is not supported
36
37
**Example:**
38
```python
39
from libcloud.base import get_driver, DriverType
40
from libcloud.compute.types import Provider
41
42
# Using universal factory
43
driver = get_driver(DriverType.COMPUTE, Provider.EC2)
44
```
45
46
### Service-Specific Factories
47
48
Each service provides its own factory function for convenience:
49
50
```python { .api }
51
# Compute
52
def get_compute_driver(provider: str) -> NodeDriver
53
54
# Storage
55
def get_storage_driver(provider: str) -> StorageDriver
56
57
# DNS
58
def get_dns_driver(provider: str) -> DNSDriver
59
60
# Load Balancer
61
def get_loadbalancer_driver(provider: str) -> Driver
62
63
# Container
64
def get_container_driver(provider: str) -> ContainerDriver
65
66
# Backup
67
def get_backup_driver(provider: str) -> BackupDriver
68
```
69
70
**Example:**
71
```python
72
from libcloud.compute.providers import get_driver
73
from libcloud.compute.types import Provider
74
75
# Using service-specific factory
76
driver = get_driver(Provider.EC2)
77
```
78
79
## Base Driver Classes
80
81
### BaseDriver
82
83
```python { .api }
84
class BaseDriver:
85
"""Base class for all Libcloud drivers"""
86
name: str
87
website: str
88
type: Type
89
api_name: str
90
91
def __init__(self, key: str, secret: str = None, secure: bool = True, host: str = None, port: int = None, **kwargs)
92
```
93
94
All driver classes inherit from BaseDriver and provide service-specific methods.
95
96
## Debugging Support
97
98
### Enable Debug Logging
99
100
```python { .api }
101
def enable_debug(fo: TextIOWrapper) -> None
102
```
103
104
Enable library-wide debugging to a file-like object. This logs all HTTP requests and responses to help with troubleshooting.
105
106
**Parameters:**
107
- `fo`: File-like object to write debug information to
108
109
**Example:**
110
```python
111
import libcloud
112
import sys
113
114
# Enable debug logging to stderr
115
libcloud.enable_debug(sys.stderr)
116
117
# Enable debug logging to a file
118
with open('libcloud.log', 'w') as f:
119
libcloud.enable_debug(f)
120
```
121
122
You can also enable debugging via environment variable:
123
```bash
124
export LIBCLOUD_DEBUG=/dev/stderr
125
# or
126
export LIBCLOUD_DEBUG=libcloud.log
127
```
128
129
## Driver Type Factory Map
130
131
```python { .api }
132
DriverTypeFactoryMap: Dict[Type, Callable]
133
```
134
135
Internal mapping of driver types to their factory functions. Used by the universal `get_driver()` function.
136
137
## Exceptions
138
139
```python { .api }
140
class DriverTypeNotFoundError(KeyError):
141
"""Raised when an unsupported driver type is requested"""
142
def __init__(self, type: Type)
143
```
144
145
## Version Information
146
147
```python { .api }
148
__version__: str
149
```
150
151
The current version of the Libcloud library.
152
153
**Example:**
154
```python
155
import libcloud
156
print(f"Libcloud version: {libcloud.__version__}")
157
```
158
159
## Usage Patterns
160
161
### Basic Driver Initialization
162
163
```python
164
from libcloud.compute.types import Provider
165
from libcloud.compute.providers import get_driver
166
167
# Get driver class
168
cls = get_driver(Provider.EC2)
169
170
# Initialize driver with credentials
171
driver = cls('access_key', 'secret_key', region='us-east-1')
172
173
# Use driver methods
174
nodes = driver.list_nodes()
175
```
176
177
### Error Handling
178
179
```python
180
from libcloud.base import get_driver, DriverType, DriverTypeNotFoundError
181
from libcloud.common.types import InvalidCredsError
182
183
try:
184
driver = get_driver(DriverType.COMPUTE, 'invalid_provider')
185
except DriverTypeNotFoundError as e:
186
print(f"Driver not found: {e}")
187
188
try:
189
cls = get_driver(DriverType.COMPUTE, Provider.EC2)
190
driver = cls('invalid_key', 'invalid_secret')
191
nodes = driver.list_nodes()
192
except InvalidCredsError as e:
193
print(f"Invalid credentials: {e}")
194
```