-
Notifications
You must be signed in to change notification settings - Fork 12
/
claim_lock.go
130 lines (108 loc) · 3 KB
/
claim_lock.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
package commands
import (
"context"
"errors"
"io"
"code.cloudfoundry.org/cfdot/commands/helpers"
"code.cloudfoundry.org/locket/models"
"github.com/spf13/cobra"
)
// flags
var (
lockKey string
lockOwner string
lockValue string
ttlInSeconds int
)
var claimLockCmd = &cobra.Command{
Use: "claim-lock",
Short: "Claim Locket lock",
Long: "Claims a Locket lock with the given key, owner, and optional value",
RunE: claimLock,
}
func init() {
AddLocketFlags(claimLockCmd)
claimLockCmd.Flags().StringVarP(&lockKey, "key", "k", "", "the key of the lock being claimed")
claimLockCmd.Flags().StringVarP(&lockOwner, "owner", "o", "", "the lock owner")
claimLockCmd.Flags().StringVarP(&lockValue, "value", "v", "", "the value associated with the key")
claimLockCmd.Flags().IntVarP(&ttlInSeconds, "ttl", "t", 0, "the TTL for the lock")
RootCmd.AddCommand(claimLockCmd)
}
func claimLock(cmd *cobra.Command, args []string) error {
err := ValidateClaimLocksArguments(cmd, args, lockKey, lockOwner, lockValue, ttlInSeconds)
if err != nil {
return NewCFDotValidationError(cmd, err)
}
logger := globalLogger.Session("locket-client")
locketClient, err := helpers.NewLocketClient(logger, cmd, Config)
if err != nil {
return NewCFDotComponentError(cmd, err)
}
err = ClaimLock(
cmd.OutOrStdout(),
cmd.OutOrStderr(),
locketClient,
lockKey,
lockOwner,
lockValue,
int64(ttlInSeconds),
)
if err != nil {
return NewCFDotComponentError(cmd, err)
}
return nil
}
func ValidateClaimLocksArguments(cmd *cobra.Command, args []string, lockKey, lockOwner, lockValue string, ttlInSeconds int) error {
if len(args) > 0 {
return errExtraArguments
}
var err error
err = ValidateConflictingShortAndLongFlag("-k", "--key", cmd)
if err != nil {
return NewCFDotValidationError(cmd, err)
}
err = ValidateConflictingShortAndLongFlag("-o", "--owner", cmd)
if err != nil {
return NewCFDotValidationError(cmd, err)
}
err = ValidateConflictingShortAndLongFlag("-v", "--value", cmd)
if err != nil {
return NewCFDotValidationError(cmd, err)
}
err = ValidateConflictingShortAndLongFlag("-t", "--ttl", cmd)
if err != nil {
return NewCFDotValidationError(cmd, err)
}
if lockKey == "" {
return NewCFDotValidationError(cmd, errors.New("key cannot be empty"))
}
if lockOwner == "" {
return NewCFDotValidationError(cmd, errors.New("owner cannot be empty"))
}
if ttlInSeconds <= 0 {
return NewCFDotValidationError(cmd, errors.New("ttl should be an integer greater than zero"))
}
return nil
}
func ClaimLock(
stdout, stderr io.Writer,
locketClient models.LocketClient,
lockKey, lockOwner, lockValue string,
ttlInSeconds int64) error {
logger := globalLogger.Session("claim-lock")
req := &models.LockRequest{
Resource: &models.Resource{
Key: lockKey,
Owner: lockOwner,
Value: lockValue,
TypeCode: models.LOCK,
},
TtlInSeconds: ttlInSeconds,
}
_, err := locketClient.Lock(context.Background(), req)
if err != nil {
return err
}
logger.Info("completed")
return nil
}