@@ -17,6 +17,7 @@ limitations under the License.
1717package userconfig
1818
1919import (
20+ "github.com/cortexlabs/cortex/pkg/lib/aws"
2021 cr "github.com/cortexlabs/cortex/pkg/lib/configreader"
2122 "github.com/cortexlabs/cortex/pkg/lib/errors"
2223 "github.com/cortexlabs/cortex/pkg/operator/api/resource"
@@ -26,9 +27,10 @@ type Constants []*Constant
2627
2728type Constant struct {
2829 ResourceFields
29- Type OutputSchema `json:"type" yaml:"type"`
30- Value interface {} `json:"value" yaml:"value"`
31- Tags Tags `json:"tags" yaml:"tags"`
30+ Type OutputSchema `json:"type" yaml:"type"`
31+ Value interface {} `json:"value" yaml:"value"`
32+ Tags Tags `json:"tags" yaml:"tags"`
33+ External * ExternalConstant `json:"external" yaml:"external"`
3234}
3335
3436var constantValidation = & cr.StructValidation {
@@ -50,14 +52,43 @@ var constantValidation = &cr.StructValidation{
5052 {
5153 StructField : "Value" ,
5254 InterfaceValidation : & cr.InterfaceValidation {
53- Required : true ,
55+ Required : false ,
5456 },
5557 },
58+ {
59+ StructField : "External" ,
60+ StructValidation : externalModelFieldValidation ,
61+ },
5662 tagsFieldValidation ,
5763 typeFieldValidation ,
5864 },
5965}
6066
67+ type ExternalConstant struct {
68+ Path string `json:"path" yaml:"path"`
69+ Region string `json:"region" yaml:"region"`
70+ }
71+
72+ var externalConstantFieldValidation = & cr.StructValidation {
73+ DefaultNil : true ,
74+ StructFieldValidations : []* cr.StructFieldValidation {
75+ {
76+ StructField : "Path" ,
77+ StringValidation : & cr.StringValidation {
78+ Validator : cr .GetS3PathValidator (),
79+ Required : true ,
80+ },
81+ },
82+ {
83+ StructField : "Region" ,
84+ StringValidation : & cr.StringValidation {
85+ Default : aws .DefaultS3Region ,
86+ AllowedValues : aws .S3Regions .Slice (),
87+ },
88+ },
89+ },
90+ }
91+
6192func (constants Constants ) Validate () error {
6293 for _ , constant := range constants {
6394 if err := constant .Validate (); err != nil {
@@ -79,6 +110,25 @@ func (constants Constants) Validate() error {
79110}
80111
81112func (constant * Constant ) Validate () error {
113+ if constant .External == nil && constant .Value == nil {
114+ return errors .Wrap (ErrorSpecifyOnlyOneMissing (ValueKey , ExternalKey ), Identify (constant ))
115+ }
116+
117+ if constant .External != nil && constant .Value != nil {
118+ return errors .Wrap (ErrorSpecifyOnlyOne (ValueKey , ExternalKey ), Identify (constant ))
119+ }
120+
121+ if constant .External != nil {
122+ bucket , key , err := aws .SplitS3Path (constant .External .Path )
123+ if err != nil {
124+ return errors .Wrap (err , Identify (constant ), ExternalKey , PathKey )
125+ }
126+
127+ if ok , err := aws .IsS3FileExternal (bucket , key , constant .External .Region ); err != nil || ! ok {
128+ return errors .Wrap (ErrorExternalNotFound (constant .External .Path ), Identify (constant ), ExternalKey , PathKey )
129+ }
130+ }
131+
82132 if constant .Type != nil {
83133 castedValue , err := CastOutputValue (constant .Value , constant .Type )
84134 if err != nil {
0 commit comments