Skip to content

Commit

Permalink
Fixed review feedback. Added new basic test & example. Updated docs.
Browse files Browse the repository at this point in the history
  • Loading branch information
lawrencegripper committed Mar 9, 2018
1 parent 051e77c commit 9db4725
Show file tree
Hide file tree
Showing 5 changed files with 145 additions and 84 deletions.
68 changes: 42 additions & 26 deletions azurerm/resource_arm_log_analytics_solution.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package azurerm
import (
"fmt"
"log"
"strings"

"github.com/Azure/azure-sdk-for-go/services/operationsmanagement/mgmt/2015-11-01-preview/operationsmanagement"

Expand All @@ -21,11 +22,6 @@ func resourceArmLogAnalyticsSolution() *schema.Resource {
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Computed: true,
},

"solution_name": {
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -68,11 +64,19 @@ func resourceArmLogAnalyticsSolution() *schema.Resource {
"workspace_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"workspace_resource_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
if strings.ToLower(old) == strings.ToLower(new) {
return true
}
return false
},
},
},
}
Expand Down Expand Up @@ -141,13 +145,33 @@ func resourceArmLogAnalyticsSolutionRead(d *schema.ResourceData, meta interface{
}

if resp.Plan == nil {
return fmt.Errorf("Error making Read request on AzureRM Log Analytics solutions '%s': %+v Plan was nil", name, err)
return fmt.Errorf("Error making Read request on AzureRM Log Analytics solutions '%s': Plan was nil", name)
}

d.Set("name", resp.Name)
d.Set("location", resp.Location)
d.Set("resource_group_name", resGroup)
d.Set("plan", flattenAzureRmLogAnalyticsSolutionPlan(*resp.Plan))

// Reversing the mapping used to get .solution_name
// expecting resp.Name to be in format "SolutionName(WorkspaceName)".
if resp.Name != nil && strings.Contains(*resp.Name, "(") {
if parts := strings.Split(*resp.Name, "("); len(parts) == 2 {
d.Set("solution_name", parts[0])
workspaceName := strings.TrimPrefix(parts[1], "(")
workspaceName = strings.TrimSuffix(workspaceName, ")")
d.Set("workspace_name", workspaceName)
} else {
return fmt.Errorf("Error making Read request on AzureRM Log Analytics solutions '%v': isn't in expected format 'Solution(WorkspaceName)'", resp.Name)
}
} else {
return fmt.Errorf("Error making Read request on AzureRM Log Analytics solutions '%v': isn't in expected format 'Solution(WorkspaceName)'", resp.Name)
}

if props := resp.Properties; props != nil {
d.Set("workspace_resource_id", props.WorkspaceResourceID)
}
if plan := resp.Plan; plan != nil {
d.Set("plan", flattenAzureRmLogAnalyticsSolutionPlan(*resp.Plan))
}
return nil
}

Expand Down Expand Up @@ -178,25 +202,16 @@ func expandAzureRmLogAnalyticsSolutionPlan(d *schema.ResourceData) operationsman
plans := d.Get("plan").([]interface{})
plan := plans[0].(map[string]interface{})

expandedPlan := operationsmanagement.SolutionPlan{}

if name := plan["name"].(string); len(name) > 0 {
expandedPlan.Name = &name
}

if publisher := plan["publisher"].(string); len(publisher) > 0 {
expandedPlan.Publisher = &publisher
}

if promotionCode := plan["promotion_code"].(string); len(promotionCode) > 0 {
expandedPlan.PromotionCode = &promotionCode
} else {
blankString := ""
expandedPlan.PromotionCode = &blankString
}
name := plan["name"].(string)
publisher := plan["publisher"].(string)
promotionCode := plan["promotion_code"].(string)
product := plan["product"].(string)

