diff --git a/azurerm/helpers/azure/resourceid.go b/azurerm/helpers/azure/resourceid.go index 3e4f59979775..0d6a075476e9 100644 --- a/azurerm/helpers/azure/resourceid.go +++ b/azurerm/helpers/azure/resourceid.go @@ -110,5 +110,5 @@ func (id *ResourceID) ValidateNoEmptySegments(sourceId string) error { return nil } - return fmt.Errorf("ID contained more segments than required: %q", sourceId) + return fmt.Errorf("ID contained more segments than required: %q, %v", sourceId, id.Path) } diff --git a/azurerm/internal/services/apimanagement/api_management_api_diagnostic_resource.go b/azurerm/internal/services/apimanagement/api_management_api_diagnostic_resource.go new file mode 100644 index 000000000000..f7cfc1f2fdc9 --- /dev/null +++ b/azurerm/internal/services/apimanagement/api_management_api_diagnostic_resource.go @@ -0,0 +1,166 @@ +package apimanagement + +import ( + "fmt" + "log" + "time" + + "github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2019-12-01/apimanagement" + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + helpersvalidate "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "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" +) + +func resourceArmApiManagementApiDiagnostic() *schema.Resource { + return &schema.Resource{ + Create: resourceArmApiManagementApiDiagnosticCreateUpdate, + Read: resourceArmApiManagementApiDiagnosticRead, + Update: resourceArmApiManagementApiDiagnosticCreateUpdate, + Delete: resourceArmApiManagementApiDiagnosticDelete, + + Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error { + _, err := parse.ApiManagementApiDiagnosticID(id) + return err + }), + + Timeouts: &schema.ResourceTimeout{ + Create: schema.DefaultTimeout(30 * time.Minute), + Read: schema.DefaultTimeout(5 * time.Minute), + Update: schema.DefaultTimeout(30 * time.Minute), + Delete: schema.DefaultTimeout(30 * time.Minute), + }, + + Schema: map[string]*schema.Schema{ + "identifier": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: validation.StringInSlice([]string{ + "applicationinsights", + "azuremonitor", + }, false), + }, + + "resource_group_name": azure.SchemaResourceGroupName(), + + "api_management_name": azure.SchemaApiManagementName(), + + "api_name": { + Type: schema.TypeString, + Required: true, + ForceNew: true, + ValidateFunc: helpersvalidate.ApiManagementApiName, + }, + + "api_management_logger_id": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.ApiManagementLoggerID, + }, + }, + } +} + +func resourceArmApiManagementApiDiagnosticCreateUpdate(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.ApiDiagnosticClient + ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d) + defer cancel() + + diagnosticId := d.Get("identifier").(string) + resourceGroup := d.Get("resource_group_name").(string) + serviceName := d.Get("api_management_name").(string) + apiName := d.Get("api_name").(string) + + if d.IsNewResource() { + existing, err := client.Get(ctx, resourceGroup, serviceName, apiName, diagnosticId) + if err != nil { + if !utils.ResponseWasNotFound(existing.Response) { + return fmt.Errorf("checking for presence of existing Diagnostic %q (Resource Group %q / API Management Service %q / API %q): %s", diagnosticId, resourceGroup, serviceName, apiName, err) + } + } + + if existing.ID != nil && *existing.ID != "" { + return tf.ImportAsExistsError("azurerm_api_management_api_diagnostic", *existing.ID) + } + } + + parameters := apimanagement.DiagnosticContract{ + DiagnosticContractProperties: &apimanagement.DiagnosticContractProperties{ + LoggerID: utils.String(d.Get("api_management_logger_id").(string)), + }, + } + + if _, err := client.CreateOrUpdate(ctx, resourceGroup, serviceName, apiName, diagnosticId, parameters, ""); err != nil { + return fmt.Errorf("creating or updating Diagnostic %q (Resource Group %q / API Management Service %q / API %q): %+v", diagnosticId, resourceGroup, serviceName, apiName, err) + } + + resp, err := client.Get(ctx, resourceGroup, serviceName, apiName, diagnosticId) + if err != nil { + return fmt.Errorf("retrieving Diagnostic %q (Resource Group %q / API Management Service %q / API %q): %+v", diagnosticId, resourceGroup, serviceName, apiName, err) + } + if resp.ID == nil || *resp.ID == "" { + return fmt.Errorf("reading ID for Diagnostic %q (Resource Group %q / API Management Service %q / API %q): ID is empty", diagnosticId, resourceGroup, serviceName, apiName) + } + d.SetId(*resp.ID) + + return resourceArmApiManagementApiDiagnosticRead(d, meta) +} + +func resourceArmApiManagementApiDiagnosticRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.ApiDiagnosticClient + ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d) + defer cancel() + + diagnosticId, err := parse.ApiManagementApiDiagnosticID(d.Id()) + if err != nil { + return err + } + + resp, err := client.Get(ctx, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName, diagnosticId.Name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + log.Printf("[DEBUG] Diagnostic %q (Resource Group %q / API Management Service %q / API %q) was not found - removing from state!", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName) + d.SetId("") + return nil + } + + return fmt.Errorf("making Read request for Diagnostic %q (Resource Group %q / API Management Service %q / API %q): %+v", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName, err) + } + + d.Set("api_name", diagnosticId.ApiName) + d.Set("identifier", resp.Name) + d.Set("resource_group_name", diagnosticId.ResourceGroup) + d.Set("api_management_name", diagnosticId.ServiceName) + if props := resp.DiagnosticContractProperties; props != nil { + d.Set("api_management_logger_id", props.LoggerID) + } + + return nil +} + +func resourceArmApiManagementApiDiagnosticDelete(d *schema.ResourceData, meta interface{}) error { + client := meta.(*clients.Client).ApiManagement.ApiDiagnosticClient + ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d) + defer cancel() + + diagnosticId, err := parse.ApiManagementApiDiagnosticID(d.Id()) + if err != nil { + return err + } + + if resp, err := client.Delete(ctx, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName, diagnosticId.Name, ""); err != nil { + if !utils.ResponseWasNotFound(resp) { + return fmt.Errorf("deleting Diagnostic %q (Resource Group %q / API Management Service %q / API %q): %+v", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName, err) + } + } + + return nil +} diff --git a/azurerm/internal/services/apimanagement/api_management_diagnostic_resource.go b/azurerm/internal/services/apimanagement/api_management_diagnostic_resource.go index 6caf48496309..20b6a62a841b 100644 --- a/azurerm/internal/services/apimanagement/api_management_diagnostic_resource.go +++ b/azurerm/internal/services/apimanagement/api_management_diagnostic_resource.go @@ -44,6 +44,7 @@ func resourceArmApiManagementDiagnostic() *schema.Resource { ForceNew: true, ValidateFunc: validation.StringInSlice([]string{ "applicationinsights", + "azuremonitor", }, false), }, diff --git a/azurerm/internal/services/apimanagement/client/client.go b/azurerm/internal/services/apimanagement/client/client.go index 736c6dcfba7d..8ceaba5dc434 100644 --- a/azurerm/internal/services/apimanagement/client/client.go +++ b/azurerm/internal/services/apimanagement/client/client.go @@ -7,6 +7,7 @@ import ( type Client struct { ApiClient *apimanagement.APIClient + ApiDiagnosticClient *apimanagement.APIDiagnosticClient ApiPoliciesClient *apimanagement.APIPolicyClient ApiOperationsClient *apimanagement.APIOperationClient ApiOperationPoliciesClient *apimanagement.APIOperationPolicyClient @@ -38,6 +39,9 @@ func NewClient(o *common.ClientOptions) *Client { apiClient := apimanagement.NewAPIClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&apiClient.Client, o.ResourceManagerAuthorizer) + apiDiagnosticClient := apimanagement.NewAPIDiagnosticClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) + o.ConfigureClient(&apiDiagnosticClient.Client, o.ResourceManagerAuthorizer) + apiPoliciesClient := apimanagement.NewAPIPolicyClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId) o.ConfigureClient(&apiPoliciesClient.Client, o.ResourceManagerAuthorizer) @@ -115,6 +119,7 @@ func NewClient(o *common.ClientOptions) *Client { return &Client{ ApiClient: &apiClient, + ApiDiagnosticClient: &apiDiagnosticClient, ApiPoliciesClient: &apiPoliciesClient, ApiOperationsClient: &apiOperationsClient, ApiOperationPoliciesClient: &apiOperationPoliciesClient, diff --git a/azurerm/internal/services/apimanagement/parse/api_diagnostic_id.go b/azurerm/internal/services/apimanagement/parse/api_diagnostic_id.go new file mode 100644 index 000000000000..3fa26e480ae1 --- /dev/null +++ b/azurerm/internal/services/apimanagement/parse/api_diagnostic_id.go @@ -0,0 +1,43 @@ +package parse + +import ( + "fmt" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure" +) + +type ApiManagementApiDiagnosticId struct { + ResourceGroup string + ServiceName string + ApiName string + Name string +} + +func ApiManagementApiDiagnosticID(input string) (*ApiManagementApiDiagnosticId, error) { + id, err := azure.ParseAzureResourceID(input) + if err != nil { + return nil, fmt.Errorf("parsing Api Management Diagnostic ID %q: %+v", input, err) + } + + diagnostic := ApiManagementApiDiagnosticId{ + ResourceGroup: id.ResourceGroup, + } + + if diagnostic.ServiceName, err = id.PopSegment("service"); err != nil { + return nil, err + } + + if diagnostic.ApiName, err = id.PopSegment("apis"); 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 +} diff --git a/azurerm/internal/services/apimanagement/parse/api_diagnostic_id_test.go b/azurerm/internal/services/apimanagement/parse/api_diagnostic_id_test.go new file mode 100644 index 000000000000..d748c67d0846 --- /dev/null +++ b/azurerm/internal/services/apimanagement/parse/api_diagnostic_id_test.go @@ -0,0 +1,111 @@ +package parse + +import "testing" + +func TestApiManagementApiDiagnosticID(t *testing.T) { + testData := []struct { + Name string + Input string + Expected *ApiManagementApiDiagnosticId + }{ + { + 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 APIs", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1", + Expected: nil, + }, + { + Name: "Missing APIs Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis", + Expected: nil, + }, + { + Name: "Missing Diagnostics", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1", + Expected: nil, + }, + { + Name: "Missing Diagnostics Value", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/diagnostics", + Expected: nil, + }, + { + Name: "Diagnostic ID", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/apis/api1/diagnostics/applicationinsights", + Expected: &ApiManagementApiDiagnosticId{ + Name: "applicationinsights", + ApiName: "api1", + ServiceName: "service1", + ResourceGroup: "resGroup1", + }, + }, + { + Name: "Wrong Casing", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/resGroup1/providers/Microsoft.ApiManagement/service/service1/APIs/api1/diagnostics/applicationinsights", + Expected: nil, + }, + { + Name: "From ACC test", + Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/acctestRG-200904094049117016/providers/Microsoft.ApiManagement/service/acctestAM-200904094049117016/apis/acctestAMA-200904094049117016/diagnostics/applicationinsights", + Expected: &ApiManagementApiDiagnosticId{ + Name: "applicationinsights", + ApiName: "acctestAMA-200904094049117016", + ServiceName: "acctestAM-200904094049117016", + ResourceGroup: "acctestRG-200904094049117016", + }, + }, + } + + for _, v := range testData { + t.Logf("[DEBUG] Testing %q", v.Name) + + actual, err := ApiManagementApiDiagnosticID(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.ApiName != v.Expected.ApiName { + t.Fatalf("Expected %q but got %q for API Name", v.Expected.ApiName, actual.ApiName) + } + + 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) + } + } +} diff --git a/azurerm/internal/services/apimanagement/registration.go b/azurerm/internal/services/apimanagement/registration.go index c3edea311cf6..1c1160925d1e 100644 --- a/azurerm/internal/services/apimanagement/registration.go +++ b/azurerm/internal/services/apimanagement/registration.go @@ -35,6 +35,7 @@ func (r Registration) SupportedResources() map[string]*schema.Resource { return map[string]*schema.Resource{ "azurerm_api_management": resourceArmApiManagementService(), "azurerm_api_management_api": resourceArmApiManagementApi(), + "azurerm_api_management_api_diagnostic": resourceArmApiManagementApiDiagnostic(), "azurerm_api_management_api_operation": resourceArmApiManagementApiOperation(), "azurerm_api_management_api_operation_policy": resourceArmApiManagementApiOperationPolicy(), "azurerm_api_management_api_policy": resourceArmApiManagementApiPolicy(), diff --git a/azurerm/internal/services/apimanagement/tests/api_management_api_diagnostic_resource_test.go b/azurerm/internal/services/apimanagement/tests/api_management_api_diagnostic_resource_test.go new file mode 100644 index 000000000000..391061039507 --- /dev/null +++ b/azurerm/internal/services/apimanagement/tests/api_management_api_diagnostic_resource_test.go @@ -0,0 +1,246 @@ +package tests + +import ( + "fmt" + "testing" + + "github.com/hashicorp/terraform-plugin-sdk/helper/resource" + "github.com/hashicorp/terraform-plugin-sdk/terraform" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/acceptance" + "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/utils" +) + +func TestAccAzureRMApiManagementApiDiagnostic_basic(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_diagnostic", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMApiManagementApiDiagnosticDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMApiManagementApiDiagnostic_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApiManagementApiDiagnosticExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMApiManagementApiDiagnostic_update(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_diagnostic", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMApiManagementApiDiagnosticDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMApiManagementApiDiagnostic_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApiManagementApiDiagnosticExists(data.ResourceName), + ), + }, + data.ImportStep(), + { + Config: testAccAzureRMApiManagementApiDiagnostic_update(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApiManagementApiDiagnosticExists(data.ResourceName), + ), + }, + data.ImportStep(), + }, + }) +} + +func TestAccAzureRMApiManagementApiDiagnostic_requiresImport(t *testing.T) { + data := acceptance.BuildTestData(t, "azurerm_api_management_api_diagnostic", "test") + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { acceptance.PreCheck(t) }, + Providers: acceptance.SupportedProviders, + CheckDestroy: testCheckAzureRMApiManagementApiDiagnosticDestroy, + Steps: []resource.TestStep{ + { + Config: testAccAzureRMApiManagementApiDiagnostic_basic(data), + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMApiManagementApiDiagnosticExists(data.ResourceName), + ), + }, + data.RequiresImportErrorStep(testAccAzureRMApiManagementApiDiagnostic_requiresImport), + }, + }) +} + +func testCheckAzureRMApiManagementApiDiagnosticDestroy(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).ApiManagement.ApiDiagnosticClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + for _, rs := range s.RootModule().Resources { + if rs.Type != "azurerm_api_management_api_diagnostic" { + continue + } + + diagnosticId, err := parse.ApiManagementApiDiagnosticID(rs.Primary.ID) + if err != nil { + return err + } + resp, err := client.Get(ctx, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName, diagnosticId.Name) + + if err != nil { + if !utils.ResponseWasNotFound(resp.Response) { + return err + } + } + + return nil + } + return nil +} + +func testCheckAzureRMApiManagementApiDiagnosticExists(resourceName string) resource.TestCheckFunc { + return func(s *terraform.State) error { + client := acceptance.AzureProvider.Meta().(*clients.Client).ApiManagement.ApiDiagnosticClient + ctx := acceptance.AzureProvider.Meta().(*clients.Client).StopContext + + rs, ok := s.RootModule().Resources[resourceName] + if !ok { + return fmt.Errorf("Not found: %s", resourceName) + } + + diagnosticId, err := parse.ApiManagementApiDiagnosticID(rs.Primary.ID) + if err != nil { + return err + } + + resp, err := client.Get(ctx, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName, diagnosticId.Name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("bad: API Management Diagnostic %q (Resource Group %q / API Management Service %q / API %q) does not exist", diagnosticId.Name, diagnosticId.ResourceGroup, diagnosticId.ServiceName, diagnosticId.ApiName) + } + return fmt.Errorf("bad: Get on apiManagementApiDiagnosticClient: %+v", err) + } + + return nil + } +} + +func testAccAzureRMApiManagementApiDiagnostic_template(data acceptance.TestData) string { + return fmt.Sprintf(` +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_application_insights" "test" { + name = "acctestappinsights-%[1]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + application_type = "web" +} + +resource "azurerm_api_management" "test" { + name = "acctestAM-%[1]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + publisher_name = "pub1" + publisher_email = "pub1@email.com" + sku_name = "Developer_1" +} + +resource "azurerm_api_management_logger" "test" { + name = "acctestapimnglogger-%[1]d" + api_management_name = azurerm_api_management.test.name + resource_group_name = azurerm_resource_group.test.name + + application_insights { + instrumentation_key = azurerm_application_insights.test.instrumentation_key + } +} + +resource "azurerm_api_management_api" "test" { + name = "acctestAMA-%[1]d" + resource_group_name = azurerm_resource_group.test.name + api_management_name = azurerm_api_management.test.name + revision = "1" + display_name = "Test API" + path = "test" + protocols = ["https"] + + import { + content_format = "swagger-link-json" + content_value = "http://conferenceapi.azurewebsites.net/?format=json" + } +} +`, data.RandomInteger, data.Locations.Primary) +} + +func testAccAzureRMApiManagementApiDiagnostic_basic(data acceptance.TestData) string { + config := testAccAzureRMApiManagementApiDiagnostic_template(data) + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_diagnostic" "test" { + identifier = "applicationinsights" + resource_group_name = azurerm_resource_group.test.name + api_management_name = azurerm_api_management.test.name + api_name = azurerm_api_management_api.test.name + api_management_logger_id = azurerm_api_management_logger.test.id +} +`, config) +} + +func testAccAzureRMApiManagementApiDiagnostic_update(data acceptance.TestData) string { + config := testAccAzureRMApiManagementApiDiagnostic_template(data) + return fmt.Sprintf(` +%[1]s + +resource "azurerm_application_insights" "test2" { + name = "acctestappinsightsUpdate-%[2]d" + location = azurerm_resource_group.test.location + resource_group_name = azurerm_resource_group.test.name + application_type = "web" +} + +resource "azurerm_api_management_logger" "test2" { + name = "acctestapimngloggerUpdate-%[2]d" + api_management_name = azurerm_api_management.test.name + resource_group_name = azurerm_resource_group.test.name + + application_insights { + instrumentation_key = azurerm_application_insights.test2.instrumentation_key + } +} + +resource "azurerm_api_management_api_diagnostic" "test" { + identifier = "applicationinsights" + resource_group_name = azurerm_resource_group.test.name + api_management_name = azurerm_api_management.test.name + api_name = azurerm_api_management_api.test.name + api_management_logger_id = azurerm_api_management_logger.test2.id +} +`, config, data.RandomInteger) +} + +func testAccAzureRMApiManagementApiDiagnostic_requiresImport(data acceptance.TestData) string { + template := testAccAzureRMApiManagementApiDiagnostic_basic(data) + return fmt.Sprintf(` +%s + +resource "azurerm_api_management_api_diagnostic" "import" { + identifier = azurerm_api_management_api_diagnostic.test.identifier + resource_group_name = azurerm_api_management_api_diagnostic.test.resource_group_name + api_management_name = azurerm_api_management_api_diagnostic.test.api_management_name + api_name = azurerm_api_management_api.test.name + api_management_logger_id = azurerm_api_management_api_diagnostic.test.api_management_logger_id +} +`, template) +} diff --git a/website/azurerm.erb b/website/azurerm.erb index b9420f547fed..a25ae51bdf86 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -687,6 +687,10 @@ azurerm_api_management_api +
  • + azurerm_api_management_api_diagnostic +
  • +
  • azurerm_api_management_api_operation
  • diff --git a/website/docs/r/api_management_api_diagnostic.html.markdown b/website/docs/r/api_management_api_diagnostic.html.markdown new file mode 100644 index 000000000000..8c3a7e0a32d2 --- /dev/null +++ b/website/docs/r/api_management_api_diagnostic.html.markdown @@ -0,0 +1,109 @@ +--- +subcategory: "API Management" +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_api_management_api_diagnostic" +description: |- + Manages a API Management Service API Diagnostics Logs. +--- + +# azurerm_api_management_api_diagnostic + +Manages a API Management Service API Diagnostics Logs. + +## Example Usage + +```hcl +provider "azurerm" { + features {} +} + +resource "azurerm_resource_group" "example" { + name = "example-resources" + location = "West Europe" +} + +resource "azurerm_application_insights" "example" { + name = "example-appinsights" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + application_type = "web" +} + +resource "azurerm_api_management" "example" { + name = "example-apim" + location = azurerm_resource_group.example.location + resource_group_name = azurerm_resource_group.example.name + publisher_name = "My Company" + publisher_email = "company@terraform.io" + sku_name = "Developer_1" +} + +resource "azurerm_api_management_api" "example" { + name = "example-api" + resource_group_name = azurerm_resource_group.example.name + api_management_name = azurerm_api_management.example.name + revision = "1" + display_name = "Example API" + path = "example" + protocols = ["https"] + + import { + content_format = "swagger-link-json" + content_value = "http://conferenceapi.azurewebsites.net/?format=json" + } +} + +resource "azurerm_api_management_logger" "example" { + name = "example-apimlogger" + api_management_name = azurerm_api_management.example.name + resource_group_name = azurerm_resource_group.example.name + + application_insights { + instrumentation_key = azurerm_application_insights.example.instrumentation_key + } +} + +resource "azurerm_api_management_api_diagnostic" "example" { + resource_group_name = azurerm_resource_group.example.name + api_management_name = azurerm_api_management.example.name + api_name = azurerm_api_management_api.example.name + api_management_logger_id = azurerm_api_management_logger.example.id +} +``` + +## Arguments Reference + +The following arguments are supported: + +* `api_management_logger_id` - (Required) The ID (name) of the Diagnostics Logger. + +* `api_management_name` - (Required) The name of the API Management Service instance. Changing this forces a new API Management Service API Diagnostics Logs to be created. + +* `api_name` - (Required) The name of the API on which to configure the Diagnostics Logs. Changing this forces a new API Management Service API Diagnostics Logs to be created. + +* `identifier` - (Required) Identifier of the Diagnostics Logs. Possible values are `applicationinsights` and `azuremonitor`. Changing this forces a new API Management Service API Diagnostics Logs to be created. + +* `resource_group_name` - (Required) The name of the Resource Group where the API Management Service API Diagnostics Logs should exist. Changing this forces a new API Management Service API Diagnostics Logs to be created. + +## Attributes Reference + +In addition to the Arguments listed above - the following Attributes are exported: + +* `id` - The ID of the API Management Service API Diagnostics Logs. + +## Timeouts + +The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/docs/configuration/resources.html#timeouts) for certain actions: + +* `create` - (Defaults to 30 minutes) Used when creating the API Management Service API Diagnostics Logs. +* `read` - (Defaults to 5 minutes) Used when retrieving the API Management Service API Diagnostics Logs. +* `update` - (Defaults to 30 minutes) Used when updating the API Management Service API Diagnostics Logs. +* `delete` - (Defaults to 30 minutes) Used when deleting the API Management Service API Diagnostics Logs. + +## Import + +API Management Service API Diagnostics Logs can be imported using the `resource id`, e.g. + +```shell +terraform import azurerm_api_management_api_diagnostic.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1/providers/Microsoft.ApiManagement/service/instance1/apis/api1/diagnostics/diagnostic1/loggers/logger1 +```