-
Notifications
You must be signed in to change notification settings - Fork 5k
New Resource: azurerm_container_app_environment_managed_certificate
#31137
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
Merged
sreallymatt
merged 7 commits into
hashicorp:main
from
jiaweitao001:container_app_env_managed_cert
Apr 14, 2026
Merged
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fe3269d
New resource: azurerm_container_app_environment_managed_certificate
jiaweitao001 c10a6f9
Fixing indent
jiaweitao001 4baa266
Address review feedback from sreallymatt
jiaweitao001 630b677
Increase create timeout for managed certificate to 60 minutes
jiaweitao001 74be517
Fix CNAME record target in managed certificate test
jiaweitao001 2c274d1
Remove TXT domain control validation from docs and tests
jiaweitao001 08d04ca
Omit TXT from domain control validation ValidateFunc
jiaweitao001 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
243 changes: 243 additions & 0 deletions
243
internal/services/containerapps/container_app_environment_managed_certificate_resource.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,243 @@ | ||
| // Copyright (c) HashiCorp, Inc. | ||
| // SPDX-License-Identifier: MPL-2.0 | ||
|
|
||
| package containerapps | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
| "time" | ||
|
|
||
| "github.com/hashicorp/go-azure-helpers/lang/pointer" | ||
| "github.com/hashicorp/go-azure-helpers/lang/response" | ||
| "github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema" | ||
| "github.com/hashicorp/go-azure-helpers/resourcemanager/tags" | ||
| "github.com/hashicorp/go-azure-sdk/resource-manager/containerapps/2025-07-01/managedenvironments" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/sdk" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/services/containerapps/validate" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk" | ||
| "github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation" | ||
| ) | ||
|
|
||
| type ContainerAppEnvironmentManagedCertificateResource struct{} | ||
|
|
||
| type ContainerAppEnvironmentManagedCertificateModel struct { | ||
| Name string `tfschema:"name"` | ||
| ContainerAppEnvironmentId string `tfschema:"container_app_environment_id"` | ||
| SubjectName string `tfschema:"subject_name"` | ||
| DomainControlValidation string `tfschema:"domain_control_validation"` | ||
| Tags map[string]interface{} `tfschema:"tags"` | ||
| ValidationToken string `tfschema:"validation_token"` | ||
| } | ||
|
|
||
| var _ sdk.ResourceWithUpdate = ContainerAppEnvironmentManagedCertificateResource{} | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) ModelObject() interface{} { | ||
| return &ContainerAppEnvironmentManagedCertificateModel{} | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) ResourceType() string { | ||
| return "azurerm_container_app_environment_managed_certificate" | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) IDValidationFunc() pluginsdk.SchemaValidateFunc { | ||
| return managedenvironments.ValidateManagedCertificateID | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) Arguments() map[string]*pluginsdk.Schema { | ||
| return map[string]*pluginsdk.Schema{ | ||
| "name": { | ||
| Type: pluginsdk.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| ValidateFunc: validate.CertificateName, | ||
| Description: "The name of the Container Apps Managed Certificate.", | ||
| }, | ||
|
|
||
| "container_app_environment_id": { | ||
| Type: pluginsdk.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| ValidateFunc: managedenvironments.ValidateManagedEnvironmentID, | ||
| Description: "The Container App Managed Environment ID to configure this Managed Certificate on.", | ||
| }, | ||
|
|
||
| "subject_name": { | ||
| Type: pluginsdk.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| ValidateFunc: validation.StringIsNotEmpty, | ||
| Description: "The Subject Name of the Certificate. Must be a valid domain name.", | ||
| }, | ||
|
|
||
| "domain_control_validation": { | ||
| Type: pluginsdk.TypeString, | ||
| Optional: true, | ||
| ForceNew: true, | ||
| Default: string(managedenvironments.ManagedCertificateDomainControlValidationHTTP), | ||
| ValidateFunc: validation.StringInSlice( | ||
| managedenvironments.PossibleValuesForManagedCertificateDomainControlValidation(), | ||
| false, | ||
| ), | ||
| Description: "The domain control validation type for the managed certificate. Possible values are `CNAME`, `HTTP` and `TXT`. Defaults to `HTTP`.", | ||
| }, | ||
|
|
||
| "tags": commonschema.Tags(), | ||
| } | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) Attributes() map[string]*pluginsdk.Schema { | ||
| return map[string]*pluginsdk.Schema{ | ||
| "validation_token": { | ||
| Type: pluginsdk.TypeString, | ||
| Computed: true, | ||
| Description: "The validation token for the managed certificate.", | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) Create() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 30 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.ContainerApps.ManagedEnvironmentClient | ||
|
|
||
| var model ContainerAppEnvironmentManagedCertificateModel | ||
|
|
||
| if err := metadata.Decode(&model); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| envId, err := managedenvironments.ParseManagedEnvironmentID(model.ContainerAppEnvironmentId) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| id := managedenvironments.NewManagedCertificateID(envId.SubscriptionId, envId.ResourceGroupName, envId.ManagedEnvironmentName, model.Name) | ||
|
|
||
| existing, err := client.ManagedCertificatesGet(ctx, id) | ||
| if err != nil { | ||
| if !response.WasNotFound(existing.HttpResponse) { | ||
| return fmt.Errorf("checking for presence of existing %s: %+v", id, err) | ||
| } | ||
| } | ||
|
|
||
| if !response.WasNotFound(existing.HttpResponse) { | ||
| return metadata.ResourceRequiresImport(r.ResourceType(), id) | ||
| } | ||
|
|
||
| env, err := client.Get(ctx, *envId) | ||
| if err != nil { | ||
| return fmt.Errorf("reading %s for %s: %+v", *envId, id, err) | ||
|
sreallymatt marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| certificate := managedenvironments.ManagedCertificate{ | ||
| Location: env.Model.Location, | ||
|
sreallymatt marked this conversation as resolved.
|
||
| Properties: &managedenvironments.ManagedCertificateProperties{ | ||
| SubjectName: pointer.To(model.SubjectName), | ||
| DomainControlValidation: pointer.To(managedenvironments.ManagedCertificateDomainControlValidation(model.DomainControlValidation)), | ||
| }, | ||
| Tags: tags.Expand(model.Tags), | ||
| } | ||
|
|
||
| if err := client.ManagedCertificatesCreateOrUpdateThenPoll(ctx, id, certificate); err != nil { | ||
| return fmt.Errorf("creating %s: %+v", id, err) | ||
| } | ||
|
|
||
| metadata.SetID(id) | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) Read() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 5 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.ContainerApps.ManagedEnvironmentClient | ||
|
|
||
| id, err := managedenvironments.ParseManagedCertificateID(metadata.ResourceData.Id()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| existing, err := client.ManagedCertificatesGet(ctx, *id) | ||
| if err != nil { | ||
| if response.WasNotFound(existing.HttpResponse) { | ||
| return metadata.MarkAsGone(id) | ||
| } | ||
| return fmt.Errorf("reading %s: %+v", *id, err) | ||
|
sreallymatt marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| var state ContainerAppEnvironmentManagedCertificateModel | ||
|
|
||
| state.Name = id.ManagedCertificateName | ||
| state.ContainerAppEnvironmentId = managedenvironments.NewManagedEnvironmentID(id.SubscriptionId, id.ResourceGroupName, id.ManagedEnvironmentName).ID() | ||
|
|
||
| if model := existing.Model; model != nil { | ||
| state.Tags = tags.Flatten(model.Tags) | ||
|
|
||
| if props := model.Properties; props != nil { | ||
| state.SubjectName = pointer.From(props.SubjectName) | ||
| state.DomainControlValidation = string(pointer.From(props.DomainControlValidation)) | ||
|
sreallymatt marked this conversation as resolved.
Outdated
|
||
| state.ValidationToken = pointer.From(props.ValidationToken) | ||
| } | ||
| } | ||
|
|
||
| return metadata.Encode(&state) | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) Delete() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 30 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.ContainerApps.ManagedEnvironmentClient | ||
|
|
||
| id, err := managedenvironments.ParseManagedCertificateID(metadata.ResourceData.Id()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if _, err := client.ManagedCertificatesDelete(ctx, *id); err != nil { | ||
| return fmt.Errorf("deleting %s: %+v", *id, err) | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func (r ContainerAppEnvironmentManagedCertificateResource) Update() sdk.ResourceFunc { | ||
| return sdk.ResourceFunc{ | ||
| Timeout: 30 * time.Minute, | ||
| Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error { | ||
| client := metadata.Client.ContainerApps.ManagedEnvironmentClient | ||
|
|
||
| var model ContainerAppEnvironmentManagedCertificateModel | ||
|
|
||
| if err := metadata.Decode(&model); err != nil { | ||
| return err | ||
| } | ||
|
|
||
| id, err := managedenvironments.ParseManagedCertificateID(metadata.ResourceData.Id()) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| if metadata.ResourceData.HasChange("tags") { | ||
| patch := managedenvironments.ManagedCertificatePatch{ | ||
| Tags: tags.Expand(model.Tags), | ||
| } | ||
|
|
||
| if _, err = client.ManagedCertificatesUpdate(ctx, *id, patch); err != nil { | ||
| return fmt.Errorf("updating tags for %s: %+v", *id, err) | ||
|
sreallymatt marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given
TXTdoes not currently appear to function, we should omit it from this ValidateFunc as well. If/when support is added, we can revisit and add it back to the resource.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure. Removed.