msgraph_resource: retry is not applied to the refresh read, so a transient 404 removes the resource from state

Open Beginner friendly
#192 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
78/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
go
Domain
backend

Research direction

Start at Read() in internal/services/msgraph_resource.go around line 518, where the refresh read builds its options without RetryOptions, and compare it with the create, read-after-create, update and delete call sites that already pass clients.NewRetryOptions(model.Retry). Read clients.NewRequestOptions and clients.NewRetryOptions to see how query parameters and retry options are assembled, then mirror the working call sites. Done means a 404 whose message matches retry.error_message_regex is retried on refresh instead of hitting ResponseErrorWasNotFound and calling RemoveResource.

Written by the indexing model from the issue text.

Description

msgraph_resource: retry is not applied to the refresh read, so a transient 404 removes the resource from state

Versions: terraform-provider-msgraph v0.6.0 (also present on main), Terraform 1.15.5

Problem

retry.error_message_regex is honoured on create, read-after-create, update and delete, but not on the regular refresh read of a non-relationship resource. Read() builds its options without RetryOptions (internal/services/msgraph_resource.go, line 518 in v0.6.0):

options := clients.NewRequestOptions(nil, AsMapOfLists(model.ReadQueryParameters))
responseBody, err := r.client.Read(ctx, resourceItemUrl(model), model.ApiVersion.ValueString(), options)
if err != nil {
    if utils.ResponseErrorWasNotFound(err) {
        resp.State.RemoveResource(ctx)

For APIs that return intermittent 404s, one unlucky refresh silently drops an existing resource from state, and Terraform then plans to create it again.

Reproduction

resource "msgraph_resource" "assignment" {
  api_version = "beta"
  url         = "roleManagement/exchange/roleAssignments"
  body = {
    principalId      = "/ServicePrincipals/<sp-object-id>"
    roleDefinitionId = "<role-definition-id>"
    directoryScopeId = "/Groups/<group-object-id>"
  }
  retry = {
    error_message_regex = ["couldn't be found on "]
  }
}

GET /beta/roleManagement/exchange/roleAssignments/<id> returns 404 for an existing assignment in roughly 1 of 4 to 6 calls (10 of 50 with plain curl), with a different domain controller named each time:

RESPONSE 404: 404 Not Found
ERROR CODE: NotFound
"message": "Error executing request. The operation couldn't be performed because object
'...\ConfigurationUnits\<tenant>.onmicrosoft.com\Application Mail.ReadWrite' couldn't be
found on '<dc>.PROD.OUTLOOK.COM'."

The provider log shows Retry: exit due to non-retriable status code, and the resource is removed from state although retry matches the message.

Proposed fix

Pass the retry options like the other call sites do:

options := clients.RequestOptions{
    QueryParameters: clients.NewQueryParameters(AsMapOfLists(model.ReadQueryParameters)),
    RetryOptions:    clients.NewRetryOptions(model.Retry),
}
Dominant language
Go
Stars
65
Forks
27
Avg merge
5d 10h
Merged PRs (30d)
2

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from microsoft/terraform-provider-msgraph

All issues in microsoft/terraform-provider-msgraph

Similar issues

More Go issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.