-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_source.go
126 lines (110 loc) · 3.43 KB
/
resource_source.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"encoding/json"
"fmt"
"github.com/ernesto-arm/go-coveo/sourceapi"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"log"
"strings"
)
func resourceSource() *schema.Resource {
return &schema.Resource{
Create: resourceSourceCreate,
Read: resourceSourceRead,
Update: resourceSourceUpdate,
Delete: resourceSourceDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"type": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"visibility": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"push_enabled": &schema.Schema {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
},
}
}
func resourceSourceCreate(d *schema.ResourceData, m interface{}) error {
log.Printf("Creating resource %s", d.Get("name"))
Source := sourceapi.Source{
Name: d.Get("name").(string),
Type: strings.ToUpper(d.Get("type").(string)),
Visibility: strings.ToUpper(d.Get("visibility").(string)),
Enabled: d.Get("push_enabled").(bool),
}
resp, err := m.(sourceapi.Client).CreateSource(Source)
if err != nil {
return fmt.Errorf("error when trying to create a source %s with message %s", d.Get("name"), err.Error())
}
src := make(map[string]interface{})
err = json.Unmarshal([]byte(resp), &src)
if err != nil {
return fmt.Errorf("error unmarshalling the source response %s", resp)
}
log.Printf("Unmarshal resource %s", src)
d.SetId(src["id"].(string))
_ = d.Set("name", src["name"])
_ = d.Set("type", src["sourceType"])
_ = d.Set("visibility", src["sourceVisibility"])
_ = d.Set("push_enabled", src["pushEnabled"])
return nil
}
func resourceSourceRead(d *schema.ResourceData, m interface{}) error {
// Attempt to read from an upstream API
s, err := m.(sourceapi.Client).ReadSource(d.Id())
if err != nil {
d.SetId("")
return fmt.Errorf("source id %s errored with message %s", d.Id(), err.Error())
}
src := make(map[string]interface{})
err = json.Unmarshal([]byte(s), &src)
if err != nil {
return fmt.Errorf("source id %s errored with message %s", d.Id(), err.Error())
}
_ = d.Set("name", src["name"])
_ = d.Set("type", src["sourceType"])
_ = d.Set("visibility", src["sourceVisibility"])
_ = d.Set("push_enabled", src["pushEnabled"])
return nil
}
func resourceSourceUpdate(d *schema.ResourceData, m interface{}) error {
Source := sourceapi.Source{
Id: d.Id(),
Name: d.Get("name").(string),
Type: strings.ToUpper(d.Get("type").(string)),
Visibility: strings.ToUpper(d.Get("visibility").(string)),
Enabled: d.Get("push_enabled").(bool),
}
resp, err := m.(sourceapi.Client).UpdateSource(d.Id(), Source)
if err != nil {
return fmt.Errorf("error when trying to update a source %s with message %s", d.Id(), err.Error())
}
src := make(map[string]interface{})
err = json.Unmarshal([]byte(resp), &src)
if err != nil {
return fmt.Errorf("error unmarshalling the source %s with message %s", d.Id(), resp)
}
_ = d.Set("name", src["name"])
_ = d.Set("type", src["sourceType"])
_ = d.Set("visibility", src["sourceVisibility"])
_ = d.Set("push_enabled", src["pushEnabled"])
return nil
}
func resourceSourceDelete(d *schema.ResourceData, m interface{}) error {
err := m.(sourceapi.Client).DeleteSource(d.Id())
if err != nil {
return fmt.Errorf("error deleting the source %s with message %s", d.Id(), err.Error())
}
d.SetId("")
return nil
}