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

CLOUDP-57848: Implement events resource in the atlas go client #72

Merged
merged 2 commits into from
Apr 2, 2020
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
166 changes: 166 additions & 0 deletions mongodbatlas/events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package mongodbatlas

import (
"context"
"fmt"
"net/http"
)

const eventsPathProjects = "groups/%s/events"
const eventsPathOrganization = "orgs/%s/events"

// EventsService is an interface for interfacing with the Events
// endpoints of the MongoDB Atlas API.
// See more: https://docs.atlas.mongodb.com/reference/api/events/
type EventsService interface {
ListOrganizationEvents(context.Context, string, *EventListOptions) (*EventResponse, *Response, error)
GetOrganizationEvent(context.Context, string, string) (*Event, *Response, error)
ListProjectEvents(context.Context, string, *ListOptions) (*EventResponse, *Response, error)
GetProjectEvent(context.Context, string, string) (*Event, *Response, error)
}

// EventsServiceOp handles communication with the Event related methods
// of the MongoDB Atlas API
type EventsServiceOp struct {
Client RequestDoer
}

var _ EventsService = &EventsServiceOp{}

// Event represents an event of the MongoDB Atlas API
type Event struct {
AlertID string `json:"alertId"`
AlertConfigID string `json:"alertConfigId"`
APIKeyID string `json:"apiKeyId,omitempty"`
Collection string `json:"collection,omitempty"`
Created string `json:"created"`
CurrentValue *CurrentValue `json:"currentValue,omitempty"`
Database string `json:"database,omitempty"`
EventTypeName string `json:"eventTypeName"`
GroupID string `json:"groupId,omitempty"`
Hostname string `json:"hostname"`
ID string `json:"id"`
InvoiceID string `json:"invoiceId,omitempty"`
IsGlobalAdmin bool `json:"isGlobalAdmin,omitempty"`
Links []*Link `json:"links"`
MetricName string `json:"metricName,omitempty"`
OpType string `json:"opType,omitempty"`
OrgID string `json:"orgId,omitempty"`
PaymentID string `json:"paymentId,omitempty"`
Port int `json:"Port,omitempty"`
PublicKey string `json:"publicKey,omitempty"`
RemoteAddress string `json:"remoteAddress,omitempty"`
ReplicaSetName string `json:"replicaSetName,omitempty"`
ShardName string `json:"shardName,omitempty"`
TargetPublicKey string `json:"targetPublicKey,omitempty"`
TargetUsername string `json:"targetUsername,omitempty"`
TeamID string `json:"teamId,omitempty"`
UserID string `json:"userId,omitempty"`
Username string `json:"username,omitempty"`
WhitelistEntry string `json:"whitelistEntry,omitempty"`
}

// EventResponse is the response from the EventsService.List.
type EventResponse struct {
Links []*Link `json:"links,omitempty"`
Results []*Event `json:"results,omitempty"`
TotalCount int `json:"totalCount,omitempty"`
}

// EventListOptions specifies the optional parameters to the Event List methods.
type EventListOptions struct {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

ListOptions
EventType string `url:"eventType,omitempty"`
MinDate string `url:"minDate,omitempty"`
MaxDate string `url:"maxDate,omitempty"`
}

// ListOrganizationEvents lists all events in the organization associated to {ORG-ID}.
// See more: https://docs.atlas.mongodb.com/reference/api/events-orgs-get-all/
func (s *EventsServiceOp) ListOrganizationEvents(ctx context.Context, orgID string, listOptions *EventListOptions) (*EventResponse, *Response, error) {
path := fmt.Sprintf(eventsPathOrganization, orgID)

//Add query params from listOptions
path, err := setListOptions(path, listOptions)
if err != nil {
return nil, nil, err
}

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(EventResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, nil
}

// GetOrganizationEvent gets the alert specified to {EVENT-ID} from the organization associated to {ORG-ID}.
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/events/get-one-event-for-org/
func (s *EventsServiceOp) GetOrganizationEvent(ctx context.Context, orgID string, eventID string) (*Event, *Response, error) {
basePath := fmt.Sprintf(eventsPathOrganization, orgID)
path := fmt.Sprintf("%s/%s", basePath, eventID)

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(Event)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}

// ListOrganizationEvents lists all events in the project associated to {PROJECT-ID}.
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/events/get-all-events-for-project/
func (s *EventsServiceOp) ListProjectEvents(ctx context.Context, projectID string, listOptions *ListOptions) (*EventResponse, *Response, error) {
path := fmt.Sprintf(eventsPathProjects, projectID)

//Add query params from listOptions
path, err := setListOptions(path, listOptions)
if err != nil {
return nil, nil, err
}

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(EventResponse)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, nil
}

// GetOrganizationEvent gets the alert specified to {EVENT-ID} from the project associated to {PROJECT-ID}.
// See more: https://docs.opsmanager.mongodb.com/current/reference/api/events/get-one-event-for-project/
func (s *EventsServiceOp) GetProjectEvent(ctx context.Context, projectID string, eventID string) (*Event, *Response, error) {
basePath := fmt.Sprintf(eventsPathProjects, projectID)
path := fmt.Sprintf("%s/%s", basePath, eventID)

req, err := s.Client.NewRequest(ctx, http.MethodGet, path, nil)
if err != nil {
return nil, nil, err
}

root := new(Event)
resp, err := s.Client.Do(ctx, req, root)
if err != nil {
return nil, resp, err
}

return root, resp, err
}