Skip to content
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

azurerm_application_insights_api_key - fix existing key check #23463

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,6 @@ func resourceApplicationInsightsAPIKeyCreate(d *pluginsdk.ResourceData, meta int

var existingAPIKeyList insights.ApplicationInsightsComponentAPIKeyListResult
var existingAPIKeyId *parse.ApiKeyId
var keyId string
existingAPIKeyList, err = client.List(ctx, appInsightsId.ResourceGroup, appInsightsId.Name)
if err != nil {
if !utils.ResponseWasNotFound(existingAPIKeyList.Response) {
Expand All @@ -120,26 +119,12 @@ func resourceApplicationInsightsAPIKeyCreate(d *pluginsdk.ResourceData, meta int
return err
}

existingAppInsightsName := existingAPIKeyId.ComponentName
if appInsightsId.Name == existingAppInsightsName {
keyId = existingAPIKeyId.Name
break
if name == *existingAPIKey.Name {
return tf.ImportAsExistsError("azurerm_application_insights_api_key", existingAPIKeyId.ID())
}
}
}

var existing insights.ApplicationInsightsComponentAPIKey
existing, err = client.Get(ctx, appInsightsId.ResourceGroup, appInsightsId.Name, keyId)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for presence of existing Application Insights API key %q (%s): %s", name, appInsightsId, err)
}
}

if !utils.ResponseWasNotFound(existing.Response) && existingAPIKeyId != nil {
return tf.ImportAsExistsError("azurerm_application_insights_api_key", existingAPIKeyId.ID())
}

linkedReadProperties := expandApplicationInsightsAPIKeyLinkedProperties(d.Get("read_permissions").(*pluginsdk.Set), appInsightsId.ID())
linkedWriteProperties := expandApplicationInsightsAPIKeyLinkedProperties(d.Get("write_permissions").(*pluginsdk.Set), appInsightsId.ID())
if len(*linkedReadProperties) == 0 && len(*linkedWriteProperties) == 0 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,22 @@ func TestAccApplicationInsightsAPIKey_full_permissions(t *testing.T) {
})
}

func TestAccApplicationInsightsAPIKey_multiple_keys(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_application_insights_api_key", "read_key")
r := AppInsightsAPIKey{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.multipleKeys(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
check.That(data.ResourceName).Key("read_permissions.#").HasValue("6"),
),
},
data.ImportStep("api_key"),
})
}

func (t AppInsightsAPIKey) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ApiKeyID(state.Attributes["id"])
if err != nil {
Expand Down Expand Up @@ -161,6 +177,38 @@ resource "azurerm_application_insights_api_key" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, readPerms, writePerms)
}

func (AppInsightsAPIKey) multipleKeys(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}

resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}

resource "azurerm_application_insights" "test" {
name = "acctestappinsights-%d"
location = azurerm_resource_group.test.location
resource_group_name = azurerm_resource_group.test.name
application_type = "web"
}

resource "azurerm_application_insights_api_key" "read_key" {
name = "acctestappinsightsapikeyread-%d"
application_insights_id = azurerm_application_insights.test.id
read_permissions = ["agentconfig", "aggregate", "api", "draft", "extendqueries", "search"]
}

resource "azurerm_application_insights_api_key" "write_key" {
name = "acctestappinsightsapikeywrite-%d"
application_insights_id = azurerm_application_insights.test.id
write_permissions = ["annotations"]
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomInteger)
}

func (AppInsightsAPIKey) requiresImport(data acceptance.TestData, readPerms, writePerms string) string {
template := AppInsightsAPIKey{}.basic(data, readPerms, writePerms)
return fmt.Sprintf(`
Expand Down