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

Support for calling REST API endpoints for non-stateful actions #565

Open
jaredfholgate opened this issue Jul 31, 2024 · 6 comments
Open
Labels
example Example request

Comments

@jaredfholgate
Copy link
Member

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:

@stemaMSFT
Copy link
Member

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).

@ms-henglu ms-henglu added the example Example request label Aug 1, 2024
@ms-henglu
Copy link
Member

ms-henglu commented Aug 1, 2024

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=="
  }
}

@jaredfholgate
Copy link
Member Author

@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.

@stemaMSFT
Copy link
Member

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

@jaredfholgate
Copy link
Member Author

jaredfholgate commented Aug 4, 2024

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.

@stemaMSFT
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
example Example request
Projects
None yet
Development

No branches or pull requests

4 participants
@jaredfholgate @ms-henglu @stemaMSFT and others