Skip to content

Commit

Permalink
Merge pull request #22 from terraform-providers/dashboard_and_dashboa…
Browse files Browse the repository at this point in the history
…rd_groups

added dashboards and dashboard groups resource and import functionality
  • Loading branch information
pyang55 authored Feb 2, 2020
2 parents 40798b9 + fd376ea commit 6454fe3
Show file tree
Hide file tree
Showing 23 changed files with 1,221 additions and 51 deletions.
14 changes: 11 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,18 @@
## 1.2.2 (Unreleased)
## 1.2.3 (Unreleased)
## 1.2.2 (February 20, 2020)

FEATURES:
* **New Resource:** `logicmonitor_dashboard` ([#22](https://github.com/terraform-providers/terraform-provider-logicmonitor/issues/22))
* **New Resource:** `logicmonitor_dashboard_group` ([#22](https://github.com/terraform-providers/terraform-provider-logicmonitor/issues/22))
* **Import Functionality:** `logicmonitor_dashboard` ([#22](https://github.com/terraform-providers/terraform-provider-logicmonitor/issues/22))
* **Import Functionality:** `logicmonitor_dashboard_group` ([#22](https://github.com/terraform-providers/terraform-provider-logicmonitor/issues/22))

## 1.2.1 (June 18, 2019)

IMPROVEMENTS:

* Add use LogicMonitor API v2 (#16)
* Add support for Terraform v0.12.0 (#18)
* Add use LogicMonitor API v2 ([#16](https://github.com/terraform-providers/terraform-provider-logicmonitor/issues/16))
* Add support for Terraform v0.12.0 ([#18](https://github.com/terraform-providers/terraform-provider-logicmonitor/issues/18))

## 1.2.0 (April 30, 2018)

Expand Down
96 changes: 96 additions & 0 deletions logicmonitor/data_source_dashboard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package logicmonitor

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/schema"
lmclient "github.com/logicmonitor/lm-sdk-go/client"
"github.com/logicmonitor/lm-sdk-go/client/lm"
)

// terraform data source to look up dashboard groups
func dataSourceFindDashboards() *schema.Resource {
return &schema.Resource{
Read: findDashboards,

Schema: map[string]*schema.Schema{
"filters": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"property": {
Type: schema.TypeString,
Optional: true,
},
"operator": {
Type: schema.TypeString,
Optional: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
},
"custom_property_name": {
Type: schema.TypeString,
Optional: true,
},
"custom_property_value": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"size": {
Type: schema.TypeInt,
Optional: true,
Default: 50,
},
"offset": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
},
}
}

// function to find dashboard with certain filters
func findDashboards(d *schema.ResourceData, m interface{}) error {
client := m.(*lmclient.LMSdkGo)
filter := getFilters(d)
size := int32(d.Get("size").(int))
offset := int32(d.Get("offset").(int))

params := lm.NewGetDashboardListParams()
params.SetFilter(&filter)
params.SetOffset(&offset)
params.SetSize(&size)

//looks for dashboard group
restDashboardGroupPaginationResponse, err := client.LM.GetDashboardList(params)
if err != nil {
return err
}

var dashboardIds []string
for _, d := range restDashboardGroupPaginationResponse.Payload.Items {
log.Printf("Found dashboard with filter %q", filter)
dashboardIds = append(dashboardIds, strconv.Itoa(int(d.ID)))
}

if len(dashboardIds) > 0 {
//comma separated string of device Ids
var ids = strings.Join(dashboardIds, ",")
d.SetId(ids)
} else {
err := fmt.Errorf("Found no dashboard with filter %s", filter)
return err
}
return nil
}
96 changes: 96 additions & 0 deletions logicmonitor/data_source_dashboard_group.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package logicmonitor

import (
"fmt"
"log"
"strconv"
"strings"

"github.com/hashicorp/terraform/helper/schema"
lmclient "github.com/logicmonitor/lm-sdk-go/client"
"github.com/logicmonitor/lm-sdk-go/client/lm"
)

// terraform data source to look up dashboard groups
func dataSourceFindDashboardGroups() *schema.Resource {
return &schema.Resource{
Read: findDashboardGroup,

Schema: map[string]*schema.Schema{
"filters": {
Type: schema.TypeSet,
Optional: true,
ForceNew: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"property": {
Type: schema.TypeString,
Optional: true,
},
"operator": {
Type: schema.TypeString,
Optional: true,
},
"value": {
Type: schema.TypeString,
Optional: true,
},
"custom_property_name": {
Type: schema.TypeString,
Optional: true,
},
"custom_property_value": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"size": {
Type: schema.TypeInt,
Optional: true,
Default: 50,
},
"offset": {
Type: schema.TypeInt,
Optional: true,
Default: 0,
},
},
}
}

// function to find dashboard groups with certain filters
func findDashboardGroup(d *schema.ResourceData, m interface{}) error {
client := m.(*lmclient.LMSdkGo)
filter := getFilters(d)
size := int32(d.Get("size").(int))
offset := int32(d.Get("offset").(int))

params := lm.NewGetDashboardGroupListParams()
params.SetFilter(&filter)
params.SetOffset(&offset)
params.SetSize(&size)

//looks for dashboard group
restDashboardGroupPaginationResponse, err := client.LM.GetDashboardGroupList(params)
if err != nil {
return err
}

var dashboardIds []string
for _, d := range restDashboardGroupPaginationResponse.Payload.Items {
log.Printf("Found dashboard group with filter %q", filter)
dashboardIds = append(dashboardIds, strconv.Itoa(int(d.ID)))
}

if len(dashboardIds) > 0 {
//comma separated string of device Ids
var ids = strings.Join(dashboardIds, ",")
d.SetId(ids)
} else {
err := fmt.Errorf("Found no dashboard groups with filter %s", filter)
return err
}
return nil
}
60 changes: 60 additions & 0 deletions logicmonitor/data_source_dashboard_group_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package logicmonitor

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
lmclient "github.com/logicmonitor/lm-sdk-go/client"
)

func TestAccCheckLogicMonitorDashboardGroupDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: TestAccCheckLogicMonitorDashboardGroupDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDashboardGroupID("data.logicmonitor_dashboard_group.board1"),
),
},
},
})
}

func testAccCheckDashboardGroupID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find logicmonitor dashboard group data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("logicmonitor dashgroup group data source ID not set")
}

client := testAccProvider.Meta().(*lmclient.LMSdkGo)
if err := testDashboardGroupExistsHelper(s, client); err != nil {
return err
}

return nil
}
}

const TestAccCheckLogicMonitorDashboardGroupDataSourceConfig = `
resource "logicmonitor_dashboard_group" "group1" {
name = "NewTestDashboardGroup"
description = "this is a test for creating dashboard groups"
}
data "logicmonitor_dashboard_group" "board1" {
filters {
property = "name"
operator = ":"
value = "${logicmonitor_dashboard_group.group1.name}"
}
}
`
60 changes: 60 additions & 0 deletions logicmonitor/data_source_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package logicmonitor

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
lmclient "github.com/logicmonitor/lm-sdk-go/client"
)

func TestAccCheckLogicMonitorDashboardDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: TestAccCheckLogicMonitorDashboardDataSourceConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckDashboardID("data.logicmonitor_dashboard.board1"),
),
},
},
})
}

func testAccCheckDashboardID(n string) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[n]
if !ok {
return fmt.Errorf("Can't find logicmonitor dashboard data source: %s", n)
}

if rs.Primary.ID == "" {
return fmt.Errorf("logicmonitor dash data source ID not set")
}

client := testAccProvider.Meta().(*lmclient.LMSdkGo)
if err := testDashboardExistsHelper(s, client); err != nil {
return err
}

return nil
}
}

const TestAccCheckLogicMonitorDashboardDataSourceConfig = `
resource "logicmonitor_dashboard" "brd" {
name = "Kobe Doin Work"
description = "Another Kobe Tribute Dashboard"
}
data "logicmonitor_dashboard" "board1" {
filters {
property = "name"
operator = ":"
value = "${logicmonitor_dashboard.brd.name}"
}
}
`
5 changes: 2 additions & 3 deletions logicmonitor/data_source_device_group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import (

func TestAccCheckLogicMonitorDeviceGroupDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testDeviceGroupDestroy,
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccCheckLogicMonitorDeviceGroupDataSourceConfig,
Expand Down
Loading

0 comments on commit 6454fe3

Please sign in to comment.