Просмотр исходного кода

remove all externalize statements and move to types

Alexander Belanger 4 лет назад
Родитель
Сommit
0676cb62a9

+ 1 - 1
api/server/handlers/release/create_subdomain.go

@@ -74,5 +74,5 @@ func (c *CreateSubdomainHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		return
 	}
 
-	c.WriteResult(w, r, record.Externalize())
+	c.WriteResult(w, r, record.ToDNSRecordType())
 }

+ 8 - 6
api/server/handlers/release/get_notifications.go

@@ -43,10 +43,12 @@ func (c *GetNotificationHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 		return
 	}
 
-	config := &models.NotificationConfigExternal{
-		Enabled: true,
-		Success: true,
-		Failure: true,
+	res := &types.GetNotificationConfigResponse{
+		NotificationConfig: &types.NotificationConfig{
+			Enabled: true,
+			Success: true,
+			Failure: true,
+		},
 	}
 
 	if release.NotificationConfig != 0 {
@@ -57,8 +59,8 @@ func (c *GetNotificationHandler) ServeHTTP(w http.ResponseWriter, r *http.Reques
 			return
 		}
 
-		config = notifConfig.Externalize()
+		res.NotificationConfig = notifConfig.ToNotificationConfigType()
 	}
 
-	c.WriteResult(w, r, config)
+	c.WriteResult(w, r, res)
 }

+ 2 - 2
api/server/handlers/release/ugprade.go

