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

fix - Proposal to resolve Getting HTTP 422 on creation of incident_rule #455

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
31 changes: 19 additions & 12 deletions opsgenie/resource_opsgenie_service_incident_rule.go
Original file line number Diff line number Diff line change
Expand Up @@ -164,19 +164,23 @@ func resourceOpsGenieServiceIncidentRuleCreate(d *schema.ResourceData, meta inte
}

service_id := d.Get("service_id").(string)
createRequest := &service.CreateIncidentRuleRequest{
ServiceId: service_id,
}

incidentRule := service.IncidentRule{}

incident_rule := d.Get("incident_rule").([]interface{})
for _, v := range incident_rule {
config := v.(map[string]interface{})
createRequest.ConditionMatchType = og.ConditionMatchType(config["condition_match_type"].(string))
createRequest.Conditions = expandOpsGenieServiceIncidentRuleConditions(config["conditions"].(*schema.Set))
createRequest.IncidentProperties = expandOpsGenieServiceIncidentRuleIncidentProperties(config["incident_properties"].([]interface{}))
incidentRule.ConditionMatchType = og.ConditionMatchType(config["condition_match_type"].(string))
incidentRule.Conditions = expandOpsGenieServiceIncidentRuleConditions(config["conditions"].(*schema.Set))
incidentRule.IncidentProperties = expandOpsGenieServiceIncidentRuleIncidentProperties(config["incident_properties"].([]interface{}))
}

createRequest := &service.CreateIncidentRuleRequest{
ServiceId: service_id,
IncidentRule: incidentRule,
}

log.Printf("[INFO] Creating OpsGenie Service Incident Rule for service '%s'", d.Get("service_id").(string))
log.Printf("[INFO] Creating OpsGenie Service Incident Rule for service '%s'", service_id)
result, err := client.CreateIncidentRule(context.Background(), createRequest)
if err != nil {
return err
Expand Down Expand Up @@ -207,6 +211,7 @@ func resourceOpsGenieServiceIncidentRuleRead(d *schema.ResourceData, meta interf
d.SetId("")
return nil
}
return err
}

incidentRuleFound := false
Expand Down Expand Up @@ -238,14 +243,15 @@ func resourceOpsGenieServiceIncidentRuleUpdate(d *schema.ResourceData, meta inte
updateRequest := &service.UpdateIncidentRuleRequest{
ServiceId: service_id,
IncidentRuleId: incident_rule_id,
IncidentRule: service.IncidentRule{},
}

incident_rule := d.Get("incident_rule").([]interface{})
for _, v := range incident_rule {
config := v.(map[string]interface{})
updateRequest.ConditionMatchType = og.ConditionMatchType(config["condition_match_type"].(string))
updateRequest.Conditions = expandOpsGenieServiceIncidentRuleConditions(config["conditions"].(*schema.Set))
updateRequest.IncidentProperties = expandOpsGenieServiceIncidentRuleIncidentProperties(config["incident_properties"].([]interface{}))
updateRequest.IncidentRule.ConditionMatchType = og.ConditionMatchType(config["condition_match_type"].(string))
updateRequest.IncidentRule.Conditions = expandOpsGenieServiceIncidentRuleConditions(config["conditions"].(*schema.Set))
updateRequest.IncidentRule.IncidentProperties = expandOpsGenieServiceIncidentRuleIncidentProperties(config["incident_properties"].([]interface{}))
}

log.Printf("[INFO] Updating Service Incident Rule for service: '%s' for rule ID: '%s'", service_id, incident_rule_id)
Expand All @@ -254,14 +260,14 @@ func resourceOpsGenieServiceIncidentRuleUpdate(d *schema.ResourceData, meta inte
return err
}

return nil
return resourceOpsGenieServiceIncidentRuleRead(d, meta)
}

func resourceOpsGenieServiceIncidentRuleDelete(d *schema.ResourceData, meta interface{}) error {
service_id := d.Get("service_id").(string)
incident_rule_id := d.Id()

log.Printf("[INFO] Deleting OpsGenie ervice Incident Rule for service: '%s' for rule ID: '%s'", service_id, incident_rule_id)
log.Printf("[INFO] Deleting OpsGenie Service Incident Rule for service: '%s' for rule ID: '%s'", service_id, incident_rule_id)
client, err := service.NewClient(meta.(*OpsgenieClient).client.Config)
if err != nil {
return err
Expand All @@ -275,6 +281,7 @@ func resourceOpsGenieServiceIncidentRuleDelete(d *schema.ResourceData, meta inte
if err != nil {
return err
}
d.SetId("")
return nil
}

Expand Down

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