Skip to content

Commit

Permalink
Merge pull request #1 from site24x7/main
Browse files Browse the repository at this point in the history
Reverse merge
  • Loading branch information
anita-mithran authored Jun 10, 2024
2 parents 5583367 + 7e3ee71 commit f3b1bea
Show file tree
Hide file tree
Showing 26 changed files with 1,560 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ jobs:
uses: goreleaser/[email protected]
with:
version: latest
args: release --rm-dist
args: release --clean
env:
GPG_FINGERPRINT: ${{ steps.import_gpg.outputs.fingerprint }}
# GitHub sets this automatically
Expand Down
1 change: 1 addition & 0 deletions api/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ const (
RESTAPISEQ MonitorType = "RESTAPISEQ"
AMAZON MonitorType = "AMAZON"
SERVER MonitorType = "SERVER"
CRON MonitorType = "CRON"
HEARTBEAT MonitorType = "HEARTBEAT"
DNS MonitorType = "DNS"
DOMAINEXPIRY MonitorType = "DOMAINEXPIRY"
Expand Down
60 changes: 60 additions & 0 deletions api/endpoints/fake/cron_monitors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package fake

import (
"github.com/site24x7/terraform-provider-site24x7/api"
"github.com/site24x7/terraform-provider-site24x7/api/endpoints/monitors"
"github.com/stretchr/testify/mock"
)

var _ monitors.CronMonitors = &CronMonitors{}

type CronMonitors struct {
mock.Mock
}

func (e *CronMonitors) Get(monitorID string) (*api.CronMonitor, error) {
args := e.Called(monitorID)
if obj, ok := args.Get(0).(*api.CronMonitor); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CronMonitors) Create(monitor *api.CronMonitor) (*api.CronMonitor, error) {
args := e.Called(monitor)
if obj, ok := args.Get(0).(*api.CronMonitor); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CronMonitors) Update(monitor *api.CronMonitor) (*api.CronMonitor, error) {
args := e.Called(monitor)
if obj, ok := args.Get(0).(*api.CronMonitor); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CronMonitors) Delete(monitorID string) error {
args := e.Called(monitorID)
return args.Error(0)
}

func (e *CronMonitors) List() ([]*api.CronMonitor, error) {
args := e.Called()
if obj, ok := args.Get(0).([]*api.CronMonitor); ok {
return obj, args.Error(1)
}
return nil, args.Error(1)
}

func (e *CronMonitors) Activate(monitorID string) error {
args := e.Called(monitorID)
return args.Error(0)
}

func (e *CronMonitors) Suspend(monitorID string) error {
args := e.Called(monitorID)
return args.Error(0)
}
103 changes: 103 additions & 0 deletions api/endpoints/monitors/cron_impl.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
package monitors

import (
"github.com/site24x7/terraform-provider-site24x7/api"
"github.com/site24x7/terraform-provider-site24x7/rest"
)

type CronMonitors interface {
Get(monitorID string) (*api.CronMonitor, error)
Create(monitor *api.CronMonitor) (*api.CronMonitor, error)
Update(monitor *api.CronMonitor) (*api.CronMonitor, error)
Delete(monitorID string) error
List() ([]*api.CronMonitor, error)
Activate(monitorID string) error
Suspend(monitorID string) error
}

type cronMonitors struct {
client rest.Client
}

func NewCronMonitors(client rest.Client) CronMonitors {
return &cronMonitors{
client: client,
}
}

func (c *cronMonitors) Get(monitorID string) (*api.CronMonitor, error) {
monitor := &api.CronMonitor{}
err := c.client.
Get().
Resource("monitors").
ResourceID(monitorID).
Do().
Parse(monitor)

return monitor, err
}

func (c *cronMonitors) Create(monitor *api.CronMonitor) (*api.CronMonitor, error) {
newMonitor := &api.CronMonitor{}
err := c.client.
Post().
Resource("monitors").
AddHeader("Content-Type", "application/json;charset=UTF-8").
Body(monitor).
Do().
Parse(newMonitor)

return newMonitor, err
}

func (c *cronMonitors) Update(monitor *api.CronMonitor) (*api.CronMonitor, error) {
updatedMonitor := &api.CronMonitor{}
err := c.client.
Put().
Resource("monitors").
ResourceID(monitor.MonitorID).
AddHeader("Content-Type", "application/json;charset=UTF-8").
Body(monitor).
Do().
Parse(updatedMonitor)

return updatedMonitor, err
}

func (c *cronMonitors) Delete(monitorID string) error {
return c.client.
Delete().
Resource("monitors").
ResourceID(monitorID).
Do().
Err()
}

func (c *cronMonitors) List() ([]*api.CronMonitor, error) {
cronMonitors := []*api.CronMonitor{}
err := c.client.
Get().
Resource("monitors").
Do().
Parse(&cronMonitors)

return cronMonitors, err
}

func (c *cronMonitors) Activate(monitorID string) error {
return c.client.
Put().
Resource("monitors/activate").
ResourceID(monitorID).
Do().
Err()
}

func (c *cronMonitors) Suspend(monitorID string) error {
return c.client.
Put().
Resource("monitors/suspend").
ResourceID(monitorID).
Do().
Err()
}
152 changes: 152 additions & 0 deletions api/endpoints/monitors/cron_impl_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
package monitors

import (
"testing"

"github.com/site24x7/terraform-provider-site24x7/api"
"github.com/site24x7/terraform-provider-site24x7/rest"
"github.com/site24x7/terraform-provider-site24x7/validation"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestCronMonitors(t *testing.T) {
validation.RunTests(t, []*validation.EndpointTest{
{
Name: "create cron monitor",
ExpectedVerb: "POST",
ExpectedPath: "/monitors",
ExpectedBody: validation.Fixture(t, "requests/create_cron_monitor.json"),
StatusCode: 200,
ResponseBody: validation.JsonAPIResponseBody(t, nil),
Fn: func(t *testing.T, c rest.Client) {
cronMonitor := &api.CronMonitor{
DisplayName: "foo",
CronExpression: "* * * * *",
CronTz: "IST",
WaitTime: 30,
Type: "CRON",
ThresholdProfileID: "012",
NotificationProfileID: "789",
MonitorGroups: []string{"234", "567"},
UserGroupIDs: []string{"123", "456"},
TagIDs: []string{"123"},
ThirdPartyServiceIDs: []string{"123", "456"},
OnCallScheduleID: "1244",
}

_, err := NewCronMonitors(c).Create(cronMonitor)
require.NoError(t, err)
},
},
{
Name: "get cron monitor",
ExpectedVerb: "GET",
ExpectedPath: "/monitors/897654345678",
StatusCode: 200,
ResponseBody: validation.Fixture(t, "responses/get_cron_monitor.json"),
Fn: func(t *testing.T, c rest.Client) {
cronMonitor, err := NewCronMonitors(c).Get("897654345678")
require.NoError(t, err)

expected := &api.CronMonitor{
MonitorID: "897654345678",
DisplayName: "foo",
CronExpression: "* * * * *",
CronTz: "IST",
WaitTime: 30,
Type: "CRON",
ThresholdProfileID: "012",
NotificationProfileID: "789",
MonitorGroups: []string{"234", "567"},
UserGroupIDs: []string{"123", "456"},
ThirdPartyServiceIDs: []string{"123", "456"},
OnCallScheduleID: "1244",
}

assert.Equal(t, expected, cronMonitor)
},
},
{
Name: "list cron monitors",
ExpectedVerb: "GET",
ExpectedPath: "/monitors",
StatusCode: 200,
ResponseBody: validation.Fixture(t, "responses/list_cron_monitors.json"),
Fn: func(t *testing.T, c rest.Client) {
cronMonitors, err := NewCronMonitors(c).List()
require.NoError(t, err)

expected := []*api.CronMonitor{
{
MonitorID: "897654345678",
DisplayName: "foo",
CronExpression: "* * * * *",
CronTz: "IST",
WaitTime: 30,
Type: "CRON",
ThresholdProfileID: "012",
NotificationProfileID: "789",
MonitorGroups: []string{"234", "567"},
UserGroupIDs: []string{"123", "456"},
ThirdPartyServiceIDs: []string{"123", "456"},
OnCallScheduleID: "1244",
},
{
MonitorID: "933654345678",
DisplayName: "foo",
CronExpression: "* * * * *",
CronTz: "IST",
WaitTime: 30,
Type: "CRON",
ThresholdProfileID: "012",
NotificationProfileID: "789",
MonitorGroups: []string{"234", "567"},
UserGroupIDs: []string{"123", "456"},
ThirdPartyServiceIDs: []string{"123", "456"},
OnCallScheduleID: "1244",
},
}

assert.Equal(t, expected, cronMonitors)
},
},
{
Name: "update cron monitor",
ExpectedVerb: "PUT",
ExpectedPath: "/monitors/123",
ExpectedBody: validation.Fixture(t, "requests/update_cron_monitor.json"),
StatusCode: 200,
ResponseBody: validation.JsonAPIResponseBody(t, nil),
Fn: func(t *testing.T, c rest.Client) {
cronMonitor := &api.CronMonitor{
MonitorID: "123",
DisplayName: "foo",
CronExpression: "* * * * *",
CronTz: "IST",
WaitTime: 30,
Type: "CRON",
ThresholdProfileID: "012",
NotificationProfileID: "789",
MonitorGroups: []string{"234", "567"},
UserGroupIDs: []string{"123", "456"},
TagIDs: []string{"123"},
ThirdPartyServiceIDs: []string{"123", "456"},
OnCallScheduleID: "1244",
}

_, err := NewCronMonitors(c).Update(cronMonitor)
require.NoError(t, err)
},
},
{
Name: "delete cron monitor",
ExpectedVerb: "DELETE",
ExpectedPath: "/monitors/123",
StatusCode: 200,
Fn: func(t *testing.T, c rest.Client) {
require.NoError(t, NewCronMonitors(c).Delete("123"))
},
},
})
}
53 changes: 53 additions & 0 deletions api/monitor_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -923,6 +923,59 @@ func (serverMonitor *ServerMonitor) String() string {
return ToString(serverMonitor)
}

//Cron Monitor resource in Site24x7
type CronMonitor struct {
_ struct{} `type:"structure"` // Enforces key based initialization.
MonitorID string `json:"monitor_id,omitempty"`
DisplayName string `json:"display_name"`
CronExpression string `json:"cron_expression"`
CronTz string `json:"cron_tz"`
WaitTime int `json:"wait_time"`
Type string `json:"type"`
ThresholdProfileID string `json:"threshold_profile_id"`
NotificationProfileID string `json:"notification_profile_id"`
MonitorGroups []string `json:"monitor_groups,omitempty"`
TagIDs []string `json:"tag_ids,omitempty"`
ThirdPartyServiceIDs []string `json:"third_party_services,omitempty"`
UserGroupIDs []string `json:"user_group_ids,omitempty"`
OnCallScheduleID string `json:"on_call_schedule_id,omitempty"`
}

func (cronMonitor *CronMonitor) SetLocationProfileID(locationProfileID string) {
}

func (cronMonitor *CronMonitor) GetLocationProfileID() string {
return ""
}

func (cronMonitor *CronMonitor) SetNotificationProfileID(notificationProfileID string) {
cronMonitor.NotificationProfileID = notificationProfileID
}

func (cronMonitor *CronMonitor) GetNotificationProfileID() string {
return cronMonitor.NotificationProfileID
}

func (cronMonitor *CronMonitor) SetUserGroupIDs(userGroupIDs []string) {
cronMonitor.UserGroupIDs = userGroupIDs
}

func (cronMonitor *CronMonitor) GetUserGroupIDs() []string {
return cronMonitor.UserGroupIDs
}

func (cronMonitor *CronMonitor) SetTagIDs(tagIDs []string) {
cronMonitor.TagIDs = tagIDs
}

func (cronMonitor *CronMonitor) GetTagIDs() []string {
return cronMonitor.TagIDs
}

func (cronMonitor *CronMonitor) String() string {
return ToString(cronMonitor)
}

// Denotes the Heartbeat monitor resource in Site24x7.
type HeartbeatMonitor struct {
_ struct{} `type:"structure"` // Enforces key based initialization.
Expand Down
Loading

0 comments on commit f3b1bea

Please sign in to comment.