@@ -112,7 +112,7 @@ func (c *UpgradeReleaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
 
 	rel, releaseErr := c.Repo().Release().ReadRelease(cluster.ID, helmRelease.Name, helmRelease.Namespace)
 
-	var notifConf *models.NotificationConfigExternal
+	var notifConf *types.NotificationConfig
 	notifConf = nil
 	if rel != nil && rel.NotificationConfig != 0 {
 		conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(rel.NotificationConfig)
@@ -122,7 +122,7 @@ func (c *UpgradeReleaseHandler) ServeHTTP(w http.ResponseWriter, r *http.Request
 			return
 		}
 
-		notifConf = conf.Externalize()
+		notifConf = conf.ToNotificationConfigType()
 	}
 
 	notifier := slack.NewSlackNotifier(notifConf, slackInts...)

+ 2 - 3
api/server/handlers/release/upgrade_webhook.go

@@ -14,7 +14,6 @@ import (
 	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/helm"
 	"github.com/porter-dev/porter/internal/integrations/slack"
-	"github.com/porter-dev/porter/internal/models"
 )
 
 type WebhookHandler struct {
@@ -112,7 +111,7 @@ func (c *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 
 	slackInts, _ := c.Repo().SlackIntegration().ListSlackIntegrationsByProjectID(release.ProjectID)
 
-	var notifConf *models.NotificationConfigExternal
+	var notifConf *types.NotificationConfig
 	notifConf = nil
 	if release != nil && release.NotificationConfig != 0 {
 		conf, err := c.Repo().NotificationConfig().ReadNotificationConfig(release.NotificationConfig)
@@ -122,7 +121,7 @@ func (c *WebhookHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
 			return
 		}
 
-		notifConf = conf.Externalize()
+		notifConf = conf.ToNotificationConfigType()
 	}
 
 	notifier := slack.NewSlackNotifier(notifConf, slackInts...)

+ 19 - 0
api/types/release.go

@@ -108,3 +108,22 @@ type UpdateReleaseStepsRequest struct {
 		Info   string      `json:"info" form:"required"`
 	} `json:"event" form:"required"`
 }
+
+type NotificationConfig struct {
+	Enabled bool `json:"enabled"`
+	Success bool `json:"success"`
+	Failure bool `json:"failure"`
+}
+
+type GetNotificationConfigResponse struct {
+	*NotificationConfig
+}
+
+type DNSRecord struct {
+	ExternalURL string `json:"external_url"`
+
+	Endpoint string `json:"endpoint"`
+	Hostname string `json:"hostname"`
+
+	ClusterID uint `json:"cluster_id"`
+}

+ 3 - 3
internal/integrations/slack/notifier.go

@@ -4,11 +4,11 @@ import (
 	"bytes"
 	"encoding/json"
 	"fmt"
-	"github.com/porter-dev/porter/internal/models"
 	"net/http"
 	"strings"
 	"time"
 
+	"github.com/porter-dev/porter/api/types"
 	"github.com/porter-dev/porter/internal/models/integrations"
 )
 
@@ -53,10 +53,10 @@ type NotifyOpts struct {
 
 type SlackNotifier struct {
 	slackInts []*integrations.SlackIntegration
-	Config    *models.NotificationConfigExternal
+	Config    *types.NotificationConfig
 }
 
-func NewSlackNotifier(conf *models.NotificationConfigExternal, slackInts ...*integrations.SlackIntegration) Notifier {
+func NewSlackNotifier(conf *types.NotificationConfig, slackInts ...*integrations.SlackIntegration) Notifier {
 	return &SlackNotifier{
 		slackInts: slackInts,
 		Config:    conf,

+ 0 - 35
internal/models/cluster_test.go

@@ -1,35 +0,0 @@
-package models_test
-
-import (
-	"encoding/json"
-	"testing"
-
-	"github.com/go-test/deep"
-	"github.com/porter-dev/porter/api/types"
-	"github.com/porter-dev/porter/internal/models"
-)
-
-func TestClusterResolverExternalize(t *testing.T) {
-	crData := types.ClusterResolverData{
-		"filename": "/hello/there.pem",
-		"key":      "value",
-	}
-
-	bytes, err := json.Marshal(crData)
-
-	if err != nil {
-		t.Fatalf("%v\n", err)
-	}
-
-	// test that the data gets unmarshalled properly
-	cr := &models.ClusterResolver{
-		Data: bytes,
-	}
-
-	crExternal := cr.ToClusterResolverType()
-
-	if diff := deep.Equal(crExternal.Data, crData); diff != nil {
-		t.Errorf("incorrect cluster resolver data")
-		t.Error(diff)
-	}
-}

+ 3 - 13
internal/models/dns_record.go

@@ -3,6 +3,7 @@ package models
 import (
 	"fmt"
 
+	"github.com/porter-dev/porter/api/types"
 	"gorm.io/gorm"
 )
 
@@ -19,19 +20,8 @@ type DNSRecord struct {
 	ClusterID uint `json:"cluster_id"`
 }
 
-// DNSRecordExternal represents the DNSRecord type that is sent over REST
-type DNSRecordExternal struct {
-	ExternalURL string `json:"external_url"`
-
-	Endpoint string `json:"endpoint"`
-	Hostname string `json:"hostname"`
-
-	ClusterID uint `json:"cluster_id"`
-}
-
-// Externalize generates an external Project to be shared over REST
-func (p *DNSRecord) Externalize() *DNSRecordExternal {
-	return &DNSRecordExternal{
+func (p *DNSRecord) ToDNSRecordType() *types.DNSRecord {
+	return &types.DNSRecord{
 		ExternalURL: fmt.Sprintf("%s.%s", p.SubdomainPrefix, p.RootDomain),
 		Endpoint:    p.Endpoint,
 		Hostname:    p.Hostname,

+ 0 - 53
internal/models/gitrepo.go

@@ -20,26 +20,6 @@ type GitRepo struct {
 	OAuthIntegrationID uint
 }
 
-// // GitRepoExternal is a repository to be shared over REST
-// type GitRepoExternal struct {
-// 	ID uint `json:"id"`
-
-// 	// The project that this integration belongs to
-// 	ProjectID uint `json:"project_id"`
-
-// 	// The username/organization that this repo integration is linked to
-// 	RepoEntity string `json:"repo_entity"`
-// }
-
-// // Externalize generates an external Repo to be shared over REST
-// func (r *GitRepo) Externalize() *GitRepoExternal {
-// 	return &GitRepoExternal{
-// 		ID:         r.Model.ID,
-// 		ProjectID:  r.ProjectID,
-// 		RepoEntity: r.RepoEntity,
-// 	}
-// }
-
 // GitActionConfig is a configuration for release's CI integration via
 // Github Actions
 type GitActionConfig struct {
@@ -75,39 +55,6 @@ type GitActionConfig struct {
 	Version string `json:"version" gorm:"default:v0.0.1"`
 }
 
-// GitActionConfigExternal is an external GitActionConfig to be shared over REST
-type GitActionConfigExternal struct {
-	// The git repo in ${owner}/${repo} form
-	GitRepo string `json:"git_repo"`
-
-	// The git branch to use
-	GitBranch string `json:"git_branch"`
-
-	// The complete image repository uri to pull from
-	ImageRepoURI string `json:"image_repo_uri"`
-
-	// The git integration id
-	GitRepoID uint `json:"git_repo_id"`
-
-	// The path to the dockerfile in the git repo
-	DockerfilePath string `json:"dockerfile_path" form:"required"`
-
-	// The build context
-	FolderPath string `json:"folder_path"`
-}
-
-// Externalize generates an external GitActionConfig to be shared over REST
-func (r *GitActionConfig) Externalize() *GitActionConfigExternal {
-	return &GitActionConfigExternal{
-		GitRepo:        r.GitRepo,
-		GitBranch:      r.GitBranch,
-		ImageRepoURI:   r.ImageRepoURI,
-		GitRepoID:      r.GithubInstallationID,
-		DockerfilePath: r.DockerfilePath,
-		FolderPath:     r.FolderPath,
-	}
-}
-
 // ToGitActionConfigType generates an external GitActionConfig to be shared over REST
 func (r *GitActionConfig) ToGitActionConfigType() *types.GitActionConfig {
 	return &types.GitActionConfig{

+ 0 - 18
internal/models/integrations/github_app.go

@@ -17,24 +17,6 @@ type GithubAppInstallation struct {
 	InstallationID int64 `json:"installation_id"`
 }
 
-type GithubAppInstallationExternal struct {
-	ID uint `json:"id"`
-
-	// Can belong to either a user or an organization
-	AccountID int64 `json:"account_id"`
-
-	// Installation ID (used for authentication)
-	InstallationID int64 `json:"installation_id"`
-}
-
-func (r *GithubAppInstallation) Externalize() *GithubAppInstallationExternal {
-	return &GithubAppInstallationExternal{
-		ID:             r.ID,
-		AccountID:      r.AccountID,
-		InstallationID: r.InstallationID,
-	}
-}
-
 func (r *GithubAppInstallation) ToGitInstallationType() *types.GitInstallation {
 	return &types.GitInstallation{
 		ID:             r.ID,

+ 0 - 24
internal/models/integrations/kube.go

@@ -45,27 +45,3 @@ type KubeIntegration struct {
 	// The raw kubeconfig, used by local auth mechanisms
 	Kubeconfig []byte `json:"kubeconfig"`
 }
-
-// KubeIntegrationExternal is a KubeIntegration to be shared over REST
-type KubeIntegrationExternal struct {
-	ID uint `json:"id"`
-
-	// The name of the auth mechanism
-	Mechanism KubeIntegrationName `json:"mechanism"`
-
-	// The id of the user that linked this auth mechanism
-	UserID uint `json:"user_id"`
-
-	// The project that this integration belongs to
-	ProjectID uint `json:"project_id"`
-}
-
-// Externalize generates an external KubeIntegration to be shared over REST
-func (k *KubeIntegration) Externalize() *KubeIntegrationExternal {
-	return &KubeIntegrationExternal{
-		ID:        k.ID,
-		Mechanism: k.Mechanism,
-		UserID:    k.UserID,
-		ProjectID: k.ProjectID,
-	}
-}

+ 0 - 24
internal/models/integrations/oidc.go

@@ -50,27 +50,3 @@ type OIDCIntegration struct {
 	// The user's refresh token
 	RefreshToken []byte `json:"refresh-token"`
 }
-
-// OIDCIntegrationExternal is a OIDCIntegration to be shared over REST
-type OIDCIntegrationExternal struct {
-	ID uint `json:"id"`
-
-	// The name of the auth mechanism
-	Client OIDCIntegrationClient `json:"client"`
-
-	// The id of the user that linked this auth mechanism
-	UserID uint `json:"user_id"`
-
-	// The project that this integration belongs to
-	ProjectID uint `json:"project_id"`
-}
-
-// Externalize generates an external KubeIntegration to be shared over REST
-func (o *OIDCIntegration) Externalize() *OIDCIntegrationExternal {
-	return &OIDCIntegrationExternal{
-		ID:        o.ID,
-		Client:    o.Client,
-		UserID:    o.UserID,
-		ProjectID: o.ProjectID,
-	}
-}

+ 0 - 22
internal/models/invite.go

@@ -23,28 +23,6 @@ type Invite struct {
 	UserID    uint
 }
 
-// InviteExternal represents the Invite type that is sent over REST
-type InviteExternal struct {
-	ID       uint   `json:"id"`
-	Token    string `json:"token"`
-	Expired  bool   `json:"expired"`
-	Email    string `json:"email"`
-	Accepted bool   `json:"accepted"`
-	Kind     string `json:"kind"`
-}
-
-// Externalize generates an external Invite to be shared over REST
-func (i *Invite) Externalize() *InviteExternal {
-	return &InviteExternal{
-		ID:       i.Model.ID,
-		Token:    i.Token,
-		Email:    i.Email,
-		Expired:  i.IsExpired(),
-		Accepted: i.IsAccepted(),
-		Kind:     i.Kind,
-	}
-}
-
 // ToInviteType generates an external Invite to be shared over REST
 func (i *Invite) ToInviteType() *types.Invite {
 	return &types.Invite{

+ 6 - 9
internal/models/notification.go

@@ -1,6 +1,9 @@
 package models
 
-import "gorm.io/gorm"
+import (
+	"github.com/porter-dev/porter/api/types"
+	"gorm.io/gorm"
+)
 
 type NotificationConfig struct {
 	gorm.Model
@@ -11,14 +14,8 @@ type NotificationConfig struct {
 	Failure bool
 }
 
-type NotificationConfigExternal struct {
-	Enabled bool `json:"enabled"`
-	Success bool `json:"success"`
-	Failure bool `json:"failure"`
-}
-
-func (conf *NotificationConfig) Externalize() *NotificationConfigExternal {
-	return &NotificationConfigExternal{
+func (conf *NotificationConfig) ToNotificationConfigType() *types.NotificationConfig {
+	return &types.NotificationConfig{
 		Enabled: conf.Enabled,
 		Success: conf.Success,
 		Failure: conf.Failure,

+ 0 - 17
internal/models/registry.go

@@ -38,23 +38,6 @@ type Registry struct {
 	TokenCache integrations.RegTokenCache
 }
 
-// RegistryExternal is an external Registry to be shared over REST
-type RegistryExternal struct {
-	ID uint `json:"id"`
-
-	// The project that this integration belongs to
-	ProjectID uint `json:"project_id"`
-
-	// Name of the registry
-	Name string `json:"name"`
-
-	// URL of the registry
-	URL string `json:"url"`
-
-	// The infra id, if registry was provisioned with Porter
-	InfraID uint `json:"infra_id"`
-}
-
 func (r *Registry) ToRegistryType() *types.Registry {
 	var serv types.RegistryService
 

+ 0 - 17
internal/models/release.go

@@ -26,23 +26,6 @@ type Release struct {
 	NotificationConfig uint
 }
 
-// ReleaseExternal represents the Release type that is sent over REST
-type ReleaseExternal struct {
-	ID uint `json:"id"`
-
-	WebhookToken    string                   `json:"webhook_token"`
-	GitActionConfig *GitActionConfigExternal `json:"git_action_config,omitempty"`
-}
-
-// Externalize generates an external User to be shared over REST
-func (r *Release) Externalize() *ReleaseExternal {
-	return &ReleaseExternal{
-		ID:              r.ID,
-		WebhookToken:    r.WebhookToken,
-		GitActionConfig: r.GitActionConfig.Externalize(),
-	}
-}
-
 func (r *Release) ToReleaseType() *types.PorterRelease {
 	res := &types.PorterRelease{
 		ID:           r.ID,

+ 0 - 18
internal/models/role.go

@@ -18,24 +18,6 @@ type Role struct {
 	types.Role
 }
 
-// RoleExternal represents the Role type that is sent over REST
-type RoleExternal struct {
-	types.Role
-	ID uint `json:"id"`
-}
-
-// Externalize generates an external Role to be shared over REST
-func (r *Role) Externalize() *RoleExternal {
-	return &RoleExternal{
-		ID: r.ID,
-		Role: types.Role{
-			Kind:      r.Kind,
-			UserID:    r.UserID,
-			ProjectID: r.ProjectID,
-		},
-	}
-}
-
 func (r *Role) ToRoleType() *types.Role {
 	return &types.Role{
 		Kind:      r.Kind,

+ 0 - 58
internal/models/templates.go

@@ -1,58 +0,0 @@
-package models
-
-// FormContext is the target context
-type FormContext struct {
-	Type   string            `yaml:"type" json:"type"`
-	Config map[string]string `yaml:"config" json:"config"`
-}
-
-// FormTab is a tab rendered in a form
-type FormTab struct {
-	Context  *FormContext   `yaml:"context" json:"context"`
-	Name     string         `yaml:"name" json:"name"`
-	Label    string         `yaml:"label" json:"label"`
-	Sections []*FormSection `yaml:"sections" json:"sections,omitempty"`
-	Settings struct {
-		OmitFromLaunch bool `yaml:"omitFromLaunch,omitempty" json:"omitFromLaunch,omitempty"`
-	} `yaml:"settings,omitempty" json:"settings,omitempty"`
-}
-
-// FormSection is a section of a form
-type FormSection struct {
-	Context  *FormContext   `yaml:"context" json:"context"`
-	Name     string         `yaml:"name" json:"name"`
-	ShowIf   interface{}    `yaml:"show_if" json:"show_if"`
-	Contents []*FormContent `yaml:"contents" json:"contents,omitempty"`
-}
-
-// FormContent is a form's atomic unit
-type FormContent struct {
-	Context     *FormContext `yaml:"context" json:"context"`
-	Type        string       `yaml:"type" json:"type"`
-	Label       string       `yaml:"label" json:"label"`
-	Required    bool         `json:"required"`
-	Name        string       `yaml:"name,omitempty" json:"name,omitempty"`
-	Variable    string       `yaml:"variable,omitempty" json:"variable,omitempty"`
-	Placeholder string       `yaml:"placeholder,omitempty" json:"placeholder,omitempty"`
-	Value       interface{}  `yaml:"value,omitempty" json:"value,omitempty"`
-	Settings    struct {
-		Docs               string      `yaml:"docs,omitempty" json:"docs,omitempty"`
-		Default            interface{} `yaml:"default,omitempty" json:"default,omitempty"`
-		Unit               interface{} `yaml:"unit,omitempty" json:"unit,omitempty"`
-		OmitUnitFromValue  bool        `yaml:"omitUnitFromValue,omitempty" json:"omitUnitFromValue,omitempty"`
-		DisableAfterLaunch bool        `yaml:"disableAfterLaunch,omitempty" json:"disableAfterLaunch,omitempty"`
-		Options            interface{} `yaml:"options,omitempty" json:"options,omitempty"`
-		Placeholder        string      `yaml:"placeholder,omitempty" json:"placeholder,omitempty"`
-	} `yaml:"settings,omitempty" json:"settings,omitempty"`
-}
-
-// FormYAML represents a chart's values.yaml form abstraction
-type FormYAML struct {
-	Name                string     `yaml:"name" json:"name"`
-	Icon                string     `yaml:"icon" json:"icon"`
-	HasSource           string     `yaml:"hasSource" json:"hasSource"`
-	IncludeHiddenFields string     `yaml:"includeHiddenFields,omitempty" json:"includeHiddenFields,omitempty"`
-	Description         string     `yaml:"description" json:"description"`
-	Tags                []string   `yaml:"tags" json:"tags"`
-	Tabs                []*FormTab `yaml:"tabs" json:"tabs,omitempty"`
-}

+ 0 - 16
internal/models/user.go

@@ -21,22 +21,6 @@ type User struct {
 	GoogleUserID string
 }
 
-// UserExternal represents the User type that is sent over REST
-type UserExternal struct {
-	ID            uint   `json:"id"`
-	Email         string `json:"email"`
-	EmailVerified bool   `json:"email_verified"`
-}
-
-// Externalize generates an external User to be shared over REST
-func (u *User) Externalize() *UserExternal {
-	return &UserExternal{
-		ID:            u.ID,
-		Email:         u.Email,
-		EmailVerified: u.EmailVerified,
-	}
-}
-
 // ToUserType generates an external types.User to be shared over REST
 func (u *User) ToUserType() *types.User {
 	return &types.User{

+ 0 - 29
internal/models/user_test.go

@@ -1,29 +0,0 @@
-package models_test
-
-import (
-	"testing"
-
-	"github.com/porter-dev/porter/internal/models"
-	"gorm.io/gorm"
-)
-
-func TestUserExternalize(t *testing.T) {
-	// create a new user
-	user := &models.User{
-		Model: gorm.Model{
-			ID: 1,
-		},
-		Email:    "testing@testing.com",
-		Password: "testing123",
-	}
-
-	extUser := *user.Externalize()
-
-	if extUser.ID != user.ID {
-		t.Errorf("Field: %s\t Int: %v\t Ext: %v\n", "ID", user.ID, extUser.ID)
-	}
-
-	if extUser.Email != user.Email {
-		t.Errorf("Field: %s\t Int: %v\t Ext: %v\n", "Email", user.Email, extUser.Email)
-	}
-}