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

New Resource: azurerm_log_analytics_storage_insights #9014

Merged
merged 20 commits into from
Nov 10, 2020
Merged
Show file tree
Hide file tree
Changes from 5 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
33 changes: 19 additions & 14 deletions azurerm/internal/services/loganalytics/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ import (
)

type Client struct {
DataExportClient *operationalinsights.DataExportsClient
DataSourcesClient *operationalinsights.DataSourcesClient
LinkedServicesClient *operationalinsights.LinkedServicesClient
SavedSearchesClient *operationalinsights.SavedSearchesClient
SharedKeysClient *operationalinsights.SharedKeysClient
SolutionsClient *operationsmanagement.SolutionsClient
WorkspacesClient *operationalinsights.WorkspacesClient
DataExportClient *operationalinsights.DataExportsClient
DataSourcesClient *operationalinsights.DataSourcesClient
LinkedServicesClient *operationalinsights.LinkedServicesClient
SavedSearchesClient *operationalinsights.SavedSearchesClient
SharedKeysClient *operationalinsights.SharedKeysClient
SolutionsClient *operationsmanagement.SolutionsClient
StorageInsightConfigClient *operationalinsights.StorageInsightConfigsClient
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
WorkspacesClient *operationalinsights.WorkspacesClient
}

func NewClient(o *common.ClientOptions) *Client {
Expand All @@ -35,16 +36,20 @@ func NewClient(o *common.ClientOptions) *Client {
SolutionsClient := operationsmanagement.NewSolutionsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId, "Microsoft.OperationsManagement", "solutions", "testing")
o.ConfigureClient(&SolutionsClient.Client, o.ResourceManagerAuthorizer)

StorageInsightConfigClient := operationalinsights.NewStorageInsightConfigsClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&StorageInsightConfigClient.Client, o.ResourceManagerAuthorizer)

LinkedServicesClient := operationalinsights.NewLinkedServicesClientWithBaseURI(o.ResourceManagerEndpoint, o.SubscriptionId)
o.ConfigureClient(&LinkedServicesClient.Client, o.ResourceManagerAuthorizer)

return &Client{
DataExportClient: &DataExportClient,
DataSourcesClient: &DataSourcesClient,
LinkedServicesClient: &LinkedServicesClient,
SavedSearchesClient: &SavedSearchesClient,
SharedKeysClient: &SharedKeysClient,
SolutionsClient: &SolutionsClient,
WorkspacesClient: &WorkspacesClient,
DataExportClient: &DataExportClient,
DataSourcesClient: &DataSourcesClient,
LinkedServicesClient: &LinkedServicesClient,
SavedSearchesClient: &SavedSearchesClient,
SharedKeysClient: &SharedKeysClient,
SolutionsClient: &SolutionsClient,
StorageInsightConfigClient: &StorageInsightConfigClient,
WorkspacesClient: &WorkspacesClient,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,218 @@
package loganalytics

import (
"fmt"
"log"
"strings"
"time"

"github.com/Azure/azure-sdk-for-go/services/preview/operationalinsights/mgmt/2020-03-01-preview/operationalinsights"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/suppress"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/clients"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/parse"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/services/loganalytics/validate"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tags"
azSchema "github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/tf/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/internal/timeouts"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmLogAnalyticsStorageInsightConfig() *schema.Resource {
return &schema.Resource{
Create: resourceArmLogAnalyticsStorageInsightConfigCreateUpdate,
Read: resourceArmLogAnalyticsStorageInsightConfigRead,
Update: resourceArmLogAnalyticsStorageInsightConfigCreateUpdate,
Delete: resourceArmLogAnalyticsStorageInsightConfigDelete,

Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(30 * time.Minute),
Read: schema.DefaultTimeout(5 * time.Minute),
Update: schema.DefaultTimeout(30 * time.Minute),
Delete: schema.DefaultTimeout(30 * time.Minute),
},

Importer: azSchema.ValidateResourceIDPriorToImport(func(id string) error {
_, err := parse.LogAnalyticsStorageInsightConfigID(id)
return err
}),

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.LogAnalyticsStorageInsightConfigName,
},

// must ignore case since API lowercases all returned data
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
"resource_group_name": azure.SchemaResourceGroupNameDiffSuppress(),

"workspace_resource_id": {
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: suppress.CaseDifference,
ValidateFunc: azure.ValidateResourceID,
},

"storage_account_resource_id": {
WodansSon marked this conversation as resolved.
Show resolved Hide resolved
Type: schema.TypeString,
Required: true,
ValidateFunc: azure.ValidateResourceID,
},

"storage_account_key": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Sensitive: true,
ValidateFunc: validate.IsBase64Encoded,
},

"blob_container_names": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Copy link
Collaborator

Choose a reason for hiding this comment

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

could we validate this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I am not sure what the rules are for this field 🙁

},

