github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/icinga2/resource_icinga2_checkcommand.go (about) 1 package icinga2 2 3 import ( 4 "fmt" 5 6 "github.com/hashicorp/terraform/helper/schema" 7 "github.com/lrsmith/go-icinga2-api/iapi" 8 ) 9 10 func resourceIcinga2Checkcommand() *schema.Resource { 11 12 return &schema.Resource{ 13 Create: resourceIcinga2CheckcommandCreate, 14 Read: resourceIcinga2CheckcommandRead, 15 Delete: resourceIcinga2CheckcommandDelete, 16 Schema: map[string]*schema.Schema{ 17 "name": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 Description: "Name", 21 ForceNew: true, 22 }, 23 "command": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 }, 28 "templates": &schema.Schema{ 29 Type: schema.TypeList, 30 Required: true, 31 ForceNew: true, 32 Elem: &schema.Schema{Type: schema.TypeString}, 33 }, 34 "arguments": &schema.Schema{ 35 Type: schema.TypeMap, 36 Optional: true, 37 ForceNew: true, 38 }, 39 }, 40 } 41 } 42 43 func resourceIcinga2CheckcommandCreate(d *schema.ResourceData, meta interface{}) error { 44 45 client := meta.(*iapi.Server) 46 47 name := d.Get("name").(string) 48 command := d.Get("command").(string) 49 50 arguments := make(map[string]string) 51 iterator := d.Get("arguments").(map[string]interface{}) 52 53 for key, value := range iterator { 54 arguments[key] = value.(string) 55 } 56 57 checkcommands, err := client.CreateCheckcommand(name, command, arguments) 58 if err != nil { 59 return err 60 } 61 62 found := false 63 for _, checkcommand := range checkcommands { 64 if checkcommand.Name == name { 65 d.SetId(name) 66 found = true 67 } 68 } 69 70 if !found { 71 return fmt.Errorf("Failed to create Checkcommand %s : %s", name, err) 72 } 73 74 return nil 75 } 76 77 func resourceIcinga2CheckcommandRead(d *schema.ResourceData, meta interface{}) error { 78 79 client := meta.(*iapi.Server) 80 81 name := d.Get("name").(string) 82 83 checkcommands, err := client.GetCheckcommand(name) 84 if err != nil { 85 return err 86 } 87 88 found := false 89 for _, checkcommand := range checkcommands { 90 if checkcommand.Name == name { 91 d.SetId(name) 92 d.Set("command", checkcommand.Attrs.Command[0]) 93 d.Set("Templates", checkcommand.Attrs.Templates) 94 d.Set("arguments", checkcommand.Attrs.Arguments) 95 found = true 96 } 97 } 98 99 if !found { 100 return fmt.Errorf("Failed to Read Checkcommand %s : %s", name, err) 101 } 102 103 return nil 104 } 105 106 func resourceIcinga2CheckcommandDelete(d *schema.ResourceData, meta interface{}) error { 107 108 client := meta.(*iapi.Server) 109 110 name := d.Get("name").(string) 111 112 err := client.DeleteCheckcommand(name) 113 if err != nil { 114 return fmt.Errorf("Failed to Delete Checkcommand %s : %s", name, err) 115 } 116 117 return nil 118 }