2024-03-19 15:49:29 +00:00
|
|
|
package v1
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
|
2024-04-29 18:03:37 +00:00
|
|
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/kubernetes"
|
2024-04-29 17:34:55 +00:00
|
|
|
"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
|
2024-03-19 15:49:29 +00:00
|
|
|
proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
|
|
|
|
"github.com/golang/protobuf/ptypes/empty"
|
2024-04-03 18:05:23 +00:00
|
|
|
ctrl "sigs.k8s.io/controller-runtime"
|
2024-03-19 15:49:29 +00:00
|
|
|
)
|
|
|
|
|
2024-04-03 18:05:23 +00:00
|
|
|
func NewapiGrpcImpl(controller ctrl.Manager) *EnvironmentsServer {
|
|
|
|
return &EnvironmentsServer{
|
|
|
|
controller: controller,
|
|
|
|
}
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type EnvironmentsServer struct {
|
|
|
|
proto.UnimplementedEnvironmentsServer
|
2024-04-03 18:05:23 +00:00
|
|
|
controller ctrl.Manager
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 09:41:18 +00:00
|
|
|
// Create an environment
|
|
|
|
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
|
2024-04-30 07:47:00 +00:00
|
|
|
provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
|
2024-04-29 17:34:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-30 07:47:00 +00:00
|
|
|
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes().String())
|
2024-04-29 18:03:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
location, err := provider.GetServerLocation(in.GetSpec().GetServerLocation().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2024-04-29 17:34:55 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 18:03:37 +00:00
|
|
|
serverType, err := provider.GetServerType(in.Spec.ServerType.String())
|
2024-04-29 17:34:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-03-19 15:49:29 +00:00
|
|
|
data := &controllers.EnvironemntData{
|
2024-04-29 12:07:02 +00:00
|
|
|
Name: in.GetMetadata().GetName(),
|
|
|
|
Description: in.GetMetadata().GetDescription(),
|
2024-04-29 18:03:37 +00:00
|
|
|
Provider: provider.GetProviderName(),
|
|
|
|
Kubernetes: k8s.GetKubernetesName(),
|
2024-04-29 17:34:55 +00:00
|
|
|
Location: location,
|
|
|
|
ServerType: serverType,
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
2024-04-29 17:34:55 +00:00
|
|
|
|
2024-03-19 15:49:29 +00:00
|
|
|
environment := &controllers.Environemnt{
|
2024-04-29 09:41:18 +00:00
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
2024-04-03 18:05:23 +00:00
|
|
|
Controller: e.controller,
|
2024-03-19 15:49:29 +00:00
|
|
|
Data: data,
|
2024-04-29 09:41:18 +00:00
|
|
|
Token: in.GetToken().GetToken(),
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
2024-04-29 09:41:18 +00:00
|
|
|
|
2024-04-29 17:34:55 +00:00
|
|
|
err = environment.Create(ctx)
|
2024-03-19 15:49:29 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-30 08:03:06 +00:00
|
|
|
return &proto.EnvironmentFull{
|
|
|
|
Metadata: in.GetMetadata(),
|
2024-05-02 11:02:38 +00:00
|
|
|
Id: &proto.EnvironmentId{
|
|
|
|
Uuid: environment.Data.UUID,
|
|
|
|
},
|
|
|
|
Spec: in.GetSpec(),
|
2024-04-30 08:03:06 +00:00
|
|
|
}, nil
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 09:41:18 +00:00
|
|
|
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
|
2024-04-30 09:19:56 +00:00
|
|
|
provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k8s, err := kubernetes.GetKubernetes(in.GetSpec().GetKubernetes().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
location, err := provider.GetServerLocation(in.GetSpec().GetServerLocation().String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
serverType, err := provider.GetServerType(in.Spec.ServerType.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
data := &controllers.EnvironemntData{
|
|
|
|
Name: in.GetMetadata().GetName(),
|
2024-05-02 11:02:38 +00:00
|
|
|
UUID: in.GetId().GetUuid(),
|
2024-04-30 09:19:56 +00:00
|
|
|
Description: in.GetMetadata().GetDescription(),
|
|
|
|
Provider: provider.GetProviderName(),
|
|
|
|
Kubernetes: k8s.GetKubernetesName(),
|
|
|
|
Location: location,
|
|
|
|
ServerType: serverType,
|
|
|
|
}
|
|
|
|
|
|
|
|
environment := &controllers.Environemnt{
|
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
|
|
|
Controller: e.controller,
|
|
|
|
Data: data,
|
|
|
|
Token: in.GetToken().GetToken(),
|
|
|
|
}
|
|
|
|
|
|
|
|
err = environment.Update(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &proto.EnvironmentFull{
|
|
|
|
Metadata: in.GetMetadata(),
|
2024-05-02 11:02:38 +00:00
|
|
|
Id: in.GetId(),
|
2024-04-30 09:19:56 +00:00
|
|
|
Spec: in.GetSpec(),
|
|
|
|
}, nil
|
|
|
|
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 09:41:18 +00:00
|
|
|
func (e *EnvironmentsServer) Delete(ctx context.Context, in *proto.DeleteOptions) (*empty.Empty, error) {
|
2024-04-05 15:40:20 +00:00
|
|
|
data := &controllers.EnvironemntData{
|
2024-04-29 12:07:02 +00:00
|
|
|
Name: in.GetMetadata().GetName(),
|
2024-05-02 11:02:38 +00:00
|
|
|
UUID: in.GetId().GetUuid(),
|
2024-04-05 15:40:20 +00:00
|
|
|
}
|
2024-04-29 09:41:18 +00:00
|
|
|
|
2024-04-05 15:40:20 +00:00
|
|
|
environment := &controllers.Environemnt{
|
2024-04-29 09:41:18 +00:00
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
2024-04-05 15:40:20 +00:00
|
|
|
Controller: e.controller,
|
|
|
|
Data: data,
|
2024-04-29 09:41:18 +00:00
|
|
|
Token: in.GetToken().GetToken(),
|
2024-04-05 15:40:20 +00:00
|
|
|
}
|
2024-04-30 09:34:10 +00:00
|
|
|
err := environment.Delete(ctx)
|
2024-04-05 15:40:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-03-19 15:49:29 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-04-29 09:41:18 +00:00
|
|
|
func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*proto.EnvironmentFull, error) {
|
|
|
|
data := &controllers.EnvironemntData{
|
2024-05-02 11:02:38 +00:00
|
|
|
UUID: in.GetId().GetUuid(),
|
2024-04-19 14:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
environment := &controllers.Environemnt{
|
2024-04-29 09:41:18 +00:00
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
2024-04-19 14:23:53 +00:00
|
|
|
Controller: e.controller,
|
2024-04-29 09:41:18 +00:00
|
|
|
Data: data,
|
|
|
|
Token: in.GetToken().GetToken(),
|
2024-04-19 14:23:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := environment.Get(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-30 07:47:00 +00:00
|
|
|
provider, err := providers.GetProvider(environment.Data.Provider)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
k8s, err := kubernetes.GetKubernetes(environment.Data.Kubernetes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-04-19 14:23:53 +00:00
|
|
|
return &proto.EnvironmentFull{
|
2024-04-30 07:47:00 +00:00
|
|
|
Spec: &proto.EnvironmentSpec{
|
|
|
|
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
|
|
|
|
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
|
2024-04-30 08:20:46 +00:00
|
|
|
ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(environment.Data.Location)]),
|
|
|
|
ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(environment.Data.ServerType)]),
|
2024-04-30 07:47:00 +00:00
|
|
|
},
|
2024-05-02 11:02:38 +00:00
|
|
|
Id: in.GetId(),
|
2024-04-29 12:07:02 +00:00
|
|
|
Metadata: &proto.EnvironmentMetadata{
|
2024-04-30 07:47:00 +00:00
|
|
|
Name: environment.Data.Name,
|
|
|
|
Description: environment.Data.Description,
|
2024-04-19 14:23:53 +00:00
|
|
|
},
|
|
|
|
}, nil
|
2024-03-19 15:49:29 +00:00
|
|
|
}
|
|
|
|
|
2024-04-29 09:41:18 +00:00
|
|
|
func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environments_ListServer) error {
|
2024-04-15 13:45:05 +00:00
|
|
|
environment := &controllers.Environemnt{
|
2024-04-29 09:41:18 +00:00
|
|
|
UserID: in.GetOwnerId().GetUuid(),
|
2024-04-15 13:45:05 +00:00
|
|
|
Controller: e.controller,
|
2024-04-29 09:41:18 +00:00
|
|
|
Token: in.GetToken().GetToken(),
|
2024-04-15 13:45:05 +00:00
|
|
|
}
|
2024-04-29 09:41:18 +00:00
|
|
|
|
2024-04-15 13:45:05 +00:00
|
|
|
envs, err := environment.ListEnvs(stream.Context())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, env := range envs {
|
2024-04-30 08:47:40 +00:00
|
|
|
provider, err := providers.GetProvider(env.Data.Provider)
|
2024-04-30 08:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-30 08:47:40 +00:00
|
|
|
k8s, err := kubernetes.GetKubernetes(env.Data.Kubernetes)
|
2024-04-30 08:00:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-04-15 13:45:05 +00:00
|
|
|
if err := stream.Send(&proto.EnvironmentFull{
|
2024-04-29 12:07:02 +00:00
|
|
|
Metadata: &proto.EnvironmentMetadata{
|
2024-04-30 08:00:06 +00:00
|
|
|
Name: env.Data.Name,
|
|
|
|
Description: env.Data.Description,
|
|
|
|
},
|
2024-05-02 11:02:38 +00:00
|
|
|
Id: &proto.EnvironmentId{
|
|
|
|
Uuid: env.Data.UUID,
|
|
|
|
},
|
2024-04-30 08:00:06 +00:00
|
|
|
Spec: &proto.EnvironmentSpec{
|
|
|
|
Provider: proto.Provider(proto.Provider_value[provider.RawProviderName()]),
|
|
|
|
Kubernetes: proto.Kubernetes(proto.Kubernetes_value[k8s.RawKubernetesName()]),
|
2024-04-30 08:47:40 +00:00
|
|
|
ServerLocation: proto.Location(proto.Location_value[provider.RawServerLocation(env.Data.Location)]),
|
|
|
|
ServerType: proto.ServerType(proto.ServerType_value[provider.RawServerType(env.Data.ServerType)]),
|
2024-04-15 13:45:05 +00:00
|
|
|
},
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2024-03-19 15:49:29 +00:00
|
|
|
return nil
|
|
|
|
}
|