Skip to content

Commit

Permalink
feat: Implement API to get latest plugin version. (#415)
Browse files Browse the repository at this point in the history
Prerequisite to fully implement: cloudquery/cloudquery#19286

In order to let CLI users when the plugins they are using are not up to date, we need to now what the latest version is.

This PR only adds this functionality within `plugin-pb-go` within the `managedplugin` module, but it isn't yet used, nor compared to the user's plugin version.

Sample check on CLI to see if it works:

```
$ go run . test-connection /Users/mariano.gappa/Code/test-aws-sync/all.yml

Loading spec(s) from /Users/mariano.gappa/Code/test-aws-sync/all.yml
You chose version v27.23.0. Latest version is v27.23.1.
```

Also note that this implementation errors in a lot of cases, but it is not the intention to actually error to the client; since the purpose is to show a warning, we'll probably elide any warnings (it's debatable) if there's no sufficient information to show a warning.
  • Loading branch information
marianogappa authored Oct 14, 2024
1 parent e02834b commit eac217c
Showing 1 changed file with 50 additions and 0 deletions.
50 changes: 50 additions & 0 deletions managedplugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"time"

"github.com/avast/retry-go/v4"
cloudquery_api "github.com/cloudquery/cloudquery-api-go"
pbBase "github.com/cloudquery/plugin-pb-go/pb/base/v0"
pbDiscovery "github.com/cloudquery/plugin-pb-go/pb/discovery/v0"
pbDiscoveryV1 "github.com/cloudquery/plugin-pb-go/pb/discovery/v1"
Expand Down Expand Up @@ -686,6 +687,55 @@ func (c *Client) Versions(ctx context.Context) ([]int, error) {
return res, nil
}

func (c *Client) FindLatestPluginVersion(ctx context.Context, typ PluginType) (string, error) {
if c.config.Registry != RegistryCloudQuery {
return "", fmt.Errorf("plugin registry is not cloudquery; cannot find latest plugin version")
}

if c.teamName == "" {
return "", fmt.Errorf("team name is required to find the latest plugin version")
}

pathSplit := strings.Split(c.config.Path, "/")
if len(pathSplit) != 2 {
return "", fmt.Errorf("invalid cloudquery plugin path: %s. format should be team/name", c.config.Path)
}
org, name := pathSplit[0], pathSplit[1]

if org != "cloudquery" {
return "", fmt.Errorf("plugin org is not cloudquery; cannot find latest plugin version")
}

ops := HubDownloadOptions{
AuthToken: c.authToken,
TeamName: c.teamName,
LocalPath: c.LocalPath,
PluginTeam: org,
PluginKind: typ.String(),
PluginName: name,
PluginVersion: c.config.Version,
}
hubClient, err := getHubClient(c.logger, ops)
if err != nil {
return "", fmt.Errorf("failed to get hub client: %w", err)
}

resp, err := hubClient.GetPluginWithResponse(ctx, ops.PluginTeam, cloudquery_api.PluginKind(ops.PluginKind), ops.PluginName)
if err != nil {
return "", fmt.Errorf("failed to get plugin: %w", err)
}

if resp.JSON200 == nil {
return "", fmt.Errorf("failed to get latest plugin version: %w", err)
}

if resp.JSON200.LatestVersion == nil {
return "", nil // It's possible to have no latest version (unpublished plugins)
}

return *resp.JSON200.LatestVersion, nil
}

func (c *Client) MaxVersion(ctx context.Context) (int, error) {
discoveryClient := pbDiscovery.NewDiscoveryClient(c.Conn)
versionsRes, err := discoveryClient.GetVersions(ctx, &pbDiscovery.GetVersions_Request{})
Expand Down

0 comments on commit eac217c

Please sign in to comment.