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

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
« no previous file with comments | « go/src/infra/tools/cipd/client_test.go ('k') | go/src/infra/tools/cipd/remote_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 303 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 case "SUCCESS": 443 case "SUCCESS":
444 return nil 444 return nil
445 case "PROCESSING_NOT_FINISHED_YET": 445 case "PROCESSING_NOT_FINISHED_YET":
446 return &pendingProcessingError{reply.ErrorMessage} 446 return &pendingProcessingError{reply.ErrorMessage}
447 case "ERROR", "PROCESSING_FAILED": 447 case "ERROR", "PROCESSING_FAILED":
448 return errors.New(reply.ErrorMessage) 448 return errors.New(reply.ErrorMessage)
449 } 449 }
450 return fmt.Errorf("Unexpected status when attaching tags: %s", reply.Sta tus) 450 return fmt.Errorf("Unexpected status when attaching tags: %s", reply.Sta tus)
451 } 451 }
452 452
453 func (r *remoteImpl) listPackages(path string, recursive bool) ([]string, []stri ng, error) {
454 endpoint, err := packageSearchEndpoint(path, recursive)
455 if err != nil {
456 return nil, nil, err
457 }
458 var reply struct {
459 Status string `json:"status"`
460 ErrorMessage string `json:"error_message"`
461 Packages []string `json:"packages"`
462 Directories []string `json:"directories"`
463 }
464 err = r.makeRequest(endpoint, "GET", nil, &reply)
465 if err != nil {
466 return nil, nil, err
467 }
468 switch reply.Status {
469 case "SUCCESS":
470 packages := reply.Packages
471 directories := reply.Directories
472 return packages, directories, nil
473 case "ERROR":
474 return nil, nil, errors.New(reply.ErrorMessage)
475 }
476 return nil, nil, fmt.Errorf("Unexpected list packages status: %s", reply .Status)
477 }
478
453 //////////////////////////////////////////////////////////////////////////////// 479 ////////////////////////////////////////////////////////////////////////////////
454 480
455 func instanceEndpoint(pin common.Pin) (string, error) { 481 func instanceEndpoint(pin common.Pin) (string, error) {
456 if err := common.ValidatePin(pin); err != nil { 482 if err := common.ValidatePin(pin); err != nil {
457 return "", err 483 return "", err
458 } 484 }
459 params := url.Values{} 485 params := url.Values{}
460 params.Add("package_name", pin.PackageName) 486 params.Add("package_name", pin.PackageName)
461 params.Add("instance_id", pin.InstanceID) 487 params.Add("instance_id", pin.InstanceID)
462 return "repo/v1/instance?" + params.Encode(), nil 488 return "repo/v1/instance?" + params.Encode(), nil
(...skipping 14 matching lines...) Expand all
477 } 503 }
478 if err := common.ValidatePackageRef(ref); err != nil { 504 if err := common.ValidatePackageRef(ref); err != nil {
479 return "", err 505 return "", err
480 } 506 }
481 params := url.Values{} 507 params := url.Values{}
482 params.Add("package_name", packageName) 508 params.Add("package_name", packageName)
483 params.Add("ref", ref) 509 params.Add("ref", ref)
484 return "repo/v1/ref?" + params.Encode(), nil 510 return "repo/v1/ref?" + params.Encode(), nil
485 } 511 }
486 512
513 func packageSearchEndpoint(path string, recursive bool) (string, error) {
514 params := url.Values{}
515 params.Add("path", path)
516 recursiveString := "false"
517 if recursive {
518 recursiveString = "true"
519 }
520 params.Add("recursive", recursiveString)
521 return "repo/v1/package/search?" + params.Encode(), nil
522 }
523
487 func tagsEndpoint(pin common.Pin, tags []string) (string, error) { 524 func tagsEndpoint(pin common.Pin, tags []string) (string, error) {
488 if err := common.ValidatePin(pin); err != nil { 525 if err := common.ValidatePin(pin); err != nil {
489 return "", err 526 return "", err
490 } 527 }
491 for _, tag := range tags { 528 for _, tag := range tags {
492 if err := common.ValidateInstanceTag(tag); err != nil { 529 if err := common.ValidateInstanceTag(tag); err != nil {
493 return "", err 530 return "", err
494 } 531 }
495 } 532 }
496 params := url.Values{} 533 params := url.Values{}
497 params.Add("package_name", pin.PackageName) 534 params.Add("package_name", pin.PackageName)
498 params.Add("instance_id", pin.InstanceID) 535 params.Add("instance_id", pin.InstanceID)
499 for _, tag := range tags { 536 for _, tag := range tags {
500 params.Add("tag", tag) 537 params.Add("tag", tag)
501 } 538 }
502 return "repo/v1/tags?" + params.Encode(), nil 539 return "repo/v1/tags?" + params.Encode(), nil
503 } 540 }
504 541
505 // convertTimestamp coverts string with int64 timestamp in microseconds since 542 // convertTimestamp coverts string with int64 timestamp in microseconds since
506 // to time.Time 543 // to time.Time
507 func convertTimestamp(ts string) (time.Time, error) { 544 func convertTimestamp(ts string) (time.Time, error) {
508 i, err := strconv.ParseInt(ts, 10, 64) 545 i, err := strconv.ParseInt(ts, 10, 64)
509 if err != nil { 546 if err != nil {
510 return time.Time{}, fmt.Errorf("Unexpected timestamp value '%s' in the server response", ts) 547 return time.Time{}, fmt.Errorf("Unexpected timestamp value '%s' in the server response", ts)
511 } 548 }
512 return time.Unix(0, i*1000), nil 549 return time.Unix(0, i*1000), nil
513 } 550 }
OLDNEW
« no previous file with comments | « go/src/infra/tools/cipd/client_test.go ('k') | go/src/infra/tools/cipd/remote_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698