Skip to content

Python DSC Adapter and Test Resource implementation - #1520

Open
shammu1 wants to merge 7 commits into
PowerShell:mainfrom
shammu1:python-adapter-v1
Open

Python DSC Adapter and Test Resource implementation#1520
shammu1 wants to merge 7 commits into
PowerShell:mainfrom
shammu1:python-adapter-v1

Conversation

@shammu1

@shammu1 shammu1 commented May 7, 2026

Copy link
Copy Markdown
Collaborator

PR Summary

The Python adapter (Microsoft.DSC.Adapters/Python) enables DSC v3 to dynamically execute Python resource classes. It acts as a bridge between the DSC framework and Python-based resource implementations, handling all DSC operations (get, set, test, export, list, validate).

Key Features
Dynamic Resource Discovery: Reads resource mappings from pyproject.toml under [tool.dsc.resources]
Operation Routing: Routes DSC operations to appropriate Python resource class methods
Structured Logging: Full trace/debug/info/error logging with optional profiling capabilities
State Management: Handles JSON serialization and properly emits state + diffs per DSC specification
Error Handling: Graceful error handling with proper exit codes and stderr logging

Core Components
adapter.py: Main ResourceAdapter class managing operation routing, class loading, and resource discovery
discovery.py: Parses pyproject.toml and dynamically imports resource classes
cli.py: Command-line interface handling arguments and stdin input
dsc_logging.py: Environment variable-based logging configuration

Test Coverage
Three comprehensive test layers:

Unit Tests (Python/unittest) — Tests adapter internals: initialization, manifest discovery, class resolution, operation routing
Component Tests (Pester) — Validates adapter subprocess behavior directly: output shapes, exit codes, error handling
Integration Tests (Pester) — End-to-end DSC CLI integration testing through the full DSC framework

PR Context

DSC already supports adapters for non-native resource ecosystems, and this PR adds a Python adapter so Python-based resources can participate in the same DSC execution model as built-in and PowerShell resources.

This adapter is needed to:

  1. Enable first-class Python resource support in DSC with the same get, set, test, export, list, and validate flow.
  2. Improve extensibility of DSC by allowing teams with existing Python tooling and expertise to author/manage resources without switching implementation languages.

@shammu1
shammu1 force-pushed the python-adapter-v1 branch 2 times, most recently from 4c04457 to 6ccefc6 Compare May 7, 2026 19:52
@shammu1

shammu1 commented May 8, 2026

Copy link
Copy Markdown
Collaborator Author

@microsoft-github-policy-service agree company="Microsoft"

@shammu1
shammu1 force-pushed the python-adapter-v1 branch 6 times, most recently from e3b036f to bb93ae9 Compare May 14, 2026 02:21
@shammu1
shammu1 marked this pull request as ready for review May 14, 2026 03:29
Copilot AI review requested due to automatic review settings May 14, 2026 03:29
@shammu1

shammu1 commented May 15, 2026

Copy link
Copy Markdown
Collaborator Author

Copilot Review this

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new Python DSC adapter implementation with test fixtures and Pester/Python tests, intended to let DSC invoke Python resource classes through adapted resource manifests.

Changes:

  • Adds Python adapter manifest and pyDscAdapter CLI/runtime modules.
  • Adds Python test resources, manifests, pyproject mapping, and unit/component/integration tests.
  • Updates ignore rules and one PowerShell adapter test stderr redirection.

Reviewed changes

Copilot reviewed 21 out of 27 changed files in this pull request and generated 46 comments.

Show a summary per file
File Description
.gitignore Adds Visual Studio and Python cache ignores.
adapters/__init__.py Adds package marker.
adapters/powershell/Tests/powershellgroup.resource.tests.ps1 Redirects stderr for one adapted-resource test.
adapters/python/.project.data.json Defines Python adapter build project metadata.
adapters/python/__init__.py Adds package marker.
adapters/python/pythonadapter.dsc.resource.json Adds DSC adapter manifest for Python operations.
adapters/python/pyDscAdapter/__init__.py Adds adapter package marker.
adapters/python/pyDscAdapter/__main__.py Adds Python adapter entry point.
adapters/python/pyDscAdapter/adapter.py Implements operation routing, class loading, logging, and profiling.
adapters/python/pyDscAdapter/cli.py Adds command-line parser and stdin/input handling.
adapters/python/pyDscAdapter/discovery.py Adds pyproject class-map parsing and dynamic file import.
adapters/python/pyDscAdapter/dsc_logging.py Adds structured DSC-style logging.
adapters/python/pyDscAdapter/utils.py Adds JSON parsing helper.
adapters/python/tests/.project.data.json Defines test project copy metadata.
adapters/python/tests/TEST_LAYERS.md Documents unit/component/integration test layers.
adapters/python/tests/__init__.py Adds test package marker.
adapters/python/tests/pythoncomponent.tests.ps1 Adds adapter subprocess component tests.
adapters/python/tests/pythonintegration.tests.ps1 Adds DSC CLI integration tests.
adapters/python/tests/pythonunit.tests.py Adds Python unittest coverage for adapter internals.
adapters/python/tests/pyproject.toml Maps PythonTest resource types to fixture classes.
adapters/python/tests/pythontest.dsc.manifests.json Adds adapted resource manifests for Python test resources.
adapters/python/tests/src/.project.data.json Defines fixture resource copy metadata.
adapters/python/tests/src/__init__.py Adds fixture package marker.
adapters/python/tests/src/export.py Adds export-only fixture resource.
adapters/python/tests/src/get.py Adds get-only fixture resource.
adapters/python/tests/src/set.py Adds set fixture resource.
adapters/python/tests/src/test.py Adds test-only fixture resource.

