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

Fix auto_pause_notifications_parameters JSON serialization #94

Merged

Conversation

pdecat
Copy link
Contributor

@pdecat pdecat commented Jun 10, 2022

@stmcallister this PR fixes my previous #92 PR which had broken JSON serialization:

---[ REQUEST ]---------------------------------------
POST /services HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Content-Length: 323
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip

{
 "service": {
  "acknowledgement_timeout": 1800,
  "alert_creation": "create_alerts_and_incidents",
  "alert_grouping": null,
  "auto_pause_notifications_parameters": {
   "Enabled": true,
   "Timeout": 300
  },
  "auto_resolve_timeout": 1800,
  "description": "foo",
  "escalation_policy": {
   "id": "PH58XGR",
   "type": "escalation_policy_reference"
  },
  "name": "tf-lrjyc"
 }
}

-----------------------------------------------------
2022/06/10 14:10:38 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 400 Bad Request
Content-Length: 116
Access-Control-Allow-Headers: Authorization, Content-Type, AuthorizationOauth, X-EARLY-ACCESS
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers:
Access-Control-Max-Age: 1728000
Cache-Control: no-cache
Content-Type: application/json
Date: Fri, 10 Jun 2022 12:10:38 GMT
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
X-Request-Id: 088c962e05cb40e17f3a6854672584f2

{
 "error": {
  "message": "Invalid Input Provided",
  "code": 2001,
  "errors": [
   "Enabled must be a TrueClass or a FalseClass."
  ]
 }
}
-----------------------------------------------------

Note that Enabled and Timeout have a capital letter.