if product := plan["product"].(string); len(product) > 0 {
expandedPlan.Product = &product
expandedPlan := operationsmanagement.SolutionPlan{
Name: utils.String(name),
PromotionCode: utils.String(promotionCode),
Publisher: utils.String(publisher),
Product: utils.String(product),
}

return expandedPlan
Expand All @@ -206,6 +221,7 @@ func flattenAzureRmLogAnalyticsSolutionPlan(plan operationsmanagement.SolutionPl
plans := make([]interface{}, 0)
values := make(map[string]interface{})

values["name"] = plan.Name
values["product"] = *plan.Product
values["promotion_code"] = *plan.PromotionCode
values["publisher"] = *plan.Publisher
Expand Down
68 changes: 49 additions & 19 deletions azurerm/resource_arm_log_analytics_solution_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,27 @@ import (
"github.com/hashicorp/terraform/terraform"
)

func TestAccAzureRMLogAnalyticsSolution(t *testing.T) {
func TestAccAzureRMLogAnalyticsSolution_basic_containerMonitoring(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsSolution(ri, testLocation())
config := testAccAzureRMLogAnalyticsSolution_containerMonitoring(ri, testLocation())

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMLogAnalyticsSolutionDestroy,
Steps: []resource.TestStep{
{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMLogAnalyticsSolutionExists("azurerm_log_analytics_solution.test"),
),
},
},
})
}