Comment thread adapters/python/.project.data.json
Comment thread adapters/python/.project.data.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/tests/src/export.py
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/dsc_logging.py
Comment thread adapters/python/tests/.project.data.json Outdated
@shammu1
shammu1 requested a review from Copilot May 21, 2026 16:41

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 21 out of 27 changed files in this pull request and generated 12 comments.

Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py
Comment thread adapters/python/pyDscAdapter/dsc_logging.py Outdated
Comment thread adapters/python/pyDscAdapter/discovery.py
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/tests/src/test.py Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/adapter.py Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 28 changed files in this pull request and generated 8 comments.

Comment thread adapters/python/pythonadapter.dsc.resource.json
Comment thread adapters/python/pythonadapter.dsc.resource.json
Comment thread adapters/python/pythonadapter.dsc.resource.json
Comment thread adapters/python/pythonadapter.dsc.resource.json
Comment thread adapters/python/pythonadapter.dsc.resource.json
Comment thread adapters/python/pyDscAdapter/adapter.py
Comment thread adapters/python/tests/src/set.py Outdated
Comment thread adapters/python/tests/pythonunit.tests.py

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 28 changed files in this pull request and generated 8 comments.

Comment thread adapters/python/pyDscAdapter/adapter.py Outdated
Comment thread adapters/python/pyDscAdapter/cli.py
Comment thread adapters/python/pyDscAdapter/__main__.py
Comment thread adapters/python/pyDscAdapter/adapter.py
Comment thread adapters/python/tests/pythonunit.tests.py
Comment thread adapters/python/tests/pythonunit.tests.py
Comment thread adapters/python/tests/pythonunit.tests.py
Comment thread adapters/python/tests/pythonintegration.tests.ps1 Outdated
Copilot AI review requested due to automatic review settings July 21, 2026 00:15
@shammu1
shammu1 force-pushed the python-adapter-v1 branch from 63a7654 to 7029eb3 Compare July 21, 2026 00:15

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 28 changed files in this pull request and generated 7 comments.

Comments suppressed due to low confidence (1)

adapters/python/tests/pythonintegration.tests.ps1:25

  • This comment claims the Python adapter manifest has a condition on python3 availability, but the manifest added in this PR does not include a condition field. This is misleading for future maintainers and for interpreting failure expectations in this helper.
                # Python adapter manifest now has a condition on python3 availability.
                # If unavailable, resource discovery should fail cleanly rather than execution failing.

Comment thread adapters/python/pyDscAdapter/cli.py
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated
Comment thread adapters/python/pythonadapter.dsc.resource.json Outdated

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 22 out of 28 changed files in this pull request and generated no new comments.

Suppressed comments (4)

adapters/python/pyDscAdapter/adapter.py:164

  • _instantiate_resource checks only for attribute existence with hasattr(cls, "from_json"), but doesn’t ensure it’s callable. If a resource defines a non-callable from_json attribute (e.g., a class variable/property), this will raise a TypeError and fail the operation unexpectedly. Prefer a callable() check on getattr().
        if hasattr(cls, "from_json"): 
            return cls.from_json(json_input, operation=operation)
        # Fallback: direct init from dict if needed
        data = json.loads(json_input or "{}")
        return cls(**data)

adapters/python/pyDscAdapter/adapter.py:197

  • When resource introspection fails in _build_list_entry, capabilities is forced to ["get","set","test"]. This can advertise operations the resource does not implement, causing DSC to attempt unsupported operations later. It’s safer to return an empty capabilities list (or only those confirmed) when introspection fails.
        if not capabilities:
            capabilities = ["get", "set", "test"]  # remove this

adapters/python/pyDscAdapter/adapter.py:243

  • _discover_python_resources appends list entries even when it can’t find a corresponding .py file for a discovered resource type. This can produce list results with non-existent paths and causes avoidable import/introspection failures. Skip the resource when no file can be resolved.
                resource_file = pyproject_path.parent / (discovered_type.split("/")[-1].lower() + ".py")
                if not resource_file.exists():
                    resource_file = pyproject_path.parent / (class_name.lower() + ".py")

adapters/python/pyDscAdapter/discovery.py:152

  • The non-tomllib fallback parser in get_class_map_from_pyproject only supports a flat [tool.dsc.resources] key/value form, but this repo’s pyproject.toml uses per-resource tables like [tool.dsc.resources."PythonTest/Get"]. On Python < 3.11 (no tomllib), discovery will return an empty class map and the adapter won’t resolve any resources.
    class_map = {}
    in_section = False
    
    for line in content.splitlines():
        stripped = line.strip()
        if stripped == "[tool.dsc.resources]":

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants