Skip to content

Commit

Permalink
azuread_application: set the requested display_name when creating, …
Browse files Browse the repository at this point in the history
…so that failed resources have the correct name
  • Loading branch information
manicminer committed Jul 27, 2023
1 parent 2735cee commit a6b36b5
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions internal/services/applications/application_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -963,13 +963,6 @@ func applicationResourceCreate(ctx context.Context, d *schema.ResourceData, meta
return applicationResourceUpdate(ctx, d, meta)
}

// Set a temporary display name as we'll attempt to patch the application with the correct name after creating it
uuid, err := uuid.GenerateUUID()
if err != nil {
return tf.ErrorDiagF(err, "Failed to generate a UUID")
}
tempDisplayName := fmt.Sprintf("TERRAFORM_UPDATE_%s", uuid)

api := expandApplicationApi(d.Get("api").([]interface{}))

// API bug: cannot set `acceptMappedClaims` when holding the Application.ReadWrite.OwnedBy role
Expand All @@ -985,7 +978,7 @@ func applicationResourceCreate(ctx context.Context, d *schema.ResourceData, meta
Api: api,
AppRoles: expandApplicationAppRoles(d.Get("app_role").(*schema.Set).List()),
Description: utils.NullableString(d.Get("description").(string)),
DisplayName: utils.String(tempDisplayName),
DisplayName: utils.String(displayName),
GroupMembershipClaims: expandApplicationGroupMembershipClaims(d.Get("group_membership_claims").(*schema.Set).List()),
IdentifierUris: tf.ExpandStringSlicePtr(d.Get("identifier_uris").(*schema.Set).List()),
Info: &msgraph.InformationalUrl{
Expand Down Expand Up @@ -1069,19 +1062,26 @@ func applicationResourceCreate(ctx context.Context, d *schema.ResourceData, meta

d.SetId(*app.ID())

// Attempt to patch the newly created group with the correct name, which will tell us whether it exists yet
// The SDK handles retries for us here in the event of 404, 429 or 5xx, then returns after giving up
status, err := client.Update(ctx, msgraph.Application{
DirectoryObject: msgraph.DirectoryObject{
Id: app.Id,
},
DisplayName: utils.String(displayName),
})
// Attempt to patch the newly created application and set the display name, which will tell us whether it exists yet, then set it back to the desired value.
// The SDK handles retries for us here in the event of 404, 429 or 5xx, then returns after giving up.
uuid, err := uuid.GenerateUUID()
if err != nil {
if status == http.StatusNotFound {
return tf.ErrorDiagF(err, "Timed out whilst waiting for new application to be replicated in Azure AD")
return tf.ErrorDiagF(err, "Failed to generate a UUID")
}
tempDisplayName := fmt.Sprintf("TERRAFORM_UPDATE_%s", uuid)
for _, displayNameToSet := range []string{tempDisplayName, displayName} {
status, err := client.Update(ctx, msgraph.Application{
DirectoryObject: msgraph.DirectoryObject{
Id: app.ID(),
},
DisplayName: utils.String(displayNameToSet),
})
if err != nil {
if status == http.StatusNotFound {
return tf.ErrorDiagF(err, "Timed out whilst waiting for new application to be replicated in Azure AD")
}
return tf.ErrorDiagF(err, "Failed to patch application with object ID %q after creating", *app.ID())
}
return tf.ErrorDiagF(err, "Failed to patch application after creating")
}

// API bug: cannot set `acceptMappedClaims` when holding the Application.ReadWrite.OwnedBy role
Expand Down

0 comments on commit a6b36b5

Please sign in to comment.