Преглед изворни кода

update infra response model

Alexander Belanger пре 4 година
родитељ
комит
519e21cea4

+ 15 - 0
api/types/infra.go

@@ -37,4 +37,19 @@ type Infra struct {
 
 	// Status is the status of the infra
 	Status InfraStatus `json:"status"`
+
+	// The AWS integration that was used to create the infra
+	AWSIntegrationID uint
+
+	// The GCP integration that was used to create the infra
+	GCPIntegrationID uint
+
+	// The DO integration that was used to create the infra:
+	// this points to an OAuthIntegrationID
+	DOIntegrationID uint
+
+	// The last-applied, non-sensitive input variables to the provisioner. For now,
+	// this is a map[string]string since we marshal into env vars anyway, but
+	// eventually this config will be more complex.
+	LastApplied map[string]string
 }

+ 1 - 0
internal/kubernetes/provisioner/input/eks.go

@@ -9,6 +9,7 @@ type EKS struct {
 	AWSAccessKey string `json:"aws_access_key"`
 	AWSSecretKey string `json:"aws_secret_key"`
 	ClusterName  string `json:"cluster_name"`
+	MachineType  string `json:"machine_type"`
 }
 
 func (eks *EKS) GetInput() ([]byte, error) {

+ 1 - 0
internal/kubernetes/provisioner/provisioner.go

@@ -150,6 +150,7 @@ func (conf *Conf) GetProvisionerJobTemplate() (*batchv1.Job, error) {
 				AWSAccessKey: conf.AWS.AWSAccessKeyID,
 				AWSSecretKey: conf.AWS.AWSSecretAccessKey,
 				ClusterName:  conf.EKS.ClusterName,
+				MachineType:  conf.EKS.MachineType,
 			}
 
 			lastApplied, err := inputConf.GetInput()

+ 87 - 4
internal/models/infra.go

@@ -1,6 +1,7 @@
 package models
 
 import (
+	"encoding/json"
 	"fmt"
 	"strconv"
 	"strings"
@@ -8,6 +9,7 @@ import (
 	"gorm.io/gorm"
 
 	"github.com/porter-dev/porter/api/types"
+	"github.com/porter-dev/porter/internal/kubernetes/provisioner/input"
 )
 
 // Infra represents the metadata for an infrastructure type provisioned on
@@ -51,13 +53,94 @@ type Infra struct {
 // ToInfraType generates an external Infra to be shared over REST
 func (i *Infra) ToInfraType() *types.Infra {
 	return &types.Infra{
-		ID:        i.ID,
-		ProjectID: i.ProjectID,
-		Kind:      i.Kind,
-		Status:    i.Status,
+		ID:               i.ID,
+		ProjectID:        i.ProjectID,
+		Kind:             i.Kind,
+		Status:           i.Status,
+		AWSIntegrationID: i.AWSIntegrationID,
+		DOIntegrationID:  i.DOIntegrationID,
+		GCPIntegrationID: i.GCPIntegrationID,
+		LastApplied:      i.SafelyGetLastApplied(),
 	}
 }
 
+// SafeGetLastApplied gets non-sensitive values for the last applied configuration
+func (i *Infra) SafelyGetLastApplied() map[string]string {
+	resp := make(map[string]string)
+
+	switch i.Kind {
+	case types.InfraECR:
+		lastApplied := &input.ECR{}
+
+		if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
+			return resp
+		}
+
+		resp["region"] = lastApplied.AWSRegion
+
+		return resp
+	case types.InfraEKS:
+		lastApplied := &input.EKS{}
+
+		if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
+			return resp
+		}
+
+		resp["region"] = lastApplied.AWSRegion
+		resp["cluster_name"] = lastApplied.ClusterName
+		resp["machine_type"] = lastApplied.MachineType
+
+		return resp
+	case types.InfraGCR:
+		lastApplied := &input.GCR{}
+
+		if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
+			return resp
+		}
+
+		resp["project_id"] = lastApplied.GCPProjectID
+		resp["region"] = lastApplied.GCPRegion
+
+		return resp
+	case types.InfraGKE:
+		lastApplied := &input.GKE{}
+
+		if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
+			return resp
+		}
+
+		resp["project_id"] = lastApplied.GCPProjectID
+		resp["region"] = lastApplied.GCPRegion
+		resp["cluster_name"] = lastApplied.ClusterName
+
+		return resp
+	case types.InfraDOCR:
+		lastApplied := &input.DOCR{}
+
+		if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
+			return resp
+		}
+
+		resp["name"] = lastApplied.DOCRName
+		resp["subscription_tier"] = lastApplied.DOCRSubscriptionTier
+
+		return resp
+	case types.InfraDOKS:
+		lastApplied := &input.DOKS{}
+
+		if err := json.Unmarshal(i.LastApplied, lastApplied); err != nil {
+			return resp
+		}
+
+		resp["cluster_name"] = lastApplied.ClusterName
+		resp["region"] = lastApplied.DORegion
+
+		return resp
+	}
+
+	return resp
+}
+
 // GetID returns the unique id for this infra
 func (i *Infra) GetUniqueName() string {
 	return fmt.Sprintf("%s-%d-%d-%s", i.Kind, i.ProjectID, i.ID, i.Suffix)