Skip to content

Commit

Permalink
r/api_management_logger: supporting requires import
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Mar 28, 2019
1 parent 0e11f9e commit 600ff88
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
17 changes: 16 additions & 1 deletion azurerm/resource_arm_api_management_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/apimanagement/mgmt/2018-01-01/apimanagement"
"github.com/hashicorp/terraform/helper/schema"
"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/helpers/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down Expand Up @@ -100,6 +101,19 @@ func resourceArmApiManagementLoggerCreate(d *schema.ResourceData, meta interface
return fmt.Errorf("Either `eventhub` or `application_insights` is required")
}

if requireResourcesToBeImported && d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, serviceName, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("Error checking for presence of existing Logger %q (API Management Service %q / Resource Group %q): %s", name, serviceName, resourceGroup, err)
}
}

if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_api_management_logger", *existing.ID)
}
}

parameters := apimanagement.LoggerContract{
LoggerContractProperties: &apimanagement.LoggerContractProperties{
IsBuffered: utils.Bool(d.Get("buffered").(bool)),
Expand Down Expand Up @@ -207,8 +221,9 @@ func resourceArmApiManagementLoggerDelete(d *schema.ResourceData, meta interface

id, err := parseAzureResourceID(d.Id())
if err != nil {
return fmt.Errorf("Error parsing API Management Logger ID %q: %+v", d.Id(), err)
return err
}

resourceGroup := id.ResourceGroup
serviceName := id.Path["service"]
name := id.Path["loggers"]
Expand Down
51 changes: 51 additions & 0 deletions azurerm/resource_arm_api_management_logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,39 @@ func TestAccAzureRMApiManagementLogger_basicEventHub(t *testing.T) {
})
}

func TestAccAzureRMApiManagementLogger_requiresImport(t *testing.T) {
if !requireResourcesToBeImported {
t.Skip("Skipping since resources aren't required to be imported")
return
}

resourceName := "azurerm_api_management_logger.test"
ri := tf.AccRandTimeInt()
location := testLocation()

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMApiManagementLoggerDestroy,
Steps: []resource.TestStep{
{
Config: testAccAzureRMApiManagementLogger_basicEventHub(ri, location),
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMApiManagementLoggerExists(resourceName),
resource.TestCheckResourceAttr(resourceName, "buffered", "true"),
resource.TestCheckResourceAttr(resourceName, "eventhub.#", "1"),
resource.TestCheckResourceAttrSet(resourceName, "eventhub.0.name"),
resource.TestCheckResourceAttrSet(resourceName, "eventhub.0.connection_string"),
),
},
{
Config: testAccAzureRMApiManagementLogger_requiresImport(ri, location),
ExpectError: testRequiresImportError("azurerm_api_management_logger"),
},
},
})
}

func TestAccAzureRMApiManagementLogger_basicApplicationInsights(t *testing.T) {
resourceName := "azurerm_api_management_logger.test"
ri := tf.AccRandTimeInt()
Expand Down Expand Up @@ -279,6 +312,24 @@ resource "azurerm_api_management_logger" "test" {
`, rInt, location, rInt, rInt, rInt, rInt)
}

func testAccAzureRMApiManagementLogger_requiresImport(rInt int, location string) string {
template := testAccAzureRMApiManagementLogger_basicEventHub(rInt, location)
return fmt.Sprintf(`
%s
resource "azurerm_api_management_logger" "import" {
name = "${azurerm_api_management_logger.test.name}"
api_management_name = "${azurerm_api_management_logger.test.api_management_name}"
resource_group_name = "${azurerm_api_management_logger.test.resource_group_name}"
eventhub {
name = "${azurerm_eventhub.test.name}"
connection_string = "${azurerm_eventhub_namespace.test.default_primary_connection_string}"
}
}
`, template)
}

func testAccAzureRMApiManagementLogger_basicApplicationInsights(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit 600ff88

Please sign in to comment.