1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

win_uri: module cleanup and added check-mode support (#20511)

The following changes have been made:
- Added check-mode support
- Added parameter type support
- Replace PSObject by a normal dictionary
- Improve idempotency (only when $dest is provided it is not idempotent (yet)
This commit is contained in:
Dag Wieers 2017-01-28 00:24:32 +01:00 committed by Matt Davis
parent ba02d9b85c
commit 93bb6b8eaf
2 changed files with 50 additions and 43 deletions

View file

@ -19,43 +19,44 @@
# WANT_JSON # WANT_JSON
# POWERSHELL_COMMON # POWERSHELL_COMMON
$params = Parse-Args $args; $ErrorActionPreference = "Stop"
$result = New-Object psobject @{
win_uri = New-Object psobject
}
# Functions ############################################### # Functions ###############################################
Function ConvertTo-SnakeCase($input_string) { Function ConvertTo-SnakeCase($input_string) {
$snake_case = $input_string -csplit "(?<!^)(?=[A-Z])" -join "_" $snake_case = $input_string -csplit "(?<!^)(?=[A-Z])" -join "_"
$snake_case = $snake_case.ToLower() return $snake_case.ToLower()
return $snake_case
} }
# Build Arguments # Build Arguments
$webrequest_opts = @{} $params = Parse-Args $args -supports_check_mode $true
$url = Get-AnsibleParam -obj $params -name "url" -failifempty $true $url = Get-AnsibleParam -obj $params -name "url" -type "string" -failifempty $true
$method = Get-AnsibleParam -obj $params "method" -default "GET" $method = Get-AnsibleParam -obj $params "method" -type "string" -default "GET" -validateset "GET","POST","PUT","HEAD","DELETE","OPTIONS","PATCH","TRACE","CONNECT","REFRESH"
$content_type = Get-AnsibleParam -obj $params -name "content_type" $content_type = Get-AnsibleParam -obj $params -name "content_type" -type "string"
$headers = Get-AnsibleParam -obj $params -name "headers" # TODO: Why is this not a normal dictionary ?
$body = Get-AnsibleParam -obj $params -name "body" $headers = Get-AnsibleParam -obj $params -name "headers" -type "string"
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path" -default $null # TODO: Why is this not a normal dictionary ?
$use_basic_parsing = ConvertTo-Bool (Get-AnsibleParam -obj $params -name "use_basic_parsing" -default $true) $body = Get-AnsibleParam -obj $params -name "body" -type "string"
$dest = Get-AnsibleParam -obj $params -name "dest" -type "path"
$use_basic_parsing = Get-AnsibleParam -obj $params -name "use_basic_parsing" -type "bool" -default $true
$webrequest_opts.Uri = $url $result = @{
Set-Attr $result.win_uri "url" $url changed = $false
win_uri = @{
content_type = $content_type
method = $method
url = $url
use_basic_parsing = $use_basic_parsing
}
}
$webrequest_opts.Method = $method $webrequest_opts = @{
Set-Attr $result.win_uri "method" $method ContentType = $content_type
Method = $method
$webrequest_opts.ContentType = $content_type Uri = $url
Set-Attr $result.win_uri "content_type" $content_type UseBasicParsing = $use_basic_parsing
}
$webrequest_opts.UseBasicParsing = $use_basic_parsing
Set-Attr $result.win_uri "use_basic_parsing" $use_basic_parsing
if ($headers -ne $null) { if ($headers -ne $null) {
$req_headers = @{} $req_headers = @{}
@ -68,25 +69,31 @@ if ($headers -ne $null) {
if ($body -ne $null) { if ($body -ne $null) {
$webrequest_opts.Body = $body $webrequest_opts.Body = $body
Set-Attr $result.win_uri "body" $body $result.win_uri.body = $body
} }
if ($dest -ne $null) { if ($dest -ne $null) {
$webrequest_opts.OutFile = $dest $webrequest_opts.OutFile = $dest
Set-Attr $result.win_uri "dest" $dest $result.win_uri.dest = $dest
} }
try { # TODO: When writing to a file, this is not idempotent !
if ($check_mode -ne $true -or $dest -eq $null) {
try {
$response = Invoke-WebRequest @webrequest_opts $response = Invoke-WebRequest @webrequest_opts
} catch { } catch {
$ErrorMessage = $_.Exception.Message Fail-Json $result $_.Exception.Message
Fail-Json $result $ErrorMessage }
}
# Assume a change when we are writing to a file
if ($dest -ne $null) {
$result.changed = $true
} }
ForEach ($prop in $response.psobject.properties) { ForEach ($prop in $response.psobject.properties) {
$result_key = ConvertTo-SnakeCase $prop.Name $result_key = ConvertTo-SnakeCase $prop.Name
$result_value = $prop.Value $result.$result_key = $prop.Value
Set-Attr $result $result_key $result_value
} }
Exit-Json $result Exit-Json $result

View file

@ -36,6 +36,7 @@ options:
url: url:
description: description:
- HTTP or HTTPS URL in the form of (http|https)://host.domain:port/path - HTTP or HTTPS URL in the form of (http|https)://host.domain:port/path
required: True
method: method:
description: description:
- The HTTP Method of the request or response. - The HTTP Method of the request or response.
@ -61,7 +62,6 @@ options:
version_added: "2.3" version_added: "2.3"
description: description:
- Output the response body to a file. - Output the response body to a file.
default: None
headers: headers:
description: description:
- 'Key Value pairs for headers. Example "Host: www.somesite.com"' - 'Key Value pairs for headers. Example "Host: www.somesite.com"'
@ -84,19 +84,19 @@ EXAMPLES = r'''
# Set a HOST header to hit an internal webserver: # Set a HOST header to hit an internal webserver:
- name: Hit a Specific Host on the Server - name: Hit a Specific Host on the Server
win_uri: win_uri:
url: http://example.com url: http://example.com/
method: GET method: GET
headers: headers:
host: www.somesite.com host: www.somesite.com
- name: Perform a HEAD on an Endpoint - name: Perform a HEAD on an Endpoint
win_uri: win_uri:
url: http://www.example.com url: http://www.example.com/
method: HEAD method: HEAD
- name: POST a Body to an Endpoint - name: POST a Body to an Endpoint
win_uri: win_uri:
url: http://www.somesite.com url: http://www.somesite.com/
method: POST method: POST
body: "{ 'some': 'json' }" body: "{ 'some': 'json' }"
''' '''
@ -106,17 +106,17 @@ url:
description: The Target URL description: The Target URL
returned: always returned: always
type: string type: string
sample: "https://www.ansible.com" sample: https://www.ansible.com
method: method:
description: The HTTP method used. description: The HTTP method used.
returned: always returned: always
type: string type: string
sample: "GET" sample: GET
content_type: content_type:
description: The "content-type" header used. description: The "content-type" header used.
returned: always returned: always
type: string type: string
sample: "application/json" sample: application/json
use_basic_parsing: use_basic_parsing:
description: The state of the "use_basic_parsing" flag. description: The state of the "use_basic_parsing" flag.
returned: always returned: always
@ -137,7 +137,7 @@ status_description:
description: A summery of the status. description: A summery of the status.
returned: success returned: success
type: string type: string
stample: "OK" sample: OK
raw_content: raw_content:
description: The raw content of the HTTP response. description: The raw content of the HTTP response.
returned: success returned: success