| 189 | } |
| 190 | |
| 191 | func (e *Etcd) Get(query *protocol.KontrolQuery) (Kites, error) { |
| 192 | // We will make a get request to etcd store with this key. So get a "etcd" |
| 193 | // key from the given query so that we can use it to query from Etcd. |
| 194 | etcdKey, err := e.etcdKey(query) |
| 195 | if err != nil { |
| 196 | return nil, err |
| 197 | } |
| 198 | |
| 199 | // If version field contains a constraint we need no make a new query up to |
| 200 | // "name" field and filter the results after getting all versions. |
| 201 | // NewVersion returns an error if it's a constraint, like: ">= 1.0, < 1.4" |
| 202 | // Because NewConstraint doesn't return an error for version's like "0.0.1" |
| 203 | // we check it with the NewVersion function. |
| 204 | var hasVersionConstraint bool // does query contains a constraint on version? |
| 205 | var keyRest string // query key after the version field |
| 206 | var versionConstraint version.Constraints |
| 207 | _, err = version.NewVersion(query.Version) |
| 208 | if err != nil && query.Version != "" { |
| 209 | // now parse our constraint |
| 210 | versionConstraint, err = version.NewConstraint(query.Version) |
| 211 | if err != nil { |
| 212 | // version is a malformed, just return the error |
| 213 | return nil, err |
| 214 | } |
| 215 | |
| 216 | hasVersionConstraint = true |
| 217 | nameQuery := &protocol.KontrolQuery{ |
| 218 | Username: query.Username, |
| 219 | Environment: query.Environment, |
| 220 | Name: query.Name, |
| 221 | } |
| 222 | // We will make a get request to all nodes under this name |
| 223 | // and filter the result later. |
| 224 | etcdKey, _ = GetQueryKey(nameQuery) |
| 225 | |
| 226 | // Rest of the key after version field |
| 227 | keyRest = "/" + strings.TrimRight( |
| 228 | query.Region+"/"+query.Hostname+"/"+query.ID, "/") |
| 229 | } |
| 230 | |
| 231 | resp, err := e.client.Get(context.TODO(), |
| 232 | KitesPrefix+"/"+etcdKey, |
| 233 | &etcd.GetOptions{ |
| 234 | Recursive: true, |
| 235 | }, |
| 236 | ) |
| 237 | if err != nil { |
| 238 | // if it's something else just return |
| 239 | return nil, err |
| 240 | } |
| 241 | |
| 242 | kites := make(Kites, 0) |
| 243 | |
| 244 | node := NewNode(resp.Node) |
| 245 | |
| 246 | // means a query with all fields were made or a query with an ID was made, |
| 247 | // in which case also returns a full path. This path has a value that |
| 248 | // contains the final kite URL. Therefore this is a single kite result, |