Skip to content

Commit d8b5d65

Browse files
authored
Merge pull request #322 from SUSE/fix-instance-data
Fix a couple of issues as reported by using SUSEConnect `next` on public cloud instances
2 parents bf0e044 + 657d29c commit d8b5d65

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

internal/connect/client.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ func Register(api WrappedAPI, opts *Options) error {
5353
printInformation(fmt.Sprintf("Registering system to %s", opts.ServerName()), opts)
5454
}
5555

56-
if err := api.RegisterOrKeepAlive(opts.Token); err != nil {
56+
if err := api.RegisterOrKeepAlive(opts.Token, opts.InstanceDataFile); err != nil {
5757
return err
5858
}
5959

internal/connect/wrapper.go

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ import (
1919
// when unit testing
2020
type WrappedAPI interface {
2121
KeepAlive() error
22-
Register(regcode string) error
23-
RegisterOrKeepAlive(regcode string) error
22+
Register(regcode, instanceDataFile string) error
23+
RegisterOrKeepAlive(regcode, instanceDataFile string) error
2424
IsRegistered() bool
2525
AssignLabels(labels []string) ([]labels.Label, error)
2626

@@ -118,25 +118,38 @@ func (w Wrapper) KeepAlive() error {
118118
return err
119119
}
120120

121-
func (w Wrapper) Register(regcode string) error {
121+
func (w Wrapper) Register(regcode, instanceDataFile string) error {
122122
hwinfo, err := FetchSystemInformation()
123123
if err != nil {
124124
return fmt.Errorf("could not fetch system's information: %v", err)
125125
}
126126
hostname := collectors.FromResult(hwinfo, "hostname", "")
127127

128-
// TODO: do something with the code
129-
_, err = registration.Register(w.Connection, regcode, hostname, hwinfo, registration.NoExtraData)
128+
// If an instance-data file was provided, try to read it and attach it as
129+
// "extra" data. This will be used inside of the `registration.Register`
130+
// code.
131+
extraData := registration.NoExtraData
132+
if instanceDataFile != "" {
133+
data, err := os.ReadFile(instanceDataFile)
134+
if err != nil {
135+
return err
136+
}
137+
extraData["instance_data"] = string(data)
138+
}
139+
140+
// NOTE: we are not interested in the code. Hence, we don't save it
141+
// anywhere.
142+
_, err = registration.Register(w.Connection, regcode, hostname, hwinfo, extraData)
130143
return err
131144
}
132145

133146
// RegisterOrKeepAlive calls either `Register` or `KeepAlive` depending on
134147
// whether the current system is registered or not.
135-
func (w Wrapper) RegisterOrKeepAlive(regcode string) error {
148+
func (w Wrapper) RegisterOrKeepAlive(regcode, instanceDataFile string) error {
136149
if w.Registered {
137150
return w.KeepAlive()
138151
}
139-
return w.Register(regcode)
152+
return w.Register(regcode, instanceDataFile)
140153
}
141154

142155
func (w Wrapper) IsRegistered() bool {

pkg/connection/api_error.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,11 @@ func ErrorFromResponse(resp *http.Response) *ApiError {
3030

3131
ae := &ApiError{Code: resp.StatusCode}
3232
if err := json.NewDecoder(resp.Body).Decode(ae); err != nil {
33-
return nil
33+
// In some servers the response is actually not a JSON message, but
34+
// rather some NGinx default page. In that case, just set the HTML
35+
// status string as the message.
36+
ae.Message = resp.Status
37+
ae.LocalizedMessage = resp.Status
3438
}
3539
return ae
3640
}

0 commit comments

Comments
 (0)