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

CNF-14938: implement GET /O2ims_infrastructureMonitoring/v1/alarms/{alarmEventRecordId} #319

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

pixelsoccupied
Copy link
Collaborator

@pixelsoccupied pixelsoccupied commented Nov 18, 2024

This PR implements alarms/{alarmEventRecordId} where you query the DB for existing record using a given UUID

// 200

{
    "alarmAcknowledged": false,
    "alarmDefinitionId": "c7e5e1e6-6f16-4b9a-807f-c18088def392",
    "alarmEventRecordId": "c1a66f8c-8454-4e3b-8d8d-9cbe119f200a",
    "alarmRaisedTime": "2024-11-19T15:25:30.485991-05:00",
    "extensions": null,
    "perceivedSeverity": 3,
    "probableCauseId": "2266f5fd-2dca-47bf-a701-802c463162c8",
    "resourceTypeID": "550e8400-e29b-41d4-a716-446655440000"
}

// 404

{
    "additionalAttributes": {
        "UUID": "0b07e1b1-3473-458a-9ce3-03911c2f581d"
    },
    "detail": "Could not find AlarmEventRecord for given UUID",
    "status": 404
}

// 400

{
    "detail": "Invalid format for parameter alarmEventRecordId: error unmarshaling 0b07e1b1-3473-458a-9ce3-03911c2f text as *uuid.UUID: invalid UUID format",
    "status": 400
}

// 500

{
    "detail": "failed to get AlarmEventRecord due to issues with DB conn: failed to call database: failed to connect to `host=localhost user=alarms database=alarms`: dial error (dial tcp 127.0.0.1:5432: connect: connection refused)",
    "status": 500
}

@openshift-ci-robot
Copy link
Collaborator

openshift-ci-robot commented Nov 18, 2024

@pixelsoccupied: This pull request references CNF-14938 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.18.0" version, but no target version was set.

In response to this:

tbd

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

Copy link

openshift-ci bot commented Nov 18, 2024

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@openshift-ci openshift-ci bot added the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 18, 2024
@openshift-ci-robot
Copy link
Collaborator

openshift-ci-robot commented Nov 19, 2024

@pixelsoccupied: This pull request references CNF-14938 which is a valid jira issue.

Warning: The referenced jira issue has an invalid target version for the target branch this PR targets: expected the story to target the "4.18.0" version, but no target version was set.

In response to this:

This PR implements alarms/{alarmEventRecordId} where you query the DB for existing record using a given UUID

// 200

{
   "alarmAcknowledged": false,
   "alarmDefinitionId": "c7e5e1e6-6f16-4b9a-807f-c18088def392",
   "alarmEventRecordId": "c1a66f8c-8454-4e3b-8d8d-9cbe119f200a",
   "alarmRaisedTime": "2024-11-19T15:25:30.485991-05:00",
   "extensions": null,
   "perceivedSeverity": 3,
   "probableCauseId": "2266f5fd-2dca-47bf-a701-802c463162c8",
   "resourceTypeID": "550e8400-e29b-41d4-a716-446655440000"
}

// 404

{
   "additionalAttributes": {
       "UUID": "0b07e1b1-3473-458a-9ce3-03911c2f581d"
   },
   "detail": "Could not find AlarmEventRecord for given UUID",
   "status": 404
}

// 400

{
   "detail": "Invalid format for parameter alarmEventRecordId: error unmarshaling 0b07e1b1-3473-458a-9ce3-03911c2f text as *uuid.UUID: invalid UUID format",
   "status": 400
}

// 500

{
   "detail": "failed to get AlarmEventRecord due to issues with DB conn: failed to call database: failed to connect to `host=localhost user=alarms database=alarms`: dial error (dial tcp 127.0.0.1:5432: connect: connection refused)",
   "status": 500
}

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the openshift-eng/jira-lifecycle-plugin repository.

@pixelsoccupied
Copy link
Collaborator Author

/test

Copy link

openshift-ci bot commented Nov 19, 2024

@pixelsoccupied: The /test command needs one or more targets.
The following commands are available to trigger required jobs:

  • /test ci-bundle-operator-bundle
  • /test ci-job
  • /test images
  • /test scorecard

