Skip to content
Closed
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
9 changes: 9 additions & 0 deletions builtin/providers/azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"net/http"
"net/http/httputil"

"github.com/Azure/azure-sdk-for-go/arm/applicationinsights"
"github.com/Azure/azure-sdk-for-go/arm/cdn"
"github.com/Azure/azure-sdk-for-go/arm/compute"
"github.com/Azure/azure-sdk-for-go/arm/eventhub"
Expand Down Expand Up @@ -59,6 +60,8 @@ type ArmClient struct {
routeTablesClient network.RouteTablesClient
routesClient network.RoutesClient

applicationInsightsClient applicationinsights.InsightsClient

cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient

Expand Down Expand Up @@ -217,6 +220,12 @@ func (c *Config) getArmClient() (*ArmClient, error) {
agc.Sender = autorest.CreateSender(withRequestLogging())
client.appGatewayClient = agc

aic := applicationinsights.NewInsightsClient(c.SubscriptionID)
setUserAgent(&aic.Client)
aic.Authorizer = spt
aic.Sender = autorest.CreateSender(withRequestLogging())
client.applicationInsightsClient = aic

ehc := eventhub.NewEventHubsClient(c.SubscriptionID)
setUserAgent(&ehc.Client)
ehc.Authorizer = spt
Expand Down
105 changes: 105 additions & 0 deletions builtin/providers/azurerm/import_arm_application_insights_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMApplicationInsights_importWeb(t *testing.T) {
resourceName := "azurerm_application_insights.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMApplicationInsights_web, ri, 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_importWebWithTags(t *testing.T) {
resourceName := "azurerm_application_insights.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMApplicationInsights_webWithTags, ri, 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_importOther(t *testing.T) {
resourceName := "azurerm_application_insights.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMApplicationInsights_other, ri, 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_importOtherWithTags(t *testing.T) {
resourceName := "azurerm_application_insights.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMApplicationInsights_otherWithTags, ri, 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,
},
},
})
}
7 changes: 4 additions & 3 deletions builtin/providers/azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,10 @@ func Provider() terraform.ResourceProvider {

ResourcesMap: map[string]*schema.Resource{
// These resources use the Azure ARM SDK
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),
"azurerm_application_insights": resourceArmApplicationInsights(),
"azurerm_availability_set": resourceArmAvailabilitySet(),
"azurerm_cdn_endpoint": resourceArmCdnEndpoint(),
"azurerm_cdn_profile": resourceArmCdnProfile(),

"azurerm_eventhub": resourceArmEventHub(),
"azurerm_eventhub_namespace": resourceArmEventHubNamespace(),
Expand Down
169 changes: 169 additions & 0 deletions builtin/providers/azurerm/resource_arm_applcation_insights.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
package azurerm

import (
"fmt"
"log"

"net/http"

"strings"

"github.com/Azure/azure-sdk-for-go/arm/applicationinsights"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceArmApplicationInsights() *schema.Resource {
return &schema.Resource{
Create: resourceArmApplicationInsightsCreate,
Read: resourceArmApplicationInsightsRead,
Update: resourceArmApplicationInsightsCreate,
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": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"application_id": {
Type: schema.TypeString,
Required: true,
},

"application_type": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validateApplicationInsightsApplicationType,
},

"app_id": {
Type: schema.TypeString,
Computed: true,
},

"instrumentation_key": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceArmApplicationInsightsCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).applicationInsightsClient
log.Printf("[INFO] preparing arguments for Azure ARM Application Insights creation.")

name := d.Get("name").(string)
location := d.Get("location").(string)
resGroup := d.Get("resource_group_name").(string)

applicationId := d.Get("application_id").(string)
applicationType := d.Get("application_type").(string)

parameters := applicationinsights.Resource{
Location: &location,
Properties: &applicationinsights.Properties{
ApplicationID: &applicationId,
ApplicationType: applicationinsights.ApplicationType(applicationType),
},
Kind: &applicationType, // TODO: make an enum
}

_, err := client.CreateOrUpdate(resGroup, name, parameters)
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 Application Insights instance %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).applicationInsightsClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["components"]

resp, err := client.Get(resGroup, name)
if err != nil {
return fmt.Errorf("Error making Read request on Azure Application Insights instance %s: %s", name, err)
}
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return nil
}

d.Set("name", resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("location", azureRMNormalizeLocation(*resp.Location))

d.Set("application_id", resp.Properties.ApplicationID)
d.Set("application_type", resp.Properties.ApplicationType)

d.Set("app_id", resp.Properties.AppID)
d.Set("instrumentation_key", resp.Properties.InstrumentationKey)

return nil
}

func resourceArmApplicationInsightsDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).applicationInsightsClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
name := id.Path["components"]

resp, err := client.Delete(resGroup, name)

if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error issuing Azure ARM delete request of Application Insights instance '%s': %s", name, err)
}

return nil
}

func validateApplicationInsightsApplicationType(v interface{}, k string) (ws []string, errors []error) {
value := strings.ToLower(v.(string))
skus := map[string]bool{
"web": true,
"other": true,
}

if !skus[value] {
errors = append(errors, fmt.Errorf("Application Insights Application Type can only be Web or Other"))
}
return
}
Loading