Git Product home page Git Product logo

Comments (14)

v-hongli1 avatar v-hongli1 commented on May 24, 2024 2

Azure Resource Graph may able to return the state on batch, e.g. (that sample is in VM of Compute) https://learn.microsoft.com/en-us/azure/virtual-machines/resource-graph-samples?tabs=azure-cli#count-of-virtual-machines-by-power-state

Though it be possible that similar query not work on ContainerGroup.

Azure Resource Graph query work on ContainerGroup.

            QueryResponse queryResponse = resourceGraphManager.resourceProviders()
                    .resources(new QueryRequest()
                            .withQuery("resources | where type == \"microsoft.containerinstance/containergroups\"")
                            .withOptions(new QueryRequestOptions().withResultFormat(ResultFormat.OBJECT_ARRAY)));
            Assertions.assertTrue(queryResponse.totalRecords() > 0L);
            ((ArrayList<LinkedHashMap>) queryResponse.data()).forEach(dataItem -> {
                ContainerGroupInner innerModel =  BinaryData.fromObject(dataItem).toObject(ContainerGroupInner.class);
                Assertions.assertNotNull(innerModel);
                Assertions.assertNotNull(innerModel.instanceView());
                Assertions.assertNotNull(innerModel.instanceView().state());
            });

You can choose to use Azure Resource Graph query or ContainerGroup via get according to your needs.

from azure-sdk-for-java.

github-actions avatar github-actions commented on May 24, 2024

Thank you for your feedback. Tagging and routing to the team member best able to assist.

from azure-sdk-for-java.

weidongxu-microsoft avatar weidongxu-microsoft commented on May 24, 2024

@v-hongli1 Please take a look.

@XiaofeiCao for awareness.

from azure-sdk-for-java.

v-hongli1 avatar v-hongli1 commented on May 24, 2024

@arballesteros
Which version were you using before migrating to the new Azure Resource Management Library ?
In addition, If you can provide code snippets for creating ContainerGroup and getting status, it will be helpful for us to analyze the issue, thank you !

from azure-sdk-for-java.

arballesteros avatar arballesteros commented on May 24, 2024

Hello @v-hongli1.
We used com.microsoft.azure:azure-mgmt-containerinstance:1.41.4 before the migration.
For the ContainerGroup creation, I can provide you a sample Azure CLI command that we are using.

az container create --name my-container-group --resource-group My-Resource-Group --sku Standard --image mcr.microsoft.com/azuredocs/aci-helloworld:latest --os-type Linux --vnet My-Group-vnet --subnet mysubnet-1

On how we get the status, we just use the state() that is provided by the ContainerGroup interface.

protected Map<String, String> getAttributes(ContainerGroup resource) {
        final Map<String, String> attributes = new LinkedHashMap<>();
        attributes.put("state", resource.state());
        ...
}

I hope these information can help you with your investigation.

from azure-sdk-for-java.

v-hongli1 avatar v-hongli1 commented on May 24, 2024

@arballesteros
Track1 test case code snippet using com.microsoft.azure:azure-mgmt-containerinstance:1.41.4

            // The existing container group was created by portal
            ContainerGroup existingContainerGroup = azure.containerGroups()
                    .getByResourceGroup("rg-hongli", "containergroup4lh001");
            Assert.assertNotNull(existingContainerGroup.inner());
            Assert.assertNotNull(existingContainerGroup.inner().instanceView());
            Assert.assertNotNull(existingContainerGroup.inner().instanceView().state());

            // The existing container group was created by AZ CLI
            // AZ command: az container create --name my-container-group --resource-group My-Resource-Group --sku Standard --image mcr.microsoft.com/azuredocs/aci-helloworld:latest --os-type Linux --vnet My-Group-vnet --subnet mysubnet-1
            existingContainerGroup = azure.containerGroups()
                    .getByResourceGroup("rg-hongli", "container777777");
            Assert.assertNotNull(existingContainerGroup.inner());
            Assert.assertNotNull(existingContainerGroup.inner().instanceView());
            Assert.assertNotNull(existingContainerGroup.inner().instanceView().state());

            // Create and getter container group by sdk
            resourceGroup = azure.resourceGroups().define("rg123456").withRegion(Region.US_EAST).create();
            createdContainerGroup = azure.containerGroups()
                    .define("container123456")
                    .withRegion(Region.US_EAST)
                    .withExistingResourceGroup("rg123456")
                    .withLinux()
                    .withPublicImageRegistryOnly()
                    .withoutVolume()
                    .withContainerInstance("nginx", 80)
                    .withDnsPrefix("nginx")
                    .create();
            Assert.assertNotNull(createdContainerGroup.inner());
            Assert.assertNotNull(createdContainerGroup.inner().instanceView());
            Assert.assertNotNull(createdContainerGroup.inner().instanceView().state());

Track2 test case code snippet using com.azure.resourcemanager/[email protected]

            // The existing container group was created by portal
            ContainerGroup existingContainerGroup = manager
                    .containerGroups().getByResourceGroup("rg-hongli", "containergroup4lh001");
            Assertions.assertNotNull(existingContainerGroup.innerModel());
            Assertions.assertNotNull(existingContainerGroup.innerModel().instanceView());
            Assertions.assertNotNull(existingContainerGroup.innerModel().instanceView().state());

            // The existing container group was created by AZ CLI
            // AZ command: az container create --name my-container-group --resource-group My-Resource-Group --sku Standard --image mcr.microsoft.com/azuredocs/aci-helloworld:latest --os-type Linux --vnet My-Group-vnet --subnet mysubnet-1
            existingContainerGroup = manager.containerGroups()
                    .getByResourceGroup("rg-hongli", "container777777");
            Assertions.assertNotNull(existingContainerGroup.innerModel());
            Assertions.assertNotNull(existingContainerGroup.innerModel().instanceView());
            Assertions.assertNotNull(existingContainerGroup.innerModel().instanceView().state());

            // Create and getter container group by sdk
            resourceGroup = resourceManager.resourceGroups().define(rgName).withRegion(Region.US_EAST).create();
            createdContainerGroup = manager
                    .containerGroups()
                    .define(containerGroupName)
                    .withRegion(region)
                    .withExistingResourceGroup(rgName)
                    .withLinux()
                    .withPublicImageRegistryOnly()
                    .withoutVolume()
                    .withContainerInstance("nginx", 80)
                    .withDnsPrefix("nginx")
                    .create();
            Assertions.assertNotNull(createdContainerGroup.innerModel());
            Assertions.assertNotNull(createdContainerGroup.innerModel().instanceView());
            Assertions.assertNotNull(createdContainerGroup.innerModel().instanceView().state());

The above test cases all work fine. Can you provide the detail code snippets for create/getter ContainerGroup and call state() function?

from azure-sdk-for-java.

weidongxu-microsoft avatar weidongxu-microsoft commented on May 24, 2024

@v-hongli1

Is createdContainerGroup.state() be correct? Is it still correct after .refresh()?

from azure-sdk-for-java.

arballesteros avatar arballesteros commented on May 24, 2024

@v-hongli1
This is how we acquire our ContainerGroup.
azureApi.getAzure().containerGroups().list().stream().toList()

I noticed that you directly acquire the ContainerGroup using getByResourceGroup() and so, I also tried it.

    // This is how we get our ContainerGroup
    List<ContainerGroup> getByList = azureApi.getAzure().containerGroups().list().stream().toList();

    //Based on your example, I tried to directly acquire the ContainerGroup by using getByResourceGroup()
    ContainerGroup getByResourceGroup = azureApi.getAzure().containerGroups().getByResourceGroup("Azure-Monitoring-Test-Resource-Group", "it-container-group");

Please see below the result.

ContainerGroups_State_Investigation

As you can see, the ContainerGroup that was returned via the list().stream().toList() has a null instanceView which lead to an invalid state().

However, the ContainerGroup that was returned by getByResourceGroup() has a valid instanceView and the state() can be acquired.

What do you think? Is this an expected behavior?

from azure-sdk-for-java.

weidongxu-microsoft avatar weidongxu-microsoft commented on May 24, 2024

It is possible that instanceView is not returned in List (this was seen on some other RPs). It may only return in Get.

from azure-sdk-for-java.

arballesteros avatar arballesteros commented on May 24, 2024

I see. So in our implementation, using List is not reliable.
Is there a plan in the future to include the instanceView in List?

from azure-sdk-for-java.

weidongxu-microsoft avatar weidongxu-microsoft commented on May 24, 2024

For Compute RP, it got an expand query param on the List that request backend to include the instanceView data. https://github.com/Azure/azure-rest-api-specs/blob/main/specification/compute/resource-manager/Microsoft.Compute/ComputeRP/stable/2024-03-01/virtualMachine.json#L1152-L1165

However, I checked the containerGroup, they don't have this in their API spec
https://github.com/Azure/azure-rest-api-specs/blob/main/specification/containerinstance/resource-manager/Microsoft.ContainerInstance/stable/2023-05-01/containerInstance.json#L75

It had to be supported in backend, before SDK can do anything about it.

from azure-sdk-for-java.

weidongxu-microsoft avatar weidongxu-microsoft commented on May 24, 2024

Azure Resource Graph may able to return the state on batch, e.g. (that sample is in VM of Compute)
https://learn.microsoft.com/en-us/azure/virtual-machines/resource-graph-samples?tabs=azure-cli#count-of-virtual-machines-by-power-state

Though it be possible that similar query not work on ContainerGroup.

from azure-sdk-for-java.

arballesteros avatar arballesteros commented on May 24, 2024

However, I checked the containerGroup, they don't have this in their API spec
https://github.com/Azure/azure-rest-api-specs/blob/main/specification/containerinstance/resource-manager/Microsoft.ContainerInstance/stable/2023-05-01/containerInstance.json#L75
It had to be supported in backend, before SDK can do anything about it.

Thank you for the assistance and I understand the circumstances.
For now, we will refrain from acquiring the ContainerGroup via List and instead, we shall be using the Get.

from azure-sdk-for-java.

arballesteros avatar arballesteros commented on May 24, 2024

Thanks for testing this out.
We will consider these options you have presented.

from azure-sdk-for-java.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.