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 9 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
56 changes: 56 additions & 0 deletions azurerm/import_arm_log_analytics_linked_service_test.go
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 TestAccAzureRMLogAnalyticsLinkedService_importRequiredOnly(t *testing.T) {
draggeta marked this conversation as resolved.
Show resolved Hide resolved
resourceName := "azurerm_log_analytics_linked_service.test"

ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsLinkedServiceRequiredOnly(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMLogAnalyticsLinkedService_importOptionalArguments(t *testing.T) {
resourceName := "azurerm_log_analytics_linked_service.test"

ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsLinkedServiceOptionalArguments(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsLinkedServiceDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_lb_probe": resourceArmLoadBalancerProbe(),
"azurerm_lb_rule": resourceArmLoadBalancerRule(),
"azurerm_local_network_gateway": resourceArmLocalNetworkGateway(),
"azurerm_log_analytics_linked_service": resourceArmLogAnalyticsLinkedService(),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree the name should probably be azurerm_log_analytics_workspace_linked_service, thoughts @tombuildsstuff?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've renamed the resource. Will also change the description up above.

"azurerm_log_analytics_solution": resourceArmLogAnalyticsSolution(),
"azurerm_log_analytics_workspace": resourceArmLogAnalyticsWorkspace(),
"azurerm_logic_app_action_custom": resourceArmLogicAppActionCustom(),
Expand Down
178 changes: 178 additions & 0 deletions azurerm/resource_arm_log_analytics_linked_service.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
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/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmLogAnalyticsLinkedService() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogAnalyticsLinkedServiceCreateUpdate,
Read: resourceArmLogAnalyticsLinkedServiceRead,
Update: resourceArmLogAnalyticsLinkedServiceCreateUpdate,
Delete: resourceArmLogAnalyticsLinkedServiceDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

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

"workspace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
draggeta marked this conversation as resolved.
Show resolved Hide resolved

"linked_service_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "automation",
},
draggeta marked this conversation as resolved.
Show resolved Hide resolved

"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,
},
},
},
},

"tags": tagsSchema(),

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

func resourceArmLogAnalyticsLinkedServiceCreateUpdate(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{
// Name: &name,
draggeta marked this conversation as resolved.
Show resolved Hide resolved
Tags: expandTags(tags),
LinkedServiceProperties: &operationalinsights.LinkedServiceProperties{
ResourceID: &resourceID,
},
}

_, err := client.CreateOrUpdate(ctx, resGroup, workspaceName, lsName, parameters)
if err != nil {
return err
draggeta marked this conversation as resolved.
Show resolved Hide resolved
}

read, err := client.Get(ctx, resGroup, workspaceName, lsName)
if err != nil {
return err
draggeta marked this conversation as resolved.
Show resolved Hide resolved
}

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 resourceArmLogAnalyticsLinkedServiceRead(d, meta)

}

func resourceArmLogAnalyticsLinkedServiceRead(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 := flattenLogAnalyticsLinkedServiceProperties(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 flattenLogAnalyticsLinkedServiceProperties(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 resourceArmLogAnalyticsLinkedServiceDelete(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