Skip to content

Commit 130e19f

Browse files
author
Lorenz Kästle
committed
Fixing Zammad 6.5 API-Changes
1 parent 6348360 commit 130e19f

File tree

3 files changed

+45
-16
lines changed

3 files changed

+45
-16
lines changed

cmd/root.go

+11-4
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,16 @@ func sendNotification(_ *cobra.Command, _ []string) {
131131

132132
var notificationErr error
133133

134+
foundTicket := false
135+
134136
if len(tickets) > 0 {
135137
// Using the first ticket found for the notification,
136138
// the SearchTickets methods returns the tickets by created_at.
137139
// If no ticket is found the zammad.Ticket type will be empty,
138140
// which can be used to detect if a new ticket needs to be created.
139141
ticket = tickets[0]
142+
143+
foundTicket = true
140144
}
141145

142146
switch notificationType {
@@ -149,7 +153,7 @@ func sendNotification(_ *cobra.Command, _ []string) {
149153
case icingadsl.Problem:
150154
// Opens a new ticket if none exists
151155
// If one exists, adds article to existing ticket
152-
notificationErr = handleProblemNotification(ctx, c, ticket)
156+
notificationErr = handleProblemNotification(ctx, c, ticket.ID, foundTicket)
153157
case icingadsl.Recovery:
154158
// Closes a ticket if one exists
155159
// If ticket is open, adds article to existing ticket
@@ -206,11 +210,10 @@ func createArticleBody(header string) string {
206210

207211
// handleProblemNotification opens a new ticket if none exists,
208212
// If one exists, adds message to existing ticket.
209-
func handleProblemNotification(ctx context.Context, c *client.Client, ticket zammad.Ticket) error {
213+
func handleProblemNotification(ctx context.Context, c *client.Client, ticketID int, ticketExists bool) error {
210214
var err error
211215

212216
a := zammad.Article{
213-
TicketID: ticket.ID,
214217
Subject: "Problem",
215218
Body: createArticleBody("Problem"),
216219
ContentType: "text/html",
@@ -220,8 +223,10 @@ func handleProblemNotification(ctx context.Context, c *client.Client, ticket zam
220223
}
221224

222225
// If a Zammad Ticket exists, add the article to this ticket.
223-
if ticket.ID != 0 {
226+
if ticketExists {
227+
a.TicketID = ticketID
224228
err = c.AddArticleToTicket(ctx, a)
229+
225230
return err
226231
}
227232

@@ -237,6 +242,8 @@ func handleProblemNotification(ctx context.Context, c *client.Client, ticket zam
237242
title.WriteString(" Service: " + cliConfig.IcingaServiceName)
238243
}
239244

245+
ticket := zammad.NewTicket{}
246+
240247
ticket.Title = title.String()
241248
ticket.Group = cliConfig.ZammadGroup
242249
ticket.Customer = cliConfig.ZammadCustomer

internal/api/api.go

+12-7
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,13 @@ const (
1212
// We currently only care about the assets in which the tickets
1313
// are contained
1414
type TicketSearchResult struct {
15-
Assets Assets `json:"assets"`
16-
}
17-
18-
// Assets represents the assets in the search result
19-
type Assets struct {
20-
Tickets map[string]Ticket `json:"Ticket"`
15+
Tickets []Ticket `json:"Ticket"`
2116
}
2217

2318
// Ticket represents a Zammad Ticket
2419
// We use two custom field attributes for the tickets
2520
// icinga_host and icinga_service to track existing tickets
26-
type Ticket struct {
21+
type NewTicket struct {
2722
ID int `json:"id,omitempty"`
2823
Title string `json:"title"`
2924
Group string `json:"group"`
@@ -33,6 +28,16 @@ type Ticket struct {
3328
Article Article `json:"article,omitempty"`
3429
}
3530

31+
type Ticket struct {
32+
ID int `json:"id,omitempty"`
33+
Title string `json:"title"`
34+
GroupID int `json:"group_id"`
35+
CustomerID int `json:"customer_id"`
36+
IcingaHost string `json:"icinga_host"`
37+
IcingaService string `json:"icinga_service"`
38+
ArticleIDs []int `json:"article_ids,omitempty"`
39+
}
40+
3641
// Article represents a Zammad Ticket Article
3742
type Article struct {
3843
TicketID int `json:"ticket_id,omitempty"`

internal/client/client.go

+22-5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"net/http"
1313
"net/url"
1414
"strconv"
15+
"strings"
1516

1617
zammad "github.com/NETWAYS/notify_zammad/internal/api"
1718
)
@@ -52,6 +53,8 @@ func (c *Client) SearchTickets(ctx context.Context, hostname, service string) ([
5253
search.Set("order_by", "desc")
5354
u.RawQuery = search.Encode()
5455

56+
// fmt.Printf("Raw Search query: %s\n", u.RawQuery)
57+
5558
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
5659

5760
if err != nil {
@@ -70,18 +73,32 @@ func (c *Client) SearchTickets(ctx context.Context, hostname, service string) ([
7073
return nil, fmt.Errorf("authentication failed for %s", c.URL.String())
7174
}
7275

73-
var result zammad.TicketSearchResult
76+
var result []zammad.Ticket
77+
78+
buf := new(strings.Builder)
79+
_, err = io.Copy(buf, resp.Body)
80+
// check errors
81+
if err != nil {
82+
return nil, fmt.Errorf("unable to read search results: %w", err)
83+
}
84+
// fmt.Println("received answer: " + buf.String() + "\n")
85+
86+
if buf.String() == "[]" {
87+
tickets := make([]zammad.Ticket, 0)
88+
return tickets, nil
89+
}
7490

75-
err = json.NewDecoder(resp.Body).Decode(&result)
91+
err = json.NewDecoder(strings.NewReader(buf.String())).Decode(&result)
7692

7793
if err != nil {
94+
// Maybe an empty result
7895
return nil, fmt.Errorf("unable to parse search results: %w", err)
7996
}
8097

8198
// We only care about the tickets, thus we create a slice to easier work with them
82-
tickets := make([]zammad.Ticket, 0, len(result.Assets.Tickets))
99+
tickets := make([]zammad.Ticket, 0, len(result))
83100

84-
for _, ticket := range result.Assets.Tickets {
101+
for _, ticket := range result {
85102
// If no service is provided we add the ticket and are done
86103
if service == "" {
87104
tickets = append(tickets, ticket)
@@ -133,7 +150,7 @@ func (c *Client) AddArticleToTicket(ctx context.Context, article zammad.Article)
133150
}
134151

135152
// CreateTicket create a new ticket in Zammad
136-
func (c *Client) CreateTicket(ctx context.Context, ticket zammad.Ticket) error {
153+
func (c *Client) CreateTicket(ctx context.Context, ticket zammad.NewTicket) error {
137154
url := c.URL.JoinPath("/api/v1/tickets")
138155

139156
data, err := json.Marshal(ticket)

0 commit comments

Comments
 (0)