From 1492903cf490068bb32c35c2feb4733dc74c980a Mon Sep 17 00:00:00 2001 From: CloudQuery Bot <102256036+cq-bot@users.noreply.github.com> Date: Fri, 17 Nov 2023 00:22:54 +0200 Subject: [PATCH 1/3] chore(deps): Update module github.com/cloudquery/plugin-sdk/v4 to v4.18.2 (#1375) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [github.com/cloudquery/plugin-sdk/v4](https://togithub.com/cloudquery/plugin-sdk) | require | patch | `v4.18.1` -> `v4.18.2` | --- ### ⚠ Dependency Lookup Warnings ⚠ Warnings were logged while processing this repo. Please check the Dependency Dashboard for more information. --- ### Release Notes
cloudquery/plugin-sdk (github.com/cloudquery/plugin-sdk/v4) ### [`v4.18.2`](https://togithub.com/cloudquery/plugin-sdk/releases/tag/v4.18.2) [Compare Source](https://togithub.com/cloudquery/plugin-sdk/compare/v4.18.1...v4.18.2) ##### Bug Fixes - Batching for mixedbatchwriter ([#​1374](https://togithub.com/cloudquery/plugin-sdk/issues/1374)) ([ca435cf](https://togithub.com/cloudquery/plugin-sdk/commit/ca435cfe4a42271dadc9ea0a119a4515804efebb)) - **deps:** Update module github.com/cloudquery/cloudquery-api-go to v1.4.6 ([#​1368](https://togithub.com/cloudquery/plugin-sdk/issues/1368)) ([ea05199](https://togithub.com/cloudquery/plugin-sdk/commit/ea0519920ab1fadced3a27320a7f50a20e0bf080)) - **deps:** Update module github.com/cloudquery/cloudquery-api-go to v1.5.1 ([#​1370](https://togithub.com/cloudquery/plugin-sdk/issues/1370)) ([309b1cb](https://togithub.com/cloudquery/plugin-sdk/commit/309b1cb8267c867d6be827f36dd63fdb138485ae)) - **deps:** Update module github.com/cloudquery/cloudquery-api-go to v1.6.0 ([#​1373](https://togithub.com/cloudquery/plugin-sdk/issues/1373)) ([63fc4bb](https://togithub.com/cloudquery/plugin-sdk/commit/63fc4bbb605bf92a79def791a2b7e5d3fd09f42a)) - **deps:** Update module github.com/cloudquery/plugin-pb-go to v1.14.0 ([#​1371](https://togithub.com/cloudquery/plugin-sdk/issues/1371)) ([8ec6a34](https://togithub.com/cloudquery/plugin-sdk/commit/8ec6a3422dc387662a5028b81a483bf8b2e8d1dc))
--- ### Configuration 📅 **Schedule**: Branch creation - At any time (no schedule defined), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] If you want to rebase/retry this PR, check this box --- This PR has been generated by [Renovate Bot](https://togithub.com/renovatebot/renovate). --- examples/simple_plugin/go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/examples/simple_plugin/go.mod b/examples/simple_plugin/go.mod index 0c607bf877..cfad19f595 100644 --- a/examples/simple_plugin/go.mod +++ b/examples/simple_plugin/go.mod @@ -4,7 +4,7 @@ go 1.21.1 require ( github.com/apache/arrow/go/v14 v14.0.0-20231031200323-c49e24273160 - github.com/cloudquery/plugin-sdk/v4 v4.18.1 + github.com/cloudquery/plugin-sdk/v4 v4.18.2 github.com/rs/zerolog v1.30.0 ) From 940d87f7cc71d8e2367c3f751dcf1d081e2b8126 Mon Sep 17 00:00:00 2001 From: Martin Norbury Date: Fri, 17 Nov 2023 10:29:03 +0000 Subject: [PATCH 2/3] fix: Retrieve team for api key (#1372) Retreive the team associated with an API key to allow usage tracking against a team name fixes: https://github.com/cloudquery/cloudquery-issues/issues/893 --- examples/simple_plugin/go.sum | 2 ++ premium/usage.go | 44 ++++++++++++++++++++++++++++------- 2 files changed, 37 insertions(+), 9 deletions(-) diff --git a/examples/simple_plugin/go.sum b/examples/simple_plugin/go.sum index c333bf9c77..629badd84f 100644 --- a/examples/simple_plugin/go.sum +++ b/examples/simple_plugin/go.sum @@ -88,6 +88,8 @@ github.com/cloudquery/arrow/go/v14 v14.0.0-20231029080147-50d3871d0804 h1:y4EwAG github.com/cloudquery/arrow/go/v14 v14.0.0-20231029080147-50d3871d0804/go.mod h1:TqWp9yvMb9yZSxFNiij6cmZefm+1jw3oZU0L0w9lT7E= github.com/cloudquery/cloudquery-api-go v1.6.0 h1:8yAbNW+njhGmJLEnh7c55WFgs4rnGsIh3koIMkrebxg= github.com/cloudquery/cloudquery-api-go v1.6.0/go.mod h1:03fojQg0UpdgqXZ9tzZ5gF5CPad/F0sok66bsX6u4RA= +github.com/cloudquery/cloudquery-api-go v1.5.1 h1:7CVbn8/A2bljPMjAUfFALI97ZnWOpRcAW/aCAr1FWYg= +github.com/cloudquery/cloudquery-api-go v1.5.1/go.mod h1:03fojQg0UpdgqXZ9tzZ5gF5CPad/F0sok66bsX6u4RA= github.com/cloudquery/plugin-pb-go v1.14.0 h1:q2eZzOLVUPBjDj6AG1Smc+xE3Usuc5JDaJxv6s4M60A= github.com/cloudquery/plugin-pb-go v1.14.0/go.mod h1:Ti4SRHVDau7oF1w0/Oimt1k45yJilD0oFE0Xr7Tf3AI= github.com/cloudquery/plugin-sdk/v2 v2.7.0 h1:hRXsdEiaOxJtsn/wZMFQC9/jPfU1MeMK3KF+gPGqm7U= diff --git a/premium/usage.go b/premium/usage.go index ef75af6482..d0ada1dda7 100644 --- a/premium/usage.go +++ b/premium/usage.go @@ -168,9 +168,10 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl op(u) } + tokenClient := auth.NewTokenClient() + // Create a default api client if none was provided if u.apiClient == nil { - tokenClient := auth.NewTokenClient() ac, err := cqapi.NewClientWithResponses(u.url, cqapi.WithRequestEditorFn(func(ctx context.Context, req *http.Request) error { token, err := tokenClient.GetToken() if err != nil { @@ -187,14 +188,9 @@ func NewUsageClient(pluginTeam cqapi.PluginTeam, pluginKind cqapi.PluginKind, pl // Set team name from configuration if not provided if u.teamName == "" { - teamName, err := config.GetValue("team") - if errors.Is(err, os.ErrNotExist) { - return nil, fmt.Errorf("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch `", err) - } else if err != nil { - return nil, fmt.Errorf("failed to get team name from config: %w", err) - } - if teamName == "" { - return nil, fmt.Errorf("team name not set. Hint: use `cloudquery switch `") + teamName, err := u.getTeamNameByTokenType(tokenClient.GetTokenType()) + if err != nil { + return nil, fmt.Errorf("failed to get team name: %w", err) } u.teamName = teamName } @@ -361,3 +357,33 @@ func (u *BatchUpdater) calculateRetryDuration(statusCode int, headers http.Heade func retryableStatusCode(statusCode int) bool { return statusCode == http.StatusTooManyRequests || statusCode == http.StatusServiceUnavailable } + +func (u *BatchUpdater) getTeamNameByTokenType(tokenType auth.TokenType) (string, error) { + switch tokenType { + case auth.BearerToken: + teamName, err := config.GetValue("team") + if errors.Is(err, os.ErrNotExist) { + return "", fmt.Errorf("config file for reading team name not found (%w). Hint: use `cloudquery login` and/or `cloudquery switch `", err) + } else if err != nil { + return "", fmt.Errorf("failed to get team name from config: %w", err) + } + if teamName == "" { + return "", fmt.Errorf("team name not set. Hint: use `cloudquery switch `") + } + return teamName, nil + case auth.APIKey: + resp, err := u.apiClient.ListTeamsWithResponse(context.Background(), &cqapi.ListTeamsParams{}) + if err != nil { + return "", fmt.Errorf("failed to list teams for API key: %w", err) + } + if resp.StatusCode() != http.StatusOK { + return "", fmt.Errorf("failed to list teams for API key, status code: %s", resp.Status()) + } + if len(resp.JSON200.Items) != 1 { + return "", fmt.Errorf("expected to find exactly one team for API key, found %d", len(resp.JSON200.Items)) + } + return resp.JSON200.Items[0].Name, nil + default: + return "", fmt.Errorf("unsupported token type: %v", tokenType) + } +} From 880939f56d72ff8808fe16d4fb84195d11d0970f Mon Sep 17 00:00:00 2001 From: CloudQuery Bot <102256036+cq-bot@users.noreply.github.com> Date: Fri, 17 Nov 2023 13:09:58 +0200 Subject: [PATCH 3/3] chore(main): Release v4.18.3 (#1376) :robot: I have created a release *beep* *boop* --- ## [4.18.3](https://github.com/cloudquery/plugin-sdk/compare/v4.18.2...v4.18.3) (2023-11-17) ### Bug Fixes * Retrieve team for api key ([#1372](https://github.com/cloudquery/plugin-sdk/issues/1372)) ([940d87f](https://github.com/cloudquery/plugin-sdk/commit/940d87f7cc71d8e2367c3f751dcf1d081e2b8126)) --- This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please). --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index b94b74ec46..c58746a46a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). +## [4.18.3](https://github.com/cloudquery/plugin-sdk/compare/v4.18.2...v4.18.3) (2023-11-17) + + +### Bug Fixes + +* Retrieve team for api key ([#1372](https://github.com/cloudquery/plugin-sdk/issues/1372)) ([940d87f](https://github.com/cloudquery/plugin-sdk/commit/940d87f7cc71d8e2367c3f751dcf1d081e2b8126)) + ## [4.18.2](https://github.com/cloudquery/plugin-sdk/compare/v4.18.1...v4.18.2) (2023-11-16)