forked from whitepages/terraform-provider-stingray
-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_action_program.go
100 lines (77 loc) · 2.05 KB
/
resource_action_program.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
99
100
package main
import (
"fmt"
"github.com/atlassian/go-vtm"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceActionProgram() *schema.Resource {
return &schema.Resource{
Create: resourceActionProgramCreate,
Read: resourceActionProgramRead,
Update: resourceActionProgramUpdate,
Delete: resourceActionProgramDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"content": &schema.Schema{
Type: schema.TypeString,
Required: true,
StateFunc: hashString,
},
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}
func resourceActionProgramCreate(d *schema.ResourceData, meta interface{}) error {
err := resourceActionProgramSet(d, meta)
if err != nil {
return err
}
return resourceActionProgramRead(d, meta)
}
func resourceActionProgramRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r, resp, err := c.GetActionProgram(d.Get("name").(string))
if err != nil {
if resp != nil && resp.StatusCode == 404 {
// The resource doesn't exist anymore
d.SetId("")
return nil
}
return fmt.Errorf("Error reading resource: %s", err)
}
d.Set("content", hashString(string(r.Content)))
return nil
}
func resourceActionProgramUpdate(d *schema.ResourceData, meta interface{}) error {
err := resourceActionProgramSet(d, meta)
if err != nil {
return err
}
return resourceActionProgramRead(d, meta)
}
func resourceActionProgramDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r := stingray.NewActionProgram(d.Id())
_, err := c.Delete(r)
if err != nil {
return err
}
return nil
}
func resourceActionProgramSet(d *schema.ResourceData, meta interface{}) error {
c := meta.(*providerConfig).client
r := stingray.NewActionProgram(d.Get("name").(string))
r.Content = []byte(d.Get("content").(string))
_, err := c.Set(r)
if err != nil {
return err
}
d.SetId(d.Get("name").(string))
return nil
}