Skip to content

Commit

Permalink
Merge pull request #2139 from draggeta/add-resource-arm-log-analytics…
Browse files Browse the repository at this point in the history
…-linked-services

Add resource azurerm_log_analytics_workspace_linked_service
  • Loading branch information
katbyte authored Oct 27, 2018
2 parents 01186f4 + 6f74329 commit b149da2
Show file tree
Hide file tree
Showing 6 changed files with 531 additions and 2 deletions.
11 changes: 9 additions & 2 deletions azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,7 @@ type ArmClient struct {
eventHubConsumerGroupClient eventhub.ConsumerGroupsClient
eventHubNamespacesClient eventhub.NamespacesClient

workspacesClient operationalinsights.WorkspacesClient
solutionsClient operationsmanagement.SolutionsClient
solutionsClient operationsmanagement.SolutionsClient

redisClient redis.Client
redisFirewallClient redis.FirewallRulesClient
Expand Down Expand Up @@ -197,6 +196,10 @@ type ArmClient struct {
keyVaultClient keyvault.VaultsClient
keyVaultManagementClient keyVault.BaseClient

// Log Analytics
linkedServicesClient operationalinsights.LinkedServicesClient
workspacesClient operationalinsights.WorkspacesClient

// Logic
logicWorkflowsClient logic.WorkflowsClient

Expand Down Expand Up @@ -1007,6 +1010,10 @@ func (c *ArmClient) registerOperationalInsightsClients(endpoint, subscriptionId
solutionsClient := operationsmanagement.NewSolutionsClientWithBaseURI(endpoint, subscriptionId, "Microsoft.OperationsManagement", "solutions", "testing")
c.configureClient(&solutionsClient.Client, auth)
c.solutionsClient = solutionsClient

lsClient := operationalinsights.NewLinkedServicesClientWithBaseURI(endpoint, subscriptionId)
c.configureClient(&lsClient.Client, auth)
c.linkedServicesClient = lsClient
}

func (c *ArmClient) registerRecoveryServiceClients(endpoint, subscriptionId string, auth autorest.Authorizer) {
Expand Down
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
"azurerm_log_analytics_solution": resourceArmLogAnalyticsSolution(),
"azurerm_log_analytics_workspace": resourceArmLogAnalyticsWorkspace(),
"azurerm_log_analytics_workspace_linked_service": resourceArmLogAnalyticsWorkspaceLinkedService(),
"azurerm_logic_app_action_custom": resourceArmLogicAppActionCustom(),
"azurerm_logic_app_action_http": resourceArmLogicAppActionHTTP(),
"azurerm_logic_app_trigger_custom": resourceArmLogicAppTriggerCustom(),
Expand Down
182 changes: 182 additions & 0 deletions azurerm/resource_arm_log_analytics_workspace_linked_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,182 @@
package azurerm

import (
"fmt"
"log"

"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2015-11-01-preview/operationalinsights"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmLogAnalyticsWorkspaceLinkedService() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate,
Read: resourceArmLogAnalyticsWorkspaceLinkedServiceRead,
Update: resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate,
Delete: resourceArmLogAnalyticsWorkspaceLinkedServiceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"resource_group_name": resourceGroupNameDiffSuppressSchema(),

"workspace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validateAzureRmLogAnalyticsWorkspaceName,
},

"linked_service_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "automation",
ValidateFunc: validation.NoZeroValues,
},

"linked_service_properties": {
Type: schema.TypeMap,
Required: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"resource_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: azure.ValidateResourceID,
},
},
},
},

"tags": tagsSchema(),

// Exported properties
"name": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func resourceArmLogAnalyticsWorkspaceLinkedServiceCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).linkedServicesClient
ctx := meta.(*ArmClient).StopContext
log.Printf("[INFO] preparing arguments for AzureRM Log Analytics linked services creation.")

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

workspaceName := d.Get("workspace_name").(string)
lsName := d.Get("linked_service_name").(string)

props := d.Get("linked_service_properties").(map[string]interface{})
resourceID := props["resource_id"].(string)

tags := d.Get("tags").(map[string]interface{})

parameters := operationalinsights.LinkedService{
Tags: expandTags(tags),
LinkedServiceProperties: &operationalinsights.LinkedServiceProperties{
ResourceID: &resourceID,
},
}

_, err := client.CreateOrUpdate(ctx, resGroup, workspaceName, lsName, parameters)
if err != nil {
return fmt.Errorf("Error issuing create request for Log Analytics Workspace Linked Service %q/%q (Resource Group %q): %+v", workspaceName, lsName, resGroup, err)
}

read, err := client.Get(ctx, resGroup, workspaceName, lsName)
if err != nil {
return fmt.Errorf("Error retrieving Analytics Workspace Linked Service %q/%q (Resource Group %q): %+v", workspaceName, lsName, resGroup, err)
}

if read.ID == nil {
return fmt.Errorf("Cannot read Log Analytics Linked Service '%s' (resource group %s) ID", lsName, resGroup)
}

d.SetId(*read.ID)

return resourceArmLogAnalyticsWorkspaceLinkedServiceRead(d, meta)

}

func resourceArmLogAnalyticsWorkspaceLinkedServiceRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).linkedServicesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
workspaceName := id.Path["workspaces"]
lsName := id.Path["linkedservices"]

resp, err := client.Get(ctx, resGroup, workspaceName, lsName)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error making Read request on AzureRM Log Analytics Linked Service '%s': %+v", lsName, err)
}
if resp.ID == nil {
d.SetId("")
return nil
}

d.Set("name", *resp.Name)
d.Set("resource_group_name", resGroup)
d.Set("workspace_name", workspaceName)
d.Set("linked_service_name", lsName)

linkedServiceProperties := flattenLogAnalyticsWorkspaceLinkedServiceProperties(resp.LinkedServiceProperties)
if err := d.Set("linked_service_properties", linkedServiceProperties); err != nil {
return fmt.Errorf("Error flattening Log Analytics Linked Service Properties: %+v", err)
}

flattenAndSetTags(d, resp.Tags)
return nil
}

func flattenLogAnalyticsWorkspaceLinkedServiceProperties(input *operationalinsights.LinkedServiceProperties) interface{} {
properties := make(map[string]interface{}, 0)

// resource id linked service
if resourceID := input.ResourceID; resourceID != nil {
properties["resource_id"] = interface{}(*resourceID)
}

return interface{}(properties)
}

func resourceArmLogAnalyticsWorkspaceLinkedServiceDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).linkedServicesClient
ctx := meta.(*ArmClient).StopContext
id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}
resGroup := id.ResourceGroup
workspaceName := id.Path["workspaces"]
lsName := id.Path["linkedservices"]

resp, err := client.Delete(ctx, resGroup, workspaceName, lsName)

if err != nil {
if utils.ResponseWasNotFound(resp) {
return nil
}

return fmt.Errorf("Error issuing AzureRM delete request for Log Analytics Linked Service '%s': %+v", lsName, err)
}

return nil
}
Loading

0 comments on commit b149da2

Please sign in to comment.