0

I'm trying to develop a feature in my app that creates an Availability Test and I'm running a lab that using Python SDK I'm able to create a resource group, AKS cluster, log analytics workspace and application insights. After it does all that, this comes in, trying to create an Availability Test.

availability_test = {
    "$schema": "ttps://schema.management.azure.com/schemas/2019-04-01/deploymentTemplate.json#",
    "contentVersion": "1.0.0.0",
    "resources": [
        {
            "apiVersion": "2019-04-01",
            "name": app_insights_name,
            "type": "microsoft.insights/webtests",
            "location": "eastus2",
            "properties": {
                "SyntheticMonitorId": None,
                "Enabled": True,
                "Locations": [
                    {
                        "Id": "us-ca-sjc-azr"
                    }
                ],
                "Configuration": {
                    "WebTest": {
                        "Timeout": "60",
                        "ParseDependentRequests": False,
                        "FollowRedirects": True
                    }
                }
            }
        }
    ]
}

deployment_url = f"https://management.azure.com/subscriptions/{subscription_id}/resourcegroups/{resource_group_name}/providers/microsoft.resources/deployments/{app_insights_name}?api-version=2023-07-01"
headers = {
    "Authorization": f"Bearer {credentials.get_token('https://management.azure.com/.default').token}",
    "Content-Type": "application/json"
}

response = requests.put(deployment_url, data=json.dumps(availability_test), headers=headers)

if response.status_code == 201:
    print("Availability test deployed successfully!")
else:
    print(f"Failed to deploy availability test. Status code: {response.status_code}")
    print(response.text)

The error that persists is this one:

Failed to deploy availability test. Status code: 400
{"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: 'Could not find member '$schema' on object of type 'DeploymentContent'. Path '$schema', line 1, position 11.'."}}

Any ideas? Or maybe another method of doing this?

Suresh Chikkam
  • 623
  • 2
  • 2
  • 6
maj1n
  • 1
  • 2
  • Did you not notice the typo? Your $schema begins `"ttps` instead of `"https`. – Tim Roberts Aug 28 '23 at 18:38
  • @TimRoberts Oh, this is probably a recent typo, since I've been trying a few different schemas. Unfortunately, not the issue. – maj1n Aug 28 '23 at 18:56
  • Nothing else stands out. This page suggests that most uses should use the 2015-01-01 version of the schema: https://learn.microsoft.com/en-us/azure/azure-resource-manager/templates/syntax – Tim Roberts Aug 28 '23 at 23:33

1 Answers1

0

{"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: 'Could not find member '$schema' on object of type 'DeploymentContent'. Path $schema', line 1, position 11.'."}}

The Azure API is not recognizing the $schema field within the deployment content. In this context, the $schema field is not expected, and the ARM deployment should only contain the resource definitions.

It's better to keep your ARM templates in separate files, as this makes your code cleaner and easier to manage.

Deploy ARM Template with py script:

from azure.identity import DefaultAzureCredential
from azure.mgmt.monitor import MonitorManagementClient
from azure.mgmt.monitor.models import (
    WebTestKind,
    SyntheticMonitorConfiguration,
    WebTestProperties,
    WebTestLocation,
    WebTestGeolocation,
    WebTestPropertiesFrequency,
    WebTestPropertiesRetryPolicy,
)

# Replace these with your actual values
subscription_id = "your-subscription-id"
resource_group_name = "your-resource-group"
app_insights_name = "your-app-insights-name"

# Initialize your Azure credentials
credential = DefaultAzureCredential()

# Initialize the monitor management client
monitor_client = MonitorManagementClient(credential, subscription_id)

# Define the properties for the Availability Test
web_test_location = WebTestLocation(
    location=WebTestGeolocation(location_id="us-ca-sjc-azr")
)

web_test_configuration = SyntheticMonitorConfiguration(
    frequency=WebTestPropertiesFrequency(time_interval="PT5M"),
    retries=3,
    locations=[web_test_location],
)

web_test_properties = WebTestProperties(
    enabled=True,
    frequency=300,
    timeout=60,
    kind=WebTestKind.multi_step,
    locations=[web_test_location],
    configurations=[web_test_configuration],
)

# Create the Availability Test
availability_test = monitor_client.web_tests.create_or_update(
    resource_group_name, app_insights_name, app_insights_name, web_test_properties
)

print("Availability test deployed successfully!")
  • ARM template is loaded from a separate file, and the parameter values are provided dynamically when constructing the deployment URL. After successful deployment of the availability test succeeds you can see the below results.

enter image description here

Suresh Chikkam
  • 623
  • 2
  • 2
  • 6
  • Now I have this error: {"error":{"code":"InvalidRequestContent","message":"The request content was invalid and could not be deserialized: 'Could not find member 'resources' on object of type 'DeploymentContent'. Path 'resources', line 1, position 13.'."}} – maj1n Aug 29 '23 at 18:25
  • As per the above error the payload is still not being deserialized correctly. using the **`MonitorManagementClient`** class may avoids the need to construct an ARM template and use HTTP requests to deploy it. I just updated the answer please check. – Suresh Chikkam Aug 30 '23 at 07:17
  • I've tried this before. Most of these imports don't actually import and some of them (WebTestKind and WebTestGeolocation) are from azure.mgmt.applicationinsights.models instead of monitor. Also, web_tests is not recognized as a part of MonitorManagementClient. – maj1n Aug 30 '23 at 12:15
  • Then use the **MonitorQueryClient** class from the **azure.monitor.query** module to interact with web tests. Please make sure you have the appropriate version of the Azure Monitor SDK for Python installed. – Suresh Chikkam Aug 31 '23 at 12:13