Use /test all to run the following jobs that were automatically triggered:

  • pull-ci-openshift-kni-oran-o2ims-main-ci-bundle-operator-bundle
  • pull-ci-openshift-kni-oran-o2ims-main-ci-job
  • pull-ci-openshift-kni-oran-o2ims-main-images

In response to this:

/test

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@pixelsoccupied
Copy link
Collaborator Author

/test all

1 similar comment
@pixelsoccupied
Copy link
Collaborator Author

/test all

@pixelsoccupied pixelsoccupied marked this pull request as ready for review November 19, 2024 22:28
@openshift-ci openshift-ci bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Nov 19, 2024
@pixelsoccupied
Copy link
Collaborator Author

/cc @alegacy

@openshift-ci openshift-ci bot requested a review from alegacy November 19, 2024 22:29
@pixelsoccupied
Copy link
Collaborator Author

pixelsoccupied commented Nov 19, 2024

will revisit the testing since testcontainers was pulling in too much dependency. Most likely look into kind cluster like TALM + something in release repo

// TODO: update config with trace, timeouts etc.
poolConfig, err := pgxpool.ParseConfig(fmt.Sprintf("postgres://%s:%s@%s:%s/%s",
poolConfig, err := pgxpool.ParseConfig(fmt.Sprintf("postgres://%s:%s@%s:%s/%s?sslmode=disable",
Copy link
Collaborator

Choose a reason for hiding this comment

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

We should try to ensure that sslmode gets enabled here.

`

// Run query
rows, _ := ar.Db.Query(ctx, query, uuid.String()) // note: err is passed on to Collect* func so we can ignore this
Copy link
Collaborator

Choose a reason for hiding this comment

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

I assume the answer is yes, but are we sure that the Query method protects us against SQL injection attacks? ... i know in this particular case it isn't possible since the UUID string would be validated for format before this point, but in other instances that may not always be the case.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah basically using $ syntax should prevent any injection issues! The author talks about it here a bit more. There's also a helper function called NamedArgs (not used here) is also safe.

@alegacy
Copy link
Collaborator

alegacy commented Nov 19, 2024

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Nov 19, 2024
func (ar *AlarmsRepository) GetAlarmEventRecordWithUuid(ctx context.Context, uuid uuid.UUID) ([]models.AlarmEventRecord, error) {
// Build sql query
query := `
SELECT alarm_event_record_id, alarm_definition_id, probable_cause_id, alarm_raised_time,
Copy link
Contributor

Choose a reason for hiding this comment

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

I think it's worth having a look at
https://github.com/doug-martin/goqu or squirrel

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ah would be nice to have a type safe code. I only experimented with sqlc (similar to goqu but takes sql and coverts to typed code) but was generating too much extra code. But this seems nice it it's simply wrapping the usual sql code with a type safety - so no random syntax errors. Does this work well with pgx? I suppose the query is simply converted to a string and then pgx simply continues as usual?

Let's experiment with it a bit more but +1 type safety. @alegacy

)

type AlarmsServer struct {
AlarmsRepository *AlarmsRepository
AlarmsRepository *repo.AlarmsRepository
Copy link
Contributor

Choose a reason for hiding this comment

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

you can mock this to have some basic unit tests

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah absolutely but there's not much to test with this current PR (other than actually sql stuff) so not creating any interface for now to mock. Will update this on a follow up PR where we have stuff outside of the sql calls to test...wdyt?

@mlguerrero12
Copy link
Contributor

/approve

Copy link

openshift-ci bot commented Nov 20, 2024

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: mlguerrero12

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci openshift-ci bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 20, 2024
@openshift-merge-robot openshift-merge-robot added the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 20, 2024
@openshift-ci openshift-ci bot removed the lgtm Indicates that a PR is ready to be merged. label Nov 20, 2024
@openshift-merge-robot openshift-merge-robot removed the needs-rebase Indicates a PR cannot be merged because it has merge conflicts with HEAD. label Nov 20, 2024
@mlguerrero12
Copy link
Contributor

/lgtm

@openshift-ci openshift-ci bot added the lgtm Indicates that a PR is ready to be merged. label Nov 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants