Browse code

refactor helper.go and move getSecret to helper.go

Signed-off-by: allencloud <allen.sun@daocloud.io>

allencloud authored on 2017/02/12 03:30:53
Showing 2 changed files
... ...
@@ -24,116 +24,178 @@ func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster,
24 24
 
25 25
 func getNode(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Node, error) {
26 26
 	// GetNode to match via full ID.
27
-	rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input})
28
-	if err != nil {
29
-		// If any error (including NotFound), ListNodes to match via full name.
30
-		rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{Filters: &swarmapi.ListNodesRequest_Filters{Names: []string{input}}})
31
-
32
-		if err != nil || len(rl.Nodes) == 0 {
33
-			// If any error or 0 result, ListNodes to match via ID prefix.
34
-			rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{Filters: &swarmapi.ListNodesRequest_Filters{IDPrefixes: []string{input}}})
35
-		}
36
-
37
-		if err != nil {
38
-			return nil, err
39
-		}
27
+	if rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input}); err == nil {
28
+		return rg.Node, nil
29
+	}
40 30
 
41
-		if len(rl.Nodes) == 0 {
42
-			err := fmt.Errorf("node %s not found", input)
43
-			return nil, errors.NewRequestNotFoundError(err)
44
-		}
31
+	// If any error (including NotFound), ListNodes to match via full name.
32
+	rl, err := c.ListNodes(ctx, &swarmapi.ListNodesRequest{
33
+		Filters: &swarmapi.ListNodesRequest_Filters{
34
+			Names: []string{input},
35
+		},
36
+	})
37
+	if err != nil || len(rl.Nodes) == 0 {
38
+		// If any error or 0 result, ListNodes to match via ID prefix.
39
+		rl, err = c.ListNodes(ctx, &swarmapi.ListNodesRequest{
40
+			Filters: &swarmapi.ListNodesRequest_Filters{
41
+				IDPrefixes: []string{input},
42
+			},
43
+		})
44
+	}
45
+	if err != nil {
46
+		return nil, err
47
+	}
45 48
 
46
-		if l := len(rl.Nodes); l > 1 {
47
-			return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
48
-		}
49
+	if len(rl.Nodes) == 0 {
50
+		err := fmt.Errorf("node %s not found", input)
51
+		return nil, errors.NewRequestNotFoundError(err)
52
+	}
49 53
 
50
-		return rl.Nodes[0], nil
54
+	if l := len(rl.Nodes); l > 1 {
55
+		return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l)
51 56
 	}
52
-	return rg.Node, nil
57
+
58
+	return rl.Nodes[0], nil
53 59
 }
54 60
 
55 61
 func getService(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Service, error) {
56 62
 	// GetService to match via full ID.
57
-	rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input})
58
-	if err != nil {
59
-		// If any error (including NotFound), ListServices to match via full name.
60
-		rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{Names: []string{input}}})
61
-		if err != nil || len(rl.Services) == 0 {
62
-			// If any error or 0 result, ListServices to match via ID prefix.
63
-			rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{Filters: &swarmapi.ListServicesRequest_Filters{IDPrefixes: []string{input}}})
64
-		}
65
-
66
-		if err != nil {
67
-			return nil, err
68
-		}
63
+	if rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input}); err == nil {
64
+		return rg.Service, nil
65
+	}
69 66
 
70
-		if len(rl.Services) == 0 {
71
-			err := fmt.Errorf("service %s not found", input)
72
-			return nil, errors.NewRequestNotFoundError(err)
73
-		}
67
+	// If any error (including NotFound), ListServices to match via full name.
68
+	rl, err := c.ListServices(ctx, &swarmapi.ListServicesRequest{
69
+		Filters: &swarmapi.ListServicesRequest_Filters{
70
+			Names: []string{input},
71
+		},
72
+	})
73
+	if err != nil || len(rl.Services) == 0 {
74
+		// If any error or 0 result, ListServices to match via ID prefix.
75
+		rl, err = c.ListServices(ctx, &swarmapi.ListServicesRequest{
76
+			Filters: &swarmapi.ListServicesRequest_Filters{
77
+				IDPrefixes: []string{input},
78
+			},
79
+		})
80
+	}
81
+	if err != nil {
82
+		return nil, err
83
+	}
74 84
 
75
-		if l := len(rl.Services); l > 1 {
76
-			return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
77
-		}
85
+	if len(rl.Services) == 0 {
86
+		err := fmt.Errorf("service %s not found", input)
87
+		return nil, errors.NewRequestNotFoundError(err)
88
+	}
78 89
 
79
-		return rl.Services[0], nil
90
+	if l := len(rl.Services); l > 1 {
91
+		return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l)
80 92
 	}
81
-	return rg.Service, nil
93
+
94
+	return rl.Services[0], nil
82 95
 }
83 96
 