"table_names": {
Type: schema.TypeSet,
Optional: true,
MinItems: 1,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.NoZeroValues,
},
},

"tags": tags.Schema(),
},
}
}
func resourceArmLogAnalyticsStorageInsightConfigCreateUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).LogAnalytics.StorageInsightConfigClient
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)
storageAccountId := d.Get("storage_account_resource_id").(string)
storageAccountKey := d.Get("storage_account_key").(string)
if len(strings.TrimSpace(storageAccountKey)) < 1 {
return fmt.Errorf("The argument 'storage_account_key' is required, but no definition was found.")
}

workspace, err := parse.LogAnalyticsWorkspaceID(d.Get("workspace_resource_id").(string))
if err != nil {
return err
}

if d.IsNewResource() {
existing, err := client.Get(ctx, resourceGroup, workspace.Name, name)
if err != nil {
if !utils.ResponseWasNotFound(existing.Response) {
return fmt.Errorf("checking for present of existing Log Analytics Storage Insight Config %q (Resource Group %q / workspaceName %q): %+v", name, resourceGroup, workspace.Name, err)
}
}
if existing.ID != nil && *existing.ID != "" {
return tf.ImportAsExistsError("azurerm_log_analytics_storage_insight_config", *existing.ID)
}
}

parameters := operationalinsights.StorageInsight{
StorageInsightProperties: &operationalinsights.StorageInsightProperties{
StorageAccount: expandArmStorageInsightConfigStorageAccount(storageAccountId, storageAccountKey),
},
Tags: tags.Expand(d.Get("tags").(map[string]interface{})),
}

if _, ok := d.GetOk("table_names"); ok {
parameters.StorageInsightProperties.Tables = utils.ExpandStringSlice(d.Get("table_names").(*schema.Set).List())
}

if _, ok := d.GetOk("blob_container_names"); ok {
parameters.StorageInsightProperties.Containers = utils.ExpandStringSlice(d.Get("blob_container_names").(*schema.Set).List())
}

if _, err := client.CreateOrUpdate(ctx, resourceGroup, workspace.Name, name, parameters); err != nil {
return fmt.Errorf("creating/updating Log Analytics Storage Insight Config %q (Resource Group %q / workspaceName %q): %+v", name, resourceGroup, workspace.Name, err)
}

resp, err := client.Get(ctx, resourceGroup, workspace.Name, name)
if err != nil {
return fmt.Errorf("retrieving Log Analytics Storage Insight Config %q (Resource Group %q / workspaceName %q): %+v", name, resourceGroup, workspace.Name, err)
}

if resp.ID == nil || *resp.ID == "" {
return fmt.Errorf("empty or nil ID returned for Log Analytics Storage Insight Config %q (Resource Group %q / workspaceName %q) ID", name, resourceGroup, workspace.Name)
}

d.SetId(*resp.ID)
return resourceArmLogAnalyticsStorageInsightConfigRead(d, meta)
}

func resourceArmLogAnalyticsStorageInsightConfigRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).LogAnalytics.StorageInsightConfigClient
ctx, cancel := timeouts.ForRead(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LogAnalyticsStorageInsightConfigID(d.Id())
if err != nil {
return err
}

// Need to pull this from the config since the API does not return this value
storageAccountKey := d.Get("storage_account_key").(string)

resp, err := client.Get(ctx, id.ResourceGroup, id.WorkspaceName, id.Name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
log.Printf("[INFO] Log Analytics Storage Insight Config %q does not exist - removing from state", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("retrieving Log Analytics Storage Insight Config %q (Resource Group %q / workspaceName %q): %+v", id.Name, id.ResourceGroup, id.WorkspaceName, err)
}

d.Set("name", id.Name)
d.Set("resource_group_name", id.ResourceGroup)
d.Set("workspace_resource_id", id.WorkspaceID)

if props := resp.StorageInsightProperties; props != nil {
d.Set("blob_container_names", utils.FlattenStringSlice(props.Containers))
d.Set("storage_account_resource_id", props.StorageAccount.ID)
d.Set("storage_account_key", storageAccountKey)
d.Set("table_names", utils.FlattenStringSlice(props.Tables))
}

return tags.FlattenAndSet(d, resp.Tags)
}

func resourceArmLogAnalyticsStorageInsightConfigDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).LogAnalytics.StorageInsightConfigClient
ctx, cancel := timeouts.ForDelete(meta.(*clients.Client).StopContext, d)
defer cancel()

id, err := parse.LogAnalyticsStorageInsightConfigID(d.Id())
if err != nil {
return err
}

