github.com/fabiokung/docker@v0.11.2-0.20170222101415-4534dcd49497/daemon/cluster/helpers.go (about) 1 package cluster 2 3 import ( 4 "fmt" 5 6 "github.com/docker/docker/api/errors" 7 swarmapi "github.com/docker/swarmkit/api" 8 "golang.org/x/net/context" 9 ) 10 11 func getSwarm(ctx context.Context, c swarmapi.ControlClient) (*swarmapi.Cluster, error) { 12 rl, err := c.ListClusters(ctx, &swarmapi.ListClustersRequest{}) 13 if err != nil { 14 return nil, err 15 } 16 17 if len(rl.Clusters) == 0 { 18 return nil, errors.NewRequestNotFoundError(errNoSwarm) 19 } 20 21 // TODO: assume one cluster only 22 return rl.Clusters[0], nil 23 } 24 25 func getNode(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Node, error) { 26 // GetNode to match via full ID. 27 if rg, err := c.GetNode(ctx, &swarmapi.GetNodeRequest{NodeID: input}); err == nil { 28 return rg.Node, nil 29 } 30 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 } 48 49 if len(rl.Nodes) == 0 { 50 err := fmt.Errorf("node %s not found", input) 51 return nil, errors.NewRequestNotFoundError(err) 52 } 53 54 if l := len(rl.Nodes); l > 1 { 55 return nil, fmt.Errorf("node %s is ambiguous (%d matches found)", input, l) 56 } 57 58 return rl.Nodes[0], nil 59 } 60 61 func getService(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Service, error) { 62 // GetService to match via full ID. 63 if rg, err := c.GetService(ctx, &swarmapi.GetServiceRequest{ServiceID: input}); err == nil { 64 return rg.Service, nil 65 } 66 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 } 84 85 if len(rl.Services) == 0 { 86 err := fmt.Errorf("service %s not found", input) 87 return nil, errors.NewRequestNotFoundError(err) 88 } 89 90 if l := len(rl.Services); l > 1 { 91 return nil, fmt.Errorf("service %s is ambiguous (%d matches found)", input, l) 92 } 93 94 return rl.Services[0], nil 95 } 96 97 func getTask(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Task, error) { 98 // GetTask to match via full ID. 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 } 117 if err != nil { 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 } 125 126 if l := len(rl.Tasks); l > 1 { 127 return nil, fmt.Errorf("task %s is ambiguous (%d matches found)", input, l) 128 } 129 130 return rl.Tasks[0], nil 131 } 132 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 } 156 157 if len(rl.Secrets) == 0 { 158 err := fmt.Errorf("secret %s not found", input) 159 return nil, errors.NewRequestNotFoundError(err) 160 } 161 162 if l := len(rl.Secrets); l > 1 { 163 return nil, fmt.Errorf("secret %s is ambiguous (%d matches found)", input, l) 164 } 165 166 return rl.Secrets[0], nil 167 } 168 169 func getNetwork(ctx context.Context, c swarmapi.ControlClient, input string) (*swarmapi.Network, error) { 170 // GetNetwork to match via full ID. 171 if rg, err := c.GetNetwork(ctx, &swarmapi.GetNetworkRequest{NetworkID: input}); err == nil { 172 return rg.Network, nil 173 } 174 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 } 191 192 if len(rl.Networks) == 0 { 193 return nil, fmt.Errorf("network %s not found", input) 194 } 195 196 if l := len(rl.Networks); l > 1 { 197 return nil, fmt.Errorf("network %s is ambiguous (%d matches found)", input, l) 198 } 199 200 return rl.Networks[0], nil 201 }