84 97
 func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) {
85 98
 	// GetTask to match via full ID.
86
-	rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input})
99
+	if rg, err := c.GetTask(ctx, &swarmapi.GetTaskRequest{TaskID: input}); err == nil {
100
+		return rg.Task, nil
101
+	}
102
+
103
+	// If any error (including NotFound), ListTasks to match via full name.
104
+	rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{
105
+		Filters: &swarmapi.ListTasksRequest_Filters{
106
+			Names: []string{input},
107
+		},
108
+	})
109
+	if err != nil || len(rl.Tasks) == 0 {
110
+		// If any error or 0 result, ListTasks to match via ID prefix.
111
+		rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{
112
+			Filters: &swarmapi.ListTasksRequest_Filters{
113
+				IDPrefixes: []string{input},
114
+			},
115
+		})
116
+	}
87 117
 	if err != nil {
88
-		// If any error (including NotFound), ListTasks to match via full name.
89
-		rl, err := c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{Names: []string{input}}})
118
+		return nil, err
119
+	}
120
+
121
+	if len(rl.Tasks) == 0 {
122
+		err := fmt.Errorf("task %s not found", input)
123
+		return nil, errors.NewRequestNotFoundError(err)
124
+	}
90 125
 
91
-		if err != nil || len(rl.Tasks) == 0 {
92
-			// If any error or 0 result, ListTasks to match via ID prefix.
93
-			rl, err = c.ListTasks(ctx, &swarmapi.ListTasksRequest{Filters: &swarmapi.ListTasksRequest_Filters{IDPrefixes: []string{input}}})
94
-		}
126
+	if l := len(rl.Tasks); l > 1 {
127
+		return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
128
+	}
95 129
 
96
-		if err != nil {
97
-			return nil, err
98
-		}
130
+	return rl.Tasks[0], nil
131
+}
99 132
 
100
-		if len(rl.Tasks) == 0 {
101
-			err := fmt.Errorf("task %s not found", input)
102
-			return nil, errors.NewRequestNotFoundError(err)
103
-		}
133
+func getSecret(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Secret, error) {
134
+	// attempt to lookup secret by full ID
135
+	if rg, err := c.GetSecret(ctx, &swarmapi.GetSecretRequest{SecretID: input}); err == nil {
136
+		return rg.Secret, nil
137
+	}
138
+
139
+	// If any error (including NotFound), ListSecrets to match via full name.
140
+	rl, err := c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
141
+		Filters: &swarmapi.ListSecretsRequest_Filters{
142
+			Names: []string{input},
143
+		},
144
+	})
145
+	if err != nil || len(rl.Secrets) == 0 {
146
+		// If any error or 0 result, ListSecrets to match via ID prefix.
147
+		rl, err = c.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
148
+			Filters: &swarmapi.ListSecretsRequest_Filters{
149
+				IDPrefixes: []string{input},
150
+			},
151
+		})
152
+	}
153
+	if err != nil {
154
+		return nil, err
155
+	}
104 156
 
105
-		if l := len(rl.Tasks); l > 1 {
106
-			return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l)
107
-		}
157
+	if len(rl.Secrets) == 0 {
158
+		err := fmt.Errorf("secret %s not found", input)
159
+		return nil, errors.NewRequestNotFoundError(err)
160
+	}
108 161
 
109
-		return rl.Tasks[0], nil
162
+	if l := len(rl.Secrets); l > 1 {
163
+		return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", input, l)
110 164
 	}
111
-	return rg.Task, nil
165
+
166
+	return rl.Secrets[0], nil
112 167
 }
113 168
 
114 169
 func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) {
115 170
 	// GetNetwork to match via full ID.
116
-	rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input})
117
-	if err != nil {
118
-		// If any error (including NotFound), ListNetworks to match via ID prefix and full name.
119
-		rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{Filters: &swarmapi.ListNetworksRequest_Filters{Names: []string{input}}})
120
-		if err != nil || len(rl.Networks) == 0 {
121
-			rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{Filters: &swarmapi.ListNetworksRequest_Filters{IDPrefixes: []string{input}}})
122
-		}
123
-
124
-		if err != nil {
125
-			return nil, err
126
-		}
171
+	if rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input}); err == nil {
172
+		return rg.Network, nil
173
+	}
127 174
 
128
-		if len(rl.Networks) == 0 {
129
-			return nil, fmt.Errorf("network %s not found", input)
130
-		}
175
+	// If any error (including NotFound), ListNetworks to match via ID prefix and full name.
176
+	rl, err := c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
177
+		Filters: &swarmapi.ListNetworksRequest_Filters{
178
+			Names: []string{input},
179
+		},
180
+	})
181
+	if err != nil || len(rl.Networks) == 0 {
182
+		rl, err = c.ListNetworks(ctx, &swarmapi.ListNetworksRequest{
183
+			Filters: &swarmapi.ListNetworksRequest_Filters{
184
+				IDPrefixes: []string{input},
185
+			},
186
+		})
187
+	}
188
+	if err != nil {
189
+		return nil, err
190
+	}
131 191
 
