Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(102)

Side by Side Diff: go/src/infra/tools/cipd/remote.go

Issue 1194803002: Add a package listing API to cipd. (Closed) Base URL: https://chromium.googlesource.com/infra/infra.git@master
Patch Set: Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package cipd 5 package cipd
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/json" 9 "encoding/json"
10 "errors" 10 "errors"
(...skipping 108 matching lines...) Expand 10 before | Expand all | Expand 10 after
119 119
120 // Success? 120 // Success?
121 if resp.StatusCode < 300 { 121 if resp.StatusCode < 300 {
122 return json.Unmarshal(responseBody, response) 122 return json.Unmarshal(responseBody, response)
123 } 123 }
124 124
125 // Fatal error? 125 // Fatal error?
126 if resp.StatusCode == 403 || resp.StatusCode == 401 { 126 if resp.StatusCode == 403 || resp.StatusCode == 401 {
127 return ErrAccessDenined 127 return ErrAccessDenined
128 } 128 }
129 » » return fmt.Errorf("Unexpected reply (HTTP %d):\n%s", resp.Status Code, string(body)) 129 » » return fmt.Errorf("Unexpected reply (HTTP %d):\n%s", resp.Status Code, string(responseBody))
130 } 130 }
131 131
132 return ErrBackendInaccessible 132 return ErrBackendInaccessible
133 } 133 }
134 134
135 func (r *remoteImpl) initiateUpload(sha1 string) (s *UploadSession, err error) { 135 func (r *remoteImpl) initiateUpload(sha1 string) (s *UploadSession, err error) {
136 var reply struct { 136 var reply struct {
137 Status string `json:"status"` 137 Status string `json:"status"`
138 UploadSessionID string `json:"upload_session_id"` 138 UploadSessionID string `json:"upload_session_id"`
139 UploadURL string `json:"upload_url"` 139 UploadURL string `json:"upload_url"`
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
411 case "SUCCESS": 411 case "SUCCESS":
412 return nil 412 return nil
413 case "PROCESSING_NOT_FINISHED_YET": 413 case "PROCESSING_NOT_FINISHED_YET":
414 return &pendingProcessingError{reply.ErrorMessage} 414 return &pendingProcessingError{reply.ErrorMessage}
415 case "ERROR", "PROCESSING_FAILED": 415 case "ERROR", "PROCESSING_FAILED":
416 return errors.New(reply.ErrorMessage) 416 return errors.New(reply.ErrorMessage)
417 } 417 }
418 return fmt.Errorf("Unexpected status when attaching tags: %s", reply.Sta tus) 418 return fmt.Errorf("Unexpected status when attaching tags: %s", reply.Sta tus)
419 } 419 }
420 420
421 func (r *remoteImpl) listPackages(path string, recursive bool) ([]string, []stri ng, error) {
422 endpoint, err := packageEndpoint(path, recursive)
423 if err != nil {
424 return nil, nil, err
425 }
426 var reply struct {
427 Status string `json:"status"`
428 ErrorMessage string `json:"error_message"`
429 Packages []string `json:"packages"`
430 Directories []string `json:"directories"`
431 }
432 err = r.makeRequest(endpoint, "GET", nil, &reply)
433 if err != nil {
434 return nil, nil, err
435 }
436 switch reply.Status {
437 case "SUCCESS":
438 packages := reply.Packages
439 directories := reply.Directories
440 return packages, directories, nil
441 case "ERROR":
442 return nil, nil, errors.New(reply.ErrorMessage)
443 }
444 return nil, nil, fmt.Errorf("Unexpected list packages status: %s", reply .Status)
445 }
446
421 //////////////////////////////////////////////////////////////////////////////// 447 ////////////////////////////////////////////////////////////////////////////////
422 448
423 func instanceEndpoint(pin common.Pin) (string, error) { 449 func instanceEndpoint(pin common.Pin) (string, error) {
424 err := common.ValidatePin(pin) 450 err := common.ValidatePin(pin)
425 if err != nil { 451 if err != nil {
426 return "", err 452 return "", err
427 } 453 }
428 params := url.Values{} 454 params := url.Values{}
429 params.Add("package_name", pin.PackageName) 455 params.Add("package_name", pin.PackageName)
430 params.Add("instance_id", pin.InstanceID) 456 params.Add("instance_id", pin.InstanceID)
431 return "repo/v1/instance?" + params.Encode(), nil 457 return "repo/v1/instance?" + params.Encode(), nil
432 } 458 }
433 459
434 func aclEndpoint(packagePath string) (string, error) { 460 func aclEndpoint(packagePath string) (string, error) {
435 err := common.ValidatePackageName(packagePath) 461 err := common.ValidatePackageName(packagePath)
436 if err != nil { 462 if err != nil {
437 return "", err 463 return "", err
438 } 464 }
439 params := url.Values{} 465 params := url.Values{}
440 params.Add("package_path", packagePath) 466 params.Add("package_path", packagePath)
441 return "repo/v1/acl?" + params.Encode(), nil 467 return "repo/v1/acl?" + params.Encode(), nil
442 } 468 }
443 469
470 func packageEndpoint(path string, recursive bool) (string, error) {
Vadim Sh. 2015/06/23 00:08:35 nit: packageSearchEndpoint
estaab 2015/06/23 02:26:14 Done.
471 params := url.Values{}
472 params.Add("path", path)
473 recursiveString := "false"
474 if recursive {
475 recursiveString = "true"
476 }
477 params.Add("recursive", recursiveString)
478 return "repo/v1/package/search?" + params.Encode(), nil
479 }
480
444 func tagsEndpoint(pin common.Pin, tags []string) (string, error) { 481 func tagsEndpoint(pin common.Pin, tags []string) (string, error) {
445 err := common.ValidatePin(pin) 482 err := common.ValidatePin(pin)
446 if err != nil { 483 if err != nil {
447 return "", err 484 return "", err
448 } 485 }
449 for _, tag := range tags { 486 for _, tag := range tags {
450 err = common.ValidateInstanceTag(tag) 487 err = common.ValidateInstanceTag(tag)
451 if err != nil { 488 if err != nil {
452 return "", err 489 return "", err
453 } 490 }
454 } 491 }
455 params := url.Values{} 492 params := url.Values{}
456 params.Add("package_name", pin.PackageName) 493 params.Add("package_name", pin.PackageName)
457 params.Add("instance_id", pin.InstanceID) 494 params.Add("instance_id", pin.InstanceID)
458 for _, tag := range tags { 495 for _, tag := range tags {
459 params.Add("tag", tag) 496 params.Add("tag", tag)
460 } 497 }
461 return "repo/v1/tags?" + params.Encode(), nil 498 return "repo/v1/tags?" + params.Encode(), nil
462 } 499 }
463 500
464 // convertTimestamp coverts string with int64 timestamp in microseconds since 501 // convertTimestamp coverts string with int64 timestamp in microseconds since
465 // to time.Time 502 // to time.Time
466 func convertTimestamp(ts string) (time.Time, error) { 503 func convertTimestamp(ts string) (time.Time, error) {
467 i, err := strconv.ParseInt(ts, 10, 64) 504 i, err := strconv.ParseInt(ts, 10, 64)
468 if err != nil { 505 if err != nil {
469 return time.Time{}, fmt.Errorf("Unexpected timestamp value '%s' in the server response", ts) 506 return time.Time{}, fmt.Errorf("Unexpected timestamp value '%s' in the server response", ts)
470 } 507 }
471 return time.Unix(0, i*1000), nil 508 return time.Unix(0, i*1000), nil
472 } 509 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698