Skip to content

Commit 4baa266

Browse files
jiaweitao001Copilot
andcommitted
Address review feedback from sreallymatt
- Use consistent error message format with retrieving/updating - Add nil check for env.Model before accessing nested fields - Use pointer.FromEnum for DomainControlValidation - Add CNAME and TXT domain control validation tests - Shorten DNS record name to avoid exceeding 64 char limit - Add custom domain with depends_on to docs example Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent c10a6f9 commit 4baa266

File tree

3 files changed

+142
-6
lines changed

3 files changed

+142
-6
lines changed

internal/services/containerapps/container_app_environment_managed_certificate_resource.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,11 @@ func (r ContainerAppEnvironmentManagedCertificateResource) Create() sdk.Resource
128128

129129
env, err := client.Get(ctx, *envId)
130130
if err != nil {
131-
return fmt.Errorf("reading %s for %s: %+v", *envId, id, err)
131+
return fmt.Errorf("retrieving %s: %+v", *envId, err)
132+
}
133+
134+
if env.Model == nil {
135+
return fmt.Errorf("retrieving %s: `model` was nil", envId)
132136
}
133137

134138
certificate := managedenvironments.ManagedCertificate{
@@ -167,7 +171,7 @@ func (r ContainerAppEnvironmentManagedCertificateResource) Read() sdk.ResourceFu
167171
if response.WasNotFound(existing.HttpResponse) {
168172
return metadata.MarkAsGone(id)
169173
}
170-
return fmt.Errorf("reading %s: %+v", *id, err)
174+
return fmt.Errorf("retrieving %s: %+v", *id, err)
171175
}
172176

173177
var state ContainerAppEnvironmentManagedCertificateModel
@@ -180,7 +184,7 @@ func (r ContainerAppEnvironmentManagedCertificateResource) Read() sdk.ResourceFu
180184

181185
if props := model.Properties; props != nil {
182186
state.SubjectName = pointer.From(props.SubjectName)
183-
state.DomainControlValidation = string(pointer.From(props.DomainControlValidation))
187+
state.DomainControlValidation = pointer.FromEnum(props.DomainControlValidation)
184188
state.ValidationToken = pointer.From(props.ValidationToken)
185189
}
186190
}
@@ -233,7 +237,7 @@ func (r ContainerAppEnvironmentManagedCertificateResource) Update() sdk.Resource
233237
}
234238

235239
if _, err = client.ManagedCertificatesUpdate(ctx, *id, patch); err != nil {
236-
return fmt.Errorf("updating tags for %s: %+v", *id, err)
240+
return fmt.Errorf("updating %s: %+v", *id, err)
237241
}
238242
}
239243

internal/services/containerapps/container_app_environment_managed_certificate_resource_test.go

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,44 @@ func TestAccContainerAppEnvironmentManagedCertificate_domainControlValidationHTT
103103
})
104104
}
105105

