github.com/newrelic/newrelic-client-go@v1.1.0/pkg/synthetics/monitors.go (about) 1 package synthetics 2 3 import ( 4 "context" 5 "path" 6 ) 7 8 const ( 9 listMonitorsLimit = 100 10 ) 11 12 // Monitor represents a New Relic Synthetics monitor. 13 type Monitor struct { 14 ID string `json:"id,omitempty"` 15 Name string `json:"name"` 16 Type MonitorType `json:"type"` 17 Frequency uint `json:"frequency"` 18 URI string `json:"uri"` 19 Locations []string `json:"locations"` 20 Status MonitorStatusType `json:"status"` 21 SLAThreshold float64 `json:"slaThreshold"` 22 UserID uint `json:"userId,omitempty"` 23 APIVersion string `json:"apiVersion,omitempty"` 24 ModifiedAt *Time `json:"modifiedAt,omitempty"` 25 CreatedAt *Time `json:"createdAt,omitempty"` 26 Options MonitorOptions `json:"options,omitempty"` 27 } 28 29 // MonitorScriptLocation represents a New Relic Synthetics monitor script location. 30 type MonitorScriptLocation struct { 31 Name string `json:"name"` 32 HMAC string `json:"hmac"` 33 } 34 35 // MonitorScript represents a New Relic Synthetics monitor script. 36 type MonitorScript struct { 37 Text string `json:"scriptText"` 38 Locations []MonitorScriptLocation `json:"scriptLocations"` 39 } 40 41 // MonitorType represents a Synthetics monitor type. 42 type MonitorType string 43 44 // MonitorStatusType represents a Synthetics monitor status type. 45 type MonitorStatusType string 46 47 // MonitorOptions represents the options for a New Relic Synthetics monitor. 48 type MonitorOptions struct { 49 ValidationString string `json:"validationString,omitempty"` 50 VerifySSL bool `json:"verifySSL,omitempty"` 51 BypassHEADRequest bool `json:"bypassHEADRequest,omitempty"` 52 TreatRedirectAsFailure bool `json:"treatRedirectAsFailure,omitempty"` 53 } 54 55 var ( 56 // MonitorTypes specifies the possible types for a Synthetics monitor. 57 MonitorTypes = struct { 58 Ping MonitorType 59 Browser MonitorType 60 ScriptedBrowser MonitorType 61 APITest MonitorType 62 }{ 63 Ping: "SIMPLE", 64 Browser: "BROWSER", 65 ScriptedBrowser: "SCRIPT_BROWSER", 66 APITest: "SCRIPT_API", 67 } 68 69 // MonitorStatus specifies the possible Synthetics monitor status types. 70 MonitorStatus = struct { 71 Enabled MonitorStatusType 72 Muted MonitorStatusType 73 Disabled MonitorStatusType 74 }{ 75 Enabled: "ENABLED", 76 Muted: "MUTED", 77 Disabled: "DISABLED", 78 } 79 ) 80 81 // ListMonitors is used to retrieve New Relic Synthetics monitors. 82 func (s *Synthetics) ListMonitors() ([]*Monitor, error) { 83 return s.ListMonitorsWithContext(context.Background()) 84 } 85 86 // ListMonitorsWithContext is used to retrieve New Relic Synthetics monitors. 87 func (s *Synthetics) ListMonitorsWithContext(ctx context.Context) ([]*Monitor, error) { 88 results := []*Monitor{} 89 nextURL := s.config.Region().SyntheticsURL("/v4/monitors") 90 queryParams := listMonitorsParams{ 91 Limit: listMonitorsLimit, 92 } 93 94 for nextURL != "" { 95 response := listMonitorsResponse{} 96 97 resp, err := s.client.GetWithContext(ctx, nextURL, &queryParams, &response) 98 99 if err != nil { 100 return nil, err 101 } 102 103 results = append(results, response.Monitors...) 104 105 paging := s.pager.Parse(resp) 106 nextURL = paging.Next 107 } 108 109 return results, nil 110 } 111 112 // GetMonitor is used to retrieve a specific New Relic Synthetics monitor. 113 func (s *Synthetics) GetMonitor(monitorID string) (*Monitor, error) { 114 return s.GetMonitorWithContext(context.Background(), monitorID) 115 } 116 117 // GetMonitorWithContext is used to retrieve a specific New Relic Synthetics monitor. 118 func (s *Synthetics) GetMonitorWithContext(ctx context.Context, monitorID string) (*Monitor, error) { 119 resp := Monitor{} 120 121 _, err := s.client.GetWithContext(ctx, s.config.Region().SyntheticsURL("/v4/monitors", monitorID), nil, &resp) 122 123 if err != nil { 124 return nil, err 125 } 126 127 return &resp, nil 128 } 129 130 // CreateMonitor is used to create a New Relic Synthetics monitor. 131 //Deprecated: Use one of the following methods instead based on your needs - 132 //syntheticsCreateBrokenLinksMonitor(Broken links monitor), 133 //syntheticsCreateCertCheckMonitor(Cert Check Monitor), 134 // syntheticsCreateScriptBrowserMonitor(Script Browser Monitor), 135 //syntheticsCreateSimpleBrowserMonitor(Simple Browser Monitor), 136 //syntheticsCreateSimpleMonitor(Simple Monitor), 137 //syntheticsCreateStepMonitor(Step Monitor). 138 func (s *Synthetics) CreateMonitor(monitor Monitor) (*Monitor, error) { 139 return s.CreateMonitorWithContext(context.Background(), monitor) 140 } 141 142 // CreateMonitorWithContext is used to create a New Relic Synthetics monitor. 143 //Deprecated: Use one of the following methods instead based on your needs - 144 //syntheticsCreateBrokenLinksMonitorWithContext(Broken links monitor), 145 //syntheticsCreateCertCheckMonitorWithContext(Cert Check Monitor), 146 // syntheticsCreateScriptBrowserMonitorWithContext(Script Browser Monitor), 147 //syntheticsCreateSimpleBrowserMonitorWithContext(Simple Browser Monitor), 148 //syntheticsCreateSimpleMonitorWithContext(Simple Monitor), 149 //syntheticsCreateStepMonitorWithContext(Step Monitor). 150 func (s *Synthetics) CreateMonitorWithContext(ctx context.Context, monitor Monitor) (*Monitor, error) { 151 resp, err := s.client.PostWithContext(ctx, s.config.Region().SyntheticsURL("/v4/monitors"), nil, &monitor, nil) 152 153 if err != nil { 154 return nil, err 155 } 156 157 l := resp.Header.Get("location") 158 monitorID := path.Base(l) 159 160 monitor.ID = monitorID 161 162 return &monitor, nil 163 } 164 165 // UpdateMonitor is used to update a New Relic Synthetics monitor. 166 //Deprecated: Use one of the following methods instead based on your needs - 167 //syntheticsUpdateBrokenLinksMonitor(Broken links monitor), 168 //syntheticsUpdateCertCheckMonitor(Cert Check Monitor), 169 // syntheticsUpdateScriptBrowserMonitor(Script Browser Monitor), 170 //syntheticsUpdateSimpleBrowserMonitor(Simple Browser Monitor), 171 //syntheticsUpdateSimpleMonitor(Simple Monitor), 172 //syntheticsUpdateStepMonitor(Step Monitor). 173 func (s *Synthetics) UpdateMonitor(monitor Monitor) (*Monitor, error) { 174 return s.UpdateMonitorWithContext(context.Background(), monitor) 175 } 176 177 // UpdateMonitorWithContext is used to update a New Relic Synthetics monitor. 178 //Deprecated: Use one of the following methods instead based on your needs - 179 //syntheticsUpdateBrokenLinksMonitorWithContext(Broken links monitor), 180 //syntheticsUpdateCertCheckMonitorWithContext(Cert Check Monitor), 181 // syntheticsUpdateScriptBrowserMonitorWithContext(Script Browser Monitor), 182 //syntheticsUpdateSimpleBrowserMonitorWithContext(Simple Browser Monitor), 183 //syntheticsUpdateSimpleMonitorWithContext(Simple Monitor), 184 //syntheticsUpdateStepMonitorWithContext(Step Monitor). 185 func (s *Synthetics) UpdateMonitorWithContext(ctx context.Context, monitor Monitor) (*Monitor, error) { 186 _, err := s.client.PutWithContext(ctx, s.config.Region().SyntheticsURL("/v4/monitors", monitor.ID), nil, &monitor, nil) 187 188 if err != nil { 189 return nil, err 190 } 191 192 return &monitor, nil 193 } 194 195 // DeleteMonitor is used to delete a New Relic Synthetics monitor. 196 // Deprecated: Use the following method to delete all New Relic Synthetics Monitors. 197 //SyntheticsDeleteMonitor 198 func (s *Synthetics) DeleteMonitor(monitorID string) error { 199 return s.DeleteMonitorWithContext(context.Background(), monitorID) 200 } 201 202 // DeleteMonitorWithContext is used to delete a New Relic Synthetics monitor. 203 // Deprecated: Use the following method to delete all New Relic Synthetics Monitors. 204 //SyntheticsDeleteMonitorWithContext 205 func (s *Synthetics) DeleteMonitorWithContext(ctx context.Context, monitorID string) error { 206 _, err := s.client.DeleteWithContext(ctx, s.config.Region().SyntheticsURL("/v4/monitors", monitorID), nil, nil) 207 208 if err != nil { 209 return err 210 } 211 212 return nil 213 } 214 215 type listMonitorsResponse struct { 216 Monitors []*Monitor `json:"monitors,omitempty"` 217 } 218 219 type listMonitorsParams struct { 220 Limit int `url:"limit,omitempty"` 221 }