terraform-provider-azapi icon indicating copy to clipboard operation
terraform-provider-azapi copied to clipboard

Support for calling REST API endpoints for non-stateful actions

Open jaredfholgate opened this issue 1 year ago • 6 comments

There are some use cases in that cannot be achieved with the ARM API, but can be achieved with the REST API.

Examples being triggering immediate runs of jobs, such as:

  • Container Registry Task
  • Container App Job

There are REST API endpoints for this and therefore the azurerm provider can support these actions, e.g. https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/container_registry_task_schedule_run_now

In order for azapi to be a full replacement for azurerm, I believe it would need to be able to handle this type of action. I don't believe it can at present, but happy to be corrected on that.

Example endpoints:

  • https://learn.microsoft.com/en-us/rest/api/containerapps/container-apps/start?view=rest-containerapps-2024-03-01&tabs=HTTP
  • https://learn.microsoft.com/en-us/rest/api/containerregistry/registries(tasks)/schedule-run?view=rest-containerregistry-2019-04-01&tabs=HTTP

jaredfholgate avatar Jul 31 '24 09:07 jaredfholgate

I'm a little naive here but what does azapi_resource_action not cover for this sort of functionality? I thought it was the means by which you could perform imperative tasks (i.e. start/stop VM).

stemaMSFT avatar Jul 31 '24 21:07 stemaMSFT

Hello @jaredfholgate ,

Thank you for taking time to report this issue.

Here's an example of how to use azapi_resource_action resource to perform an HTTP request.

resource "azapi_resource_action" "start" {
  type        = "Microsoft.App/containerApps@2024-03-01"
  resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-ai-studio/providers/Microsoft.App/containerApps/ai-studio-project"
  action      = "start"
  body = {

  }
}

resource "azapi_resource_action" "scheduleRun" {
  type        = "Microsoft.ContainerRegistry/registries@2023-07-01"
  resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-ai-studio/providers/Microsoft.ContainerRegistry/registries/acr-ai-studio"
  action      = "scheduleRun"
  body = {
    type = "EncodedTaskRunRequest"
    values = [
      {
        name     = "mytestargument"
        value    = "mytestvalue"
        isSecret = false
      },
      {
        name     = "mysecrettestargument"
        value    = "mysecrettestvalue"
        isSecret = true
      }
    ]
    platform = {
      os = "Linux"
    }
    agentConfiguration = {
      cpu = 2
    }
    encodedTaskContent   = "c3RlcHM6Cnt7IGlmIFZhbHVlcy5lbnZpcm9ubWVudCA9PSAncHJvZCcgfX0KICAtIHJ1bjogcHJvZCBzZXR1cAp7eyBlbHNlIGlmIFZhbHVlcy5lbnZpcm9ubWVudCA9PSAnc3RhZ2luZycgfX0KICAtIHJ1bjogc3RhZ2luZyBzZXR1cAp7eyBlbHNlIH19CiAgLSBydW46IGRlZmF1bHQgc2V0dXAKe3sgZW5kIH19CgogIC0gcnVuOiBidWlsZCAtdCBGYW5jeVRoaW5nOnt7LlZhbHVlcy5lbnZpcm9ubWVudH19LXt7LlZhbHVlcy52ZXJzaW9ufX0gLgoKcHVzaDogWydGYW5jeVRoaW5nOnt7LlZhbHVlcy5lbnZpcm9ubWVudH19LXt7LlZhbHVlcy52ZXJzaW9ufX0nXQ=="
    encodedValuesContent = "ZW52aXJvbm1lbnQ6IHByb2QKdmVyc2lvbjogMQ=="
  }
}

ms-henglu avatar Aug 01 '24 02:08 ms-henglu

@ms-henglu and @stemaMSFT Perhaps it is just my googling skills, but I don't see a way to trigger a container job manual run via the ARM API? https://learn.microsoft.com/en-us/azure/templates/microsoft.app/jobs?pivots=deployment-language-terraform

Given that, then how would it be possible to trigger a run with azapi? I don't believe it can hit the REST API endpoint? Only the ARM API endpoint.

It is very possible I am just missing something here, so apologies if that is the case.

jaredfholgate avatar Aug 02 '24 14:08 jaredfholgate

you make a great point, @jaredfholgate that you can't find it within the current AzAPI reference documentation. Resource actions require a synthesis of understanding of the ARM API behavior and the REST API command. The fusion of these two helps you create the exact example you're looking for. @ms-henglu do you think there would be a way that we could auto-generate on actions like start/stop within azapi_resource_action documentation? Otherwise, I can just ensure the quickstart makes clear that the AzAPI reference docs would not contain this info. @grayzu for FYI

stemaMSFT avatar Aug 02 '24 20:08 stemaMSFT

Hi. I was able to get this working for a container app job using this code:

resource "azapi_resource_action" "placeholder_trigger" {
  type        = "Microsoft.App/jobs@2024-03-01"
  resource_id = "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/rg-h95qg9/providers/Microsoft.App/jobs/my-job"
  action      = "start"
  body = {}
  lifecycle {
    replace_triggered_by = [ azapi_resource.placeholder ]
  }
}

Thank you for pointing me in the right direction and apologies for my lack of understanding around this. It makes more sense now, but I think it could potentially be called out more in the docs since it is a fantastic feature.

I don't see a reference to the action resource in here? https://learn.microsoft.com/en-us/azure/developer/terraform/overview-azapi-provider

The docs here could possibly be clearer on this use case: https://registry.terraform.io/providers/Azure/azapi/latest/docs/resources/azapi_resource_action

I do appreciate that the example code shows stopping and starting something, but the description could be expanded a little maybe?

Let me know if you'd like me to raise a PR to update the docs, would be happy to.

jaredfholgate avatar Aug 04 '24 11:08 jaredfholgate

I believe I have a PR under review that mentions this. On the other hand, would love if you wanted to contribute to the docs to improve some of the examples regardless. Still wondering if there’s a way for us to autogenerate some examples.

stemaMSFT avatar Aug 05 '24 06:08 stemaMSFT

Close this issue as it's completed. Feel free to reopen it if there's any question.

ms-henglu avatar Jul 21 '25 08:07 ms-henglu