MCPcopy Create free account
hub / github.com/cloudbase/garm / ListTemplates

Method ListTemplates

database/sql/templates.go:32–69  ·  view source on GitHub ↗
(ctx context.Context, osType *commonParams.OSType, forgeType *params.EndpointType, partialName *string)

Source from the content-addressed store, hash-verified

30)
31
32func (s *sqlDatabase) ListTemplates(ctx context.Context, osType *commonParams.OSType, forgeType *params.EndpointType, partialName *string) ([]params.Template, error) {
33 var templates []Template
34 q := s.conn.Model(&Template{}).Omit("data").Preload("User")
35 if !auth.IsAdmin(ctx) {
36 userID, err := getUIDFromContext(ctx)
37 if err != nil {
38 return nil, fmt.Errorf("error listing templates: %w", err)
39 }
40 q = q.Where("user_id = ? or user_id IS NULL", userID)
41 }
42
43 if osType != nil {
44 q = q.Where("os_type = ?", *osType)
45 }
46
47 if partialName != nil {
48 q = q.Where("name like ? COLLATE NOCASE", fmt.Sprintf("%%%s%%", *partialName))
49 }
50
51 if forgeType != nil {
52 q = q.Where("forge_type = ?", *forgeType)
53 }
54
55 q = q.Find(&templates)
56 if q.Error != nil {
57 return nil, fmt.Errorf("failed to get templates: %w", q.Error)
58 }
59
60 ret := make([]params.Template, len(templates))
61 for idx, tpl := range templates {
62 retTpl, err := s.sqlToParamTemplate(tpl)
63 if err != nil {
64 return nil, fmt.Errorf("failed to convert template: %w", err)
65 }
66 ret[idx] = retTpl
67 }
68 return ret, nil
69}
70
71func (s *sqlDatabase) getTemplate(ctx context.Context, tx *gorm.DB, id uint, preload ...string) (Template, error) {
72 var template Template

Callers

nothing calls this directly

Calls 3

sqlToParamTemplateMethod · 0.95
IsAdminFunction · 0.92
getUIDFromContextFunction · 0.85

Tested by

no test coverage detected