Ensure¶
The problem with ALTER¶
Every alter_*() call sends an ALTER command to the queue manager,
even when every specified attribute already matches the current state.
MQ updates ALTDATE and ALTTIME on every ALTER, regardless of
whether any values actually changed. This makes ALTER unsuitable for
declarative configuration management where idempotency matters — running
the same configuration twice should not corrupt audit timestamps.
The ensure pattern¶
The ensure_*() methods implement a declarative upsert pattern:
- DEFINE the object when it does not exist.
- ALTER only the attributes that differ from the current state.
- Do nothing when all specified attributes already match,
preserving
ALTDATEandALTTIME.
EnsureAction¶
An enum indicating the action taken by an ensure method:
class EnsureAction(Enum):
CREATED = "created" # Object did not exist; DEFINE was issued
UPDATED = "updated" # Object existed but attributes differed; ALTER was issued
UNCHANGED = "unchanged" # Object already matched the desired state
EnsureResult¶
A named tuple containing the action taken and the list of attribute names that triggered the change (if any):
class EnsureResult(NamedTuple):
action: EnsureAction # What happened
changed: list[str] # Attribute names that differed (empty for CREATED/UNCHANGED)
| Attribute | Type | Description |
|---|---|---|
action |
EnsureAction |
What happened: CREATED, UPDATED, or UNCHANGED |
changed |
list[str] |
Attribute names that triggered an ALTER (in the caller's namespace) |
Method signature patterns¶
Most methods share the same signature:
def ensure_qlocal(
self,
name: str,
request_parameters: Mapping[str, object] | None = None,
) -> EnsureResult:
The queue manager ensure method omits the name parameter:
response_parameters is not exposed — the ensure logic always requests
["all"] internally so it can compare the full current state.
Basic usage¶
from pymqrest import EnsureAction, EnsureResult
# First call — queue does not exist yet
result = session.ensure_qlocal(
"APP.REQUEST.Q",
request_parameters={
"max_queue_depth": 50000,
"description": "Application request queue",
},
)
assert result.action is EnsureAction.CREATED
# Second call — same attributes, nothing to change
result = session.ensure_qlocal(
"APP.REQUEST.Q",
request_parameters={
"max_queue_depth": 50000,
"description": "Application request queue",
},
)
assert result.action is EnsureAction.UNCHANGED
# Third call — description changed, only that attribute is altered
result = session.ensure_qlocal(
"APP.REQUEST.Q",
request_parameters={
"max_queue_depth": 50000,
"description": "Updated request queue",
},
)
assert result.action is EnsureAction.UPDATED
assert result.changed == ("description",)
Comparison logic¶
The ensure methods compare only the attributes the caller passes in
request_parameters against the current state returned by DISPLAY.
Attributes not specified by the caller are ignored.
Comparison is:
- Case-insensitive —
"ENABLED"matches"enabled". - Type-normalizing — integer
5000matches string"5000". - Whitespace-trimming —
" YES "matches"YES".
An attribute present in request_parameters but absent from the
DISPLAY response is treated as changed and included in the ALTER.
Selective ALTER¶
When an update is needed, only the changed attributes are sent in the
ALTER command. Attributes that already match are excluded from the
request. This minimizes the scope of each ALTER to the strict delta.
Available methods¶
Each method targets a specific MQ object type with the correct MQSC qualifier triple (DISPLAY / DEFINE / ALTER):
| Method | Object type | DISPLAY | DEFINE | ALTER |
|---|---|---|---|---|
ensure_qmgr() |
Queue manager | QMGR |
— | QMGR |
ensure_qlocal() |
Local queue | QUEUE |
QLOCAL |
QLOCAL |
ensure_qremote() |
Remote queue | QUEUE |
QREMOTE |
QREMOTE |
ensure_qalias() |
Alias queue | QUEUE |
QALIAS |
QALIAS |
ensure_qmodel() |
Model queue | QUEUE |
QMODEL |
QMODEL |
ensure_channel() |
Channel | CHANNEL |
CHANNEL |
CHANNEL |
ensure_authinfo() |
Auth info | AUTHINFO |
AUTHINFO |
AUTHINFO |
ensure_listener() |
Listener | LISTENER |
LISTENER |
LISTENER |
ensure_namelist() |
Namelist | NAMELIST |
NAMELIST |
NAMELIST |
ensure_process() |
Process | PROCESS |
PROCESS |
PROCESS |
ensure_service() |
Service | SERVICE |
SERVICE |
SERVICE |
ensure_topic() |
Topic | TOPIC |
TOPIC |
TOPIC |
ensure_sub() |
Subscription | SUB |
SUB |
SUB |
ensure_stgclass() |
Storage class | STGCLASS |
STGCLASS |
STGCLASS |
ensure_comminfo() |
Comm info | COMMINFO |
COMMINFO |
COMMINFO |
ensure_cfstruct() |
CF structure | CFSTRUCT |
CFSTRUCT |
CFSTRUCT |
Queue manager (singleton)¶
ensure_qmgr() has no name parameter because the queue manager is a
singleton that always exists. It can only return UPDATED or
UNCHANGED (never CREATED):
This makes it ideal for asserting queue manager-level settings such as
statistics, monitoring, events, and logging attributes without
corrupting ALTDATE/ALTTIME on every run.
Attribute mapping¶
The ensure methods participate in the same
mapping pipeline as all other command methods.
Pass snake_case attribute names in request_parameters and the
mapping layer translates them to MQSC names for the DISPLAY, DEFINE,
and ALTER commands automatically.
Configuration management example¶
The ensure pattern is designed for scripts that declare desired state:
def configure_queue_manager(session):
"""Ensure queue manager attributes are set for production."""
result = session.ensure_qmgr(request_parameters={
"queue_statistics": "on",
"channel_statistics": "on",
"queue_monitoring": "medium",
"channel_monitoring": "medium",
})
print(f"Queue manager: {result.action.value}")
queues = {
"APP.REQUEST.Q": {"max_queue_depth": 50000, "default_persistence": "yes"},
"APP.REPLY.Q": {"max_queue_depth": 10000, "default_persistence": "no"},
"APP.DLQ": {"max_queue_depth": 100000, "default_persistence": "yes"},
}
for name, attrs in queues.items():
result = session.ensure_qlocal(name, request_parameters=attrs)
print(f"{name}: {result.action.value}")
Running this script repeatedly produces no side effects when the
configuration is already correct. Only genuine changes trigger ALTER
commands, keeping ALTDATE/ALTTIME accurate.
API reference¶
Bases: Enum
Action taken by an ensure operation.
Attributes:
| Name | Type | Description |
|---|---|---|
CREATED |
The object did not exist and was defined. |
|
UPDATED |
The object existed but attributes differed and were altered. |
|
UNCHANGED |
The object existed and all specified attributes already matched. |
Source code in src/pymqrest/ensure.py
Result of an ensure operation.
Attributes:
| Name | Type | Description |
|---|---|---|
action |
EnsureAction
|
The :class: |
changed |
tuple[str, ...]
|
Attribute names that triggered the ALTER, in the caller's namespace (snake_case when mapping is enabled, MQSC when disabled). Empty for CREATED and UNCHANGED actions. |
Source code in src/pymqrest/ensure.py
Mixin providing idempotent ensure methods for MQ objects.
Each ensure_* method implements a declarative upsert pattern:
DEFINE when the object does not exist, ALTER only when specified
attributes differ, and no-op when they already match — preserving
ALTDATE/ALTTIME for unchanged objects.
Source code in src/pymqrest/ensure.py
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 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 | |
_mqsc_command(*, command, mqsc_qualifier, name, request_parameters, response_parameters, where=None)
¶
Source code in src/pymqrest/ensure.py
_ensure_object(*, name, request_parameters, display_qualifier, define_qualifier, alter_qualifier)
¶
Core ensure logic shared by all ensure_* methods.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
MQ object name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
required |
display_qualifier
|
str
|
MQSC qualifier for the DISPLAY command. |
required |
define_qualifier
|
str
|
MQSC qualifier for the DEFINE command. |
required |
alter_qualifier
|
str
|
MQSC qualifier for the ALTER command. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_qmgr(request_parameters=None)
¶
Ensure the queue manager has the specified attributes.
Unlike other ensure methods, the queue manager always exists and
cannot be defined or deleted. This method compares the requested
attributes against the current state and issues ALTER QMGR
only when values differ. Returns UPDATED or UNCHANGED
(never CREATED).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_qlocal(name, request_parameters=None)
¶
Ensure a local queue exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Queue name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_qremote(name, request_parameters=None)
¶
Ensure a remote queue exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Queue name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_qalias(name, request_parameters=None)
¶
Ensure an alias queue exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Queue name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_qmodel(name, request_parameters=None)
¶
Ensure a model queue exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Queue name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_channel(name, request_parameters=None)
¶
Ensure a channel exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Channel name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_authinfo(name, request_parameters=None)
¶
Ensure an authentication information object exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Authentication information object name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_listener(name, request_parameters=None)
¶
Ensure a listener exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Listener name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_namelist(name, request_parameters=None)
¶
Ensure a namelist exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Namelist name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_process(name, request_parameters=None)
¶
Ensure a process exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Process name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_service(name, request_parameters=None)
¶
Ensure a service exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Service name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_topic(name, request_parameters=None)
¶
Ensure a topic exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Topic name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_sub(name, request_parameters=None)
¶
Ensure a subscription exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Subscription name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_stgclass(name, request_parameters=None)
¶
Ensure a storage class exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Storage class name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_comminfo(name, request_parameters=None)
¶
Ensure a communication information object exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Communication information object name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |
Source code in src/pymqrest/ensure.py
ensure_cfstruct(name, request_parameters=None)
¶
Ensure a CF structure exists with the specified attributes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
CF structure name. |
required |
request_parameters
|
Mapping[str, object] | None
|
Desired attributes to assert/set. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
The |
EnsureResult
|
class: |