• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender-server / 1978029483

11 Aug 2025 02:15PM UTC coverage: 65.755% (+0.3%) from 65.495%
1978029483

Pull #860

gitlab-ci

kjaskiewiczz
docs(useradm): move API specification to single file

Changelog: Title
Ticket: QA-1094
Signed-off-by: Krzysztof Jaskiewicz <krzysztof.jaskiewicz@northern.tech>
Pull Request #860: docs(useradm): move API specification to single file

29261 of 44500 relevant lines covered (65.76%)

1.44 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

71.43
/backend/pkg/store/v2/utils.go
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
package store
16

17
import (
18
        "context"
19
        "strings"
20

21
        "go.mongodb.org/mongo-driver/bson"
22

23
        "github.com/mendersoftware/mender-server/pkg/identity"
24
        mdoc "github.com/mendersoftware/mender-server/pkg/mongo/doc"
25
        v1 "github.com/mendersoftware/mender-server/pkg/store"
26
)
27

28
const FieldTenantID = "tenant_id"
29

30
// WithTenantID adds the tenant_id field to a bson document using the value extracted
31
// from the identity of the context
32
func WithTenantID(ctx context.Context, doc interface{}) bson.D {
6✔
33
        var (
6✔
34
                tenantID string
6✔
35
                res      bson.D
6✔
36
        )
6✔
37

6✔
38
        identity := identity.FromContext(ctx)
6✔
39
        if identity != nil {
12✔
40
                tenantID = identity.Tenant
6✔
41
        }
6✔
42
        tenantElem := bson.E{Key: FieldTenantID, Value: tenantID}
6✔
43

6✔
44
        switch v := doc.(type) {
6✔
45
        case map[string]interface{}:
×
46
                res = make(bson.D, 0, len(v)+1)
×
47
                for k, v := range v {
×
48
                        res = append(res, bson.E{Key: k, Value: v})
×
49
                }
×
50
        case bson.M:
3✔
51
                res = make(bson.D, 0, len(v)+1)
3✔
52
                for k, v := range v {
6✔
53
                        res = append(res, bson.E{Key: k, Value: v})
3✔
54
                }
3✔
55
        case bson.D:
6✔
56
                res = make(bson.D, len(v), len(v)+1)
6✔
57
                copy(res, v)
6✔
58

59
        case bson.Marshaler:
3✔
60
                b, err := v.MarshalBSON()
3✔
61
                if err != nil {
3✔
62
                        return nil
×
63
                }
×
64
                err = bson.Unmarshal(b, &res)
3✔
65
                if err != nil {
3✔
66
                        return nil
×
67
                }
×
68
        default:
5✔
69
                return mdoc.DocumentFromStruct(v, tenantElem)
5✔
70
        }
71
        res = append(res, tenantElem)
6✔
72

6✔
73
        return res
6✔
74
}
75

76
// ArrayWithTenantID adds the tenant_id field to an array of bson documents
77
// using the value extracted from the identity of the context
78
func ArrayWithTenantID(ctx context.Context, doc bson.A) bson.A {
×
79
        res := bson.A{}
×
80
        for _, item := range doc {
×
81
                res = append(res, WithTenantID(ctx, item))
×
82
        }
×
83
        return res
×
84
}
85

86
// DbFromContext generates database name using tenant field from identity extracted
87
// from context and original database name
88
func DbFromContext(ctx context.Context, origDbName string) string {
3✔
89
        return origDbName
3✔
90
}
3✔
91

92
// IsTenantDb returns a function of `TenantDbMatchFunc` that can be used for
93
// checking if database has a tenant DB name format
94
func IsTenantDb(baseDb string) v1.TenantDbMatchFunc {
2✔
95
        prefix := baseDb + "-"
2✔
96
        return func(name string) bool {
4✔
97
                return strings.HasPrefix(name, prefix)
2✔
98
        }
2✔
99
}
100

101
// TenantFromDbName attempts to extract tenant ID from provided tenant DB name.
102
// Returns extracted tenant ID or an empty string.
103
func TenantFromDbName(dbName string, baseDb string) string {
3✔
104
        noBase := strings.TrimPrefix(dbName, baseDb+"-")
3✔
105
        if noBase == dbName {
6✔
106
                return ""
3✔
107
        }
3✔
108
        return noBase
1✔
109
}
110

111
// DbNameForTenant composes tenant's db name.
112
func DbNameForTenant(tenantId string, baseDb string) string {
×
113
        return baseDb
×
114
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc