vet/internal/auth/trial.go
Abhisek Datta a18c204b5d
Sync Develop to Main (#4)
* Update Insight service API and client

* Add cli banner

* Show API errors from insight API

* Use standard error model

* Add reporting interface

* Update markdown template

* Add trials registration client

* Add trials registration support

* Add supported ecosystem filter to parsers

* Update OSV scanner

* Use table renderer for CEL filter output

* Rename filter opt to filter

* Add an opinionated console summary reporter

* Update README

* Update README

* Update README

* Add filter spec

* Update spec driven CEL filtering

* Add query workflow with docs

* Add secrets scan workflow
2023-02-03 12:30:48 +05:30

78 lines
1.8 KiB
Go

package auth
import (
"context"
"errors"
"fmt"
"net/http"
"time"
"github.com/deepmap/oapi-codegen/pkg/types"
"github.com/safedep/dry/utils"
"github.com/safedep/vet/gen/controlplane"
"github.com/safedep/vet/pkg/common/logger"
apierr "github.com/safedep/dry/errors"
)
type TrialConfig struct {
Email string
ControlPlaneApiUrl string
}
type trialRegistrationResponse struct {
Id string
ExpiresAt time.Time
}
type trialRegistrationClient struct {
config TrialConfig
}
func NewTrialRegistrationClient(config TrialConfig) *trialRegistrationClient {
return &trialRegistrationClient{config: config}
}
func (client *trialRegistrationClient) Execute() (*trialRegistrationResponse, error) {
if utils.IsEmptyString(client.config.Email) {
return nil, errors.New("email is required")
}
if utils.IsEmptyString(client.config.ControlPlaneApiUrl) {
return nil, errors.New("control plane API is required")
}
logger.Infof("Trial registrations using Control Plane: %s",
client.config.ControlPlaneApiUrl)
cpClient, err := controlplane.NewClientWithResponses(client.config.ControlPlaneApiUrl)
if err != nil {
return nil, err
}
logger.Infof("Trial registration requesting API key for: %s",
client.config.Email)
res, err := cpClient.RegisterTrialUserWithResponse(context.Background(),
controlplane.RegisterTrialUserJSONRequestBody{
Email: types.Email(client.config.Email),
})
if err != nil {
return nil, err
}
if res.HTTPResponse.StatusCode != http.StatusCreated {
if err, ok := apierr.UnmarshalApiError(res.Body); ok {
return nil, err
} else {
return nil, fmt.Errorf("unexpected status code:%d from control plane",
res.HTTPResponse.StatusCode)
}
}
return &trialRegistrationResponse{
Id: utils.SafelyGetValue(res.JSON201.Id),
ExpiresAt: utils.SafelyGetValue(res.JSON201.ExpiresAt),
}, nil
}