forked from hashicorp/terraform
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_icinga2_hostgroup.go
98 lines (76 loc) · 1.96 KB
/
resource_icinga2_hostgroup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package icinga2
import (
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/lrsmith/go-icinga2-api/iapi"
)
func resourceIcinga2Hostgroup() *schema.Resource {
return &schema.Resource{
Create: resourceIcinga2HostgroupCreate,
Read: resourceIcinga2HostgroupRead,
Delete: resourceIcinga2HostgroupDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "name",
ForceNew: true,
},
"display_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
Description: "Display name of Host Group",
ForceNew: true,
},
},
}
}
func resourceIcinga2HostgroupCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*iapi.Server)
name := d.Get("name").(string)
displayName := d.Get("display_name").(string)
hostgroups, err := client.CreateHostgroup(name, displayName)
if err != nil {
return err
}
found := false
for _, hostgroup := range hostgroups {
if hostgroup.Name == name {
d.SetId(name)
found = true
}
}
if !found {
return fmt.Errorf("Failed to Create Hostgroup %s : %s", name, err)
}
return nil
}
func resourceIcinga2HostgroupRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*iapi.Server)
name := d.Get("name").(string)
hostgroups, err := client.GetHostgroup(name)
if err != nil {
return err
}
found := false
for _, hostgroup := range hostgroups {
if hostgroup.Name == name {
d.SetId(name)
d.Set("display_name", hostgroup.Attrs.DisplayName)
found = true
}
}
if !found {
return fmt.Errorf("Failed to Read Hostgroup %s : %s", name, err)
}
return nil
}
func resourceIcinga2HostgroupDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*iapi.Server)
name := d.Get("name").(string)
err := client.DeleteHostgroup(name)
if err != nil {
return fmt.Errorf("Failed to Delete Hostgroup %s : %s", name, err)
}
return nil
}