1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: Apache-2.0
15 // KeepService is an arvados#keepService record
16 type KeepService struct {
17 UUID string `json:"uuid"`
18 ServiceHost string `json:"service_host"`
19 ServicePort int `json:"service_port"`
20 ServiceSSLFlag bool `json:"service_ssl_flag"`
21 ServiceType string `json:"service_type"`
22 ReadOnly bool `json:"read_only"`
25 // KeepServiceList is an arvados#keepServiceList record
26 type KeepServiceList struct {
27 Items []KeepService `json:"items"`
28 ItemsAvailable int `json:"items_available"`
29 Offset int `json:"offset"`
30 Limit int `json:"limit"`
33 // KeepServiceIndexEntry is what a keep service's index response tells
34 // us about a stored block.
35 type KeepServiceIndexEntry struct {
37 // Time of last write, in nanoseconds since Unix epoch
41 // EachKeepService calls f once for every readable
42 // KeepService. EachKeepService stops if it encounters an
43 // error, such as f returning a non-nil error.
44 func (c *Client) EachKeepService(f func(KeepService) error) error {
45 params := ResourceListParams{}
47 var page KeepServiceList
48 err := c.RequestAndDecode(&page, "GET", "arvados/v1/keep_services", nil, params)
52 for _, item := range page.Items {
58 params.Offset = params.Offset + len(page.Items)
59 if params.Offset >= page.ItemsAvailable {
65 func (s *KeepService) url(path string) string {
68 f = "https://%s:%d/%s"
72 return fmt.Sprintf(f, s.ServiceHost, s.ServicePort, path)
75 // String implements fmt.Stringer
76 func (s *KeepService) String() string {
80 // Index returns an unsorted list of blocks that can be retrieved from
82 func (s *KeepService) Index(c *Client, prefix string) ([]KeepServiceIndexEntry, error) {
83 url := s.url("index/" + prefix)
84 req, err := http.NewRequest("GET", url, nil)
86 return nil, fmt.Errorf("NewRequest(%v): %v", url, err)
88 resp, err := c.Do(req)
90 return nil, fmt.Errorf("Do(%v): %v", url, err)
91 } else if resp.StatusCode != 200 {
92 return nil, fmt.Errorf("%v: %v", url, resp.Status)
94 defer resp.Body.Close()
96 var entries []KeepServiceIndexEntry
97 scanner := bufio.NewScanner(resp.Body)
101 return nil, fmt.Errorf("Index response contained non-terminal blank line")
103 line := scanner.Text()
108 fields := strings.Split(line, " ")
109 if len(fields) != 2 {
110 return nil, fmt.Errorf("Malformed index line %q: %d fields", line, len(fields))
112 mtime, err := strconv.ParseInt(fields[1], 10, 64)
114 return nil, fmt.Errorf("Malformed index line %q: mtime: %v", line, err)
117 // An old version of keepstore is giving us
118 // timestamps in seconds instead of
119 // nanoseconds. (This threshold correctly
120 // handles all times between 1970-01-02 and
124 entries = append(entries, KeepServiceIndexEntry{
125 SizedDigest: SizedDigest(fields[0]),
129 if err := scanner.Err(); err != nil {
130 return nil, fmt.Errorf("Error scanning index response: %v", err)
133 return nil, fmt.Errorf("Index response had no EOF marker")