-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathresource_character.go
213 lines (184 loc) · 5.24 KB
/
resource_character.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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package main
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/opensearch-project/opensearch-go/v2"
"github.com/opensearch-project/opensearch-go/v2/opensearchapi"
)
func resourceMarvelCharacter() *schema.Resource {
return &schema.Resource{
CreateContext: characterCreate,
ReadContext: characterRead,
UpdateContext: characterUpdate,
DeleteContext: characterDelete,
Schema: map[string]*schema.Schema{
fullNameField: {
Type: schema.TypeString,
Required: true,
},
identityField: {
Type: schema.TypeString,
Required: true,
},
knownasField: {
Type: schema.TypeString,
Required: true,
},
typeField: {
Type: schema.TypeString,
Required: true,
ValidateFunc: func(v interface{}, k string) (wrs []string, ers []error) {
var errors []error
var warns []string
value, _ := v.(string)
validType := false
for _, characterType := range characterTypes {
if value == characterType {
validType = true
break
}
}
if !validType {
errors = append(errors, fmt.Errorf("Invalid value for "+
"type. Valid values are: "+
strings.Join(characterTypes, ", ")))
return warns, errors
}
return warns, errors
},
},
},
}
}
func characterCreate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
backendClient := meta.(*opensearch.Client)
marvelCharacter := &MarvelCharacter{
FullName: data.Get(fullNameField).(string),
Identity: data.Get(identityField).(string),
KnownAs: data.Get(knownasField).(string),
Type: data.Get(typeField).(string),
}
bodyContent, _ := json.Marshal(marvelCharacter)
bodyReader := bytes.NewReader(bodyContent)
indexRequest := opensearchapi.IndexRequest{
Index: backendIndex,
Body: bodyReader,
}
indexResponse, err := indexRequest.Do(ctx, backendClient)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure to create character",
Detail: "Reason: " + err.Error(),
})
return diags
}
defer indexResponse.Body.Close()
bodyContent, err = io.ReadAll(indexResponse.Body)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure reading response",
Detail: "Reason: " + err.Error(),
})
return diags
}
backendResponse := &BackendResponse{}
json.Unmarshal(bodyContent, backendResponse)
data.SetId(backendResponse.ID)
return diags
}
func characterRead(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
backendClient := meta.(*opensearch.Client)
documentID := data.Id()
getRequest := opensearchapi.GetRequest{
Index: backendIndex,
DocumentID: documentID,
}
getResponse, err := getRequest.Do(ctx, backendClient)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure to retrieve character",
Detail: "Reason: " + err.Error(),
})
return diags
}
defer getResponse.Body.Close()
bodyContent, err := io.ReadAll(getResponse.Body)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure reading response",
Detail: "Reason: " + err.Error(),
})
return diags
}
backendResponse := &BackendResponse{}
json.Unmarshal(bodyContent, backendResponse)
data.Set(fullNameField, backendResponse.Source.FullName)
data.Set(identityField, backendResponse.Source.Identity)
data.Set(knownasField, backendResponse.Source.KnownAs)
data.Set(typeField, backendResponse.Source.Type)
return diags
}
func characterUpdate(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
backendClient := meta.(*opensearch.Client)
documentID := data.Id()
updateBody := &struct {
Doc MarvelCharacter `json:"doc,omitempty"`
}{
Doc: MarvelCharacter{
FullName: data.Get(fullNameField).(string),
Identity: data.Get(identityField).(string),
KnownAs: data.Get(knownasField).(string),
Type: data.Get(typeField).(string),
},
}
bodyContent, _ := json.Marshal(updateBody)
bodyReader := bytes.NewReader(bodyContent)
updateRequest := opensearchapi.UpdateRequest{
Index: backendIndex,
DocumentID: documentID,
Body: bodyReader,
}
_, err := updateRequest.Do(ctx, backendClient)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure to update character",
Detail: "Reason: " + err.Error(),
})
return diags
}
return diags
}
func characterDelete(ctx context.Context, data *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
backendClient := meta.(*opensearch.Client)
documentID := data.Id()
deleteRequest := opensearchapi.DeleteRequest{
Index: backendIndex,
DocumentID: documentID,
}
_, err := deleteRequest.Do(ctx, backendClient)
if err != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Failure to delete character",
Detail: "Reason: " + err.Error(),
})
return diags
}
data.SetId("")
return diags
}