From c100b32b3eea0b39a545f10d01ffc7024502d52f Mon Sep 17 00:00:00 2001 From: Damir Sayfutdinov Date: Fri, 15 Nov 2024 17:04:39 +0300 Subject: [PATCH] Add keypairs list opts (#172) Added the ability to pass user_id as a filter to speed up the key list query. Also fix GitHub Actions: - enable for PR from fork - add .semgrepignore for tests - remove wrong --severity flag for semgrep --- .github/workflows/secure.yml | 8 ++++++-- .github/workflows/verify.yml | 6 +++++- .semgrepignore | 4 ++++ selvpcclient/resell/v2/keypairs/requests.go | 16 ++++++++++++++++ selvpcclient/resell/v2/keypairs/requests_opts.go | 7 +++++++ 5 files changed, 38 insertions(+), 3 deletions(-) create mode 100644 .semgrepignore diff --git a/.github/workflows/secure.yml b/.github/workflows/secure.yml index c58331a..c8bd79a 100644 --- a/.github/workflows/secure.yml +++ b/.github/workflows/secure.yml @@ -1,6 +1,10 @@ name: Secure -on: push +on: + push: + branches: + - master + pull_request: jobs: # Sample GitHub Actions: @@ -17,7 +21,7 @@ jobs: security-events: write steps: - uses: actions/checkout@v4 - - run: semgrep scan --sarif --output=semgrep.sarif --error --severity=WARNING + - run: semgrep scan --sarif --output=semgrep.sarif --error env: SEMGREP_RULES: >- p/command-injection diff --git a/.github/workflows/verify.yml b/.github/workflows/verify.yml index 14ca100..0d20d3f 100644 --- a/.github/workflows/verify.yml +++ b/.github/workflows/verify.yml @@ -1,6 +1,10 @@ name: Verify -on: push +on: + push: + branches: + - master + pull_request: jobs: tests: diff --git a/.semgrepignore b/.semgrepignore new file mode 100644 index 0000000..ab58da9 --- /dev/null +++ b/.semgrepignore @@ -0,0 +1,4 @@ +*/testing/*_test.go +*/testing/fixtures.go +README.md +selvpcclient/doc.go diff --git a/selvpcclient/resell/v2/keypairs/requests.go b/selvpcclient/resell/v2/keypairs/requests.go index e869a8b..3a2b6b1 100644 --- a/selvpcclient/resell/v2/keypairs/requests.go +++ b/selvpcclient/resell/v2/keypairs/requests.go @@ -5,6 +5,8 @@ import ( "net/http" "strings" + "github.com/google/go-querystring/query" + "github.com/selectel/go-selvpcclient/v3/selvpcclient" clientservices "github.com/selectel/go-selvpcclient/v3/selvpcclient/clients/services" ) @@ -13,12 +15,26 @@ const resourceURL = "keypairs" // List gets a list of keypairs in the current domain. func List(client *selvpcclient.Client) ([]*Keypair, *clientservices.ResponseResult, error) { + return ListWithOpts(client, ListOpts{}) +} + +// ListWithOpts gets a list of keypairs with filter options. +func ListWithOpts(client *selvpcclient.Client, opts ListOpts) ([]*Keypair, *clientservices.ResponseResult, error) { endpoint, err := client.Resell.GetEndpoint() if err != nil { return nil, nil, fmt.Errorf("failed to get endpoint, err: %w", err) } url := strings.Join([]string{endpoint, resourceURL}, "/") + if opts.UserID != "" { + queryParams, err := query.Values(opts) + if err != nil { + return nil, nil, err + } + + url = strings.Join([]string{url, queryParams.Encode()}, "?") + } + responseResult, err := client.Resell.Requests.Do(http.MethodGet, url, &clientservices.RequestOptions{ OkCodes: []int{200}, }) diff --git a/selvpcclient/resell/v2/keypairs/requests_opts.go b/selvpcclient/resell/v2/keypairs/requests_opts.go index b29a9fb..49360bd 100644 --- a/selvpcclient/resell/v2/keypairs/requests_opts.go +++ b/selvpcclient/resell/v2/keypairs/requests_opts.go @@ -16,3 +16,10 @@ type KeypairOpts struct { // this keypair. UserID string `json:"user_id"` } + +// ListOpts represents options for the keypair ListWithFilter request. +type ListOpts struct { + // UserID contains an ID of an OpenStack Identity service user that owns + // this keypair. + UserID string `url:"user_id"` +}