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

Error deploying ARM template using local json file with java SDK 1.0.0-beta1 release. #923

Closed
amitarali opened this issue Jun 29, 2016 · 15 comments

Comments

@amitarali
Copy link

Error deploying ARM template using local json file with java SDK 1.0.0-beta1 release.
Getting below error while deploying ARM template. Please advise.

Exception in thread "main" java.lang.StackOverflowError
at com.google.common.reflect.TypeToken$TypeCollector$2.getSuperclass(TypeToken.java:1097)
at com.google.common.reflect.TypeToken$TypeCollector$2.getSuperclass(TypeToken.java:1086)
at com.google.common.reflect.TypeToken$TypeCollector$ForwardingTypeCollector.getSuperclass(TypeToken.java:1189)
at com.google.common.reflect.TypeToken$TypeCollector.collectTypes(TypeToken.java:1145)
at com.google.common.reflect.TypeToken$TypeCollector.collectTypes(TypeToken.java:1127)
at com.google.common.reflect.TypeToken$TypeCollector$3.collectTypes(TypeToken.java:1114)
at com.google.common.reflect.TypeToken$ClassSet.rawTypes(TypeToken.java:664)
at com.microsoft.rest.Validator.validate(Validator.java:59)
at com.microsoft.rest.Validator.validate(Validator.java:94)
at com.microsoft.rest.Validator.validate(Validator.java:94)

Below is the code snippet. Is it expected to have a pojo for template and pass that object?

    HashMap<String, LinkedHashMap> templateObj = new ObjectMapper().readValue(new File("c://ARMTemplate.json"), LinkedHashMap.class);
        Map<String, String> parameters = new HashMap<String, String>();

        DeploymentProperties deploymentProperties = new DeploymentProperties();
        deploymentProperties.setMode(DeploymentMode.INCREMENTAL);
        deploymentProperties.setTemplate(templateObj);
        deploymentProperties.setParameters(parameters);

        Deployment deployment = new Deployment();
        deployment.setProperties(deploymentProperties);

        String resourceGroupName = "resourceGroupName";
        String deploymentName = "DeplymentName";
        // Validate the Template
        ServiceResponse<DeploymentValidateResult> vResult = armClient.getDeploymentsOperations().validate(resourceGroupName, deploymentName ,
                deployment);

@amitarali
Copy link
Author

any idea on this?

@jianghaolu
Copy link
Contributor

This should be fixed in 1.0.0-beta2. Please verify.

@amitarali
Copy link
Author

Hi Jianghaolu,

I am getting exact same error with 1.0.0-beta2 as well. Can you please help here.

Thanks,
Amit

@jianghaolu jianghaolu reopened this Jul 27, 2016
@vimukthir
Copy link

vimukthir commented Aug 16, 2016

Hi jianghaolu,

I'm trying to create IoT hub using local json files. (template file and parameter file). And I'm getting following StackOverflow error. Don't have any idea about root cause. I'm using 1.0.0-beta2 version.

java.lang.StackOverflowError
at java.lang.Integer.compare(Integer.java:1234)
at java.lang.Integer.compareTo(Integer.java:1216)
at java.lang.Integer.compareTo(Integer.java:52)
at com.google.common.collect.ReverseNaturalOrdering.compare(ReverseNaturalOrdering.java:39)
at com.google.common.collect.ReverseNaturalOrdering.compare(ReverseNaturalOrdering.java:27)
at com.google.common.reflect.TypeToken$TypeCollector$4.compare(TypeToken.java:1162)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:216)
at java.util.Arrays.sort(Arrays.java:1438)
at com.google.common.collect.Ordering.immutableSortedCopy(Ordering.java:846)
at com.google.common.reflect.TypeToken$TypeCollector.sortKeysByValue(TypeToken.java:1165)
at com.google.common.reflect.TypeToken$TypeCollector.collectTypes(TypeToken.java:1129)
at com.google.common.reflect.TypeToken$TypeCollector$3.collectTypes(TypeToken.java:1114)
at com.google.common.reflect.TypeToken$ClassSet.rawTypes(TypeToken.java:664)
at com.microsoft.rest.Validator.validate(Validator.java:59)
at com.microsoft.rest.Validator.validate(Validator.java:94)
at com.microsoft.rest.Validator.validate(Validator.java:94)
at com.microsoft.rest.Validator.validate(Validator.java:94)
at com.microsoft.rest.Validator.validate(Validator.java:94)

Thanks,
Vimukthi

@jianghaolu
Copy link
Contributor

Hi @vimukthir can you share a code snippet that throws this exception? If I can reproduce it I should be able to fix it. Thanks!

