Skip to content

Commit

Permalink
POL-1357 Azure Hybrid Use Benefit Policy Actions (#2639)
Browse files Browse the repository at this point in the history
* update

* fix

* update

* fix

* update

* fix

* update

* update
  • Loading branch information
XOmniverse authored Sep 24, 2024
1 parent bfe5806 commit 9489256
Show file tree
Hide file tree
Showing 8 changed files with 126 additions and 56 deletions.
4 changes: 4 additions & 0 deletions cost/azure/hybrid_use_benefit/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v5.2.1

- Fixed issue where policy actions were not properly reporting errors

## v5.2.0

- Added `Resource Hourly Cost` to incident table for added context
Expand Down
81 changes: 56 additions & 25 deletions cost/azure/hybrid_use_benefit/azure_hybrid_use_benefit.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ severity "low"
category "Cost"
default_frequency "daily"
info(
version: "5.2.0",
version: "5.2.1",
provider: "Azure",
service: "Compute",
policy_set: "Hybrid Use Benefit",
Expand Down Expand Up @@ -943,33 +943,64 @@ end
###############################################################################

define license_instances($data, $param_azure_endpoint) return $all_responses do
$all_responses = []

foreach $item in $data do
sub on_error: skip do
$response = http_request(
auth: $$auth_azure,
verb: "patch",
host: $param_azure_endpoint,
https: true,
href: $item["id"],
headers: {
"cache-control": "no-cache",
"content-type": "application/json"
},
query_strings: {
"api-version": "2018-06-01"
},
body: {
"properties": {
"licenseType": "Windows_Server"
}
}
)
$$all_responses = []

$all_responses << $response
foreach $instance in $data do
sub on_error: handle_error() do
call license_instance($instance, $param_azure_endpoint) retrieve $license_response
end
end

if inspect($$errors) != "null"
raise join($$errors, "\n")
end
end

define license_instance($instance, $param_azure_endpoint) return $response do
$host = $param_azure_endpoint
$href = $instance["id"]
$params = "?api-version=2018-06-01"
$url = $host + $href + $params
task_label("PATCH " + $url)

$response = http_request(
auth: $$auth_azure,
verb: "patch",
host: $host,
https: true,
href: $href,
query_strings: {
"api-version": "2018-06-01"
},
headers: {
"cache-control": "no-cache",
"content-type": "application/json"
},
body: {
"properties": {
"licenseType": "Windows_Server"
}
}
)

task_label("Patch Azure Virtual Machine response: " + $instance["id"] + " " + to_json($response))
$$all_responses << to_json({"req": "PATCH " + $url, "resp": $response})

if $response["code"] != 204 && $response["code"] != 202 && $response["code"] != 200
raise "Unexpected response patching Azure Virtual Machine: "+ $instance["id"] + " " + to_json($response)
else
task_label("Patch Azure Virtual Machine successful: " + $instance["id"])
end
end

define handle_error() do
if !$$errors
$$errors = []
end
$$errors << $_error["type"] + ": " + $_error["message"]
# We check for errors at the end, and raise them all together
# Skip errors handled by this definition
$_error_behavior = "skip"
end

###############################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ category "Meta"
default_frequency "15 minutes"
info(
provider: "Azure",
version: "5.2.0", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
version: "5.2.1", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
publish: "true",
deprecated: "false"
)
Expand Down
4 changes: 4 additions & 0 deletions cost/azure/hybrid_use_benefit_linux/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## v5.1.1

- Fixed issue where policy actions were not properly reporting errors

## v5.1.0

- Added `Resource Hourly Cost` and `Currency` to incident table for added context
Expand Down
81 changes: 56 additions & 25 deletions cost/azure/hybrid_use_benefit_linux/ahub_linux.pt
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ severity "low"
category "Cost"
default_frequency "daily"
info(
version: "5.1.0",
version: "5.1.1",
provider: "Azure",
service: "Compute",
policy_set: "Hybrid Use Benefit"
Expand Down Expand Up @@ -785,33 +785,64 @@ end
###############################################################################

define license_instances($data, $param_azure_endpoint) return $all_responses do
$all_responses = []

foreach $item in $data do
sub on_error: skip do
$response = http_request(
auth: $$auth_azure,
verb: "patch",
host: $param_azure_endpoint,
https: true,
href: $item["id"],
query_strings: {
"api-version": "2018-06-01"
},
headers: {
"cache-control": "no-cache",
"content-type": "application/json"
},
body: {
"properties": {
"licenseType": $item["license"]
}
}
)
$$all_responses = []

$all_responses << $response
foreach $instance in $data do
sub on_error: handle_error() do
call license_instance($instance, $param_azure_endpoint) retrieve $license_response
end
end

if inspect($$errors) != "null"
raise join($$errors, "\n")
end
end

define license_instance($instance, $param_azure_endpoint) return $response do
$host = $param_azure_endpoint
$href = $instance["id"]
$params = "?api-version=2018-06-01"
$url = $host + $href + $params
task_label("PATCH " + $url)

$response = http_request(
auth: $$auth_azure,
verb: "patch",
host: $host,
https: true,
href: $href,
query_strings: {
"api-version": "2018-06-01"
},
headers: {
"cache-control": "no-cache",
"content-type": "application/json"
},
body: {
"properties": {
"licenseType": $instance["license"]
}
}
)

task_label("Patch Azure Virtual Machine response: " + $instance["id"] + " " + to_json($response))
$$all_responses << to_json({"req": "PATCH " + $url, "resp": $response})

if $response["code"] != 204 && $response["code"] != 202 && $response["code"] != 200
raise "Unexpected response patching Azure Virtual Machine: "+ $instance["id"] + " " + to_json($response)
else
task_label("Patch Azure Virtual Machine successful: " + $instance["id"])
end
end

define handle_error() do
if !$$errors
$$errors = []
end
$$errors << $_error["type"] + ": " + $_error["message"]
# We check for errors at the end, and raise them all together
# Skip errors handled by this definition
$_error_behavior = "skip"
end

###############################################################################
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ category "Meta"
default_frequency "15 minutes"
info(
provider: "Azure",
version: "5.1.0", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
version: "5.1.1", # This version of the Meta Parent Policy Template should match the version of the Child Policy Template as it appears in the Catalog for best reliability
publish: "true",
deprecated: "false"
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3207,7 +3207,7 @@
{
"id": "./cost/azure/hybrid_use_benefit/azure_hybrid_use_benefit.pt",
"name": "Azure Hybrid Use Benefit for Windows Server",
"version": "5.2.0",
"version": "5.2.1",
"providers": [
{
"name": "azure_rm",
Expand Down Expand Up @@ -3240,7 +3240,7 @@
{
"id": "./cost/azure/hybrid_use_benefit_linux/ahub_linux.pt",
"name": "Azure Hybrid Use Benefit for Linux Server",
"version": "5.1.0",
"version": "5.1.1",
"providers": [
{
"name": "azure_rm",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1857,7 +1857,7 @@
required: true
- id: "./cost/azure/hybrid_use_benefit/azure_hybrid_use_benefit.pt"
name: Azure Hybrid Use Benefit for Windows Server
version: 5.2.0
version: 5.2.1
:providers:
- :name: azure_rm
:permissions:
Expand All @@ -1876,7 +1876,7 @@
required: true
- id: "./cost/azure/hybrid_use_benefit_linux/ahub_linux.pt"
name: Azure Hybrid Use Benefit for Linux Server
version: 5.1.0
version: 5.1.1
:providers:
- :name: azure_rm
:permissions:
Expand Down

0 comments on commit 9489256

Please sign in to comment.