| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- package models
- import (
- "fmt"
- "strconv"
- "strings"
- "gorm.io/gorm"
- )
- // InfraStatus is the status that an infrastructure can take
- type InfraStatus string
- // The allowed statuses
- const (
- StatusCreating InfraStatus = "creating"
- StatusCreated InfraStatus = "created"
- StatusError InfraStatus = "error"
- StatusDestroying InfraStatus = "destroying"
- StatusDestroyed InfraStatus = "destroyed"
- )
- // InfraKind is the kind that infra can be
- type InfraKind string
- // The supported infra kinds
- const (
- InfraTest InfraKind = "test"
- InfraECR InfraKind = "ecr"
- InfraEKS InfraKind = "eks"
- InfraGCR InfraKind = "gcr"
- InfraGKE InfraKind = "gke"
- InfraDOCR InfraKind = "docr"
- InfraDOKS InfraKind = "doks"
- )
- // Infra represents the metadata for an infrastructure type provisioned on
- // Porter
- type Infra struct {
- gorm.Model
- // The type of infra that was provisioned
- Kind InfraKind `json:"kind"`
- // A random 6-byte suffix to ensure workspace/stream ids are unique
- Suffix string
- // The project that this infra belongs to
- ProjectID uint `json:"project_id"`
- // 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
- // ------------------------------------------------------------------
- // All fields below this line are encrypted before storage
- // ------------------------------------------------------------------
- // The last-applied input variables to the provisioner
- LastApplied []byte
- }
- // InfraExternal is an external Infra to be shared over REST
- type InfraExternal struct {
- ID uint `json:"id"`
- // The project that this integration belongs to
- ProjectID uint `json:"project_id"`
- // The type of infra that was provisioned
- Kind InfraKind `json:"kind"`
- // Status is the status of the infra
- Status InfraStatus `json:"status"`
- }
- // Externalize generates an external Infra to be shared over REST
- func (i *Infra) Externalize() *InfraExternal {
- return &InfraExternal{
- ID: i.ID,
- ProjectID: i.ProjectID,
- Kind: i.Kind,
- Status: i.Status,
- }
- }
- // 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)
- }
- // ParseUniqueName returns the (kind, projectID, infraID, suffix)
- func ParseUniqueName(workspaceID string) (string, uint, uint, error) {
- strArr := strings.Split(workspaceID, "-")
- if len(strArr) < 3 {
- return "", 0, 0, fmt.Errorf("workspace id improperly formatted")
- }
- projID, err := strconv.ParseUint(strArr[1], 10, 64)
- if err != nil {
- return "", 0, 0, err
- }
- infraID, err := strconv.ParseUint(strArr[2], 10, 64)
- if err != nil {
- return "", 0, 0, err
- }
- return strArr[0], uint(projID), uint(infraID), nil
- }
|