A small refactoring
This commit is contained in:
		@@ -8,7 +8,9 @@ import (
 | 
				
			|||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
 | 
				
			||||||
	proto_email "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
 | 
						proto_email "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/email"
 | 
				
			||||||
	"google.golang.org/grpc"
 | 
						"google.golang.org/grpc"
 | 
				
			||||||
 | 
						"google.golang.org/grpc/codes"
 | 
				
			||||||
	"google.golang.org/grpc/metadata"
 | 
						"google.golang.org/grpc/metadata"
 | 
				
			||||||
 | 
						"google.golang.org/grpc/status"
 | 
				
			||||||
	"google.golang.org/protobuf/types/known/emptypb"
 | 
						"google.golang.org/protobuf/types/known/emptypb"
 | 
				
			||||||
	ctrl "sigs.k8s.io/controller-runtime"
 | 
						ctrl "sigs.k8s.io/controller-runtime"
 | 
				
			||||||
)
 | 
					)
 | 
				
			||||||
@@ -17,6 +19,7 @@ type EmailServer struct {
 | 
				
			|||||||
	proto_email.UnimplementedEmailValidationServer
 | 
						proto_email.UnimplementedEmailValidationServer
 | 
				
			||||||
	emailConfig email.EmailConf
 | 
						emailConfig email.EmailConf
 | 
				
			||||||
	controller  ctrl.Manager
 | 
						controller  ctrl.Manager
 | 
				
			||||||
 | 
						// When dev mode is enabled, email won't be sent, instead the code will be returned in metadata
 | 
				
			||||||
	devMode bool
 | 
						devMode bool
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@@ -28,7 +31,14 @@ func InitEmailServer(controller ctrl.Manager, emailConfig *email.EmailConf, devM
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Send the validation code to email
 | 
				
			||||||
func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestValidation) (*emptypb.Empty, error) {
 | 
					func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestValidation) (*emptypb.Empty, error) {
 | 
				
			||||||
 | 
						// Validation
 | 
				
			||||||
 | 
						if len(in.GetUserId()) == 0 {
 | 
				
			||||||
 | 
							return nil, status.Error(codes.InvalidArgument, "user id must not be empty")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Body
 | 
				
			||||||
	emailSvc := controllers.EmailSvc{
 | 
						emailSvc := controllers.EmailSvc{
 | 
				
			||||||
		Data: controllers.EmailData{
 | 
							Data: controllers.EmailData{
 | 
				
			||||||
			UserID: in.GetUserId(),
 | 
								UserID: in.GetUserId(),
 | 
				
			||||||
@@ -46,15 +56,20 @@ func (c *EmailServer) SendRequest(ctx context.Context, in *proto_email.RequestVa
 | 
				
			|||||||
		if err := grpc.SendHeader(ctx, header); err != nil {
 | 
							if err := grpc.SendHeader(ctx, header); err != nil {
 | 
				
			||||||
			return nil, err
 | 
								return nil, err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
		trailer := metadata.Pairs("trailer-key", "val")
 | 
					 | 
				
			||||||
		if err := grpc.SetTrailer(ctx, trailer); err != nil {
 | 
					 | 
				
			||||||
			return nil, err
 | 
					 | 
				
			||||||
		}
 | 
					 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	return &emptypb.Empty{}, nil
 | 
						return &emptypb.Empty{}, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (c *EmailServer) ValidateEmail(ctx context.Context, in *proto_email.ConfirmValidation) (*emptypb.Empty, error) {
 | 
					func (c *EmailServer) ValidateEmail(ctx context.Context, in *proto_email.ConfirmValidation) (*emptypb.Empty, error) {
 | 
				
			||||||
 | 
						// Validation
 | 
				
			||||||
 | 
						if len(in.GetUserId()) == 0 {
 | 
				
			||||||
 | 
							return nil, status.Error(codes.InvalidArgument, "user id must not be empty")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
						if in.GetCode() == 0 {
 | 
				
			||||||
 | 
							return nil, status.Error(codes.InvalidArgument, "code must not be empty")
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
						// Body
 | 
				
			||||||
	emailSvc := controllers.EmailSvc{
 | 
						emailSvc := controllers.EmailSvc{
 | 
				
			||||||
		Data: controllers.EmailData{
 | 
							Data: controllers.EmailData{
 | 
				
			||||||
			UserID: in.GetUserId(),
 | 
								UserID: in.GetUserId(),
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -4,8 +4,8 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/controllers"
 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/kubernetes"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/infra"
 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/providers/kubernetes"
 | 
				
			||||||
	proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
						proto "git.badhouseplants.net/softplayer/softplayer-go-proto/pkg/environments"
 | 
				
			||||||
	"github.com/golang/protobuf/ptypes/empty"
 | 
						"github.com/golang/protobuf/ptypes/empty"
 | 
				
			||||||
	ctrl "sigs.k8s.io/controller-runtime"
 | 
						ctrl "sigs.k8s.io/controller-runtime"
 | 
				
			||||||
@@ -24,7 +24,7 @@ type EnvironmentsServer struct {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
// Create an environment
 | 
					// Create an environment
 | 
				
			||||||
func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
 | 
					func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions) (*proto.EnvironmentFull, error) {
 | 
				
			||||||
	provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
 | 
						provider, err := infra.GetProvider(in.GetSpec().GetProvider().String())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -74,7 +74,7 @@ func (e *EnvironmentsServer) Create(ctx context.Context, in *proto.CreateOptions
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
 | 
					func (e *EnvironmentsServer) Update(ctx context.Context, in *proto.UpdateOptions) (*proto.EnvironmentFull, error) {
 | 
				
			||||||
	provider, err := providers.GetProvider(in.GetSpec().GetProvider().String())
 | 
						provider, err := infra.GetProvider(in.GetSpec().GetProvider().String())
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -159,7 +159,7 @@ func (e *EnvironmentsServer) Get(ctx context.Context, in *proto.GetOptions) (*pr
 | 
				
			|||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	provider, err := providers.GetProvider(environment.Data.Provider)
 | 
						provider, err := infra.GetProvider(environment.Data.Provider)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return nil, err
 | 
							return nil, err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
@@ -197,7 +197,7 @@ func (e *EnvironmentsServer) List(in *proto.ListOptions, stream proto.Environmen
 | 
				
			|||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	for _, env := range envs {
 | 
						for _, env := range envs {
 | 
				
			||||||
		provider, err := providers.GetProvider(env.Data.Provider)
 | 
							provider, err := infra.GetProvider(env.Data.Provider)
 | 
				
			||||||
		if err != nil {
 | 
							if err != nil {
 | 
				
			||||||
			return err
 | 
								return err
 | 
				
			||||||
		}
 | 
							}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -7,9 +7,9 @@ import (
 | 
				
			|||||||
	"os"
 | 
						"os"
 | 
				
			||||||
	"strings"
 | 
						"strings"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/helmhelper"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/helm"
 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/helmrelease"
 | 
					 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/kube"
 | 
				
			||||||
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/types/helmrelease"
 | 
				
			||||||
	"github.com/google/uuid"
 | 
						"github.com/google/uuid"
 | 
				
			||||||
	"github.com/sirupsen/logrus"
 | 
						"github.com/sirupsen/logrus"
 | 
				
			||||||
	"gopkg.in/yaml.v2"
 | 
						"gopkg.in/yaml.v2"
 | 
				
			||||||
@@ -62,8 +62,8 @@ func (app *Application) Create(ctx context.Context) error {
 | 
				
			|||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	helm := helmhelper.NewHelm()
 | 
						helmEntry := helm.NewHelm()
 | 
				
			||||||
	release := &helmhelper.ReleaseData{
 | 
						release := &helm.ReleaseData{
 | 
				
			||||||
		Name:           app.Data.Name,
 | 
							Name:           app.Data.Name,
 | 
				
			||||||
		Chart:          app.Data.Application,
 | 
							Chart:          app.Data.Application,
 | 
				
			||||||
		Version:        app.Data.Version,
 | 
							Version:        app.Data.Version,
 | 
				
			||||||
@@ -75,7 +75,7 @@ func (app *Application) Create(ctx context.Context) error {
 | 
				
			|||||||
	if err := os.MkdirAll(goPath, 0777); err != nil {
 | 
						if err := os.MkdirAll(goPath, 0777); err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
	}
 | 
						}
 | 
				
			||||||
	path, err := helm.PullChart(goPath, release)
 | 
						path, err := helmEntry.PullChart(goPath, release)
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		logrus.Error("0")
 | 
							logrus.Error("0")
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -34,6 +34,7 @@ type EnvironemntData struct {
 | 
				
			|||||||
	Kubernetes  string
 | 
						Kubernetes  string
 | 
				
			||||||
	Location    string
 | 
						Location    string
 | 
				
			||||||
	ServerType  string
 | 
						ServerType  string
 | 
				
			||||||
 | 
						DiskSize    int
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func (e *EnvironemntData) buildVars() (string, error) {
 | 
					func (e *EnvironemntData) buildVars() (string, error) {
 | 
				
			||||||
@@ -41,16 +42,19 @@ func (e *EnvironemntData) buildVars() (string, error) {
 | 
				
			|||||||
SP_PROVIDER=%s
 | 
					SP_PROVIDER=%s
 | 
				
			||||||
SP_KUBERNETES=%s
 | 
					SP_KUBERNETES=%s
 | 
				
			||||||
SP_SERVER_TYPE=%s
 | 
					SP_SERVER_TYPE=%s
 | 
				
			||||||
SP_SERVER_LOCATION=%s`,
 | 
					SP_SERVER_LOCATION=%s
 | 
				
			||||||
 | 
					SP_DISK_SIZE=%d`,
 | 
				
			||||||
		e.Provider,
 | 
							e.Provider,
 | 
				
			||||||
		e.Kubernetes,
 | 
							e.Kubernetes,
 | 
				
			||||||
		e.ServerType,
 | 
							e.ServerType,
 | 
				
			||||||
		e.Location,
 | 
							e.Location,
 | 
				
			||||||
 | 
							e.DiskSize,
 | 
				
			||||||
	)
 | 
						)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	return vars, nil
 | 
						return vars, nil
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					// Check whether used has passed the email verification
 | 
				
			||||||
func (env *Environemnt) isNsVerified(ctx context.Context) error {
 | 
					func (env *Environemnt) isNsVerified(ctx context.Context) error {
 | 
				
			||||||
	client := env.Controller.GetClient()
 | 
						client := env.Controller.GetClient()
 | 
				
			||||||
	ns := &corev1.Namespace{}
 | 
						ns := &corev1.Namespace{}
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package helmhelper
 | 
					package helm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package helmhelper
 | 
					package helm
 | 
				
			||||||
 | 
					
 | 
				
			||||||
type Helmhelper interface {
 | 
					type Helmhelper interface {
 | 
				
			||||||
	FindLatestVersion(workdirPath string, release *ReleaseData) (string, error)
 | 
						FindLatestVersion(workdirPath string, release *ReleaseData) (string, error)
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package providers
 | 
					package infra
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
@@ -1,4 +1,4 @@
 | 
				
			|||||||
package providers
 | 
					package infra
 | 
				
			||||||
 | 
					
 | 
				
			||||||
import (
 | 
					import (
 | 
				
			||||||
	"errors"
 | 
						"errors"
 | 
				
			||||||
							
								
								
									
										7
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										7
									
								
								main.go
									
									
									
									
									
								
							@@ -4,6 +4,7 @@ import (
 | 
				
			|||||||
	"context"
 | 
						"context"
 | 
				
			||||||
	"fmt"
 | 
						"fmt"
 | 
				
			||||||
	"net"
 | 
						"net"
 | 
				
			||||||
 | 
						"os"
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1"
 | 
						v1 "git.badhouseplants.net/softplayer/softplayer-backend/api/v1"
 | 
				
			||||||
	"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
 | 
						"git.badhouseplants.net/softplayer/softplayer-backend/internal/helpers/email"
 | 
				
			||||||
@@ -27,6 +28,7 @@ type Serve struct {
 | 
				
			|||||||
	SmtpPort     string `env:"SOFTPLAYER_SMTP_PORT" default:"587"`
 | 
						SmtpPort     string `env:"SOFTPLAYER_SMTP_PORT" default:"587"`
 | 
				
			||||||
	SmtpFrom     string `env:"SOFTPLAYER_SMTP_FROM" default:"overlord@badhouseplants.net"`
 | 
						SmtpFrom     string `env:"SOFTPLAYER_SMTP_FROM" default:"overlord@badhouseplants.net"`
 | 
				
			||||||
	SmtpPassword string `env:"SOFTPLAYER_SMTP_PASSWORD"`
 | 
						SmtpPassword string `env:"SOFTPLAYER_SMTP_PASSWORD"`
 | 
				
			||||||
 | 
						DownloadDir  string `env:"SOFTPLAYER_DOWNLOAD_DIR" default:"/tmp/softplayer"`
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
var CLI struct {
 | 
					var CLI struct {
 | 
				
			||||||
@@ -46,6 +48,11 @@ func main() {
 | 
				
			|||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
func server(params Serve) error {
 | 
					func server(params Serve) error {
 | 
				
			||||||
 | 
						// Make sure the download dir exists
 | 
				
			||||||
 | 
						if err := os.MkdirAll(params.DownloadDir, 0777); err != nil {
 | 
				
			||||||
 | 
							return err
 | 
				
			||||||
 | 
						}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
	controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
 | 
						controller, err := ctrl.NewManager(ctrl.GetConfigOrDie(), ctrl.Options{})
 | 
				
			||||||
	if err != nil {
 | 
						if err != nil {
 | 
				
			||||||
		return err
 | 
							return err
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user