132
-		if l := len(rl.Networks); l > 1 {
133
-			return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)
134
-		}
192
+	if len(rl.Networks) == 0 {
193
+		return nil, fmt.Errorf("network %s not found", input)
194
+	}
135 195
 
136
-		return rl.Networks[0], nil
196
+	if l := len(rl.Networks); l > 1 {
197
+		return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l)
137 198
 	}
138
-	return rg.Network, nil
199
+
200
+	return rl.Networks[0], nil
139 201
 }
... ...
@@ -1,63 +1,14 @@
1 1
 package cluster
2 2
 
3 3
 import (
4
-	"fmt"
5
-	"strings"
6
-
7 4
 	apitypes "github.com/docker/docker/api/types"
8 5
 	types "github.com/docker/docker/api/types/swarm"
9 6
 	"github.com/docker/docker/daemon/cluster/convert"
10 7
 	swarmapi "github.com/docker/swarmkit/api"
11
-	"golang.org/x/net/context"
12 8
 )
13 9
 
14
-func getSecretByNameOrIDPrefix(ctx context.Context, state *nodeState, nameOrIDPrefix string) (*swarmapi.Secret, error) {
15
-	// attempt to lookup secret by full ID
16
-	if r, err := state.controlClient.GetSecret(ctx, &swarmapi.GetSecretRequest{
17
-		SecretID: nameOrIDPrefix,
18
-	}); err == nil {
19
-		return r.Secret, nil
20
-	}
21
-
22
-	// attempt to lookup secret by full name and partial ID
23
-	// Note here ListSecretRequest_Filters operate with `or`
24
-	r, err := state.controlClient.ListSecrets(ctx, &swarmapi.ListSecretsRequest{
25
-		Filters: &swarmapi.ListSecretsRequest_Filters{
26
-			Names:      []string{nameOrIDPrefix},
27
-			IDPrefixes: []string{nameOrIDPrefix},
28
-		},
29
-	})
30
-	if err != nil {
31
-		return nil, err
32
-	}
33
-
34
-	// attempt to lookup secret by full name
35
-	for _, s := range r.Secrets {
36
-		if s.Spec.Annotations.Name == nameOrIDPrefix {
37
-			return s, nil
38
-		}
39
-	}
40
-	// attempt to lookup secret by partial ID (prefix)
41
-	// return error if more than one matches found (ambiguous)
42
-	n := 0
43
-	var found *swarmapi.Secret
44
-	for _, s := range r.Secrets {
45
-		if strings.HasPrefix(s.ID, nameOrIDPrefix) {
46
-			found = s
47
-			n++
48
-		}
49
-	}
50
-	if n > 1 {
51
-		return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", nameOrIDPrefix, n)
52
-	}
53
-	if found == nil {
54
-		return nil, fmt.Errorf("no such secret: %s", nameOrIDPrefix)
55
-	}
56
-	return found, nil
57
-}
58
-
59 10
 // GetSecret returns a secret from a managed swarm cluster
60
-func (c *Cluster) GetSecret(nameOrIDPrefix string) (types.Secret, error) {
11
+func (c *Cluster) GetSecret(input string) (types.Secret, error) {
61 12
 	c.mu.RLock()
62 13
 	defer c.mu.RUnlock()
63 14
 
... ...
@@ -69,7 +20,7 @@ func (c *Cluster) GetSecret(nameOrIDPrefix string) (types.Secret, error) {
69 69
 	ctx, cancel := c.getRequestContext()
70 70
 	defer cancel()
71 71
 
72
-	secret, err := getSecretByNameOrIDPrefix(ctx, &state, nameOrIDPrefix)
72
+	secret, err := getSecret(ctx, state.controlClient, input)
73 73
 	if err != nil {
74 74
 		return types.Secret{}, err
75 75
 	}
... ...
@@ -133,7 +84,7 @@ func (c *Cluster) CreateSecret(s types.SecretSpec) (string, error) {
133 133
 }
134 134
 
135 135
 // RemoveSecret removes a secret from a managed swarm cluster.
136
-func (c *Cluster) RemoveSecret(nameOrIDPrefix string) error {
136
+func (c *Cluster) RemoveSecret(input string) error {
137 137
 	c.mu.RLock()
138 138
 	defer c.mu.RUnlock()
139 139
 
... ...
@@ -145,7 +96,7 @@ func (c *Cluster) RemoveSecret(nameOrIDPrefix string) error {
145 145
 	ctx, cancel := c.getRequestContext()
146 146
 	defer cancel()
147 147
 
148
-	secret, err := getSecretByNameOrIDPrefix(ctx, &state, nameOrIDPrefix)
148
+	secret, err := getSecret(ctx, state.controlClient, input)
149 149
 	if err != nil {
150 150
 		return err
151 151
 	}