func TestAccAzureRMLogAnalyticsSolution_basic_security(t *testing.T) {
ri := acctest.RandInt()
config := testAccAzureRMLogAnalyticsSolution_security(ri, testLocation())

resource.Test(t, resource.TestCase{
Providers: testAccProviders,
Expand Down Expand Up @@ -84,7 +102,7 @@ func testCheckAzureRMLogAnalyticsSolutionExists(name string) resource.TestCheckF
}
}

func testAccAzureRMLogAnalyticsSolution(rInt int, location string) string {
func testAccAzureRMLogAnalyticsSolution_containerMonitoring(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "oms-acctestRG-%d"
Expand Down Expand Up @@ -113,19 +131,31 @@ resource "azurerm_log_analytics_solution" "test" {
`, rInt, location, rInt)
}

// func testAccAzureRMLogAnalyticsSolution_Temp(rInt int, location string) string {
// return fmt.Sprintf(`
// resource "azurerm_log_analytics_solution" "solution" {
// name = "acctest"
// location = "westeurope"
// resource_group_name = "lg-terraformtest"
// workspace_resource_id = "/subscriptions/5774ad8f-d51e-4456-a72e-0447910568d3/resourcegroups/lg-terraformtest/providers/microsoft.operationalinsights/workspaces/lg-testoms"

// plan {
// name = "Containers"
// publisher = "Microsoft"
// product = "OMSGallery/Containers"
// }
// }
// `)
// }
func testAccAzureRMLogAnalyticsSolution_security(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "oms-acctestRG-%d"
location = "%s"
}
resource "azurerm_log_analytics_workspace" "test" {
name = "acctest-dep-%d"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Free"
}
resource "azurerm_log_analytics_solution" "test" {
solution_name = "Security"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
workspace_resource_id = "${azurerm_log_analytics_workspace.test.id}"
workspace_name = "${azurerm_log_analytics_workspace.test.name}"
plan {
publisher = "Microsoft"
product = "OMSGallery/Security"
}
}
`, rInt, location, rInt)
}
35 changes: 24 additions & 11 deletions examples/log-analytics-container-monitoring/main.tf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
resource "azurerm_resource_group" "test" {
name = "k8s-log-analytics-test"
location = "westeurope"
name = "k8s-log-analytics-test"
location = "westeurope"
}

resource "random_id" "workspace" {
Expand All @@ -11,23 +11,36 @@ resource "random_id" "workspace" {

byte_length = 8
}

resource "azurerm_log_analytics_workspace" "test" {
name = "k8s-workspace-${random_id.workspace.hex}"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Free"
name = "k8s-workspace-${random_id.workspace.hex}"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Free"
}

resource "azurerm_log_analytics_solution" "test" {
solution_name = "Containers"
solution_name = "Containers"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
workspace_resource_id = "${azurerm_log_analytics_workspace.test.id}"
workspace_name = "${azurerm_log_analytics_workspace.test.name}"

plan {
publisher = "Microsoft"
product = "OMSGallery/Containers"
}
}

resource "azurerm_log_analytics_solution" "test2" {
solution_name = "Security"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
workspace_resource_id = "${azurerm_log_analytics_workspace.test.id}"
workspace_name = "${azurerm_log_analytics_workspace.test.name}"

plan {
publisher = "Microsoft"
product = "OMSGallery/Containers"
product = "OMSGallery/Security"
}
}
}
4 changes: 3 additions & 1 deletion vendor/vendor.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,9 @@
"checksumSHA1": "WMfs+FCE3stapwrAvAS3fjFZlHk=",
"path": "github.com/Azure/azure-sdk-for-go/services/operationsmanagement/mgmt/2015-11-01-preview/operationsmanagement",
"revision": "21b68149ccf7c16b3f028bb4c7fd0ab458fe308f",
"revisionTime": "2018-02-12T16:31:56Z"
"revisionTime": "2018-02-16T17:41:56Z",
"version": "v12.5.0-beta",
"versionExact": "v12.5.0-beta"
},
{
"checksumSHA1": "qOcKrxfayIRgAKlAZu9XYTSfeA0=",
Expand Down
54 changes: 27 additions & 27 deletions website/docs/r/log_analytics_solution.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ Creates a new Log Analytics (formally Operational Insights) Solution.

```hcl
resource "azurerm_resource_group" "test" {
name = "k8s-log-analytics-test"
location = "westeurope"
name = "k8s-log-analytics-test"
location = "westeurope"
}
resource "random_id" "workspace" {
Expand All @@ -26,50 +26,50 @@ resource "random_id" "workspace" {
byte_length = 8
}
resource "azurerm_log_analytics_workspace" "test" {
name = "k8s-workspace-${random_id.workspace.hex}"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Free"
name = "k8s-workspace-${random_id.workspace.hex}"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
sku = "Free"
}
resource "azurerm_log_analytics_solution" "test" {
solution_name = "Containers"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
workspace_resource_id = "${azurerm_log_analytics_workspace.test.id}"
workspace_name = "${azurerm_log_analytics_workspace.test.name}"
plan {
publisher = "Microsoft"
product = "OMSGallery/Containers"
}
solution_name = "Containers"
location = "${azurerm_resource_group.test.location}"
resource_group_name = "${azurerm_resource_group.test.name}"
workspace_resource_id = "${azurerm_log_analytics_workspace.test.id}"
workspace_name = "${azurerm_log_analytics_workspace.test.name}"
plan {
publisher = "Microsoft"
product = "OMSGallery/Containers"
}
}
```

## Argument Reference

The following arguments are supported:

* `solution_name` - (Required) Specifies the name of the solution to be deployed. See [here for options](https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-add-solutions). Note: Resource tested with only `Container` solution. Changing this forces a new resource to be created.
* `solution_name` - (Required) Specifies the name of the solution to be deployed. See [here for options](https://docs.microsoft.com/en-us/azure/log-analytics/log-analytics-add-solutions).Changing this forces a new resource to be created.

* `resource_group_name` - (Required) The name of the resource group in which the Log Analytics solution is created. Changing this forces a new resource to be created. Note: The solution and it's related workspace can only exist in the same resource group.

* `location` - (Required) Specifies the supported Azure location where the resource exists. Changing this forces a new resource to be created.

* `workspace_resource_id` - (Required) The full resource ID of the Log Analytics workspace with which the solution will be linked. For example: `/subscriptions/00000000-0000-0000-0000-00000000/resourcegroups/examplegroupname/providers/Microsoft.OperationalInsights/workspaces/exampleWorkspaceName`
* `workspace_resource_id` - (Required) The full resource ID of the Log Analytics workspace with which the solution will be linked. Changing this forces a new resource to be created.

* `workspace_resource_name` - (Required) The full name of the Log Analytics workspace with which the solution will be linked. For example: `exampleWorkspaceName`
* `workspace_resource_name` - (Required) The full name of the Log Analytics workspace with which the solution will be linked. Changing this forces a new resource to be created.

* `plan.publisher` - (Required) The publisher of the solution. For example `Microsoft`. Changing this forces a new resource to be created.
* `plan` - A `plan` block as documented below.

* `plan.product` - (Required) The product name of the solution. For example `OMSGallery/Containers`. Changing this forces a new resource to be created.
---

* `plan.promotion_code` - (Optional) A promotion code to be used with the solution.
A `plan` block includes:

## Attributes Reference
* `publisher` - (Required) The publisher of the solution. For example `Microsoft`. Changing this forces a new resource to be created.

The following attributes are exported:
* `product` - (Required) The product name of the solution. For example `OMSGallery/Containers`. Changing this forces a new resource to be created.

* `name` and `plan.name` - These are identical and are generated from the `plan.product` and the `workspace_resource_name`.
* `promotion_code` - (Optional) A promotion code to be used with the solution.

0 comments on commit 9db4725

Please sign in to comment.