@@ -34,31 +34,44 @@ func NewClient(id uuid.UUID, proxyURL *url.URL, tlsConfig *tls.Config) Client {
34
34
// Update is a single node from the update graph.
35
35
type Update node
36
36
37
+ // Error is returned when are unable to get updates.
38
+ type Error struct {
39
+ // Reason is the reason suggested for the ClusterOperator status condition.
40
+ Reason string
41
+
42
+ // Message is the message suggested for the ClusterOperator status condition.
43
+ Message string
44
+
45
+ // cause is the upstream error, if any, being wrapped by this error.
46
+ cause error
47
+ }
48
+
49
+ // Error serializes the error as a string, to satisfy the error interface.
50
+ func (err * Error ) Error () string {
51
+ return fmt .Sprintf ("%s: %s" , err .Reason , err .Message )
52
+ }
53
+
37
54
// GetUpdates fetches the next-applicable update payloads from the specified
38
55
// upstream Cincinnati stack given the current version and channel. The next-
39
56
// applicable updates are determined by downloading the update graph, finding
40
57
// the current version within that graph (typically the root node), and then
41
58
// finding all of the children. These children are the available updates for
42
59
// the current version and their payloads indicate from where the actual update
43
60
// image can be downloaded.
44
- func (c Client ) GetUpdates (upstream string , arch string , channel string , version semver.Version ) ([]Update , error ) {
61
+ func (c Client ) GetUpdates (uri * url. URL , arch string , channel string , version semver.Version ) ([]Update , error ) {
45
62
transport := http.Transport {}
46
63
// Prepare parametrized cincinnati query.
47
- cincinnatiURL , err := url .Parse (upstream )
48
- if err != nil {
49
- return nil , fmt .Errorf ("failed to parse upstream URL: %s" , err )
50
- }
51
- queryParams := cincinnatiURL .Query ()
64
+ queryParams := uri .Query ()
52
65
queryParams .Add ("arch" , arch )
53
66
queryParams .Add ("channel" , channel )
54
67
queryParams .Add ("id" , c .id .String ())
55
68
queryParams .Add ("version" , version .String ())
56
- cincinnatiURL .RawQuery = queryParams .Encode ()
69
+ uri .RawQuery = queryParams .Encode ()
57
70
58
71
// Download the update graph.
59
- req , err := http .NewRequest ("GET" , cincinnatiURL .String (), nil )
72
+ req , err := http .NewRequest ("GET" , uri .String (), nil )
60
73
if err != nil {
61
- return nil , err
74
+ return nil , & Error { Reason : "InvalidRequest" , Message : err . Error (), cause : err }
62
75
}
63
76
req .Header .Add ("Accept" , GraphMediaType )
64
77
if c .tlsConfig != nil {
@@ -72,23 +85,23 @@ func (c Client) GetUpdates(upstream string, arch string, channel string, version
72
85
client := http.Client {Transport : & transport }
73
86
resp , err := client .Do (req )
74
87
if err != nil {
75
- return nil , err
88
+ return nil , & Error { Reason : "RemoteFailed" , Message : err . Error (), cause : err }
76
89
}
77
90
defer resp .Body .Close ()
78
91
79
92
if resp .StatusCode != http .StatusOK {
80
- return nil , fmt .Errorf ("unexpected HTTP status: %s" , resp .Status )
93
+ return nil , & Error { Reason : "ResponseFailed" , Message : fmt .Sprintf ("unexpected HTTP status: %s" , resp .Status )}
81
94
}
82
95
83
96
// Parse the graph.
84
97
body , err := ioutil .ReadAll (resp .Body )
85
98
if err != nil {
86
- return nil , err
99
+ return nil , & Error { Reason : "ResponseFailed" , Message : err . Error (), cause : err }
87
100
}
88
101
89
102
var graph graph
90
103
if err = json .Unmarshal (body , & graph ); err != nil {
91
- return nil , err
104
+ return nil , & Error { Reason : "ResponseInvalid" , Message : err . Error (), cause : err }
92
105
}
93
106
94
107
// Find the current version within the graph.
@@ -102,7 +115,10 @@ func (c Client) GetUpdates(upstream string, arch string, channel string, version
102
115
}
103
116
}
104
117
if ! found {
105
- return nil , fmt .Errorf ("currently installed version %s not found in the %q channel" , version , channel )
118
+ return nil , & Error {
119
+ Reason : "VersionNotFound" ,
120
+ Message : fmt .Sprintf ("currently installed version %s not found in the %q channel" , version , channel ),
121
+ }
106
122
}
107
123
108
124
// Find the children of the current version.
0 commit comments