github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/builtin/providers/icinga2/resource_icinga2_service.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 resourceIcinga2Service() *schema.Resource { 11 12 return &schema.Resource{ 13 Create: resourceIcinga2ServiceCreate, 14 Read: resourceIcinga2ServiceRead, 15 Delete: resourceIcinga2ServiceDelete, 16 Schema: map[string]*schema.Schema{ 17 "name": &schema.Schema{ 18 Type: schema.TypeString, 19 Required: true, 20 Description: "ServiceName", 21 ForceNew: true, 22 }, 23 "hostname": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 Description: "Hostname", 27 ForceNew: true, 28 }, 29 "check_command": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 Description: "CheckCommand", 33 ForceNew: true, 34 }, 35 }, 36 } 37 } 38 39 func resourceIcinga2ServiceCreate(d *schema.ResourceData, meta interface{}) error { 40 41 client := meta.(*iapi.Server) 42 43 hostname := d.Get("hostname").(string) 44 name := d.Get("name").(string) 45 checkcommand := d.Get("check_command").(string) 46 47 services, err := client.CreateService(name, hostname, checkcommand) 48 if err != nil { 49 return err 50 } 51 52 found := false 53 for _, service := range services { 54 if service.Name == hostname+"!"+name { 55 d.SetId(hostname + "!" + name) 56 found = true 57 } 58 } 59 60 if !found { 61 return fmt.Errorf("Failed to Create Service %s!%s : %s", hostname, name, err) 62 } 63 64 return nil 65 66 } 67 68 func resourceIcinga2ServiceRead(d *schema.ResourceData, meta interface{}) error { 69 70 client := meta.(*iapi.Server) 71 72 hostname := d.Get("hostname").(string) 73 name := d.Get("name").(string) 74 75 services, err := client.GetService(name, hostname) 76 if err != nil { 77 return err 78 } 79 80 found := false 81 for _, service := range services { 82 if service.Name == hostname+"!"+name { 83 d.SetId(hostname + "!" + name) 84 d.Set("hostname", hostname) 85 d.Set("check_command", service.Attrs.CheckCommand) 86 found = true 87 } 88 } 89 90 if !found { 91 return fmt.Errorf("Failed to Read Service %s!%s : %s", hostname, name, err) 92 } 93 94 return nil 95 96 } 97 98 func resourceIcinga2ServiceDelete(d *schema.ResourceData, meta interface{}) error { 99 100 client := meta.(*iapi.Server) 101 102 hostname := d.Get("hostname").(string) 103 name := d.Get("name").(string) 104 105 err := client.DeleteService(name, hostname) 106 if err != nil { 107 return fmt.Errorf("Failed to Delete Service %s!%s : %s", hostname, name, err) 108 } 109 110 return nil 111 }