diff --git a/dsc/src/subcommand.rs b/dsc/src/subcommand.rs index 064921084..e5decc185 100644 --- a/dsc/src/subcommand.rs +++ b/dsc/src/subcommand.rs @@ -495,7 +495,7 @@ pub fn validate_config(config: &Configuration, progress_format: ProgressFormat) let Some(type_name) = resource_block["type"].as_str() else { return Err(DscError::Validation(t!("subcommand.resourceTypeNotSpecified").to_string())); }; - resource_types.push(DiscoveryFilter::new(type_name, resource_block["api_version"].as_str(), None)); + resource_types.push(DiscoveryFilter::new(type_name, resource_block["requireVersion"].as_str(), None)); } dsc.find_resources(&resource_types, progress_format)?; @@ -507,7 +507,7 @@ pub fn validate_config(config: &Configuration, progress_format: ProgressFormat) trace!("{} '{}'", t!("subcommand.validatingResource"), resource_block["name"].as_str().unwrap_or_default()); // get the actual resource - let Some(resource) = get_resource(&mut dsc, type_name, resource_block["api_version"].as_str()) else { + let Some(resource) = get_resource(&mut dsc, type_name, resource_block["requireVersion"].as_str()) else { return Err(DscError::Validation(format!("{}: '{type_name}'", t!("subcommand.resourceNotFound")))); }; diff --git a/dsc/tests/dsc_config_version.tests.ps1 b/dsc/tests/dsc_config_version.tests.ps1 index 68bc99991..672c0dae0 100644 --- a/dsc/tests/dsc_config_version.tests.ps1 +++ b/dsc/tests/dsc_config_version.tests.ps1 @@ -19,7 +19,7 @@ Describe 'Tests for resource versioning' { resources: - name: Test Version type: Test/Version - apiVersion: $version + requireVersion: $version properties: version: $version "@ @@ -48,7 +48,7 @@ Describe 'Tests for resource versioning' { resources: - name: Test Version type: Test/Version - apiVersion: '$req' + requireVersion: '$req' properties: version: $expected "@ @@ -63,13 +63,13 @@ Describe 'Tests for resource versioning' { resources: - name: Test Version 1 type: Test/Version - apiVersion: '1.1.2' + requireVersion: '1.1.2' - name: Test Version 2 type: Test/Version - apiVersion: '1.1.0' + requireVersion: '1.1.0' - name: Test Version 3 type: Test/Version - apiVersion: '2' + requireVersion: '2' "@ $out = dsc -l trace config get -i $config_yaml 2> $TestDrive/error.log | ConvertFrom-Json $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw) @@ -77,4 +77,19 @@ Describe 'Tests for resource versioning' { $out.results[1].result.actualState.version | Should -BeExactly '1.1.0' $out.results[2].result.actualState.version | Should -BeExactly '2.0.0' } + + It 'apiVersion alias works' { + $config_yaml = @" + `$schema: https://aka.ms/dsc/schemas/v3/bundled/config/document.json + resources: + - name: Test Version + type: Test/Version + apiVersion: '1.1.2' + properties: + version: '1.1.2' +"@ + $out = dsc -l trace config get -i $config_yaml 2> $TestDrive/error.log | ConvertFrom-Json + $LASTEXITCODE | Should -Be 0 -Because (Get-Content $TestDrive/error.log -Raw) + $out.results[0].result.actualState.version | Should -BeExactly '1.1.2' + } } diff --git a/lib/dsc-lib/src/configure/config_doc.rs b/lib/dsc-lib/src/configure/config_doc.rs index fd3eb680e..ee945ec77 100644 --- a/lib/dsc-lib/src/configure/config_doc.rs +++ b/lib/dsc-lib/src/configure/config_doc.rs @@ -353,8 +353,8 @@ pub struct Resource { /// The fully qualified name of the resource type #[serde(rename = "type")] pub resource_type: FullyQualifiedTypeName, - #[serde(skip_serializing_if = "Option::is_none", rename = "apiVersion")] - pub api_version: Option, + #[serde(skip_serializing_if = "Option::is_none", rename = "requireVersion", alias = "apiVersion")] + pub require_version: Option, /// A friendly name for the resource instance #[serde(default)] pub name: String, // friendly unique instance name @@ -429,7 +429,7 @@ impl Resource { comments: None, location: None, tags: None, - api_version: None, + require_version: None, } } } @@ -517,12 +517,12 @@ mod test { { "type": "Microsoft.DSC.Debug/Echo", "name": "echoResource", - "apiVersion": "1.0.0" + "requireVersion": "1.0.0" }, { "type": "Microsoft/Process", "name": "processResource", - "apiVersion": "0.1.0" + "requireVersion": "0.1.0" } ] }"#; @@ -532,11 +532,11 @@ mod test { assert_eq!(config.resources.len(), 2); assert_eq!(config.resources[0].name, "echoResource"); assert_eq!(config.resources[0].resource_type, "Microsoft.DSC.Debug/Echo"); - assert_eq!(config.resources[0].api_version.as_deref(), Some("1.0.0")); + assert_eq!(config.resources[0].require_version.as_deref(), Some("1.0.0")); assert_eq!(config.resources[1].name, "processResource"); assert_eq!(config.resources[1].resource_type, "Microsoft/Process"); - assert_eq!(config.resources[1].api_version.as_deref(), Some("0.1.0")); + assert_eq!(config.resources[1].require_version.as_deref(), Some("0.1.0")); } } diff --git a/lib/dsc-lib/src/configure/mod.rs b/lib/dsc-lib/src/configure/mod.rs index 47816ec06..efc37cfc0 100644 --- a/lib/dsc-lib/src/configure/mod.rs +++ b/lib/dsc-lib/src/configure/mod.rs @@ -395,8 +395,8 @@ impl Configurator { continue; } let adapter = get_require_adapter_from_metadata(&resource.metadata); - let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.api_version.as_deref(), adapter.as_deref()))? else { - return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.api_version.as_deref().unwrap_or("").to_string())); + let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.require_version.as_deref(), adapter.as_deref()))? else { + return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.require_version.as_deref().unwrap_or("").to_string())); }; let properties = self.get_properties(&resource, &dsc_resource.kind)?; let filter = add_metadata(&dsc_resource, properties, resource.metadata.clone())?; @@ -480,8 +480,8 @@ impl Configurator { continue; } let adapter = get_require_adapter_from_metadata(&resource.metadata); - let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.api_version.as_deref(), adapter.as_deref()))? else { - return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.api_version.as_deref().unwrap_or("").to_string())); + let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.require_version.as_deref(), adapter.as_deref()))? else { + return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.require_version.as_deref().unwrap_or("").to_string())); }; let properties = self.get_properties(&resource, &dsc_resource.kind)?; debug!("resource_type {}", &resource.resource_type); @@ -665,8 +665,8 @@ impl Configurator { continue; } let adapter = get_require_adapter_from_metadata(&resource.metadata); - let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.api_version.as_deref(), adapter.as_deref()))? else { - return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.api_version.as_deref().unwrap_or("").to_string())); + let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.require_version.as_deref(), adapter.as_deref()))? else { + return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.require_version.as_deref().unwrap_or("").to_string())); }; let properties = self.get_properties(&resource, &dsc_resource.kind)?; debug!("resource_type {}", &resource.resource_type); @@ -749,8 +749,8 @@ impl Configurator { continue; } let adapter = get_require_adapter_from_metadata(&resource.metadata); - let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.api_version.as_deref(), adapter.as_deref()))? else { - return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.api_version.as_deref().unwrap_or("").to_string())); + let Some(dsc_resource) = discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.require_version.as_deref(), adapter.as_deref()))? else { + return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.require_version.as_deref().unwrap_or("").to_string())); }; let properties = self.get_properties(resource, &dsc_resource.kind)?; debug!("resource_type {}", &resource.resource_type); @@ -1045,12 +1045,12 @@ impl Configurator { if !skip_resource_validation { // Perform discovery of resources used in config - // create an array of DiscoveryFilter using the resource types and api_versions from the config + // create an array of DiscoveryFilter using the resource types and requireVersion from the config let mut discovery_filter: Vec = Vec::new(); let config_copy = config.clone(); for resource in config_copy.resources { let adapter = get_require_adapter_from_metadata(&resource.metadata); - let filter = DiscoveryFilter::new(&resource.resource_type, resource.api_version.as_deref(), adapter.as_deref()); + let filter = DiscoveryFilter::new(&resource.resource_type, resource.require_version.as_deref(), adapter.as_deref()); if !discovery_filter.contains(&filter) { discovery_filter.push(filter); } @@ -1070,8 +1070,8 @@ impl Configurator { // now check that each resource in the config was found for resource in config.resources.iter() { let adapter = get_require_adapter_from_metadata(&resource.metadata); - let Some(_dsc_resource) = self.discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.api_version.as_deref(), adapter.as_deref()))? else { - return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.api_version.as_deref().unwrap_or("").to_string())); + let Some(_dsc_resource) = self.discovery.find_resource(&DiscoveryFilter::new(&resource.resource_type, resource.require_version.as_deref(), adapter.as_deref()))? else { + return Err(DscError::ResourceNotFound(resource.resource_type.to_string(), resource.require_version.as_deref().unwrap_or("").to_string())); }; } }