|
| 1 | +package dynamicparameters |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/uuid" |
| 7 | + "golang.org/x/xerrors" |
| 8 | + |
| 9 | + "github.com/coder/coder/v2/apiversion" |
| 10 | + "github.com/coder/coder/v2/coderd/database" |
| 11 | + "github.com/coder/coder/v2/coderd/database/dbauthz" |
| 12 | + "github.com/coder/coder/v2/coderd/files" |
| 13 | + "github.com/coder/preview" |
| 14 | + |
| 15 | + "github.com/hashicorp/hcl/v2" |
| 16 | +) |
| 17 | + |
| 18 | +type Loader struct { |
| 19 | + templateVersionID uuid.UUID |
| 20 | + |
| 21 | + // cache of objects |
| 22 | + templateVersion *database.TemplateVersion |
| 23 | + job *database.ProvisionerJob |
| 24 | + terraformValues *database.TemplateVersionTerraformValue |
| 25 | +} |
| 26 | + |
| 27 | +func New(ctx context.Context, versionID uuid.UUID) *Loader { |
| 28 | + return &Loader{ |
| 29 | + templateVersionID: versionID, |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +func (r *Loader) WithTemplateVersion(tv database.TemplateVersion) *Loader { |
| 34 | + if tv.ID == r.templateVersionID { |
| 35 | + r.templateVersion = &tv |
| 36 | + } |
| 37 | + |
| 38 | + return r |
| 39 | +} |
| 40 | + |
| 41 | +func (r *Loader) WithProvisionerJob(job database.ProvisionerJob) *Loader { |
| 42 | + r.job = &job |
| 43 | + |
| 44 | + return r |
| 45 | +} |
| 46 | + |
| 47 | +func (r *Loader) WithTerraformValues(values database.TemplateVersionTerraformValue) *Loader { |
| 48 | + if values.TemplateVersionID == r.templateVersionID { |
| 49 | + r.terraformValues = &values |
| 50 | + } |
| 51 | + |
| 52 | + return r |
| 53 | +} |
| 54 | + |
| 55 | +func (r *Loader) Load(ctx context.Context, db database.Store) error { |
| 56 | + if r.templateVersion == nil { |
| 57 | + tv, err := db.GetTemplateVersionByID(ctx, r.templateVersionID) |
| 58 | + if err != nil { |
| 59 | + return xerrors.Errorf("template version: %w", err) |
| 60 | + } |
| 61 | + r.templateVersion = &tv |
| 62 | + } |
| 63 | + |
| 64 | + if r.job == nil { |
| 65 | + job, err := db.GetProvisionerJobByID(ctx, r.templateVersion.JobID) |
| 66 | + if err != nil { |
| 67 | + return xerrors.Errorf("provisioner job: %w", err) |
| 68 | + } |
| 69 | + r.job = &job |
| 70 | + } |
| 71 | + |
| 72 | + if !r.job.CompletedAt.Valid { |
| 73 | + return xerrors.Errorf("job has not completed") |
| 74 | + } |
| 75 | + |
| 76 | + if r.terraformValues == nil { |
| 77 | + values, err := db.GetTemplateVersionTerraformValues(ctx, r.templateVersion.ID) |
| 78 | + if err != nil { |
| 79 | + return xerrors.Errorf("template version terraform values: %w", err) |
| 80 | + } |
| 81 | + r.terraformValues = &values |
| 82 | + } |
| 83 | + |
| 84 | + return nil |
| 85 | +} |
| 86 | + |
| 87 | +func (r *Loader) loaded() bool { |
| 88 | + return r.templateVersion != nil && r.job != nil && r.terraformValues != nil |
| 89 | +} |
| 90 | + |
| 91 | +func (r *Loader) Renderer(ctx context.Context, cache *files.Cache) (any, error) { |
| 92 | + if !r.loaded() { |
| 93 | + return nil, xerrors.New("Load() must be called before Renderer()") |
| 94 | + } |
| 95 | + |
| 96 | + // If they can read the template version, then they can read the file. |
| 97 | + fileCtx := dbauthz.AsFileReader(ctx) |
| 98 | + templateFS, err := cache.Acquire(fileCtx, r.job.FileID) |
| 99 | + if err != nil { |
| 100 | + return nil, xerrors.Errorf("acquire template file: %w", err) |
| 101 | + } |
| 102 | + |
| 103 | +} |
| 104 | + |
| 105 | +func (r *Loader) Render(ctx context.Context, ownerID uuid.UUID, values map[string]string) (*preview.Output, hcl.Diagnostics) { |
| 106 | + |
| 107 | + return nil, nil |
| 108 | +} |
| 109 | + |
| 110 | +func ProvisionerVersionSupportsDynamicParameters(version string) bool { |
| 111 | + major, minor, err := apiversion.Parse(version) |
| 112 | + // If the api version is not valid or less than 1.6, we need to use the static parameters |
| 113 | + useStaticParams := err != nil || major < 1 || (major == 1 && minor < 6) |
| 114 | + return !useStaticParams |
| 115 | +} |
0 commit comments