-
Notifications
You must be signed in to change notification settings - Fork 4.7k
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
Adding Operational Insight Workspace (a.k.a Log Analytics) #331
Changes from 20 commits
78cca50
db9621c
357bc06
ae309e0
d5ebef8
75dc27c
1fe74ea
e86a8a2
c3408b2
2f74b8e
4d4678f
011e38f
c821a98
577502d
3e8c7b5
a578ba2
0cd140a
ea0692e
87b6e9a
9b64611
7c6ffaa
a260e4f
204c2fb
68bec07
01fa92d
5ced761
8e33ee6
57b177f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 TestAccAzureRMOperationalInsightWorkspace_importRequiredOnly(t *testing.T) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! |
||
resourceName := "azurerm_log_analytics.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMOperationalInsightWorkspace_requiredOnly(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMOperationalInsightWorkspaceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccAzureRMOperationalInsightWorkspace_importRetentionInDaysComplete(t *testing.T) { | ||
resourceName := "azurerm_log_analytics.test" | ||
|
||
ri := acctest.RandInt() | ||
config := testAccAzureRMOperationalInsightWorkspace_retentionInDaysComplete(ri, testLocation()) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
CheckDestroy: testCheckAzureRMOperationalInsightWorkspaceDestroy, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: config, | ||
}, | ||
|
||
{ | ||
ResourceName: resourceName, | ||
ImportState: true, | ||
ImportStateVerify: true, | ||
}, | ||
}, | ||
}) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
package azurerm | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (as above) - could we rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. resource_arm_log_analytics_workspace.go instead. :) Done! |
||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"regexp" | ||
|
||
"github.com/Azure/azure-sdk-for-go/arm/operationalinsights" | ||
"github.com/hashicorp/terraform/helper/schema" | ||
"github.com/hashicorp/terraform/helper/validation" | ||
|
||
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" | ||
) | ||
|
||
func resourceArmOperationalInsightWorkspaceService() *schema.Resource { | ||
return &schema.Resource{ | ||
Create: resourceArmOperationalInsightWorkspaceCreateUpdate, | ||
Read: resourceArmOperationalInsightWorkspaceRead, | ||
Update: resourceArmOperationalInsightWorkspaceCreateUpdate, | ||
Delete: resourceArmOperationalInsightWorkspaceDelete, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as above - could we rename this to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
Importer: &schema.ResourceImporter{ | ||
State: schema.ImportStatePassthrough, | ||
}, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
ValidateFunc: validateAzureRmOperationalInsightWorkspaceName, | ||
}, | ||
"location": locationSchema(), | ||
"resource_group_name": resourceGroupNameSchema(), | ||
"workspace_id": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"portal_url": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"sku": { | ||
Type: schema.TypeString, | ||
Required: true, | ||
ForceNew: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add some validation to this for the possible SKU types, i.e.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh! This is good one! I've done! |
||
ValidateFunc: validation.StringInSlice([]string{ | ||
string(operationalinsights.Free), | ||
string(operationalinsights.PerNode), | ||
string(operationalinsights.Premium), | ||
string(operationalinsights.Standalone), | ||
string(operationalinsights.Standard), | ||
string(operationalinsights.Unlimited), | ||
}, true), | ||
DiffSuppressFunc: ignoreCaseDiffSuppressFunc, | ||
}, | ||
"retention_in_days": { | ||
Type: schema.TypeInt, | ||
Optional: true, | ||
Computed: true, | ||
ValidateFunc: validateAzureRmOperationalInsightWorkspaceRetentionInDays, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we should be able to swap this out for:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
}, | ||
"primary_shared_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"secondary_shared_key": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
}, | ||
"tags": tagsSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func resourceArmOperationalInsightWorkspaceCreateUpdate(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).workspacesClient | ||
log.Printf("[INFO] preparing arguments for AzureRM Operational Insight workspace creation.") | ||
|
||
name := d.Get("name").(string) | ||
location := d.Get("location").(string) | ||
resGroup := d.Get("resource_group_name").(string) | ||
|
||
skuName := d.Get("sku").(string) | ||
sku := &operationalinsights.Sku{ | ||
Name: operationalinsights.SkuNameEnum(skuName), | ||
} | ||
|
||
retentionInDays := int32(d.Get("retention_in_days").(int)) | ||
|
||
tags := d.Get("tags").(map[string]interface{}) | ||
|
||
parameters := operationalinsights.Workspace{ | ||
Name: &name, | ||
Location: &location, | ||
Tags: expandTags(tags), | ||
WorkspaceProperties: &operationalinsights.WorkspaceProperties{ | ||
Sku: sku, | ||
RetentionInDays: &retentionInDays, | ||
}, | ||
} | ||
|
||
_, error := client.CreateOrUpdate(resGroup, name, parameters, make(chan struct{})) | ||
err := <-error | ||
if err != nil { | ||
return err | ||
} | ||
|
||
read, err := client.Get(resGroup, name) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if read.ID == nil { | ||
return fmt.Errorf("Cannot read Operational Inight Workspace '%s' (resource group %s) ID", name, resGroup) | ||
} | ||
|
||
d.SetId(*read.ID) | ||
|
||
return resourceArmOperationalInsightWorkspaceRead(d, meta) | ||
|
||
} | ||
|
||
func resourceArmOperationalInsightWorkspaceRead(d *schema.ResourceData, meta interface{}) error { | ||
|
||
client := meta.(*ArmClient).workspacesClient | ||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["workspaces"] | ||
|
||
resp, err := client.Get(resGroup, name) | ||
if err != nil { | ||
if utils.ResponseWasNotFound(resp.Response) { | ||
d.SetId("") | ||
return nil | ||
} | ||
return fmt.Errorf("Error making Read request on AzureRM Operational Insight workspaces '%s': %+v", name, err) | ||
} | ||
|
||
d.Set("name", resp.Name) | ||
d.Set("location", resp.Location) | ||
d.Set("resource_group_name", resGroup) | ||
d.Set("workspace_id", resp.CustomerID) | ||
d.Set("portal_url", resp.PortalURL) | ||
d.Set("sku", resp.Sku.Name) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could we add a check to ensure that the returned SKU object isn't nil here? (this can happen when the Swagger doesn't match the API response, when the SDK is upgraded)
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks, I didn't know that! Good info! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done |
||
d.Set("retention_in_days", resp.RetentionInDays) | ||
|
||
sharedKeys, err := client.GetSharedKeys(resGroup, name) | ||
if err != nil { | ||
log.Printf("[ERROR] Unable to List Shared keys for Operatinal Insight workspaces %s: %+v", name, err) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor / not a blocker There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I remove Operational Insight -> Log Analytics :) |
||
} else { | ||
d.Set("primary_shared_key", sharedKeys.PrimarySharedKey) | ||
d.Set("secondary_shared_key", sharedKeys.SecondarySharedKey) | ||
} | ||
|
||
flattenAndSetTags(d, resp.Tags) | ||
return nil | ||
} | ||
|
||
func resourceArmOperationalInsightWorkspaceDelete(d *schema.ResourceData, meta interface{}) error { | ||
client := meta.(*ArmClient).workspacesClient | ||
|
||
id, err := parseAzureResourceID(d.Id()) | ||
if err != nil { | ||
return err | ||
} | ||
resGroup := id.ResourceGroup | ||
name := id.Path["workspaces"] | ||
|
||
resp, err := client.Delete(resGroup, name) | ||
|
||
if err != nil { | ||
if utils.ResponseWasNotFound(resp) { | ||
return nil | ||
} | ||
|
||
return fmt.Errorf("Error issuing AzureRM delete request for Operational Insight Workspaces '%s': %+v", name, err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func validateAzureRmOperationalInsightWorkspaceName(v interface{}, k string) (ws []string, errors []error) { | ||
value := v.(string) | ||
|
||
r, _ := regexp.Compile("^[A-Za-z0-9][A-Za-z0-9-]+[A-Za-z0-9]$") | ||
if !r.MatchString(value) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I can't see a max length listed in the API Documentation - is there one in the Portal we should add checks for? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Name requirements are shown in the azure portal as a tooltip.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you @piotrgo ! I added the validation with test. |
||
errors = append(errors, fmt.Errorf("Workspace Name can only contain alphabet, number, and '-' charactor. You can not use '-' as the start and end of the name")) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. minor and not a blocker There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Done |
||
} | ||
|
||
length := len(value) | ||
if length > 63 || 4 > length { | ||
errors = append(errors, fmt.Errorf("Workspace Name can only be between 4 and 63 letters")) | ||
} | ||
|
||
return | ||
} | ||
|
||
func validateAzureRmOperationalInsightWorkspaceRetentionInDays(v interface{}, k string) (ws []string, errors []error) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (as above) I think we can remove this function and replace it with the built-in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removed! |
||
value := v.(int) | ||
if value < 30 || value > 730 { | ||
errors = append(errors, fmt.Errorf("The `retention_in_days` can only be between 30 and 730")) | ||
} | ||
return | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
given the resource has been renamed - can we rename this file to
import_arm_log_analytics_workspace_test.go
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done!