Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ORCA-4600] Add support for cache variables #149

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions pagerduty/event_orchestration_cache_variable.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
package pagerduty

import (
"context"
"fmt"
)

type EventOrchestrationCacheVariableService service

type EventOrchestrationCacheVariableCondition struct {
// A PCL string: https://developer.pagerduty.com/docs/ZG9jOjM1NTE0MDc0-pcl-overview
Expression string `json:"expression,omitempty"`
}

// Configuration for a cache variable changes depending on the type:
// - if `Type` is `recent_value`; then use `Regex` and `Source`
// - if `Type` is `trigger_event_count`; then use `TTLSeconds`
type EventOrchestrationCacheVariableConfiguration struct {
Type string `json:"type,omitempty"`
Regex string `json:"regex,omitempty"`
Source string `json:"source,omitempty"`
TTLSeconds int `json:"ttl_seconds,omitempty"`
imjaroiswebdev marked this conversation as resolved.
Show resolved Hide resolved
}

type EventOrchestrationCacheVariable struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Disabled bool `json:"disabled"`
Conditions []*EventOrchestrationCacheVariableCondition `json:"conditions"`
Configuration *EventOrchestrationCacheVariableConfiguration `json:"configuration,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
CreatedBy *UserReference `json:"created_by,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
UpdatedBy *UserReference `json:"updated_by,omitempty"`
}

type EventOrchestrationCacheVariablePayload struct {
CacheVariable *EventOrchestrationCacheVariable `json:"cache_variable,omitempty"`
}

type ListEventOrchestrationCacheVariablesResponse struct {
Total int `json:"total,omitempty"`
CacheVariables []*EventOrchestrationCacheVariable `json:"cache_variables,omitempty"`
}

const CacheVariableTypeGlobal string = "global"
const CacheVariableTypeService string = "service"

func buildEventOrchestrationCacheVariableUrl(cacheVariableType string, orchestrationId string, cacheVariableId string) string {
if cacheVariableType == CacheVariableTypeService {
return fmt.Sprintf("%s/services/%s/cache_variables/%s", eventOrchestrationBaseUrl, orchestrationId, cacheVariableId)
}

return fmt.Sprintf("%s/%s/cache_variables/%s", eventOrchestrationBaseUrl, orchestrationId, cacheVariableId)
}

func (s *EventOrchestrationCacheVariableService) List(ctx context.Context, cacheVariableType string, orchestrationId string) (*ListEventOrchestrationCacheVariablesResponse, *Response, error) {
u := buildEventOrchestrationCacheVariableUrl(cacheVariableType, orchestrationId, "")
v := new(ListEventOrchestrationCacheVariablesResponse)

resp, err := s.client.newRequestDoContext(ctx, "GET", u, nil, nil, v)

if err != nil {
return nil, nil, err
}

return v, resp, nil
}

func (s *EventOrchestrationCacheVariableService) Create(ctx context.Context, cacheVariableType string, orchestrationId string, cacheVariable *EventOrchestrationCacheVariable) (*EventOrchestrationCacheVariable, *Response, error) {
u := buildEventOrchestrationCacheVariableUrl(cacheVariableType, orchestrationId, "")
v := new(EventOrchestrationCacheVariablePayload)
p := &EventOrchestrationCacheVariablePayload{CacheVariable: cacheVariable}

resp, err := s.client.newRequestDoContext(ctx, "POST", u, nil, p, v)

if err != nil {
return nil, nil, err
}

return v.CacheVariable, resp, nil
}

func (s *EventOrchestrationCacheVariableService) Get(ctx context.Context, cacheVariableType string, orchestrationId string, cacheVariableId string) (*EventOrchestrationCacheVariable, *Response, error) {
u := buildEventOrchestrationCacheVariableUrl(cacheVariableType, orchestrationId, cacheVariableId)
v := new(EventOrchestrationCacheVariablePayload)

resp, err := s.client.newRequestDoContext(ctx, "GET", u, nil, nil, v)
if err != nil {
return nil, nil, err
}

return v.CacheVariable, resp, nil
}

func (s *EventOrchestrationCacheVariableService) Update(ctx context.Context, cacheVariableType string, orchestrationId string, cacheVariableId string, cacheVariable *EventOrchestrationCacheVariable) (*EventOrchestrationCacheVariable, *Response, error) {
u := buildEventOrchestrationCacheVariableUrl(cacheVariableType, orchestrationId, cacheVariableId)
v := new(EventOrchestrationCacheVariablePayload)
p := &EventOrchestrationCacheVariablePayload{CacheVariable: cacheVariable}

resp, err := s.client.newRequestDoContext(ctx, "PUT", u, nil, p, v)
if err != nil {
return nil, nil, err
}

return v.CacheVariable, resp, nil
}

func (s *EventOrchestrationCacheVariableService) Delete(ctx context.Context, cacheVariableType string, orchestrationId string, cacheVariableId string) (*Response, error) {
u := buildEventOrchestrationCacheVariableUrl(cacheVariableType, orchestrationId, cacheVariableId)
return s.client.newRequestDoContext(ctx, "DELETE", u, nil, nil, nil)
}
Loading
Loading