Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Resource for managing choco config #132

Closed

Conversation

mrhockeymonkey
Copy link
Contributor

@mrhockeymonkey mrhockeymonkey commented Aug 14, 2019

This is in answer to #20 and partly related to #124.

This resource allows control over config settings via "choco config set/unset. It also has an option for querying the xml settings file directly. This is for performance reasons as discussed in #124. It is optional and not recommended for ongoing management but makes the resource much quicker, 2.82 seconds vs 0.68 seconds (see sample output below).

Sample output:

# normal mode
VERBOSE [SURFACE]: LCM:[ Start  Set]
VERBOSE [SURFACE]:[DSCEngine] Importing the module C:\Program Files\WindowsPowerShell\Modules\cChoco\DscResources\cChocoConfig\cChocoConfig.psm1 in force mode.
VERBOSE [SURFACE]: LCM:  [ Start  Resource ]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE [SURFACE]: LCM:  [ Start  Test]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE [SURFACE]:[[cChocoConfig]webRequestTimeoutSeconds] Importing the module cChocoConfig in force mode.
VERBOSE [SURFACE]:[[cChocoConfig]webRequestTimeoutSeconds] Querying choco config via CLI
VERBOSE [SURFACE]:[[cChocoConfig]webRequestTimeoutSeconds] webRequestTimeoutSeconds is in desired state
VERBOSE [SURFACE]: LCM:  [ End    Test]  [[cChocoConfig]webRequestTimeoutSeconds]  in 1.9260 seconds.
VERBOSE [SURFACE]: LCM:  [ Skip   Set]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE [SURFACE]: LCM:  [ End    Resource ]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE [SURFACE]: LCM:  [ End    Set]
VERBOSE [SURFACE]: LCM:  [ End    Set]    in  2.8210 seconds.   

# querying xml directly
VERBOSE: [SURFACE]: LCM:  [ Start  Set]
VERBOSE: [SURFACE]:[DSCEngine] Importing the module C:\Program Files\WindowsPowerShell\Modules\cChoco\DscResources\cChocoConfig\cChocoConfig.psm1 in force mode.
VERBOSE: [SURFACE]: LCM:  [ Start  Resource ]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE: [SURFACE]: LCM:  [ Start  Test]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE: [SURFACE]:[[cChocoConfig]webRequestTimeoutSeconds] Importing the module cChocoConfig in force mode.
VERBOSE: [SURFACE]:[[cChocoConfig]webRequestTimeoutSeconds] Querying chco config via chocolatey.config xml...
VERBOSE: [SURFACE]:[[cChocoConfig]webRequestTimeoutSeconds] webRequestTimeoutSeconds is in desired state
VERBOSE: [SURFACE]: LCM:  [ End    Test]  [[cChocoConfig]webRequestTimeoutSeconds]  in 0.4470 seconds.
VERBOSE: [SURFACE]: LCM:  [ Skip   Set]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE: [SURFACE]: LCM:  [ End    Resource ]  [[cChocoConfig]webRequestTimeoutSeconds]
VERBOSE: [SURFACE]: LCM:  [ End    Set]
VERBOSE: [SURFACE]: LCM:  [ End    Set]    in  0.6830 seconds.  

feedback welcome.

ferventcoder and others added 3 commits August 29, 2017 11:22
* development:
  (chocolateyGH-71) Implemented cChocoFeature
  (chocolateyGH-87) Moved to using reduced output
  (maint) formatting
  (chocolateyGH-70) Apply Apache v2 Licensing
  (chocolateyGH-85) Pass the source to cChocoPackageInstallerSet correctly
  (chocolateyGH-44) Pass the Source to choco correctly
  Added version to example
  (chocolateyGH-77) Push to gallery on new tags
  (maint) formatting
  (chocolateyGH-72) PowerShell scripts in format for signing
* development:
  (build) Updated module version
  (chocolateyGH-114) Fix typo
  (chocolateyGH-114) Skip verifying Pester publisher check
  [chocolatey#105] Chocolatey params passed in when upgrading a package.
  Choco Run Errors out because of Null Source
  (chocolateyGH-90) Cache choco list output
@pauby
Copy link
Member

pauby commented Oct 5, 2019

@mrhockeymonkey You've added two previous commits in your PR. Can you remove those?

Copy link
Member

@pauby pauby left a comment

Choose a reason for hiding this comment

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

Thanks for that and apologies for it taking so long to look at!

Comment on lines +25 to +38
[OutputType([System.Collections.Hashtable])]
param (
[Parameter(Mandatory)]
[System.String]$Key,

[Parameter()]
[System.String]$Value,

[Parameter()]
[ValidateSet('Present', 'Absent')]
[System.String]$Ensure = 'Present',

[Parameter()]
[System.Boolean]$QueryXML
Copy link
Member

Choose a reason for hiding this comment

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

The OutputType and Parameter types all use the .NET types. Can we stick with the PowerShell types?

[System.Boolean]$QueryXML
)

$return = @{
Copy link
Member

Choose a reason for hiding this comment

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

Can you rename this as return is a statement and giving a variable the same name is going to cause confusion.

[CmdletBinding()]
[OutputType([System.Collections.Hashtable])]
param (
[Parameter(Mandatory)]
Copy link
Member

Choose a reason for hiding this comment

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

To avoid confusion can we use Mandatory = $true?

Comment on lines +48 to +51
if ($Ensure -eq 'Absent') {
# if value and absent specified remove value as it can be confusing output
$return['Value'] = $null
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm confused a little but this. The comment says 'if value and absent specified ...' but the if statement only checks for absent. As $Value is an optional parameter it might not be provided.

function Set-TargetResource {
[CmdletBinding()]
param (
[Parameter(Mandatory)]
Copy link
Member

Choose a reason for hiding this comment

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

Mandatory = $true

)

if ($QueryXML.IsPresent) {
Write-Verbose "Querying chco config via chocolatey.config xml..."
Copy link
Member

Choose a reason for hiding this comment

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

Typo in 'choco'

$ConfigValue = $ConfigXml.SelectSingleNode($XPath).value
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
Copy link
Member

Choose a reason for hiding this comment

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

Just use a throw here rather than the .NET

Comment on lines +164 to +172
try {
$ConfigXml = [xml](Get-Content -Path $ConfigXmlPath)
# xpath is case sensitive so we must convert all to lower case
$XPath = "/chocolatey/config/add[translate(@key, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz') = '$($Key.ToLower())']"
$ConfigValue = $ConfigXml.SelectSingleNode($XPath).value
}
catch {
$PSCmdlet.ThrowTerminatingError($_)
}
Copy link
Member

Choose a reason for hiding this comment

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

I'm a little unsure about this. The code catches any exceptions being thrown but just throws the same exception. This would be no different than not having the try / catch block. So I'm wondering why it's there?

}
else {
Write-Verbose "Querying choco config via CLI"
$ConfigValue = & choco config get $Key -r
Copy link
Member

Choose a reason for hiding this comment

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

Please don't use aliases (&)? Can you also use the full named Chocolatey switches - so instead of -r use --limit-output.

There is no sanity check being done on the output of this command. What if the key does not exist? The caller will get a response back that is not what they are expecting.

# limitations under the License.

#----------------------------------------#
# Pester tests for cChocoPackageInstall #
Copy link
Member

Choose a reason for hiding this comment

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

Wrong one 😄

@pauby
Copy link
Member

pauby commented May 10, 2020

This is being superceded by #145. Closing.

@pauby pauby closed this May 10, 2020
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.

3 participants