@@ -51,8 +51,8 @@ type AlertGroupingParameters struct {

// AutoPauseNotificationsParameters defines how alerts on this service are automatically suspended for a period of time before triggering, when identified as likely being transient.
type AutoPauseNotificationsParameters struct {
Enabled bool `json:enabled,omitempty`
Timeout int `json:timeout,omitempty`
Enabled bool `json:"enabled"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removing omitempty is required to avoid the following error when enabled is false:

---[ REQUEST ]---------------------------------------
PUT /services/P49BQU7 HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Content-Length: 369
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip

{
 "service": {
  "acknowledgement_timeout": 1800,
  "alert_creation": "create_alerts_and_incidents",
  "alert_grouping": null,
  "auto_pause_notifications_parameters": {
   "timeout": 120
  },
  "auto_resolve_timeout": 1800,
  "description": "foo",
  "escalation_policy": {
   "id": "PW95IJV",
   "type": "escalation_policy_reference"
  },
  "incident_urgency_rule": {
   "type": "constant",
   "urgency": "high"
  },
  "name": "tf-7vwjy"
 }
}

-----------------------------------------------------
2022/06/10 14:22:53 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 400 Bad Request
Content-Length: 116
Access-Control-Allow-Headers: Authorization, Content-Type, AuthorizationOauth, X-EARLY-ACCESS
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers:
Access-Control-Max-Age: 1728000
Cache-Control: no-cache
Content-Type: application/json
Date: Fri, 10 Jun 2022 12:22:53 GMT
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
X-Request-Id: c1841333efa37b0fd7acd858b7648312

{
 "error": {
  "message": "Invalid Input Provided",
  "code": 2001,
  "errors": [
   "Enabled must be a TrueClass or a FalseClass."
  ]
 }
}
-----------------------------------------------------

Enabled bool `json:enabled,omitempty`
Timeout int `json:timeout,omitempty`
Enabled bool `json:"enabled"`
Timeout int `json:"timeout"`
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Turns out the API returns null for timeout if enabled is set to false:

---[ REQUEST ]---------------------------------------
PUT /services/PDKFHYX HTTP/1.1
Host: api.pagerduty.com
User-Agent: heimweh/go-pagerduty(terraform)
Content-Length: 385
Accept: application/vnd.pagerduty+json;version=2
Authorization: Token token=******
Content-Type: application/json
Accept-Encoding: gzip

{
 "service": {
  "acknowledgement_timeout": 1800,
  "alert_creation": "create_alerts_and_incidents",
  "alert_grouping": null,
  "auto_pause_notifications_parameters": {
   "enabled": false,
   "timeout": 300
  },
  "auto_resolve_timeout": 1800,
  "description": "foo",
  "escalation_policy": {
   "id": "PPF7AEC",
   "type": "escalation_policy_reference"
  },
  "incident_urgency_rule": {
   "type": "constant",
   "urgency": "high"
  },
  "name": "tf-43e4n"
 }
}

-----------------------------------------------------
2022/06/10 15:06:33 [DEBUG] PagerDuty API Response Details:
---[ RESPONSE ]--------------------------------------
HTTP/2.0 200 OK
Access-Control-Allow-Headers: Authorization, Content-Type, AuthorizationOauth, X-EARLY-ACCESS
Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS
Access-Control-Allow-Origin: *
Access-Control-Expose-Headers:
Access-Control-Max-Age: 1728000
Cache-Control: max-age=0, private, must-revalidate
Content-Type: application/json
Date: Fri, 10 Jun 2022 13:06:33 GMT
Etag: W/"45b125457a612501fdfc61e82a2213e0"
Feature-Policy: accelerometer 'none'; camera 'none'; geolocation 'none'; gyroscope 'none'; magnetometer 'none'; microphone 'none'; payment 'none'; usb 'none'
Referrer-Policy: strict-origin-when-cross-origin
Referrer-Policy: strict-origin-when-cross-origin
Server: nginx
Strict-Transport-Security: max-age=31536000; includeSubDomains
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Request-Id: 218f2e5d06395dc71236b61febd23c66
X-Xss-Protection: 1; mode=block

{
 "service": {
  "id": "PDKFHYX",
  "name": "tf-43e4n",
  "description": "foo",
  "created_at": "2022-06-10T15:06:29+02:00",
  "updated_at": "2022-06-10T15:06:33+02:00",
  "status": "active",
  "teams": [],
  "alert_creation": "create_alerts_and_incidents",
  "addons": [],
  "scheduled_actions": [],
  "support_hours": null,
  "last_incident_timestamp": null,
  "escalation_policy": {
   "id": "PPF7AEC",
   "type": "escalation_policy_reference",
   "summary": "tf-xgyxe",
   "self": "https://api.pagerduty.com/escalation_policies/PPF7AEC",
   "html_url": "https://dev-claranet.pagerduty.com/escalation_policies/PPF7AEC"
  },
  "incident_urgency_rule": {
   "type": "constant",
   "urgency": "high"
  },
  "acknowledgement_timeout": 1800,
  "auto_resolve_timeout": 1800,
  "auto_pause_notifications_parameters": {
   "enabled": false,
   "timeout": null
  },
  "alert_grouping": null,
  "alert_grouping_timeout": null,
  "alert_grouping_parameters": {
   "type": null,
   "config": null
  },
  "integrations": [],
  "response_play": null,
  "type": "service",
  "summary": "tf-43e4n",
  "self": "https://api.pagerduty.com/services/PDKFHYX",
  "html_url": "https://dev-claranet.pagerduty.com/service-directory/PDKFHYX"
 }
}
-----------------------------------------------------

timeout probably needs to be an int pointer instead of an int.

@pdecat
Copy link
Contributor Author

pdecat commented Jun 13, 2022

Note that running go vet in CI would have caught the issue in the original PR:

# go vet ./...
# github.com/heimweh/go-pagerduty/pagerduty
pagerduty/service.go:54:2: struct field tag `json:enabled,omitempty` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
pagerduty/service.go:55:2: struct field tag `json:timeout,omitempty` not compatible with reflect.StructTag.Get: bad syntax for struct tag value
# echo $?
2

I've submitted #96 to run go vet ./... in CI.

@pdecat pdecat force-pushed the fix_auto_pause_notifications_parameters branch from b60e050 to ceae303 Compare July 13, 2022 06:24
@pdecat pdecat mentioned this pull request Jul 13, 2022
@imjaroiswebdev imjaroiswebdev self-requested a review July 22, 2022 22:53
@imjaroiswebdev
Copy link
Collaborator

Hey @pdecat! It looks great! Thank you for fixing JSON serialization for this... 👏🏽🎉

@imjaroiswebdev imjaroiswebdev merged commit 5d470ed into heimweh:master Jul 22, 2022
@imjaroiswebdev imjaroiswebdev requested review from imjaroiswebdev and removed request for imjaroiswebdev July 22, 2022 23:06
@pdecat pdecat deleted the fix_auto_pause_notifications_parameters branch July 23, 2022 07:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants