Terraform Module for VictorOps Alerts

My first attempt at a basic terraform module written in GO to interface with VictorOps thorugh the VOPS API

resource_alerts.go
This is rough, just to connect the terraform module to generate an alert in VictorOps
Need to clean up the passing of parameters from the test.tf instead of hard coding in GO
But this is easy enough

package main

import (
"github.com/hashicorp/terraform/helper/schema"
  "bytes"
  "encoding/json"
  "net/http"
  "io/ioutil"
)

type VictoropsAlerts struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
MessageType       string `json:"message_type"`
EntityID          string `json:"entity_id"`
EntityDisplayName string `json:"entity_display_name"`
StateMessage      string `json:"state_message"`
} `json:"data"`

}

package main

import (
"github.com/hashicorp/terraform/helper/schema"
  "bytes"
  "encoding/json"
  "net/http"
  "io/ioutil"
)

type VictoropsAlerts struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
MessageType       string `json:"message_type"`
EntityID          string `json:"entity_id"`
EntityDisplayName string `json:"entity_display_name"`
StateMessage      string `json:"state_message"`
} `json:"data"`

}


func resourceAlerts() *schema.Resource {
return &schema.Resource{
Create: resourceAlertsCreate,
Read:   resourceAlertsRead,
Update: resourceAlertsUpdate,
Delete: resourceAlertsDelete,


Schema: map[string]*schema.Schema{
      "display_name": &schema.Schema{
Type:     schema.TypeString,
        Required: true,
},


      "description": &schema.Schema{
Type:     schema.TypeString,
        Optional: true,
},

},
}
}

func resourceAlertsCreate(d *schema.ResourceData, m interface{}) error {
  var data VictoropsAlerts
  url := "https://alert.victorops.com/integrations/generic/20131114/alert/a988edbf-3333-22227-a381-72d6c8e5cd77"
  content_type := "application/json"

  var jsonStr = []byte(`{"message_type":"Critical","entity_display_name":"Error","monitor_name":"QueueThreshold Monitor","monitoring_tool":"MSMQ Monitoring Service","state_message":"QueueThreshold Monitor Exceeded","custom_message":"Counter Messages in Queue has Exceeded 5000","host_name":"server_name","ip_address":"10.10.10.1"}`)

  req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  req.Header.Set("Content-Type", content_type)

  client := &http.Client{}
  resp, err := client.Do(req)
  if err != nil {
        panic(err)
  }
  defer resp.Body.Close()

  body, _ := ioutil.ReadAll(resp.Body)
  err = json.Unmarshal(body, &data)
d.SetId((data.Data.EntityID))
return nil
}

func resourceAlertsRead(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceAlertsUpdate(d *schema.ResourceData, m interface{}) error {
return nil
}

func resourceAlertsDelete(d *schema.ResourceData, m interface{}) error {
return nil
}



Comments

Popular posts from this blog

Basic Send Message to MQ with Java and IBM MQ JMS

Basic Receive Message to MQ with Java and IBM MQ JMS

Creating a simple Alert / Success Message with ASP.NET/VB using Bootstrap