Skip to content

Commit

Permalink
Merge pull request #718 from terraform-providers/sdk-migration-appins…
Browse files Browse the repository at this point in the history
…ights

refactoring appinsights use the new sdk version
  • Loading branch information
tombuildsstuff authored Jan 19, 2018
2 parents ab4e2f9 + aa9fdb5 commit 1eea975
Show file tree
Hide file tree
Showing 10 changed files with 34 additions and 1,682 deletions.
27 changes: 16 additions & 11 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"sync"
"time"

"github.com/Azure/azure-sdk-for-go/arm/appinsights"
"github.com/Azure/azure-sdk-for-go/arm/authorization"
"github.com/Azure/azure-sdk-for-go/arm/automation"
"github.com/Azure/azure-sdk-for-go/arm/compute"
Expand All @@ -27,6 +26,7 @@ import (
"github.com/Azure/azure-sdk-for-go/arm/postgresql"
"github.com/Azure/azure-sdk-for-go/arm/redis"
keyVault "github.com/Azure/azure-sdk-for-go/dataplane/keyvault"
appinsights "github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/Azure/azure-sdk-for-go/services/cdn/mgmt/2017-04-02/cdn"
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2017-09-30/containerservice"
"github.com/Azure/azure-sdk-for-go/services/eventgrid/mgmt/2017-09-15-preview/eventgrid"
Expand Down Expand Up @@ -97,9 +97,6 @@ type ArmClient struct {
dnsClient dns.RecordSetsClient
zonesClient dns.ZonesClient

cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient

containerRegistryClient containerregistry.RegistriesClient
containerServicesClient containerservice.ContainerServicesClient
containerGroupsClient containerinstance.ContainerGroupsClient
Expand All @@ -118,13 +115,18 @@ type ArmClient struct {
keyVaultClient keyvault.VaultsClient
keyVaultManagementClient keyVault.ManagementClient

// Application Insights
appInsightsClient appinsights.ComponentsClient

// Authentication
roleAssignmentsClient authorization.RoleAssignmentsClient
roleDefinitionsClient authorization.RoleDefinitionsClient
servicePrincipalsClient graphrbac.ServicePrincipalsClient

// CDN
cdnProfilesClient cdn.ProfilesClient
cdnEndpointsClient cdn.EndpointsClient

// Databases
mysqlConfigurationsClient mysql.ConfigurationsClient
mysqlDatabasesClient mysql.DatabasesClient
Expand Down Expand Up @@ -526,13 +528,7 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
zo.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.zonesClient = zo

ai := appinsights.NewComponentsClientWithBaseURI(endpoint, c.SubscriptionID)
setUserAgent(&ai.Client)
ai.Authorizer = auth
ai.Sender = sender
ai.SkipResourceProviderRegistration = c.SkipProviderRegistration
client.appInsightsClient = ai

client.registerAppInsightsClient(endpoint, c.SubscriptionID, auth, sender)
client.registerAutomationClients(endpoint, c.SubscriptionID, auth, sender)
client.registerAuthentication(endpoint, graphEndpoint, c.SubscriptionID, c.TenantID, auth, graphAuth, sender)
client.registerCDNClients(endpoint, c.SubscriptionID, auth, sender)
Expand All @@ -553,6 +549,15 @@ func getArmClient(c *authentication.Config) (*ArmClient, error) {
return &client, nil
}

func (c *ArmClient) registerAppInsightsClient(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
ai := appinsights.NewComponentsClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&ai.Client)
ai.Authorizer = auth
ai.Sender = sender
ai.SkipResourceProviderRegistration = c.skipProviderRegistration
c.appInsightsClient = ai
}

func (c *ArmClient) registerAutomationClients(endpoint, subscriptionId string, auth autorest.Authorizer, sender autorest.Sender) {
accountClient := automation.NewAccountClientWithBaseURI(endpoint, subscriptionId)
setUserAgent(&accountClient.Client)
Expand Down
25 changes: 14 additions & 11 deletions azurerm/resource_arm_application_insights.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"log"
"net/http"

"github.com/Azure/azure-sdk-for-go/arm/appinsights"
"github.com/Azure/azure-sdk-for-go/services/appinsights/mgmt/2015-05-01/insights"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down Expand Up @@ -38,8 +38,8 @@ func resourceArmApplicationInsights() *schema.Resource {
ForceNew: true,
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
ValidateFunc: validation.StringInSlice([]string{
string(appinsights.Web),
string(appinsights.Other),
string(insights.Web),
string(insights.Other),
}, true),
},

Expand All @@ -60,6 +60,7 @@ func resourceArmApplicationInsights() *schema.Resource {

func resourceArmApplicationInsightsCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appInsightsClient
ctx := meta.(*ArmClient).StopContext

log.Printf("[INFO] preparing arguments for AzureRM Application Insights creation.")

Expand All @@ -69,25 +70,25 @@ func resourceArmApplicationInsightsCreateOrUpdate(d *schema.ResourceData, meta i
location := d.Get("location").(string)
tags := d.Get("tags").(map[string]interface{})

applicationInsightsComponentProperties := appinsights.ApplicationInsightsComponentProperties{
applicationInsightsComponentProperties := insights.ApplicationInsightsComponentProperties{
ApplicationID: &name,
ApplicationType: appinsights.ApplicationType(applicationType),
ApplicationType: insights.ApplicationType(applicationType),
}

insightProperties := appinsights.ApplicationInsightsComponent{
insightProperties := insights.ApplicationInsightsComponent{
Name: &name,
Location: &location,
Kind: &applicationType,
ApplicationInsightsComponentProperties: &applicationInsightsComponentProperties,
Tags: expandTags(tags),
}

_, err := client.CreateOrUpdate(resGroup, name, insightProperties)
_, err := client.CreateOrUpdate(ctx, resGroup, name, insightProperties)
if err != nil {
return err
}

read, err := client.Get(resGroup, name)
read, err := client.Get(ctx, resGroup, name)
if err != nil {
return err
}
Expand All @@ -102,6 +103,7 @@ func resourceArmApplicationInsightsCreateOrUpdate(d *schema.ResourceData, meta i

func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).appInsightsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -113,7 +115,7 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}
resGroup := id.ResourceGroup
name := id.Path["components"]

resp, err := client.Get(resGroup, name)
resp, err := client.Get(ctx, resGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
Expand All @@ -138,7 +140,8 @@ func resourceArmApplicationInsightsRead(d *schema.ResourceData, meta interface{}
}

func resourceArmApplicationInsightsDelete(d *schema.ResourceData, meta interface{}) error {
AppInsightsClient := meta.(*ArmClient).appInsightsClient
client := meta.(*ArmClient).appInsightsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
Expand All @@ -149,7 +152,7 @@ func resourceArmApplicationInsightsDelete(d *schema.ResourceData, meta interface

log.Printf("[DEBUG] Deleting AzureRM Application Insights '%s' (resource group '%s')", name, resGroup)

resp, err := AppInsightsClient.Delete(resGroup, name)
resp, err := client.Delete(ctx, resGroup, name)
if err != nil {
if resp.StatusCode == http.StatusNotFound {
return nil
Expand Down
6 changes: 4 additions & 2 deletions azurerm/resource_arm_application_insights_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func TestAccAzureRMApplicationInsights_basicOther(t *testing.T) {

func testCheckAzureRMApplicationInsightsDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).appInsightsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_application_insights" {
Expand All @@ -61,7 +62,7 @@ func testCheckAzureRMApplicationInsightsDestroy(s *terraform.State) error {
name := rs.Primary.Attributes["name"]
resourceGroup := rs.Primary.Attributes["resource_group_name"]

resp, err := conn.Get(resourceGroup, name)
resp, err := conn.Get(ctx, resourceGroup, name)

if err != nil {
return nil
Expand Down Expand Up @@ -90,8 +91,9 @@ func testCheckAzureRMApplicationInsightsExists(name string) resource.TestCheckFu
}

conn := testAccProvider.Meta().(*ArmClient).appInsightsClient
ctx := testAccProvider.Meta().(*ArmClient).StopContext

resp, err := conn.Get(resourceGroup, name)
resp, err := conn.Get(ctx, resourceGroup, name)
if err != nil {
return fmt.Errorf("Bad: Get on appInsightsClient: %+v", err)
}
Expand Down
53 changes: 0 additions & 53 deletions vendor/github.com/Azure/azure-sdk-for-go/arm/appinsights/client.go

This file was deleted.

Loading

0 comments on commit 1eea975

Please sign in to comment.