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

Add resource azurerm_log_analytics_workspace_linked_service #2139

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
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 @@ -996,6 +999,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