Ver Fonte

better documentation and comments

Alexander Belanger há 5 anos atrás
pai
commit
1e0d301b91

+ 15 - 26
internal/kubernetes/kubeconfig.go

@@ -1,5 +1,7 @@
 package kubernetes
 
+import "github.com/porter-dev/porter/internal/models"
+
 // KubeConfigCluster represents the cluster field in a kubeconfig
 type KubeConfigCluster struct {
 	Cluster struct {
@@ -30,28 +32,15 @@ type KubeConfig struct {
 	Users          []KubeConfigUser    `yaml:"users"`
 }
 
-// ClusterConfig represents the configuration for a single cluster-user pair. This gets
-// associated with a specific user, and is primarily used for simplicity.
-type ClusterConfig struct {
-	// Name is the name of the cluster
-	Name,
-	// Server is the endpoint of the kube apiserver for a cluster
-	Server,
-	// Context is the name of the context
-	Context,
-	// User is the name of the user for a cluster
-	User string
-}
-
-// ToClusterConfigs converts a KubeConfig to a set of ClusterConfigs by
+// ToClusterConfigs converts a KubeConfig to a set of ClusterConfigExternals by
 // joining users and clusters on the context.
-func (k *KubeConfig) ToClusterConfigs() []*ClusterConfig {
-	clusters := make([]*ClusterConfig, 0)
+func (k *KubeConfig) ToClusterConfigs() []*models.ClusterConfigExternal {
+	clusters := make([]*models.ClusterConfigExternal, 0)
 
 	// convert clusters, contexts, and users to maps for fast lookup
-	clusterMap := k.CreateClusterMap()
-	contextMap := k.CreateContextMap()
-	userMap := k.CreateUserMap()
+	clusterMap := k.createClusterMap()
+	contextMap := k.createContextMap()
+	userMap := k.createUserMap()
 
 	// iterate through context maps and link to a user-cluster pair
 	for contextName, context := range contextMap {
@@ -61,7 +50,7 @@ func (k *KubeConfig) ToClusterConfigs() []*ClusterConfig {
 		cluster, clusterFound := clusterMap[clusterName]
 
 		if userFound && clusterFound {
-			clusters = append(clusters, &ClusterConfig{
+			clusters = append(clusters, &models.ClusterConfigExternal{
 				Name:    clusterName,
 				Server:  cluster.Cluster.Server,
 				Context: contextName,
@@ -73,8 +62,8 @@ func (k *KubeConfig) ToClusterConfigs() []*ClusterConfig {
 	return clusters
 }
 
-// CreateClusterMap creates a map from a cluster name to a KubeConfigCluster object
-func (k *KubeConfig) CreateClusterMap() map[string]KubeConfigCluster {
+// createClusterMap creates a map from a cluster name to a KubeConfigCluster object
+func (k *KubeConfig) createClusterMap() map[string]KubeConfigCluster {
 	clusterMap := make(map[string]KubeConfigCluster)
 
 	for _, cluster := range k.Clusters {
@@ -84,8 +73,8 @@ func (k *KubeConfig) CreateClusterMap() map[string]KubeConfigCluster {
 	return clusterMap
 }
 
-// CreateContextMap creates a map from a context name to a KubeConfigContext object
-func (k *KubeConfig) CreateContextMap() map[string]KubeConfigContext {
+// createContextMap creates a map from a context name to a KubeConfigContext object
+func (k *KubeConfig) createContextMap() map[string]KubeConfigContext {
 	contextMap := make(map[string]KubeConfigContext)
 
 	for _, context := range k.Contexts {
@@ -95,8 +84,8 @@ func (k *KubeConfig) CreateContextMap() map[string]KubeConfigContext {
 	return contextMap
 }
 
-// CreateUserMap creates a map from a user name to a KubeConfigUser object
-func (k *KubeConfig) CreateUserMap() map[string]KubeConfigUser {
+// createUserMap creates a map from a user name to a KubeConfigUser object
+func (k *KubeConfig) createUserMap() map[string]KubeConfigUser {
 	userMap := make(map[string]KubeConfigUser)
 
 	for _, user := range k.Users {

+ 10 - 1
internal/queries/user.go

@@ -5,10 +5,19 @@ import (
 	"gorm.io/gorm"
 )
 
-// CreateUser is majestic
+// CreateUser adds a new User row to the Users table in the database
 func CreateUser(db *gorm.DB, user *models.User) (*models.User, error) {
 	if err := db.Create(user).Error; err != nil {
 		return nil, err
 	}
 	return user, nil
 }
+
+// UpdateUser modifies an existing User in the database
+func UpdateUser(db *gorm.DB, user *models.User) error {
+	if err := db.First(&models.User{}, user.ID).Updates(user).Error; err != nil {
+		return err
+	}
+
+	return nil
+}

+ 4 - 8
server/api/api.go

@@ -5,18 +5,14 @@ import (
 	"gorm.io/gorm"
 )
 
-const (
-	appErrDataCreationFailure = "data creation failure"
-	appErrFormDecodingFailure = "form decoding failure"
-)
-
-// App is majestic
+// App represents an API instance with handler methods attached, a DB connection
+// and a logger instance
 type App struct {
 	logger *lr.Logger
 	db     *gorm.DB
 }
 
-// New is majestic
+// New returns a new App instance
 func New(
 	logger *lr.Logger,
 	db *gorm.DB,
@@ -27,7 +23,7 @@ func New(
 	}
 }
 
-// Logger is majestic
+// Logger returns the logger instance in use by App
 func (app *App) Logger() *lr.Logger {
 	return app.logger
 }

+ 23 - 0
server/api/errors.go

@@ -0,0 +1,23 @@
+package api
+
+import (
+	"fmt"
+	"net/http"
+)
+
+const (
+	appErrDataCreationFailure = "data creation failure"
+	appErrFormDecodingFailure = "form decoding failure"
+)
+
+func (app *App) handleUnprocessableEntity(err error, w http.ResponseWriter) {
+	app.logger.Warn().Err(err).Msg("")
+	w.WriteHeader(http.StatusUnprocessableEntity)
+	fmt.Fprintf(w, `{"error": "%v"}`, appErrFormDecodingFailure)
+}
+
+func (app *App) handleDataWriteFailure(err error, w http.ResponseWriter) {
+	app.logger.Warn().Err(err).Msg("")
+	w.WriteHeader(http.StatusInternalServerError)
+	fmt.Fprintf(w, `{"error": "%v"}`, appErrDataCreationFailure)
+}

+ 7 - 11
server/api/user_handler.go

@@ -2,7 +2,6 @@ package api
 
 import (
 	"encoding/json"
-	"fmt"
 	"net/http"
 
 	"github.com/porter-dev/porter/internal/queries"
@@ -10,30 +9,27 @@ import (
 	"github.com/porter-dev/porter/internal/models"
 )
 
-// HandleCreateUser is majestic
+// HandleCreateUser validates a user form entry, converts the user to a gorm
+// model, and saves the user to the database
 func (app *App) HandleCreateUser(w http.ResponseWriter, r *http.Request) {
 	form := &models.CreateUserForm{}
 
 	if err := json.NewDecoder(r.Body).Decode(form); err != nil {
-		app.logger.Warn().Err(err).Msg("")
-		w.WriteHeader(http.StatusUnprocessableEntity)
-		fmt.Fprintf(w, `{"error": "%v"}`, appErrFormDecodingFailure)
+		app.handleUnprocessableEntity(err, w)
 		return
 	}
 
 	userModel, err := form.ToUser()
+
 	if err != nil {
-		app.logger.Warn().Err(err).Msg("")
-		w.WriteHeader(http.StatusUnprocessableEntity)
-		fmt.Fprintf(w, `{"error": "%v"}`, appErrFormDecodingFailure)
+		app.handleUnprocessableEntity(err, w)
 		return
 	}
 
 	user, err := queries.CreateUser(app.db, userModel)
+
 	if err != nil {
-		app.logger.Warn().Err(err).Msg("")
-		w.WriteHeader(http.StatusInternalServerError)
-		fmt.Fprintf(w, `{"error": "%v"}`, appErrDataCreationFailure)
+		app.handleDataWriteFailure(err, w)
 		return
 	}