Skip to content

Commit

Permalink
azurerm_api_management_diagnostic - support required property `api_…
Browse files Browse the repository at this point in the history
…management_logger_id` (#6682)

fix #6619

This will be a breaking change for it adds a required field "logger_id"

=== RUN TestAccAzureRMApiManagementDiagnostic_basic
=== PAUSE TestAccAzureRMApiManagementDiagnostic_basic
=== CONT TestAccAzureRMApiManagementDiagnostic_basic
--- PASS: TestAccAzureRMApiManagementDiagnostic_basic (2498.17s)
=== RUN TestAccAzureRMApiManagementDiagnostic_update
=== PAUSE TestAccAzureRMApiManagementDiagnostic_update
=== CONT TestAccAzureRMApiManagementDiagnostic_update
--- PASS: TestAccAzureRMApiManagementDiagnostic_update (2566.13s)
=== RUN TestAccAzureRMApiManagementDiagnostic_requiresImport
=== PAUSE TestAccAzureRMApiManagementDiagnostic_requiresImport
=== CONT TestAccAzureRMApiManagementDiagnostic_requiresImport
--- PASS: TestAccAzureRMApiManagementDiagnostic_requiresImport (2330.79s)
PASS

(also fixes #6104)
  • Loading branch information
yupwei68 committed May 14, 2020
1 parent 38d4961 commit 00aaff2
Show file tree
Hide file tree
Showing 7 changed files with 424 additions and 54 deletions.
Expand Up @@ -11,6 +11,9 @@ import (
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/apimanagement/validate"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand All @@ -21,9 +24,11 @@ func resourceArmApiManagementDiagnostic() *schema.Resource {
Read: resourceArmApiManagementDiagnosticRead,
Update: resourceArmApiManagementDiagnosticCreateUpdate,
Delete: resourceArmApiManagementDiagnosticDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.ApiManagementDiagnosticID(id)
return err
}),

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Expand All @@ -46,6 +51,12 @@ func resourceArmApiManagementDiagnostic() *schema.Resource {

"api_management_name": azure.SchemaApiManagementName(),

"api_management_logger_id": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validate.ApiManagementLoggerID,
},

"enabled": {
Type: schema.TypeBool,
Optional: true,
Expand Down Expand Up @@ -78,7 +89,9 @@ func resourceArmApiManagementDiagnosticCreateUpdate(d *schema.ResourceData, meta
}

parameters := apimanagement.DiagnosticContract{
DiagnosticContractProperties: &apimanagement.DiagnosticContractProperties{},
DiagnosticContractProperties: &apimanagement.DiagnosticContractProperties{
LoggerID: utils.String(d.Get("api_management_logger_id").(string)),
},
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, diagnosticId, parameters, ""); err != nil {
Expand All @@ -90,7 +103,7 @@ func resourceArmApiManagementDiagnosticCreateUpdate(d *schema.ResourceData, meta
return fmt.Errorf("retrieving Diagnostic %q (Resource Group %q / API Management Service %q): %+v", diagnosticId, resourceGroup, serviceName, err)
}
if resp.ID == nil {
return fmt.Errorf("Cannot read ID for Diagnostic %q (Resource Group %q / API Management Service %q)", diagnosticId, resourceGroup, serviceName)
return fmt.Errorf("reading ID for Diagnostic %q (Resource Group %q / API Management Service %q): ID is empty", diagnosticId, resourceGroup, serviceName)
}
d.SetId(*resp.ID)

Expand All @@ -102,28 +115,26 @@ func resourceArmApiManagementDiagnosticRead(d *schema.ResourceData, meta interfa
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
diagnosticId, err := parse.ApiManagementDiagnosticID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
diagnosticId := id.Path["diagnostics"]

resp, err := client.Get(ctx, resourceGroup, serviceName, diagnosticId)
resp, err := client.Get(ctx, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[DEBUG] Diagnostic %q (Resource Group %q / API Management Service %q) was not found - removing from state!", diagnosticId, resourceGroup, serviceName)
log.Printf("[DEBUG] Diagnostic %q (Resource Group %q / API Management Service %q) was not found - removing from state!", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName)
d.SetId("")
return nil
}

return fmt.Errorf("making Read request for Diagnostic %q (Resource Group %q / API Management Service %q): %+v", diagnosticId, resourceGroup, serviceName, err)
return fmt.Errorf("making Read request for Diagnostic %q (Resource Group %q / API Management Service %q): %+v", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName, err)
}

d.Set("identifier", resp.Name)
d.Set("resource_group_name", resourceGroup)
d.Set("api_management_name", serviceName)
d.Set("resource_group_name", diagnosticId.ResourceGroup)
d.Set("api_management_name", diagnosticId.ServiceName)
d.Set("api_management_logger_id", resp.LoggerID)

return nil
}
Expand All @@ -133,17 +144,14 @@ func resourceArmApiManagementDiagnosticDelete(d *schema.ResourceData, meta inter
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := azure.ParseAzureResourceID(d.Id())
diagnosticId, err := parse.ApiManagementDiagnosticID(d.Id())
if err != nil {
return err
}
resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
diagnosticId := id.Path["diagnostics"]

if resp, err := client.Delete(ctx, resourceGroup, serviceName, diagnosticId, ""); err != nil {
if resp, err := client.Delete(ctx, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.Name, ""); err != nil {
if !utils.ResponseWasNotFound(resp) {
return fmt.Errorf("deleting Diagnostic %q (Resource Group %q / API Management Service %q): %+v", diagnosticId, resourceGroup, serviceName, err)
return fmt.Errorf("deleting Diagnostic %q (Resource Group %q / API Management Service %q): %+v", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName, err)
}
}

Expand Down
69 changes: 69 additions & 0 deletions azurerm/internal/services/apimanagement/parse/apimanagement.go
@@ -0,0 +1,69 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type ApiManagementLoggerId struct {
ResourceGroup string
ServiceName string
Name string
}

func ApiManagementLoggerID(input string) (*ApiManagementLoggerId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Api Management Logger ID %q: %+v", input, err)
}

logger := ApiManagementLoggerId{
ResourceGroup: id.ResourceGroup,
}

if logger.ServiceName, err = id.PopSegment("service"); err != nil {
return nil, err
}

if logger.Name, err = id.PopSegment("loggers"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &logger, nil
}

type ApiManagementDiagnosticId struct {
ResourceGroup string
ServiceName string
Name string
}

func ApiManagementDiagnosticID(input string) (*ApiManagementDiagnosticId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Api Management Diagnostic ID %q: %+v", input, err)
}

diagnostic := ApiManagementDiagnosticId{
ResourceGroup: id.ResourceGroup,
}

if diagnostic.ServiceName, err = id.PopSegment("service"); err != nil {
return nil, err
}

if diagnostic.Name, err = id.PopSegment("diagnostics"); err != nil {
return nil, err
}

if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &diagnostic, nil
}
169 changes: 169 additions & 0 deletions azurerm/internal/services/apimanagement/parse/apimanagement_test.go
@@ -0,0 +1,169 @@
package parse

import "testing"

func TestApiManagementLoggerID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *ApiManagementLoggerId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Service Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/",
Expected: nil,
},
{
Name: "Missing Logger",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1",
Expected: nil,
},
{
Name: "Missing Logger Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/loggers",
Expected: nil,
},
{
Name: "Logger ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/loggers/logger1",
Expected: &ApiManagementLoggerId{
Name: "logger1",
ServiceName: "service1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/Loggers/logger1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ApiManagementLoggerID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ServiceName != v.Expected.ServiceName {
t.Fatalf("Expected %q but got %q for Service Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}

func TestApiManagementDiagnosticID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *ApiManagementDiagnosticId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing Service Name",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/",
Expected: nil,
},
{
Name: "Missing Diagnostic",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1",
Expected: nil,
},
{
Name: "Missing Diagnostic Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/diagnostics",
Expected: nil,
},
{
Name: "Diagnostic ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/diagnostics/diagnostic1",
Expected: &ApiManagementDiagnosticId{
Name: "diagnostic1",
ServiceName: "service1",
ResourceGroup: "resGroup1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/Diagnostics/diagnostic1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q", v.Name)

actual, err := ApiManagementDiagnosticID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}

t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}

if actual.ServiceName != v.Expected.ServiceName {
t.Fatalf("Expected %q but got %q for Service Name", v.Expected.Name, actual.Name)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for Resource Group", v.Expected.ResourceGroup, actual.ResourceGroup)
}
}
}

0 comments on commit 00aaff2

Please sign in to comment.