-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from lstolyarov/azurerm/app-insights-resource
Creating azure app insights resource
- Loading branch information
Showing
14 changed files
with
1,876 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccAzureRMApplicationInsights_importBasicWeb(t *testing.T) { | ||
resourceName := "azurerm_application_insights.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMApplicationInsights_basicWeb(ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMApplicationInsightsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMApplicationInsights_importBasicOther(t *testing.T) { | ||
resourceName := "azurerm_application_insights.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMApplicationInsights_basicWeb(ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMApplicationInsightsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"net/http" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/appinsights" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
) | ||
|
||
func resourceArmApplicationInsights() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmApplicationInsightsCreateOrUpdate, | ||
Read: resourceArmApplicationInsightsRead, | ||
Update: resourceArmApplicationInsightsCreateOrUpdate, | ||
Delete: resourceArmApplicationInsightsDelete, | ||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"resource_group_name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
}, | ||
|
||
"location": locationSchema(), | ||
|
||
"application_type": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
DiffSuppressFunc: ignoreCaseDiffSuppressFunc, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(appinsights.Web), | ||
string(appinsights.Other), | ||
}, true), | ||
}, | ||
|
||
"tags": tagsSchema(), | ||
|
||
"app_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
|
||
"instrumentation_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmApplicationInsightsCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).appInsightsClient | ||
|
||
log.Printf("[INFO] preparing arguments for AzureRM Application Insights creation.") | ||
|
||
name := d.Get("name").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
applicationType := d.Get("application_type").(string) | ||
location := d.Get("location").(string) | ||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
applicationInsightsComponentProperties := appinsights.ApplicationInsightsComponentProperties{ | ||
ApplicationID: &name, | ||
ApplicationType: appinsights.ApplicationType(applicationType), | ||
} | ||
|
||
insightProperties := appinsights.ApplicationInsightsComponent{ | ||
Name: &name, | ||
Location: &location, | ||
Kind: &applicationType, | ||
ApplicationInsightsComponentProperties: &applicationInsightsComponentProperties, | ||
Tags: expandTags(tags), | ||
} | ||
|
||
_, err := client.CreateOrUpdate(resGroup, name, insightProperties) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := client.Get(resGroup, name) | ||
if err != nil { | ||
return err | ||
} | ||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read AzureRM Application Insights '%s' (Resource Group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmApplicationInsightsRead(d, meta) | ||
} | ||
|
||
func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).appInsightsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
log.Printf("[DEBUG] Reading AzureRM Application Insights '%s'", id) | ||
|
||
resGroup := id.ResourceGroup | ||
name := id.Path["components"] | ||
|
||
resp, err := client.Get(resGroup, name) | ||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on AzureRM Application Insights '%s': %+v", name, err) | ||
} | ||
|
||
d.Set("name", name) | ||
d.Set("resource_group_name", resGroup) | ||
d.Set("location", azureRMNormalizeLocation(*resp.Location)) | ||
|
||
if props := resp.ApplicationInsightsComponentProperties; props != nil { | ||
d.Set("application_type", string(props.ApplicationType)) | ||
d.Set("app_id", props.AppID) | ||
d.Set("instrumentation_key", props.InstrumentationKey) | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
|
||
return nil | ||
} | ||
|
||
func resourceArmApplicationInsightsDelete(d *schema.ResourceData, meta interface{}) error { | ||
AppInsightsClient := meta.(*ArmClient).appInsightsClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["components"] | ||
|
||
log.Printf("[DEBUG] Deleting AzureRM Application Insights '%s' (resource group '%s')", name, resGroup) | ||
|
||
resp, err := AppInsightsClient.Delete(resGroup, name) | ||
if err != nil { | ||
if resp.StatusCode == http.StatusNotFound { | ||
return nil | ||
} | ||
return fmt.Errorf("Error issuing AzureRM delete request for Application Insights '%s': %+v", name, err) | ||
} | ||
|
||
return err | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
package azurerm | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
"github.com/hashicorp/terraform/terraform" | ||
) | ||
|
||
func TestAccAzureRMApplicationInsights_basicWeb(t *testing.T) { | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMApplicationInsights_basicWeb(ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMApplicationInsightsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMApplicationInsightsExists("azurerm_application_insights.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMApplicationInsights_basicOther(t *testing.T) { | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMApplicationInsights_basicOther(ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMApplicationInsightsDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
Check: resource.ComposeTestCheckFunc( | ||
testCheckAzureRMApplicationInsightsExists("azurerm_application_insights.test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testCheckAzureRMApplicationInsightsDestroy(s *terraform.State) error { | ||
conn := testAccProvider.Meta().(*ArmClient).appInsightsClient | ||
|
||
for _, rs := range s.RootModule().Resources { | ||
if rs.Type != "azurerm_application_insights" { | ||
continue | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
|
||
resp, err := conn.Get(resourceGroup, name) | ||
|
||
if err != nil { | ||
return nil | ||
} | ||
|
||
if resp.StatusCode != http.StatusNotFound { | ||
return fmt.Errorf("Application Insights still exists:\n%#v", resp.ApplicationInsightsComponentProperties) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func testCheckAzureRMApplicationInsightsExists(name string) resource.TestCheckFunc { | ||
return func(s *terraform.State) error { | ||
// Ensure we have enough information in state to look up in API | ||
rs, ok := s.RootModule().Resources[name] | ||
if !ok { | ||
return fmt.Errorf("Not found: %s", name) | ||
} | ||
|
||
name := rs.Primary.Attributes["name"] | ||
resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] | ||
if !hasResourceGroup { | ||
return fmt.Errorf("Bad: no resource group found in state for App Insights: %s", name) | ||
} | ||
|
||
conn := testAccProvider.Meta().(*ArmClient).appInsightsClient | ||
|
||
resp, err := conn.Get(resourceGroup, name) | ||
if err != nil { | ||
return fmt.Errorf("Bad: Get on appInsightsClient: %s", err) | ||
} | ||
|
||
if resp.StatusCode == http.StatusNotFound { | ||
return fmt.Errorf("Bad: Application Insights '%q' (resource group: '%q') does not exist", name, resourceGroup) | ||
} | ||
|
||
return nil | ||
} | ||
} | ||
|
||
func testAccAzureRMApplicationInsights_basicWeb(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "West Europe" | ||
} | ||
resource "azurerm_application_insights" "test" { | ||
name = "acctestappinsights-%d" | ||
location = "West Europe" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
application_type = "web" | ||
} | ||
`, rInt, rInt) | ||
} | ||
|
||
func testAccAzureRMApplicationInsights_basicOther(rInt int) string { | ||
return fmt.Sprintf(` | ||
resource "azurerm_resource_group" "test" { | ||
name = "acctestRG-%d" | ||
location = "West Europe" | ||
} | ||
resource "azurerm_application_insights" "test" { | ||
name = "acctestappinsights-%d" | ||
location = "West Europe" | ||
resource_group_name = "${azurerm_resource_group.test.name}" | ||
application_type = "other" | ||
} | ||
`, rInt, rInt) | ||
} |
Oops, something went wrong.