@@ -22,6 +22,7 @@ import (
22
22
"github.com/coder/coder/v2/coderd/audit"
23
23
"github.com/coder/coder/v2/coderd/database"
24
24
"github.com/coder/coder/v2/coderd/database/db2sdk"
25
+ "github.com/coder/coder/v2/coderd/database/dbauthz"
25
26
"github.com/coder/coder/v2/coderd/database/dbtime"
26
27
"github.com/coder/coder/v2/coderd/database/provisionerjobs"
27
28
"github.com/coder/coder/v2/coderd/externalauth"
@@ -32,6 +33,7 @@ import (
32
33
"github.com/coder/coder/v2/coderd/rbac/policy"
33
34
"github.com/coder/coder/v2/coderd/render"
34
35
"github.com/coder/coder/v2/coderd/tracing"
36
+ "github.com/coder/coder/v2/coderd/util/ptr"
35
37
"github.com/coder/coder/v2/codersdk"
36
38
"github.com/coder/coder/v2/examples"
37
39
"github.com/coder/coder/v2/provisioner/terraform/tfparse"
@@ -60,6 +62,22 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
60
62
return
61
63
}
62
64
65
+ var matchedProvisioners * codersdk.MatchedProvisioners
66
+ if jobs [0 ].ProvisionerJob .JobStatus == database .ProvisionerJobStatusPending {
67
+ // nolint: gocritic // The user hitting this endpoint may not have
68
+ // permission to read provisioner daemons, but we want to show them
69
+ // information about the provisioner daemons that are available.
70
+ provisioners ,err := api .Database .GetProvisionerDaemonsByOrganization (dbauthz .AsSystemReadProvisionerDaemons (ctx ), database.GetProvisionerDaemonsByOrganizationParams {
71
+ OrganizationID :jobs [0 ].ProvisionerJob .OrganizationID ,
72
+ WantTags :jobs [0 ].ProvisionerJob .Tags ,
73
+ })
74
+ if err != nil {
75
+ api .Logger .Error (ctx ,"failed to fetch provisioners for job id" ,slog .F ("job_id" ,jobs [0 ].ProvisionerJob .ID ),slog .Error (err ))
76
+ }else {
77
+ matchedProvisioners = ptr .Ref (db2sdk .MatchedProvisioners (provisioners ,dbtime .Now (),provisionerdserver .StaleInterval ))
78
+ }
79
+ }
80
+
63
81
schemas ,err := api .Database .GetParameterSchemasByJobID (ctx ,jobs [0 ].ProvisionerJob .ID )
64
82
if errors .Is (err ,sql .ErrNoRows ) {
65
83
err = nil
@@ -77,7 +95,7 @@ func (api *API) templateVersion(rw http.ResponseWriter, r *http.Request) {
77
95
warnings = append (warnings ,codersdk .TemplateVersionWarningUnsupportedWorkspaces )
78
96
}
79
97
80
- httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (templateVersion ,convertProvisionerJob (jobs [0 ]),nil ,warnings ))
98
+ httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (templateVersion ,convertProvisionerJob (jobs [0 ]),matchedProvisioners ,warnings ))
81
99
}
82
100
83
101
// @Summary Patch template version by ID
@@ -173,7 +191,23 @@ func (api *API) patchTemplateVersion(rw http.ResponseWriter, r *http.Request) {
173
191
return
174
192
}
175
193
176
- httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (updatedTemplateVersion ,convertProvisionerJob (jobs [0 ]),nil ,nil ))
194
+ var matchedProvisioners * codersdk.MatchedProvisioners
195
+ if jobs [0 ].ProvisionerJob .JobStatus == database .ProvisionerJobStatusPending {
196
+ // nolint: gocritic // The user hitting this endpoint may not have
197
+ // permission to read provisioner daemons, but we want to show them
198
+ // information about the provisioner daemons that are available.
199
+ provisioners ,err := api .Database .GetProvisionerDaemonsByOrganization (dbauthz .AsSystemReadProvisionerDaemons (ctx ), database.GetProvisionerDaemonsByOrganizationParams {
200
+ OrganizationID :jobs [0 ].ProvisionerJob .OrganizationID ,
201
+ WantTags :jobs [0 ].ProvisionerJob .Tags ,
202
+ })
203
+ if err != nil {
204
+ api .Logger .Error (ctx ,"failed to fetch provisioners for job id" ,slog .F ("job_id" ,jobs [0 ].ProvisionerJob .ID ),slog .Error (err ))
205
+ }else {
206
+ matchedProvisioners = ptr .Ref (db2sdk .MatchedProvisioners (provisioners ,dbtime .Now (),provisionerdserver .StaleInterval ))
207
+ }
208
+ }
209
+
210
+ httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (updatedTemplateVersion ,convertProvisionerJob (jobs [0 ]),matchedProvisioners ,nil ))
177
211
}
178
212
179
213
// @Summary Cancel template version by ID
@@ -868,8 +902,23 @@ func (api *API) templateVersionByName(rw http.ResponseWriter, r *http.Request) {
868
902
})
869
903
return
870
904
}
905
+ var matchedProvisioners * codersdk.MatchedProvisioners
906
+ if jobs [0 ].ProvisionerJob .JobStatus == database .ProvisionerJobStatusPending {
907
+ // nolint: gocritic // The user hitting this endpoint may not have
908
+ // permission to read provisioner daemons, but we want to show them
909
+ // information about the provisioner daemons that are available.
910
+ provisioners ,err := api .Database .GetProvisionerDaemonsByOrganization (dbauthz .AsSystemReadProvisionerDaemons (ctx ), database.GetProvisionerDaemonsByOrganizationParams {
911
+ OrganizationID :jobs [0 ].ProvisionerJob .OrganizationID ,
912
+ WantTags :jobs [0 ].ProvisionerJob .Tags ,
913
+ })
914
+ if err != nil {
915
+ api .Logger .Error (ctx ,"failed to fetch provisioners for job id" ,slog .F ("job_id" ,jobs [0 ].ProvisionerJob .ID ),slog .Error (err ))
916
+ }else {
917
+ matchedProvisioners = ptr .Ref (db2sdk .MatchedProvisioners (provisioners ,dbtime .Now (),provisionerdserver .StaleInterval ))
918
+ }
919
+ }
871
920
872
- httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (templateVersion ,convertProvisionerJob (jobs [0 ]),nil ,nil ))
921
+ httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (templateVersion ,convertProvisionerJob (jobs [0 ]),matchedProvisioners ,nil ))
873
922
}
874
923
875
924
// @Summary Get template version by organization, template, and name
@@ -934,7 +983,23 @@ func (api *API) templateVersionByOrganizationTemplateAndName(rw http.ResponseWri
934
983
return
935
984
}
936
985
937
- httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (templateVersion ,convertProvisionerJob (jobs [0 ]),nil ,nil ))
986
+ var matchedProvisioners * codersdk.MatchedProvisioners
987
+ if jobs [0 ].ProvisionerJob .JobStatus == database .ProvisionerJobStatusPending {
988
+ // nolint: gocritic // The user hitting this endpoint may not have
989
+ // permission to read provisioner daemons, but we want to show them
990
+ // information about the provisioner daemons that are available.
991
+ provisioners ,err := api .Database .GetProvisionerDaemonsByOrganization (dbauthz .AsSystemReadProvisionerDaemons (ctx ), database.GetProvisionerDaemonsByOrganizationParams {
992
+ OrganizationID :jobs [0 ].ProvisionerJob .OrganizationID ,
993
+ WantTags :jobs [0 ].ProvisionerJob .Tags ,
994
+ })
995
+ if err != nil {
996
+ api .Logger .Error (ctx ,"failed to fetch provisioners for job id" ,slog .F ("job_id" ,jobs [0 ].ProvisionerJob .ID ),slog .Error (err ))
997
+ }else {
998
+ matchedProvisioners = ptr .Ref (db2sdk .MatchedProvisioners (provisioners ,dbtime .Now (),provisionerdserver .StaleInterval ))
999
+ }
1000
+ }
1001
+
1002
+ httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (templateVersion ,convertProvisionerJob (jobs [0 ]),matchedProvisioners ,nil ))
938
1003
}
939
1004
940
1005
// @Summary Get previous template version by organization, template, and name
@@ -1020,7 +1085,23 @@ func (api *API) previousTemplateVersionByOrganizationTemplateAndName(rw http.Res
1020
1085
return
1021
1086
}
1022
1087
1023
- httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (previousTemplateVersion ,convertProvisionerJob (jobs [0 ]),nil ,nil ))
1088
+ var matchedProvisioners * codersdk.MatchedProvisioners
1089
+ if jobs [0 ].ProvisionerJob .JobStatus == database .ProvisionerJobStatusPending {
1090
+ // nolint: gocritic // The user hitting this endpoint may not have
1091
+ // permission to read provisioner daemons, but we want to show them
1092
+ // information about the provisioner daemons that are available.
1093
+ provisioners ,err := api .Database .GetProvisionerDaemonsByOrganization (dbauthz .AsSystemReadProvisionerDaemons (ctx ), database.GetProvisionerDaemonsByOrganizationParams {
1094
+ OrganizationID :jobs [0 ].ProvisionerJob .OrganizationID ,
1095
+ WantTags :jobs [0 ].ProvisionerJob .Tags ,
1096
+ })
1097
+ if err != nil {
1098
+ api .Logger .Error (ctx ,"failed to fetch provisioners for job id" ,slog .F ("job_id" ,jobs [0 ].ProvisionerJob .ID ),slog .Error (err ))
1099
+ }else {
1100
+ matchedProvisioners = ptr .Ref (db2sdk .MatchedProvisioners (provisioners ,dbtime .Now (),provisionerdserver .StaleInterval ))
1101
+ }
1102
+ }
1103
+
1104
+ httpapi .Write (ctx ,rw ,http .StatusOK ,convertTemplateVersion (previousTemplateVersion ,convertProvisionerJob (jobs [0 ]),matchedProvisioners ,nil ))
1024
1105
}
1025
1106
1026
1107
// @Summary Archive template unused versions by template id
@@ -1540,7 +1621,10 @@ func (api *API) postTemplateVersionsByOrganization(rw http.ResponseWriter, r *ht
1540
1621
1541
1622
// Check for eligible provisioners. This allows us to return a warning to the user if they
1542
1623
// submit a job for which no provisioner is available.
1543
- eligibleProvisioners ,err := tx .GetProvisionerDaemonsByOrganization (ctx , database.GetProvisionerDaemonsByOrganizationParams {
1624
+ // nolint: gocritic // The user hitting this endpoint may not have
1625
+ // permission to read provisioner daemons, but we want to show them
1626
+ // information about the provisioner daemons that are available.
1627
+ eligibleProvisioners ,err := tx .GetProvisionerDaemonsByOrganization (dbauthz .AsSystemReadProvisionerDaemons (ctx ), database.GetProvisionerDaemonsByOrganizationParams {
1544
1628
OrganizationID :organization .ID ,
1545
1629
WantTags :provisionerJob .Tags ,
1546
1630
})