106+
func TestAccContainerAppEnvironmentManagedCertificate_domainControlValidationCNAME(t *testing.T) {
107+
if os.Getenv("ARM_TEST_DNS_ZONE") == "" || os.Getenv("ARM_TEST_DATA_RESOURCE_GROUP") == "" {
108+
t.Skipf("Skipping as either ARM_TEST_DNS_ZONE or ARM_TEST_DATA_RESOURCE_GROUP is not set")
109+
}
110+
111+
data := acceptance.BuildTestData(t, "azurerm_container_app_environment_managed_certificate", "test")
112+
r := ContainerAppEnvironmentManagedCertificateResource{}
113+
114+
data.ResourceTest(t, r, []acceptance.TestStep{
115+
{
116+
Config: r.domainControlValidationCNAME(data),
117+
Check: acceptance.ComposeTestCheckFunc(
118+
check.That(data.ResourceName).ExistsInAzure(r),
119+
),
120+
},
121+
data.ImportStep(),
122+
})
123+
}
124+
125+
func TestAccContainerAppEnvironmentManagedCertificate_domainControlValidationTXT(t *testing.T) {
126+
if os.Getenv("ARM_TEST_DNS_ZONE") == "" || os.Getenv("ARM_TEST_DATA_RESOURCE_GROUP") == "" {
127+
t.Skipf("Skipping as either ARM_TEST_DNS_ZONE or ARM_TEST_DATA_RESOURCE_GROUP is not set")
128+
}
129+
130+
data := acceptance.BuildTestData(t, "azurerm_container_app_environment_managed_certificate", "test")
131+
r := ContainerAppEnvironmentManagedCertificateResource{}
132+
133+
data.ResourceTest(t, r, []acceptance.TestStep{
134+
{
135+
Config: r.domainControlValidationTXT(data),
136+
Check: acceptance.ComposeTestCheckFunc(
137+
check.That(data.ResourceName).ExistsInAzure(r),
138+
),
139+
},
140+
data.ImportStep(),
141+
})
142+
}
143+
106144
func (r ContainerAppEnvironmentManagedCertificateResource) Exists(ctx context.Context, client *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
107145
id, err := managedenvironments.ParseManagedCertificateID(state.ID)
108146
if err != nil {
@@ -222,6 +260,62 @@ resource "azurerm_container_app_environment_managed_certificate" "test" {
222260
`, r.template(data), data.RandomInteger)
223261
}
224262

263+
func (r ContainerAppEnvironmentManagedCertificateResource) domainControlValidationCNAME(data acceptance.TestData) string {
264+
return fmt.Sprintf(`
265+
provider "azurerm" {
266+
features {}
267+
}
268+
269+
%[1]s
270+
271+
resource "azurerm_container_app_custom_domain" "test" {
272+
name = trimsuffix(trimprefix(azurerm_dns_txt_record.test.fqdn, "asuid."), ".")
273+
container_app_id = azurerm_container_app.test.id
274+
275+
lifecycle {
276+
ignore_changes = [certificate_binding_type, container_app_environment_certificate_id]
277+
}
278+
}
279+
280+
resource "azurerm_container_app_environment_managed_certificate" "test" {
281+
name = "acctest-cacertmgd%[2]d"
282+
container_app_environment_id = azurerm_container_app_environment.test.id
283+
subject_name = trimsuffix(trimprefix(azurerm_dns_txt_record.test.fqdn, "asuid."), ".")
284+
domain_control_validation = "CNAME"
285+
286+
depends_on = [azurerm_container_app_custom_domain.test]
287+
}
288+
`, r.template(data), data.RandomInteger)
289+
}
290+
291+
func (r ContainerAppEnvironmentManagedCertificateResource) domainControlValidationTXT(data acceptance.TestData) string {
292+
return fmt.Sprintf(`
293+
provider "azurerm" {
294+
features {}
295+
}
296+
297+
%[1]s
298+
299+
resource "azurerm_container_app_custom_domain" "test" {
300+
name = trimsuffix(trimprefix(azurerm_dns_txt_record.test.fqdn, "asuid."), ".")
301+
container_app_id = azurerm_container_app.test.id
302+
303+
lifecycle {
304+
ignore_changes = [certificate_binding_type, container_app_environment_certificate_id]
305+
}
306+
}
307+
308+
resource "azurerm_container_app_environment_managed_certificate" "test" {
309+
name = "acctest-cacertmgd%[2]d"
310+
container_app_environment_id = azurerm_container_app_environment.test.id
311+
subject_name = trimsuffix(trimprefix(azurerm_dns_txt_record.test.fqdn, "asuid."), ".")
312+
domain_control_validation = "TXT"
313+
314+
depends_on = [azurerm_container_app_custom_domain.test]
315+
}
316+
`, r.template(data), data.RandomInteger)
317+
}
318+
225319
func (r ContainerAppEnvironmentManagedCertificateResource) template(data acceptance.TestData) string {
226320
dnsZone := os.Getenv("ARM_TEST_DNS_ZONE")
227321
dataResourceGroup := os.Getenv("ARM_TEST_DATA_RESOURCE_GROUP")
@@ -281,15 +375,15 @@ resource "azurerm_container_app" "test" {
281375
}
282376
283377
resource "azurerm_dns_cname_record" "test" {
284-
name = "containerapp%[1]d"
378+
name = "contapp%[1]d"
285379
resource_group_name = data.azurerm_dns_zone.test.resource_group_name
286380
zone_name = data.azurerm_dns_zone.test.name
287381
ttl = 300
288382
record = azurerm_container_app.test.latest_revision_fqdn
289383
}
290384
291385
resource "azurerm_dns_txt_record" "test" {
292-
name = "asuid.containerapp%[1]d"
386+
name = "asuid.contapp%[1]d"
293387
resource_group_name = data.azurerm_dns_zone.test.resource_group_name
294388
zone_name = data.azurerm_dns_zone.test.name
295389
ttl = 60

website/docs/r/container_app_environment_managed_certificate.html.markdown

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,49 @@ resource "azurerm_container_app_environment" "example" {
3333
log_analytics_workspace_id = azurerm_log_analytics_workspace.example.id
3434
}
3535
36+
resource "azurerm_container_app" "example" {
37+
name = "example-app"
38+
resource_group_name = azurerm_resource_group.example.name
39+
container_app_environment_id = azurerm_container_app_environment.example.id
40+
revision_mode = "Single"
41+
42+
template {
43+
container {
44+
name = "example-container"
45+
image = "mcr.microsoft.com/k8se/quickstart:latest"
46+
cpu = 0.25
47+
memory = "0.5Gi"
48+
}
49+
}
50+
51+
ingress {
52+
external_enabled = true
53+
target_port = 80
54+
transport = "http"
55+
56+
traffic_weight {
57+
latest_revision = true
58+
percentage = 100
59+
}
60+
}
61+
}
62+
63+
resource "azurerm_container_app_custom_domain" "example" {
64+
name = "example.com"
65+
container_app_id = azurerm_container_app.example.id
66+
67+
lifecycle {
68+
ignore_changes = [certificate_binding_type, container_app_environment_certificate_id]
69+
}
70+
}
71+
3672
resource "azurerm_container_app_environment_managed_certificate" "example" {
3773
name = "example-managed-cert"
3874
container_app_environment_id = azurerm_container_app_environment.example.id
3975
subject_name = "example.com"
4076
domain_control_validation = "HTTP"
77+
78+
depends_on = [azurerm_container_app_custom_domain.example]
4179
}
4280
```
4381

0 commit comments

Comments
 (0)