From c49e885255db7c0aa460a8b795f9da0aec2ab423 Mon Sep 17 00:00:00 2001 From: Nikolai Rodionov Date: Tue, 30 Apr 2024 10:20:46 +0200 Subject: [PATCH] Return the created env on create req --- api/v1/environments.go | 28 ++++------------------------ internal/controllers/environments.go | 1 + internal/providers/common.go | 4 ++-- internal/providers/hetzner.go | 27 +++++++++++++-------------- 4 files changed, 20 insertions(+), 40 deletions(-) diff --git a/api/v1/environments.go b/api/v1/environments.go index a1fc52f..f28154d 100644 --- a/api/v1/environments.go +++ b/api/v1/environments.go @@ -113,16 +113,6 @@ func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*pr return nil, err } - serverType, err := provider.RawServerType(environment.Data.ServerType) - if err != nil { - return nil, err - } - - location, err := provider.RawServerLocation(environment.Data.Location) - if err != nil { - return nil, err - } - k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes) if err != nil { return nil, err @@ -132,8 +122,8 @@ func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*pr Spec: &proto.EnvironmentSpec{ Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]), Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]), - ServerLocation: proto.Location(proto.Location_value[location]), - ServerType: proto.ServerType(proto.ServerType_value[serverType]), + ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(environment.Data.Location)]), + ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(environment.Data.ServerType)]), }, Metadata: &proto.EnvironmentMetadata{ Name: environment.Data.Name, @@ -160,16 +150,6 @@ func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environmen return err } - serverType, err := provider.RawServerType(environment.Data.ServerType) - if err != nil { - return err - } - - location, err := provider.RawServerLocation(environment.Data.Location) - if err != nil { - return err - } - k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes) if err != nil { return err @@ -183,8 +163,8 @@ func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environmen Spec: &proto.EnvironmentSpec{ Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]), Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]), - ServerLocation: proto.Location(proto.Location_value[location]), - ServerType: proto.ServerType(proto.ServerType_value[serverType]), + ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(environment.Data.Location)]), + ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(environment.Data.ServerType)]), }, }); err != nil { return err diff --git a/internal/controllers/environments.go b/internal/controllers/environments.go index 3d7d930..6920daa 100644 --- a/internal/controllers/environments.go +++ b/internal/controllers/environments.go @@ -171,6 +171,7 @@ func (env *Environemnt) ListEnvs(ctx context.Context) ([]*Environemnt, error) { i.Token = env.Token i.UserID = env.UserID i.Data = data + i.Controller = env.Controller if err := i.Get(ctx); err != nil { return nil, err } diff --git a/internal/providers/common.go b/internal/providers/common.go index b55b89d..f2e671e 100644 --- a/internal/providers/common.go +++ b/internal/providers/common.go @@ -11,8 +11,8 @@ type Providers interface { RawProviderName() string GetServerType(string) (string, error) GetServerLocation(string) (string, error) - RawServerType(string) (string, error) - RawServerLocation(string) (string, error) + RawServerType(string) string + RawServerLocation(string) string } func GetProvider(provider string) (Providers, error) { diff --git a/internal/providers/hetzner.go b/internal/providers/hetzner.go index 6613e86..47d84d2 100644 --- a/internal/providers/hetzner.go +++ b/internal/providers/hetzner.go @@ -21,38 +21,37 @@ func (h *Hetzner) RawProviderName() string { } // RawServerLocation implements Providers. -func (h *Hetzner) RawServerLocation(location string) (string, error) { +func (h *Hetzner) RawServerLocation(location string) string { switch location { case "ash": - return proto.Location_LOCATION_HETZNER_ASHBURN.String(), nil + return proto.Location_LOCATION_HETZNER_ASHBURN.String() case "hil": - return proto.Location_LOCATION_HETZNER_HILLSBORO.String(), nil + return proto.Location_LOCATION_HETZNER_HILLSBORO.String() case "fsn1": - return proto.Location_LOCATION_HETZNER_FALKENSTEIN.String(), nil + return proto.Location_LOCATION_HETZNER_FALKENSTEIN.String() case "nbg1": - return proto.Location_LOCATION_HETZNER_NUREMBERG.String(), nil + return proto.Location_LOCATION_HETZNER_NUREMBERG.String() case "hel1": - return proto.Location_LOCATION_HETZNER_HELSINKI.String(), nil + return proto.Location_LOCATION_HETZNER_HELSINKI.String() default: - return "", fmt.Errorf("unknown location: %s", location) + return proto.Location_LOCATION_UNSPECIFIED.String() } } // RawServerType implements Providers. -func (h *Hetzner) RawServerType(kind string) (string, error) { +func (h *Hetzner) RawServerType(kind string) string { switch kind { case "cpx21": - return proto.ServerType_SERVER_TYPE_STARTER.String(), nil + return proto.ServerType_SERVER_TYPE_STARTER.String() case "cpx31": - return proto.ServerType_SERVER_TYPE_REGULAR.String(), nil + return proto.ServerType_SERVER_TYPE_REGULAR.String() case "cpx41": - return proto.ServerType_SERVER_TYPE_PLUS.String(), nil + return proto.ServerType_SERVER_TYPE_PLUS.String() case "cpx51": - return proto.ServerType_SERVER_TYPE_PRO.String(), nil + return proto.ServerType_SERVER_TYPE_PRO.String() default: - err := fmt.Errorf("unknown server type: %s", kind) - return "", err + return proto.ServerType_SERVER_TYPE_UNSPECIFIED.String() } }