@vimukthir
Copy link

vimukthir commented Aug 17, 2016

Hi jianghaolu,

My code snippet and template files are as follow.

Main Class

========

private void createIotHub() {
        InputStream resourceTemplate = IotHubUtil.readFile(azureConfigurationManager.getResourceTemplateFilePath());
        InputStream resourceParameters = IotHubUtil.readFile(azureConfigurationManager.getResourceParameterFilePath());
        IotHubUtil.createIoTHub(resourceManagementClient, resourceGroupName, deploymentName, resourceTemplate, resourceParameters);
    }

Util Class

=======
#Reading template files

public static InputStream readFile(String resourcePath) {

        ClassLoader classloader = Thread.currentThread().getContextClassLoader();
        InputStream is = classloader.getResourceAsStream(resourcePath);
        return is;
    }

#Creating Iot hub

public static void createIoTHub(ResourceManagementClientImpl resourceManagementClient, String resourceGroupName, String deploymentName, Object template, Object parameters) {

        DeploymentInner deploymentInner = new DeploymentInner().withProperties(new DeploymentProperties()
                .withMode(DeploymentMode.COMPLETE)
                .withTemplate(template)
                .withParameters(parameters));

        try {
            ServiceResponse<DeploymentExtendedInner> response = resourceManagementClient.deployments().createOrUpdate(resourceGroupName, deploymentName, deploymentInner);
        } catch (CloudException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

Please find the attachments of template and parameter files which I used for creating Iot hub. As .json format not allowed to send as attachment here I changed them to .txt format.
iot_hub_parameter.txt
iot_hub_template.txt

Thanks,
Regards,
Vimukthi.

@jianghaolu
Copy link
Contributor

Hi @vimukthir, thanks for posting this detailed code snippet! This is super helpful.

I see you have this in your Deployment construction:

        DeploymentInner deploymentInner = new DeploymentInner().withProperties(new DeploymentProperties()
                .withMode(DeploymentMode.COMPLETE)
                .withTemplate(template)
                .withParameters(parameters));

Are template and parameters of type InputStream? If that's the case, this is not going to work.

You can try to read the whole file into a string and call them as following:

String template = readFile("template.txt");
String parameters = readFile("parameters.txt");
ResourceManager resourceManager = ResourceManager.authenticate(credentials).withSubscription(subscriptionId);
Deployment deployment = resourceManager.deployments
        .define("deploymentName")
        .withTemplate(template)
        .withParameters(parameters)
        .withMode(DeploymentMode.COMPLETE)
        .create();

@vimukthir
Copy link

Hi Jianghaolu,

Thanks for replying. But seems that there are some functionality mismatch between your proposed code and the java SDK which I am using (1.0.0-beta2). With this SDK I'm allowed to write the following code.

  import com.microsoft.azure.management.resources.implementation.ResourceManager;
                .
                .
                .
` Deployment deployment = resourceManager.deployments().define("");`

I can't access deployments property (resourceManager.deployments) in ResourceManager as it has private access.

Any ideas?

@jianghaolu
Copy link
Contributor

This is the ResourceManager.java file being released as part of v1.0.0-beta2: https://github.com/Azure/azure-sdk-for-java/blob/v1.0.0-beta2/azure-mgmt-resources/src/main/java/com/microsoft/azure/management/resources/implementation/ResourceManager.java#L180. I think deployments() getter is public even though the property itself is private.

@vimukthir
Copy link

HI jianghaolu,

I was trying with following code. It is bit similar to way you suggested. But now I'm getting different type of error. Is this because json parsing error. I validated two json files and they are ok.

 try {
            Deployment deployment = resourceManager.deployments()
                    .define(deploymentName)
                    .withExistingResourceGroup(resourceGroupName)
                    .withTemplate(template)
                    .withParameters(parameters)
                    .withMode(DeploymentMode.COMPLETE)
                    .create();
        } catch (Exception e) {
            e.printStackTrace();
        }


ERROR
======

com.microsoft.azure.CloudException: 400 is not a valid polling status code: The request content was invalid and could not be deserialized: 'Error converting value "{    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#",    "contentVersion": "1.0.0.0",    "parameters": {        "IotHubs_acceleriteiotphub_name": {            "defaultValue": "acceleriteiotphub",            "type": "String"        }    },    "variables": {},    "resources": [        {            "comments": "Generalized from resource: '/subscriptions/74a5c44d-7529-41f8-9567-2d7fb7063d06/resourceGroups/acceleriteconcertgroup/providers/Microsoft.Devices/IotHubs/acceleriteiotphub'.",            "type": "Microsoft.Devices/IotHubs",            "sku": {                "name": "F1",                "tier": "Free",                "capacity": 1            },            "name": "[parameters('IotHubs_acceleriteiotphub_name')]",            "apiVersion": "2016-02-03",            "location": "eastasia",            "tags": {},            "properties": {                "eventHubEndpoints": {                    "events": {                        "retentionTimeInDays": 1,                        "partitionCount": 2,                        "partitionIds": [                            "0",                            "1"                        ],                        "path": "iothub-ehub-accelerite-60491-14a99e4b43",                        "endpoint": "sb://ihsuprodhkres019dednamespace.servicebus.windows.net/"                    },                    "operationsMonitoringEvents": {                        "retentionTimeInDays": 1,                        "partitionCount": 2,                        "partitionIds": [                            "0",                            "1"                        ],                        "path": "iothub-ehub-accelerite-60491-69c322cb03",                        "endpoint": "sb://ihsuprodhkres018dednamespace.servicebus.windows.net/"                    }                },                "storageEndpoints": {                    "$default": {                        "sasTtlAsIso8601": "PT1H",                        "connectionString": "",                        "containerName": ""                    }                },                "enableFileUploadNotifications": false,                "cloudToDevice": {                    "maxDeliveryCount": 10,                    "defaultTtlAsIso8601": "PT1H",                    "feedback": {                        "lockDurationAsIso8601": "PT1M",                        "ttlAsIso8601": "PT1H",                        "maxDeliveryCount": 10                    }                },                "features": "DeviceManagement"            },            "resources": [],            "dependsOn": []        }    ]}" to type 'Microsoft.WindowsAzure.ResourceStack.Frontdoor.Templates.Schema.Template'. Path 'properties.template', line 1, position 2817.'.
    at com.microsoft.azure.AzureClient.getPutOrPatchResult(AzureClient.java:86)
    at com.microsoft.azure.management.resources.implementation.DeploymentsInner.createOrUpdate(DeploymentsInner.java:369)
    at com.microsoft.azure.management.resources.implementation.DeploymentImpl.createResource(DeploymentImpl.java:345)
    at com.microsoft.azure.management.resources.implementation.DeploymentImpl.create(DeploymentImpl.java:287)
    at com.microsoft.azure.management.resources.implementation.DeploymentImpl.create(DeploymentImpl.java:38)

Thanks,

Regards,
VImukthi

@jianghaolu
Copy link
Contributor

How are you constructing template and parameters?
Are they strings or objects?
And can you also go to declaration on .withTemplate and see which overload is being used? We used to see this error a lot when a string is perceived as an object and server side would consider it as a JSON string instead of a JSON object.

@vimukthir
Copy link

Hi jianghaolu,
I could overcome the problem with the help of resolution defined under following issue.
#708

But now another problem has come to the picture. While creating the resource (IoT hub in my case) creation failing due to following error.

com.microsoft.azure.CloudException: Async operation failed
at com.microsoft.azure.AzureClient.getPutOrPatchResult(AzureClient.java:118)
at com.microsoft.azure.management.resources.implementation.DeploymentsInner.createOrUpdate(DeploymentsInner.java:369)
at com.microsoft.azure.management.resources.implementation.DeploymentImpl.createResource(DeploymentImpl.java:345)
at com.microsoft.azure.management.resources.implementation.DeploymentImpl.create(DeploymentImpl.java:287)
at com.microsoft.azure.management.resources.implementation.DeploymentImpl.create(DeploymentImpl.java:38)

The code is as follow

Map<String, ParameterValue> parametervalues = new HashMap<>();
        parametervalues.put("IotHubs_acceleriteiotphub_name", new ParameterValue("acceleriteiotphub"));

        JSONObject json = new JSONObject();
        json.putAll(parametervalues);

        try {
            Deployment deployment = resourceManager.deployments()
                    .define(deploymentName)
                    .withExistingResourceGroup(resourceGroupName)
                    .withTemplate(template)
                    .withParameters(json)
                    .withMode(DeploymentMode.INCREMENTAL)
                    .create();
        } catch (Exception e) {
            e.printStackTrace();
        }

As you can see this is not the Async call I have used to create the resource.

Any idea?

Thanks,
Regards
Vimukthi

@vimukthir
Copy link

Hi jianghaolu,

The previous error I was talking about happens due to wrong parameter value in the template file. I fixed it and now IoT hub is creating successfully.

Many thanks to you for all the support given.

Regards,
Vimukthi

@jianghaolu
Copy link
Contributor

Sounds good @vimukthir

@amitarali
Copy link
Author

This worked for me as well.. thanks,

@github-actions github-actions bot locked and limited conversation to collaborators Apr 13, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants