get.porter.sh/porter@v1.3.0/pkg/storage/schema.go (about)

     1  package storage
     2  
     3  import (
     4  	"get.porter.sh/porter/pkg/cnab"
     5  	"get.porter.sh/porter/pkg/schema"
     6  	"github.com/Masterminds/semver/v3"
     7  )
     8  
     9  var _ Document = Schema{}
    10  
    11  const (
    12  	// SchemaTypeCredentialSet is the default schemaType value for CredentialSet resources
    13  	SchemaTypeCredentialSet = "CredentialSet"
    14  
    15  	// SchemaTypeInstallation is the default schemaType value for Installation resources
    16  	SchemaTypeInstallation = "Installation"
    17  
    18  	// SchemaTypeParameterSet is the default schemaType value for ParameterSet resources
    19  	SchemaTypeParameterSet = "ParameterSet"
    20  
    21  	// DefaultCredentialSetSchemaVersion represents the version associated with the schema
    22  	// credential set documents.
    23  	DefaultCredentialSetSchemaVersion = cnab.SchemaVersion("1.0.1")
    24  
    25  	// DefaultInstallationSchemaVersion represents the version associated with the schema
    26  	// for all installation documents: installations, runs, results and outputs.
    27  	DefaultInstallationSchemaVersion = cnab.SchemaVersion("1.0.2")
    28  
    29  	// DefaultParameterSetSchemaVersion represents the version associated with the schema
    30  	//	// for parameter set documents.
    31  	DefaultParameterSetSchemaVersion = cnab.SchemaVersion("1.1.0")
    32  )
    33  
    34  var (
    35  	// DefaultCredentialSetSemverSchemaVersion is the semver representation of the DefaultCredentialSetSchemaVersion  that is suitable for doing semver comparisons.
    36  	DefaultCredentialSetSemverSchemaVersion = semver.MustParse(string(DefaultCredentialSetSchemaVersion))
    37  
    38  	// DefaultInstallationSemverSchemaVersion is the semver representation of the DefaultInstallationSchemaVersion that is suitable for doing semver comparisons.
    39  	DefaultInstallationSemverSchemaVersion = semver.MustParse(string(DefaultInstallationSchemaVersion))
    40  
    41  	// DefaultParameterSetSemverSchemaVersion is the semver representation of the DefaultParameterSetSchemaVersion  that is suitable for doing semver comparisons.
    42  	DefaultParameterSetSemverSchemaVersion = semver.MustParse(string(DefaultParameterSetSchemaVersion))
    43  
    44  	// SupportedCredentialSetSchemaVersions represents the set of allowed schema versions for CredentialSet documents.
    45  	SupportedCredentialSetSchemaVersions = schema.MustParseConstraint("1.0.1")
    46  
    47  	// SupportedInstallationSchemaVersions represents the set of allowed schema versions for Installation documents.
    48  	SupportedInstallationSchemaVersions = schema.MustParseConstraint("1.0.2")
    49  
    50  	// SupportedParameterSetSchemaVersions represents the set of allowed schema versions for ParameterSet documents.
    51  	SupportedParameterSetSchemaVersions = schema.MustParseConstraint("1.0.1 || 1.1.0")
    52  )
    53  
    54  type Schema struct {
    55  	ID string `json:"_id"`
    56  
    57  	// Installations is the schema for the installation documents.
    58  	Installations cnab.SchemaVersion `json:"installations"`
    59  
    60  	// Credentials is the schema for the credential spec documents.
    61  	Credentials cnab.SchemaVersion `json:"credentials"`
    62  
    63  	// Parameters is the schema for the parameter spec documents.
    64  	Parameters cnab.SchemaVersion `json:"parameters"`
    65  }
    66  
    67  // NewSchema creates a schema document with the currently supported version for all subsystems.
    68  func NewSchema() Schema {
    69  	return Schema{
    70  		ID:            "schema",
    71  		Installations: DefaultInstallationSchemaVersion,
    72  		Credentials:   DefaultCredentialSetSchemaVersion,
    73  		Parameters:    DefaultParameterSetSchemaVersion,
    74  	}
    75  }
    76  
    77  func (s Schema) DefaultDocumentFilter() map[string]interface{} {
    78  	return map[string]interface{}{"_id": "schema"}
    79  }
    80  
    81  func (s Schema) IsOutOfDate() bool {
    82  	return s.ShouldMigrateInstallations() || s.ShouldMigrateCredentialSets() || s.ShouldMigrateParameterSets()
    83  }
    84  
    85  // ShouldMigrateInstallations checks if the minimum version of the installation resources in the database is unsupported and requires a migration to work with this version of Porter.
    86  // Since Porter can support a range of resource versions, this means that the db may have multiple representations of a resource in the database, and will migrate them to the latest support schema version on an as-needed basis.
    87  func (s Schema) ShouldMigrateInstallations() bool {
    88  	// Determine if the minimum installation version in the db is completely unsupported by this version of Porter
    89  	warnOnly, err := schema.ValidateSchemaVersion(schema.CheckStrategyExact, SupportedInstallationSchemaVersions, string(s.Installations), DefaultInstallationSemverSchemaVersion)
    90  	return !warnOnly && err != nil
    91  }
    92  
    93  // ShouldMigrateCredentialSets checks if the minimum version of the Credential set resources in the database is unsupported and requires a migration to work with this version of Porter.
    94  // Since Porter can support a range of resource versions, this means that the db may have multiple representations of a resource in the database, and will migrate them to the latest support schema version on an as-needed basis.
    95  func (s Schema) ShouldMigrateCredentialSets() bool {
    96  	// Determine if the minimum Credential set version in the db is completely unsupported by this version of Porter
    97  	warnOnly, err := schema.ValidateSchemaVersion(schema.CheckStrategyExact, SupportedCredentialSetSchemaVersions, string(s.Credentials), DefaultCredentialSetSemverSchemaVersion)
    98  	return !warnOnly && err != nil
    99  }
   100  
   101  // ShouldMigrateParameterSets checks if the minimum version of the parameter set resources in the database is unsupported and requires a migration to work with this version of Porter.
   102  // Since Porter can support a range of resource versions, this means that the db may have multiple representations of a resource in the database, and will migrate them to the latest support schema version on an as-needed basis.
   103  func (s Schema) ShouldMigrateParameterSets() bool {
   104  	// Determine if the minimum parameter set version in the db is completely unsupported by this version of Porter
   105  	warnOnly, err := schema.ValidateSchemaVersion(schema.CheckStrategyExact, SupportedParameterSetSchemaVersions, string(s.Parameters), DefaultParameterSetSemverSchemaVersion)
   106  	return !warnOnly && err != nil
   107  }