-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathstatus_update_template_resource.go
117 lines (96 loc) · 3.72 KB
/
status_update_template_resource.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
package provider
import (
"context"
"errors"
"github.com/firehydrant/terraform-provider-firehydrant/firehydrant"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceStatusUpdateTemplate() *schema.Resource {
return &schema.Resource{
CreateContext: createResourceFireHydrantStatusUpdateTemplate,
UpdateContext: updateResourceFireHydrantStatusUpdateTemplate,
ReadContext: readResourceFireHydrantStatusUpdateTemplate,
DeleteContext: deleteResourceFireHydrantStatusUpdateTemplate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"body": {
Type: schema.TypeString,
Required: true,
},
"id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
},
}
}
func readResourceFireHydrantStatusUpdateTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
firehydrantAPIClient := m.(firehydrant.Client)
id := d.Id()
tflog.Debug(ctx, "Read status update template", map[string]interface{}{"id": id})
template, err := firehydrantAPIClient.StatusUpdateTemplates().Get(ctx, id)
if err != nil {
if errors.Is(err, firehydrant.ErrorNotFound) {
tflog.Debug(ctx, "Status update template %s does not exist", map[string]interface{}{"id": id})
d.SetId("")
return nil
}
return diag.Errorf("Error reading status update template %s: %v", id, err)
}
attributes := map[string]interface{}{
"name": template.Name,
"body": template.Body,
"id": template.ID,
}
for key, value := range attributes {
if err := d.Set(key, value); err != nil {
return diag.Errorf("Error setting %s for status update template %s: %v", key, id, err)
}
}
return diag.Diagnostics{}
}
func createResourceFireHydrantStatusUpdateTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
firehydrantAPIClient := m.(firehydrant.Client)
createRequest := firehydrant.CreateStatusUpdateTemplateRequest{
Name: d.Get("name").(string),
Body: d.Get("body").(string),
}
tflog.Debug(ctx, "Create status update template", map[string]interface{}{"id": d.Id()})
statusUpdateTemplateResponse, err := firehydrantAPIClient.StatusUpdateTemplates().Create(ctx, createRequest)
if err != nil {
return diag.Errorf("Error creating status update template %s: %v", d.Id(), err)
}
d.SetId(statusUpdateTemplateResponse.ID)
return readResourceFireHydrantStatusUpdateTemplate(ctx, d, m)
}
func updateResourceFireHydrantStatusUpdateTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
firehydrantAPIClient := m.(firehydrant.Client)
updateRequest := firehydrant.UpdateStatusUpdateTemplateRequest{
Name: d.Get("name").(string),
Body: d.Get("body").(string),
}
tflog.Debug(ctx, "Update status update template", map[string]interface{}{"id": d.Id()})
_, err := firehydrantAPIClient.StatusUpdateTemplates().Update(ctx, d.Id(), updateRequest)
if err != nil {
return diag.Errorf("Error updating status update template %s: %v", d.Id(), err)
}
return readResourceFireHydrantStatusUpdateTemplate(ctx, d, m)
}
func deleteResourceFireHydrantStatusUpdateTemplate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
firehydrantAPIClient := m.(firehydrant.Client)
tflog.Debug(ctx, "Delete status update template", map[string]interface{}{"id": d.Id()})
err := firehydrantAPIClient.StatusUpdateTemplates().Delete(ctx, d.Id())
if err != nil {
return diag.Errorf("Error deleting status update template %s: %v", d.Id(), err)
}
return diag.Diagnostics{}
}