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

nats-io / nsc / 14765656943

30 Apr 2025 10:34PM UTC coverage: 69.949% (-4.2%) from 74.148%
14765656943

Pull #691

github

web-flow
Merge b4ee16f94 into a9c0df586
Pull Request #691: Generate docs

1 of 1 new or added line in 1 file covered. (100.0%)

3680 existing lines in 93 files now uncovered.

12325 of 17620 relevant lines covered (69.95%)

1.66 hits per line

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

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

14
package cmd
15

16
import (
17
        "fmt"
18

19
        "github.com/nats-io/jwt/v2"
20
        "github.com/nats-io/nkeys"
21
        "github.com/nats-io/nsc/v2/cmd/store"
22
        "github.com/spf13/cobra"
23
)
24

25
func createDeleteMappingCmd() *cobra.Command {
26
        var params DeleteMappingParams
27
        cmd := &cobra.Command{
2✔
28
                Use:          "mapping",
2✔
29
                Short:        "Delete a mapping",
2✔
30
                Args:         MaxArgs(0),
2✔
31
                SilenceUsage: true,
2✔
32
                RunE: func(cmd *cobra.Command, args []string) error {
2✔
33
                        return RunAction(cmd, args, &params)
2✔
34
                },
4✔
35
        }
2✔
36
        cmd.Flags().StringVarP((*string)(&params.from), "from", "f", "", "map from subject (required)")
2✔
37
        cmd.Flags().StringVarP((*string)(&params.to), "to", "t", "", "to subject. When present, only that particular mapping is removed. Otherwise all mappings for from subject are.")
38
        cmd.Flags().StringVarP(&params.cluster, "cluster", "", "", "in which cluster this mapping should apply")
2✔
39

2✔
40
        params.AccountContextParams.BindFlags(cmd)
2✔
41
        return cmd
2✔
42
}
2✔
43

2✔
44
func init() {
45
        deleteCmd.AddCommand(createDeleteMappingCmd())
46
}
2✔
47

2✔
48
type DeleteMappingParams struct {
2✔
49
        AccountContextParams
50
        SignerParams
51
        claim   *jwt.AccountClaims
52
        from    jwt.Subject
53
        to      jwt.Subject
54
        cluster string
55
}
56

57
func (p *DeleteMappingParams) SetDefaults(ctx ActionCtx) error {
58
        p.AccountContextParams.SetDefaults(ctx)
59
        p.SignerParams.SetDefaults(nkeys.PrefixByteOperator, true, ctx)
2✔
60
        return nil
2✔
61
}
2✔
62

2✔
63
func (p *DeleteMappingParams) PreInteractive(ctx ActionCtx) error {
2✔
64
        var err error
65
        if err = p.AccountContextParams.Edit(ctx); err != nil {
×
66
                return err
×
67
        }
×
68
        return nil
×
UNCOV
69
}
×
UNCOV
70

×
71
func (p *DeleteMappingParams) Load(ctx ActionCtx) error {
72
        var err error
73

2✔
74
        if err = p.AccountContextParams.Validate(ctx); err != nil {
2✔
75
                return err
2✔
76
        }
2✔
UNCOV
77

×
UNCOV
78
        p.claim, err = ctx.StoreCtx().Store.ReadAccountClaim(p.AccountContextParams.Name)
×
79
        if err != nil {
80
                return err
2✔
81
        }
2✔
UNCOV
82

×
UNCOV
83
        return nil
×
84
}
85

2✔
86
func (p *DeleteMappingParams) PostInteractive(ctx ActionCtx) error {
87
        return nil
88
}
×
UNCOV
89

×
UNCOV
90
func (p *DeleteMappingParams) Validate(ctx ActionCtx) error {
×
91
        if p.from == "" {
92
                return fmt.Errorf("from subject is required")
2✔
93
        }
2✔
UNCOV
94
        if err := p.SignerParams.Resolve(ctx); err != nil {
×
95
                return err
×
96
        }
2✔
UNCOV
97
        return nil
×
UNCOV
98
}
×
99

2✔
100
func (p *DeleteMappingParams) Run(ctx ActionCtx) (store.Status, error) {
101
        r := store.NewDetailedReport(true)
102
        if p.to != "" {
2✔
103
                list := p.claim.Mappings[p.from]
2✔
104
                for i, l := range list {
4✔
105
                        if l.Subject == p.to && l.Cluster == p.cluster {
2✔
106
                                if i == 0 {
4✔
107
                                        if len(list) == 1 {
4✔
108
                                                delete(p.claim.Mappings, p.from)
4✔
109
                                        } else {
4✔
110
                                                p.claim.Mappings[p.from] = list[1:]
2✔
111
                                        }
4✔
112
                                } else {
2✔
113
                                        p.claim.Mappings[p.from] = append(list[0:i], list[i+i:]...)
2✔
114
                                }
×
UNCOV
115
                                break
×
UNCOV
116
                        }
×
117
                }
2✔
118
        }
119
        if p.to == "" || len(p.claim.Mappings[p.from]) == 0 {
120
                delete(p.claim.Mappings, p.from)
121
        }
4✔
122
        if len(p.claim.Mappings) == 0 {
2✔
123
                p.claim.Mappings = nil
2✔
124
        }
4✔
125
        token, err := p.claim.Encode(p.signerKP)
2✔
126
        if err != nil {
2✔
127
                return nil, err
2✔
128
        }
2✔
UNCOV
129
        if p.to == "" {
×
UNCOV
130
                if p.cluster != "" {
×
131
                        r.AddOK("deleted all mapping in cluster %s for %s", p.cluster, p.from)
4✔
132
                } else {
2✔
UNCOV
133
                        r.AddOK("deleted all mapping for %s", p.from)
×
134
                }
2✔
135
        } else {
2✔
136
                if p.cluster != "" {
2✔
137
                        r.AddOK("deleted mapping %s -> %s in cluster %s", p.cluster, p.from, p.cluster)
2✔
138
                } else {
4✔
139
                        r.AddOK("deleted mapping %s -> %s", p.from, p.to)
2✔
140
                }
4✔
141
        }
2✔
142
        rs, err := ctx.StoreCtx().Store.StoreClaim([]byte(token))
2✔
143
        if rs != nil {
144
                r.Add(rs)
2✔
145
        }
2✔
UNCOV
146
        if err != nil {
×
147
                r.AddFromError(err)
×
148
        }
2✔
UNCOV
149
        return r, nil
×
UNCOV
150
}
×
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