@@ -1076,6 +1076,91 @@ func (api *API) postArchiveTemplateVersions(rw http.ResponseWriter, r *http.Requ
1076
1076
})
1077
1077
}
1078
1078
1079
+ // @Summary Archive template version
1080
+ // @ID archive-template-version
1081
+ // @Security CoderSessionToken
1082
+ // @Accept json
1083
+ // @Tags Templates
1084
+ // @Param templateversion path string true "Template version ID" format(uuid)
1085
+ // @Success 200 {object} codersdk.Response
1086
+ // @Router /templateversions/{templateversion}/archive [post]
1087
+ func (api * API ) postArchiveTemplateVersion () func (rw http.ResponseWriter , r * http.Request ) {
1088
+ return api .setArchiveTemplateVersion (true )
1089
+ }
1090
+
1091
+ // @Summary Unarchive template version
1092
+ // @ID unarchive-template-version
1093
+ // @Security CoderSessionToken
1094
+ // @Accept json
1095
+ // @Tags Templates
1096
+ // @Param templateversion path string true "Template version ID" format(uuid)
1097
+ // @Success 200 {object} codersdk.Response
1098
+ // @Router /templateversions/{templateversion}/unarchive [post]
1099
+ func (api * API ) postUnarchiveTemplateVersion () func (rw http.ResponseWriter , r * http.Request ) {
1100
+ return api .setArchiveTemplateVersion (false )
1101
+ }
1102
+
1103
+ func (api * API ) setArchiveTemplateVersion (archive bool ) func (rw http.ResponseWriter , r * http.Request ) {
1104
+ return func (rw http.ResponseWriter , r * http.Request ) {
1105
+ var (
1106
+ ctx = r .Context ()
1107
+ templateVersion = httpmw .TemplateVersionParam (r )
1108
+ auditor = * api .Auditor .Load ()
1109
+ aReq , commitAudit = audit .InitRequest [database.TemplateVersion ](rw , & audit.RequestParams {
1110
+ Audit : auditor ,
1111
+ Log : api .Logger ,
1112
+ Request : r ,
1113
+ Action : database .AuditActionWrite ,
1114
+ })
1115
+ )
1116
+ defer commitAudit ()
1117
+ aReq .Old = templateVersion
1118
+
1119
+ verb := "archived"
1120
+ if ! archive {
1121
+ verb = "unarchived"
1122
+ }
1123
+ if templateVersion .Archived == archive {
1124
+ httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
1125
+ Message : fmt .Sprintf ("Template version already %s" , verb ),
1126
+ })
1127
+ return
1128
+ }
1129
+
1130
+ if ! templateVersion .TemplateID .Valid {
1131
+ // Maybe we should allow this?
1132
+ httpapi .Write (ctx , rw , http .StatusBadRequest , codersdk.Response {
1133
+ Message : "Cannot archive template versions not associate with a template." ,
1134
+ })
1135
+ return
1136
+ }
1137
+
1138
+ archived , err := api .Database .ArchiveUnusedTemplateVersions (ctx , database.ArchiveUnusedTemplateVersionsParams {
1139
+ UpdatedAt : dbtime .Now (),
1140
+ TemplateID : templateVersion .TemplateID .UUID ,
1141
+ TemplateVersionID : templateVersion .ID ,
1142
+ JobStatus : database.NullProvisionerJobStatus {},
1143
+ Unarchive : ! archive ,
1144
+ })
1145
+
1146
+ if httpapi .Is404Error (err ) || (len (archived ) == 0 && err == nil ) {
1147
+ httpapi .Write (ctx , rw , http .StatusNotFound , codersdk.Response {
1148
+ Message : "Template or template versions not found." ,
1149
+ })
1150
+ return
1151
+ }
1152
+ if err != nil {
1153
+ httpapi .Write (ctx , rw , http .StatusInternalServerError , codersdk.Response {
1154
+ Message : "Internal error fetching template version." ,
1155
+ Detail : err .Error (),
1156
+ })
1157
+ return
1158
+ }
1159
+
1160
+ httpapi .Write (ctx , rw , http .StatusOK , fmt .Sprintf ("template version %q %s" , templateVersion .ID .String (), verb ))
1161
+ }
1162
+ }
1163
+
1079
1164
// @Summary Update active template version by template ID
1080
1165
// @ID update-active-template-version-by-template-id
1081
1166
// @Security CoderSessionToken
0 commit comments