Skip to content

Commit

Permalink
Merge pull request #86 from logicmonitor/DEV-164489-TestLocationAndSt…
Browse files Browse the repository at this point in the history
…ageFix

TestLocation And EscalationChain Stage Fix
  • Loading branch information
lm-madhvi authored May 3, 2024
2 parents b85b9cc + 4ae21c1 commit 3f8f2ad
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 38 deletions.
31 changes: 23 additions & 8 deletions examples/escalation_chain/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,34 @@ resource "logicmonitor_escalation_chain" "my_escalation_chain"{
destinations = [
{
period = [{
week_days = 2
week_days = [2,3]
timezone = "UTC"
start_minutes = 10
end_minutes = 15
}]
stages = [
[
{
type = "Admin"
addr = "[email protected]"
method = "EMAIL"
contact = "78362637"
},
{
type = "Admin"
addr = "[email protected]"
method = "EMAIL"
contact = "78362637"
}],
stages = [{
type = "Admin"
addr = "[email protected]"
method = "EMAIL"
contact = "78362637"
[{
type = "Admin"
addr = "[email protected]"
method = "EMAIL"
contact = "78362637"
}]
type = "timebased"
}
]
type = "timebased"
}
]
name = "Escalation Chain Test"
description = "escalation chain test"
Expand Down
8 changes: 8 additions & 0 deletions examples/website/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,14 @@ resource "logicmonitor_website" "my_website"{
disable_alerting = true
stop_monitoring = true
user_permission = "string"
test_location = [
{
all = false
collector_ids = [1, 2, 3]
collectors = null
smg_ids = [85, 90]
}
]
group_id = 8
individual_sm_alert_enable = false
steps = [
Expand Down
5 changes: 3 additions & 2 deletions logicmonitor/schemata/chain_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ func ChainSchema() map[string]*schema.Schema {

"stages": {
Type: schema.TypeList, //GoType: [][]*Recipient
Elem: &schema.Resource{
Schema: RecipientSchema(),
Elem: &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Resource{Schema: RecipientSchema()},
},
ConfigMode: schema.SchemaConfigModeAttr,
Required: true,
Expand Down
70 changes: 70 additions & 0 deletions logicmonitor/schemata/website_collector_info_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package schemata

import (
"terraform-provider-logicmonitor/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func WebsiteCollectorInfoSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"collector_group_id": {
Type: schema.TypeInt,
Computed: true,
},

"collector_group_name": {
Type: schema.TypeString,
Computed: true,
},

"description": {
Type: schema.TypeString,
Computed: true,
},

"hostname": {
Type: schema.TypeString,
Computed: true,
},

"id": {
Type: schema.TypeInt,
Computed: true,
},

"status": {
Type: schema.TypeString,
Computed: true,
},

}
}

func SetWebsiteCollectorInfoSubResourceData(m []*models.WebsiteCollectorInfo) (d []*map[string]interface{}) {
for _, websiteCollectorInfo := range m {
if websiteCollectorInfo != nil {
properties := make(map[string]interface{})
properties["collector_group_id"] = websiteCollectorInfo.CollectorGroupID
properties["collector_group_name"] = websiteCollectorInfo.CollectorGroupName
properties["description"] = websiteCollectorInfo.Description
properties["hostname"] = websiteCollectorInfo.Hostname
properties["id"] = websiteCollectorInfo.ID
properties["status"] = websiteCollectorInfo.Status
d = append(d, &properties)
}
}
return
}

func WebsiteCollectorInfoModel(d map[string]interface{}) *models.WebsiteCollectorInfo {
// assume that the incoming map only contains the relevant resource data

return &models.WebsiteCollectorInfo {
}
}

func GetWebsiteCollectorInfoPropertyFields() (t []string) {
return []string{
"id",
}
}
82 changes: 82 additions & 0 deletions logicmonitor/schemata/website_location_schema.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package schemata

import (
"terraform-provider-logicmonitor/logicmonitor/utils"
"terraform-provider-logicmonitor/models"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func WebsiteLocationSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
"all": {
Type: schema.TypeBool,
Optional: true,
},

"collector_ids": {
Type: schema.TypeList, //GoType: []int32
Elem: &schema.Schema{
Type: schema.TypeInt,
},
ConfigMode: schema.SchemaConfigModeAttr,
Optional: true,
},

"collectors": {
Type: schema.TypeList, //GoType: []*WebsiteCollectorInfo
Elem: &schema.Resource{
Schema: WebsiteCollectorInfoSchema(),
},
ConfigMode: schema.SchemaConfigModeAttr,
Optional: true,
},

"smg_ids": {
Type: schema.TypeList, //GoType: []int32
Elem: &schema.Schema{
Type: schema.TypeInt,
},
ConfigMode: schema.SchemaConfigModeAttr,
Optional: true,
},

}
}

func SetWebsiteLocationSubResourceData(m []*models.WebsiteLocation) (d []*map[string]interface{}) {
for _, websiteLocation := range m {
if websiteLocation != nil {
properties := make(map[string]interface{})
properties["all"] = websiteLocation.All
properties["collector_ids"] = websiteLocation.CollectorIds
properties["collectors"] = SetWebsiteCollectorInfoSubResourceData(websiteLocation.Collectors)
properties["smg_ids"] = websiteLocation.SmgIds
d = append(d, &properties)
}
}
return
}

func WebsiteLocationModel(d map[string]interface{}) *models.WebsiteLocation {
// assume that the incoming map only contains the relevant resource data
all := d["all"].(bool)
collectorIds := utils.ConvertSetToInt32Slice(d["collectorIds"].([]interface{}))
collectors := d["collectors"].([]*models.WebsiteCollectorInfo)
smgIds := utils.ConvertSetToInt32Slice(d["smgIds"].([]interface{}))

return &models.WebsiteLocation {
All: all,
CollectorIds: collectorIds,
Collectors: collectors,
SmgIds: smgIds,
}
}

func GetWebsiteLocationPropertyFields() (t []string) {
return []string{
"all",
"collector_ids",
"collectors",
"smg_ids",
}
}
22 changes: 22 additions & 0 deletions logicmonitor/schemata/website_schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ func WebsiteSchema() map[string]*schema.Schema {
Optional: true,
},

"test_location": {
Type: schema.TypeList, //GoType: WebsiteLocation
Elem: &schema.Resource{
Schema: WebsiteLocationSchema(),
},
ConfigMode: schema.SchemaConfigModeAttr,
Optional: true,
},

"transition": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -247,6 +256,14 @@ func DataSourceWebsiteSchema() map[string]*schema.Schema {
Optional: true,
},

"test_location": {
Type: schema.TypeList, //GoType: WebsiteLocation
Elem: &schema.Resource{
Schema: WebsiteLocationSchema(),
},
Optional: true,
},

"transition": {
Type: schema.TypeInt,
Optional: true,
Expand Down Expand Up @@ -299,6 +316,7 @@ func SetWebsiteResourceData(d *schema.ResourceData, m *models.Website) {
d.Set("stop_monitoring", m.StopMonitoring)
d.Set("stop_monitoring_by_folder", m.StopMonitoringByFolder)
d.Set("template", m.Template)
d.Set("test_location", SetWebsiteLocationSubResourceData([]*models.WebsiteLocation{m.TestLocation}))
d.Set("transition", m.Transition)
d.Set("type", m.Type)
d.Set("use_default_alert_setting", m.UseDefaultAlertSetting)
Expand Down Expand Up @@ -329,6 +347,7 @@ func SetWebsiteSubResourceData(m []*models.Website) (d []*map[string]interface{}
properties["stop_monitoring"] = website.StopMonitoring
properties["stop_monitoring_by_folder"] = website.StopMonitoringByFolder
properties["template"] = website.Template
properties["test_location"] = SetWebsiteLocationSubResourceData([]*models.WebsiteLocation{website.TestLocation})
properties["transition"] = website.Transition
properties["type"] = website.Type
properties["use_default_alert_setting"] = website.UseDefaultAlertSetting
Expand Down Expand Up @@ -357,6 +376,7 @@ func WebsiteModel(d *schema.ResourceData) *models.Website {
steps := utils.GetPropFromSteps(d, "steps")
stopMonitoring := d.Get("stop_monitoring").(bool)
template := d.Get("template")
testLocation := utils.GetPropFromLocationMap(d, "test_location")
transition := int32(d.Get("transition").(int))
typeVar := d.Get("type").(string)
useDefaultAlertSetting := d.Get("use_default_alert_setting").(bool)
Expand All @@ -380,6 +400,7 @@ func WebsiteModel(d *schema.ResourceData) *models.Website {
Steps: steps,
StopMonitoring: stopMonitoring,
Template: template,
TestLocation: testLocation,
Transition: transition,
Type: &typeVar,
UseDefaultAlertSetting: useDefaultAlertSetting,
Expand All @@ -405,6 +426,7 @@ func GetWebsitePropertyFields() (t []string) {
"steps",
"stop_monitoring",
"template",
"test_location",
"transition",
"type",
"use_default_alert_setting",
Expand Down
54 changes: 43 additions & 11 deletions logicmonitor/utils/helper_functions.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,21 +285,31 @@ func getPropFromDesInterface(r interface{}) (t []*models.Chain) {
return
}
func GetRecipient(d []interface{}) (t [][]*models.Recipient) {
for _, i := range d {
if m, ok := i.(map[string]interface{}); ok {
var addr = m["addr"].(string)
var contact = m["contact"].(string)
var method = m["method"].(string)
var Type = m["type"].(string)

for _, stageData := range d {
stage, ok := stageData.([]interface{})
if !ok {
continue
}
var stageRecipients []*models.Recipient
for _, recipientData := range stage {
recipient, ok := recipientData.(map[string]interface{})
if !ok {
continue
}
var addr = recipient["addr"].(string)
var contact = recipient["contact"].(string)
var method = recipient["method"].(string)
var Type = recipient["type"].(string)

model := &models.Recipient{
Addr: addr,
Addr: addr,
Contact: contact,
Method: &method,
Type: &Type,
Method: &method,
Type: &Type,
}
t = append(t, []*models.Recipient{model})
stageRecipients = append(stageRecipients, model)
}
t = append(t, stageRecipients)
}
return
}
Expand Down Expand Up @@ -474,4 +484,26 @@ func getPropFromDPInterface(r interface{}) (t []*models.DataPoint ) {
}
}
return
}
func GetPropFromLocationMap(d *schema.ResourceData, key string) (t *models.WebsiteLocation) {
if r, ok := d.GetOk(key); ok {
return getPropFromLocInterface(r)
}
return
}
func getPropFromLocInterface(r interface{}) (t *models.WebsiteLocation) {
for _, i := range r.([]interface{}) {
if m, ok := i.(map[string]interface{}); ok {
var smgIds = ConvertSetToInt32Slice(m["smg_ids"])
var collectorIds = ConvertSetToInt32Slice(m["collector_ids"])
var all = m["all"].(bool)
model := &models.WebsiteLocation{
CollectorIds: collectorIds,
SmgIds: smgIds,
All: all,
}
t = model
}
}
return
}
8 changes: 4 additions & 4 deletions models/escalation_chain.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 3f8f2ad

Please sign in to comment.