if _, err := client.Delete(ctx, id.ResourceGroup, id.WorkspaceName, id.Name); err != nil {
return fmt.Errorf("deleting LogAnalytics Storage Insight Config %q (Resource Group %q / workspaceName %q): %+v", id.Name, id.ResourceGroup, id.WorkspaceName, err)
}
return nil
}

func expandArmStorageInsightConfigStorageAccount(id string, key string) *operationalinsights.StorageAccount {
return &operationalinsights.StorageAccount{
ID: utils.String(id),
Key: utils.String(key),
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func TestLogAnalyticsDataExportID(t *testing.T) {
Expected: &LogAnalyticsDataExportId{
ResourceGroup: "resourceGroup1",
WorkspaceName: "workspace1",
WorkspaceID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.OperationalInsights/workspaces/workspace1",
Name: "dataExport1",
},
},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package parse

import (
"fmt"

"github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/azure"
)

type LogAnalyticsStorageInsightConfigId struct {
ResourceGroup string
WorkspaceName string
WorkspaceID string
Name string
}

func LogAnalyticsStorageInsightConfigID(input string) (*LogAnalyticsStorageInsightConfigId, error) {
id, err := azure.ParseAzureResourceID(input)
if err != nil {
return nil, fmt.Errorf("parsing Log Analytics Storage Insight Config ID %q: %+v", input, err)
}

logAnalyticsStorageInsightConfig := LogAnalyticsStorageInsightConfigId{
ResourceGroup: id.ResourceGroup,
}
if logAnalyticsStorageInsightConfig.WorkspaceName, err = id.PopSegment("workspaces"); err != nil {
return nil, err
}
if logAnalyticsStorageInsightConfig.WorkspaceID = fmt.Sprintf("/subscriptions/%s/resourcegroups/%s/providers/%s/workspaces/%s", id.SubscriptionID, id.ResourceGroup, id.Provider, logAnalyticsStorageInsightConfig.WorkspaceName); err != nil {
return nil, fmt.Errorf("formatting Log Analytics Storage Insight Config workspace ID %q", input)
}
if logAnalyticsStorageInsightConfig.Name, err = id.PopSegment("storageInsightConfigs"); err != nil {
if logAnalyticsStorageInsightConfig.Name, err = id.PopSegment("storageinsightconfigs"); err != nil {
return nil, err
}
}
if err := id.ValidateNoEmptySegments(input); err != nil {
return nil, err
}

return &logAnalyticsStorageInsightConfig, nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package parse

import (
"testing"
)

func TestLogAnalyticsStorageInsightConfigID(t *testing.T) {
testData := []struct {
Name string
Input string
Expected *LogAnalyticsStorageInsightConfigId
}{
{
Name: "Empty",
Input: "",
Expected: nil,
},
{
Name: "No Resource Groups Segment",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000",
Expected: nil,
},
{
Name: "No Resource Groups Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/",
Expected: nil,
},
{
Name: "Resource Group ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/foo/",
Expected: nil,
},
{
Name: "Missing StorageInsightConfig Value",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.OperationalInsights/workspaces/workspace1/storageInsightConfigs",
Expected: nil,
},
{
Name: "operationalinsights StorageInsightConfig ID",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.OperationalInsights/workspaces/workspace1/storageInsightConfigs/storageInsight1",
Expected: &LogAnalyticsStorageInsightConfigId{
ResourceGroup: "resourceGroup1",
WorkspaceName: "workspace1",
WorkspaceID: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.OperationalInsights/workspaces/workspace1",
Name: "storageInsight1",
},
},
{
Name: "Wrong Casing",
Input: "/subscriptions/00000000-0000-0000-0000-000000000000/resourcegroups/resourceGroup1/providers/Microsoft.OperationalInsights/workspaces/workspace1/StorageInsightConfigs/storageInsight1",
Expected: nil,
},
}

for _, v := range testData {
t.Logf("[DEBUG] Testing %q..", v.Name)

actual, err := LogAnalyticsStorageInsightConfigID(v.Input)
if err != nil {
if v.Expected == nil {
continue
}
t.Fatalf("Expected a value but got an error: %s", err)
}

if actual.ResourceGroup != v.Expected.ResourceGroup {
t.Fatalf("Expected %q but got %q for ResourceGroup", v.Expected.ResourceGroup, actual.ResourceGroup)
}

if actual.WorkspaceName != v.Expected.WorkspaceName {
t.Fatalf("Expected %q but got %q for WorkspaceName", v.Expected.WorkspaceName, actual.WorkspaceName)
}

if actual.Name != v.Expected.Name {
t.Fatalf("Expected %q but got %q for Name", v.Expected.Name, actual.Name)
}
}
}
Loading