github.com/PagerDuty/go-pagerduty@v1.8.0/log_entry.go (about)

     1  package pagerduty
     2  
     3  import (
     4  	"context"
     5  	"encoding/json"
     6  	"fmt"
     7  
     8  	"github.com/google/go-querystring/query"
     9  )
    10  
    11  // Agent is the actor who carried out the action.
    12  type Agent APIObject
    13  
    14  // Channel is the means by which the action was carried out.
    15  type Channel struct {
    16  	Type string
    17  	Raw  map[string]interface{}
    18  }
    19  
    20  // Context are to be included with the trigger such as links to graphs or images.
    21  type Context struct {
    22  	Alt  string
    23  	Href string
    24  	Src  string
    25  	Text string
    26  	Type string
    27  }
    28  
    29  // CommonLogEntryField is the list of shared log entry between Incident and LogEntry
    30  type CommonLogEntryField struct {
    31  	APIObject
    32  	CreatedAt              string            `json:"created_at,omitempty"`
    33  	Agent                  Agent             `json:"agent,omitempty"`
    34  	Channel                Channel           `json:"channel,omitempty"`
    35  	Teams                  []Team            `json:"teams,omitempty"`
    36  	Contexts               []Context         `json:"contexts,omitempty"`
    37  	AcknowledgementTimeout int               `json:"acknowledgement_timeout"`
    38  	EventDetails           map[string]string `json:"event_details,omitempty"`
    39  	Assignees              []APIObject       `json:"assignees,omitempty"`
    40  }
    41  
    42  // LogEntry is a list of all of the events that happened to an incident.
    43  type LogEntry struct {
    44  	CommonLogEntryField
    45  	Incident Incident  `json:"incident"`
    46  	Service  APIObject `json:"service"`
    47  	User     APIObject `json:"user"`
    48  }
    49  
    50  // ListLogEntryResponse is the response data when calling the ListLogEntry API endpoint.
    51  type ListLogEntryResponse struct {
    52  	APIListObject
    53  	LogEntries []LogEntry `json:"log_entries"`
    54  }
    55  
    56  // ListLogEntriesOptions is the data structure used when calling the ListLogEntry API endpoint.
    57  type ListLogEntriesOptions struct {
    58  	// Limit is the pagination parameter that limits the number of results per
    59  	// page. PagerDuty defaults this value to 25 if omitted, and sets an upper
    60  	// bound of 100.
    61  	Limit uint `url:"limit,omitempty"`
    62  
    63  	// Offset is the pagination parameter that specifies the offset at which to
    64  	// start pagination results. When trying to request the next page of
    65  	// results, the new Offset value should be currentOffset + Limit.
    66  	Offset uint `url:"offset,omitempty"`
    67  
    68  	// Total is the pagination parameter to request that the API return the
    69  	// total count of items in the response. If this field is omitted or set to
    70  	// false, the total number of results will not be sent back from the PagerDuty API.
    71  	//
    72  	// Setting this to true will slow down the API response times, and so it's
    73  	// recommended to omit it unless you've a specific reason for wanting the
    74  	// total count of items in the collection.
    75  	Total bool `url:"total,omitempty"`
    76  
    77  	TimeZone   string   `url:"time_zone,omitempty"`
    78  	Since      string   `url:"since,omitempty"`
    79  	Until      string   `url:"until,omitempty"`
    80  	IsOverview bool     `url:"is_overview,omitempty"`
    81  	Includes   []string `url:"include,omitempty,brackets"`
    82  	TeamIDs    []string `url:"team_ids,omitempty,brackets"`
    83  }
    84  
    85  // ListLogEntries lists all of the incident log entries across the entire
    86  // account.
    87  //
    88  // Deprecated: Use ListLogEntriesWithContext instead.
    89  func (c *Client) ListLogEntries(o ListLogEntriesOptions) (*ListLogEntryResponse, error) {
    90  	return c.ListLogEntriesWithContext(context.Background(), o)
    91  }
    92  
    93  // ListLogEntriesWithContext lists all of the incident log entries across the entire account.
    94  func (c *Client) ListLogEntriesWithContext(ctx context.Context, o ListLogEntriesOptions) (*ListLogEntryResponse, error) {
    95  	v, err := query.Values(o)
    96  	if err != nil {
    97  		return nil, err
    98  	}
    99  
   100  	resp, err := c.get(ctx, "/log_entries?"+v.Encode(), nil)
   101  	if err != nil {
   102  		return nil, err
   103  	}
   104  
   105  	var result ListLogEntryResponse
   106  	if err = c.decodeJSON(resp, &result); err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	return &result, err
   111  }
   112  
   113  // GetLogEntryOptions is the data structure used when calling the GetLogEntry API endpoint.
   114  type GetLogEntryOptions struct {
   115  	TimeZone string   `url:"time_zone,omitempty"`
   116  	Includes []string `url:"include,omitempty,brackets"`
   117  }
   118  
   119  // GetLogEntry list log entries for the specified incident.
   120  //
   121  // Deprecated: Use GetLogEntryWithContext instead.
   122  func (c *Client) GetLogEntry(id string, o GetLogEntryOptions) (*LogEntry, error) {
   123  	return c.GetLogEntryWithContext(context.Background(), id, o)
   124  }
   125  
   126  // GetLogEntryWithContext list log entries for the specified incident.
   127  func (c *Client) GetLogEntryWithContext(ctx context.Context, id string, o GetLogEntryOptions) (*LogEntry, error) {
   128  	v, err := query.Values(o)
   129  	if err != nil {
   130  		return nil, err
   131  	}
   132  
   133  	resp, err := c.get(ctx, "/log_entries/"+id+"?"+v.Encode(), nil)
   134  	if err != nil {
   135  		return nil, err
   136  	}
   137  
   138  	var result map[string]LogEntry
   139  	if err := c.decodeJSON(resp, &result); err != nil {
   140  		return nil, err
   141  	}
   142  
   143  	le, ok := result["log_entry"]
   144  	if !ok {
   145  		return nil, fmt.Errorf("JSON response does not have log_entry field")
   146  	}
   147  
   148  	return &le, nil
   149  }
   150  
   151  // UnmarshalJSON Expands the LogEntry.Channel object to parse out a raw value
   152  func (c *Channel) UnmarshalJSON(b []byte) error {
   153  	var raw map[string]interface{}
   154  	if err := json.Unmarshal(b, &raw); err != nil {
   155  		return err
   156  	}
   157  	ct, ok := raw["type"]
   158  	if ok {
   159  		c.Type = ct.(string)
   160  		c.Raw = raw
   161  	}
   162  
   163  	return nil
   164  }
   165  
   166  // MarshalJSON Expands the LogEntry.Channel object to correctly marshal it back
   167  func (c *Channel) MarshalJSON() ([]byte, error) {
   168  	raw := map[string]interface{}{}
   169  	if c != nil && c.Type != "" {
   170  		for k, v := range c.Raw {
   171  			raw[k] = v
   172  		}
   173  	}
   174  
   175  	return json.Marshal(raw)
   176  }