ROUGH - First Attempt at Site24x7 Provider for Terraform

This is fairly rough at the moment but its the basic structure of a resource within a terraform provider to support Site24x7 API using GO/REST API.

// resource_MonitorGroup.go
package main

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

type Site24x7MonitorGroup struct {
Code int `json:"code"`
Message string `json:"message"`
Data struct {
GroupID string `json:"group_id"`
DisplayName string `json:"display_name"`
Description string `json:"description"`
Monitors []interface{} `json:"monitors"`
} `json:"data"`
}


func resourceMonitorGroup() *schema.Resource {
return &schema.Resource{
Create: resourceMonitorGroupCreate,
Read:   resourceMonitorGroupRead,
Update: resourceMonitorGroupUpdate,
Delete: resourceMonitorGroupDelete,


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


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

},
}
}


func resourceMonitorGroupCreate(d *schema.ResourceData, m interface{}) error {
display_name:= d.Get("display_name").(string)
description:= d.Get("description").(string)

  var data Site24x7MonitorGroup
  url := "https://www.site24x7.com/api/monitor_groups"
  var jsonStr = []byte(`{"display_name": "` + display_name + `","description": "` + description + `"}`)
  req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr))
  req.Header.Set("Authorization", "Zoho-authtoken <add your own key here>")
  req.Header.Set("Content-Type", "application/json")

  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.GroupID))
return nil
}

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

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

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


// main.go
package main

import (
"github.com/hashicorp/terraform/plugin"
"github.com/hashicorp/terraform/terraform"
)


func main() {
plugin.Serve(&plugin.ServeOpts{
ProviderFunc: func() terraform.ResourceProvider {
return Provider()
},
})
}

// provider.go
package main

import (
"github.com/hashicorp/terraform/helper/schema"
)

func Provider() *schema.Provider {
return &schema.Provider{
ResourcesMap: map[string]*schema.Resource{
"site24x7_monitor_group": resourceMonitorGroup(),
},
}
}

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