| OLD | NEW |
| 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 400 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 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(prefix string) ([]string, error) { |
| 422 endpoint, err := packageEndpoint(prefix) |
| 423 if err != nil { |
| 424 return nil, err |
| 425 } |
| 426 var reply struct { |
| 427 Status string `json:"status"` |
| 428 ErrorMessage string `json:"error_message"` |
| 429 Packages []string `json:"packages"` |
| 430 } |
| 431 err = r.makeRequest(endpoint, "GET", nil, &reply) |
| 432 if err != nil { |
| 433 return nil, err |
| 434 } |
| 435 switch reply.Status { |
| 436 case "SUCCESS": |
| 437 out := reply.Packages |
| 438 return out, nil |
| 439 case "ERROR": |
| 440 return nil, errors.New(reply.ErrorMessage) |
| 441 } |
| 442 return nil, fmt.Errorf("Unexpected list packages status: %s", reply.Stat
us) |
| 443 } |
| 444 |
| 421 //////////////////////////////////////////////////////////////////////////////// | 445 //////////////////////////////////////////////////////////////////////////////// |
| 422 | 446 |
| 423 func instanceEndpoint(pin common.Pin) (string, error) { | 447 func instanceEndpoint(pin common.Pin) (string, error) { |
| 424 err := common.ValidatePin(pin) | 448 err := common.ValidatePin(pin) |
| 425 if err != nil { | 449 if err != nil { |
| 426 return "", err | 450 return "", err |
| 427 } | 451 } |
| 428 params := url.Values{} | 452 params := url.Values{} |
| 429 params.Add("package_name", pin.PackageName) | 453 params.Add("package_name", pin.PackageName) |
| 430 params.Add("instance_id", pin.InstanceID) | 454 params.Add("instance_id", pin.InstanceID) |
| 431 return "repo/v1/instance?" + params.Encode(), nil | 455 return "repo/v1/instance?" + params.Encode(), nil |
| 432 } | 456 } |
| 433 | 457 |
| 434 func aclEndpoint(packagePath string) (string, error) { | 458 func aclEndpoint(packagePath string) (string, error) { |
| 435 err := common.ValidatePackageName(packagePath) | 459 err := common.ValidatePackageName(packagePath) |
| 436 if err != nil { | 460 if err != nil { |
| 437 return "", err | 461 return "", err |
| 438 } | 462 } |
| 439 params := url.Values{} | 463 params := url.Values{} |
| 440 params.Add("package_path", packagePath) | 464 params.Add("package_path", packagePath) |
| 441 return "repo/v1/acl?" + params.Encode(), nil | 465 return "repo/v1/acl?" + params.Encode(), nil |
| 442 } | 466 } |
| 443 | 467 |
| 468 func packageEndpoint(prefix string) (string, error) { |
| 469 params := url.Values{} |
| 470 params.Add("prefix", prefix) |
| 471 return "repo/v1/packages?" + params.Encode(), nil |
| 472 } |
| 473 |
| 444 func tagsEndpoint(pin common.Pin, tags []string) (string, error) { | 474 func tagsEndpoint(pin common.Pin, tags []string) (string, error) { |
| 445 err := common.ValidatePin(pin) | 475 err := common.ValidatePin(pin) |
| 446 if err != nil { | 476 if err != nil { |
| 447 return "", err | 477 return "", err |
| 448 } | 478 } |
| 449 for _, tag := range tags { | 479 for _, tag := range tags { |
| 450 err = common.ValidateInstanceTag(tag) | 480 err = common.ValidateInstanceTag(tag) |
| 451 if err != nil { | 481 if err != nil { |
| 452 return "", err | 482 return "", err |
| 453 } | 483 } |
| 454 } | 484 } |
| 455 params := url.Values{} | 485 params := url.Values{} |
| 456 params.Add("package_name", pin.PackageName) | 486 params.Add("package_name", pin.PackageName) |
| 457 params.Add("instance_id", pin.InstanceID) | 487 params.Add("instance_id", pin.InstanceID) |
| 458 for _, tag := range tags { | 488 for _, tag := range tags { |
| 459 params.Add("tag", tag) | 489 params.Add("tag", tag) |
| 460 } | 490 } |
| 461 return "repo/v1/tags?" + params.Encode(), nil | 491 return "repo/v1/tags?" + params.Encode(), nil |
| 462 } | 492 } |
| 463 | 493 |
| 464 // convertTimestamp coverts string with int64 timestamp in microseconds since | 494 // convertTimestamp coverts string with int64 timestamp in microseconds since |
| 465 // to time.Time | 495 // to time.Time |
| 466 func convertTimestamp(ts string) (time.Time, error) { | 496 func convertTimestamp(ts string) (time.Time, error) { |
| 467 i, err := strconv.ParseInt(ts, 10, 64) | 497 i, err := strconv.ParseInt(ts, 10, 64) |
| 468 if err != nil { | 498 if err != nil { |
| 469 return time.Time{}, fmt.Errorf("Unexpected timestamp value '%s'
in the server response", ts) | 499 return time.Time{}, fmt.Errorf("Unexpected timestamp value '%s'
in the server response", ts) |
| 470 } | 500 } |
| 471 return time.Unix(0, i*1000), nil | 501 return time.Unix(0, i*1000), nil |
| 472 } | 502 } |
| OLD | NEW |