Allow promoting any vehicle config to cloud usage (#2549)

This commit is contained in:
andig 2022-02-11 08:45:02 +01:00 • committed by GitHub
parent ff193a6211
commit da94c8761f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 29 additions and 11 deletions

View file

@ -86,10 +86,9 @@ func configureSponsorship(token string) error {
defer cancel()
res, err := client.IsAuthorized(ctx, &pb.AuthRequest{Token: token})
if err == nil {
if res.Authorized {
sponsor.Subject = res.Subject
}
if err == nil && res.Authorized {
sponsor.Subject = res.Subject
sponsor.Token = token
}
if err != nil {

View file

@ -10,6 +10,7 @@ import (
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/sponsor"
"github.com/evcc-io/evcc/vehicle"
)
@ -49,6 +50,10 @@ func main() {
case "action":
action = arg
key = ""
case "token":
sponsor.Subject = arg // TODO placeholder
sponsor.Token = arg
key = ""
default:
params[key] = arg
key = ""

View file

@ -1,6 +1,6 @@
package sponsor
var Subject string
var Subject, Token string
func IsAuthorized() bool {
return len(Subject) > 0

View file

@ -2,7 +2,6 @@ package vehicle
import (
"context"
"errors"
"strings"
"time"
@ -12,6 +11,7 @@ import (
"github.com/evcc-io/evcc/util"
"github.com/evcc-io/evcc/util/cloud"
"github.com/evcc-io/evcc/util/request"
"github.com/evcc-io/evcc/util/sponsor"
)
// Cloud is an api.Vehicle implementation
@ -32,7 +32,6 @@ func init() {
// NewCloudFromConfig creates a new vehicle
func NewCloudFromConfig(other map[string]interface{}) (api.Vehicle, error) {
cc := struct {
Token string
embed `mapstructure:",squash"`
Brand string
Other map[string]string `mapstructure:",remain"`
@ -45,8 +44,8 @@ func NewCloudFromConfig(other map[string]interface{}) (api.Vehicle, error) {
return nil, err
}
if cc.Token == "" {
return nil, errors.New("missing required token")
if !sponsor.IsAuthorized() {
return nil, api.ErrSponsorRequired
}
host := util.Getenv("GRPC_URI", cloud.Host)
@ -57,7 +56,7 @@ func NewCloudFromConfig(other map[string]interface{}) (api.Vehicle, error) {
v := &Cloud{
embed: &cc.embed,
token: cc.Token,
token: sponsor.Token,
brand: cc.Brand,
config: cc.Other,
client: pb.NewVehicleClient(conn),

View file

@ -6,6 +6,7 @@ import (
"time"
"github.com/evcc-io/evcc/api"
"github.com/evcc-io/evcc/util"
)
const (
@ -43,9 +44,23 @@ func Types() []string {
// NewFromConfig creates vehicle from configuration
func NewFromConfig(typ string, other map[string]interface{}) (v api.Vehicle, err error) {
cc := struct {
Cloud bool
Other map[string]interface{} `mapstructure:",remain"`
}{}
if err := util.DecodeOther(other, &cc); err != nil {
return nil, err
}
if cc.Cloud {
cc.Other["brand"] = typ
typ = "cloud"
}
factory, err := registry.Get(strings.ToLower(typ))
if err == nil {
if v, err = factory(other); err != nil {
if v, err = factory(cc.Other); err != nil {
err = fmt.Errorf("cannot create vehicle '%s': %w", typ, err)
}
} else {