Skip to content

Commit 19c93b5

Browse files
committed
Move installer updates from internal to pkg
Signed-off-by: Miquel Sabaté Solà <[email protected]>
1 parent 6525d1f commit 19c93b5

File tree

5 files changed

+69
-51
lines changed

5 files changed

+69
-51
lines changed

cmd/suseconnect/suseconnect.go

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ var (
2424
connectUsageText string
2525
)
2626

27+
const (
28+
outdatedRegProxy = "Your Registration Proxy server doesn't support this function. Please update and try again."
29+
)
30+
2731
// singleStringFlag cannot be set more than once.
2832
// e.g. `cmd -p abc -p def` will give a parse error.
2933
type singleStringFlag struct {
@@ -331,16 +335,6 @@ func main() {
331335
}
332336
}
333337

334-
func maybeBrokenSMTError(opts *connect.Options) error {
335-
// TODO(mssola): to be removed once we sort out the token callback
336-
// for the `internal/connect` library.
337-
if !connect.CFG.IsScc() && !connect.UpToDate() {
338-
return fmt.Errorf("Your Registration Proxy server doesn't support this function. " +
339-
"Please update it and try again.")
340-
}
341-
return nil
342-
}
343-
344338
func exitOnError(err error, opts *connect.Options) {
345339
if err == nil {
346340
return
@@ -354,8 +348,8 @@ func exitOnError(err error, opts *connect.Options) {
354348
os.Exit(64)
355349
}
356350
if je, ok := err.(connect.JSONError); ok {
357-
if err := maybeBrokenSMTError(opts); err != nil {
358-
fmt.Println(err)
351+
if connect.IsOutdatedRegProxy(opts) {
352+
fmt.Println(outdatedRegProxy)
359353
} else {
360354
fmt.Print("Error: Cannot parse response from server\n")
361355
fmt.Println(je)
@@ -368,8 +362,8 @@ func exitOnError(err error, opts *connect.Options) {
368362
fmt.Print("registered system was deleted in SUSE Customer Center. ")
369363
fmt.Print("Check ", connect.CFG.BaseURL, " whether your system appears there. ")
370364
fmt.Print("If it does not, please call SUSEConnect --cleanup and re-register this system.\n")
371-
} else if err := maybeBrokenSMTError(opts); err != nil {
372-
fmt.Println(err)
365+
} else if connect.IsOutdatedRegProxy(opts) {
366+
fmt.Println(outdatedRegProxy)
373367
} else {
374368
fmt.Println(ae)
375369
}

internal/connect/api.go

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"bufio"
55
"encoding/json"
66
"errors"
7-
"net/http"
87
"os"
98

109
"github.com/SUSE/connect-ng/internal/collectors"
@@ -16,21 +15,6 @@ const (
1615
UptimeLogFilePath = "/etc/zypp/suse-uptime.log"
1716
)
1817

19-
func upToDate() bool {
20-
// REVIST 404 case - see original
21-
// Should fail in any case. 422 error means that the endpoint is there and working right
22-
_, err := callHTTP("GET", "/connect/repositories/installer", nil, nil, authNone)
23-
if err == nil {
24-
return false
25-
}
26-
if ae, ok := err.(APIError); ok {
27-
if ae.Code == http.StatusUnprocessableEntity {
28-
return true
29-
}
30-
}
31-
return false
32-
}
33-
3418
// systemActivations returns a map keyed by "Identifier/Version/Arch"
3519
func systemActivations() (map[string]Activation, error) {
3620
activeMap := make(map[string]Activation)
@@ -310,18 +294,6 @@ func offlineProductMigrations(installed []Product, target Product) ([]MigrationP
310294
return migrations, nil
311295
}
312296

313-
func installerUpdates(product Product) ([]zypper.Repository, error) {
314-
repos := make([]zypper.Repository, 0)
315-
resp, err := callHTTP("GET", "/connect/repositories/installer", nil, product.toQuery(), authNone)
316-
if err != nil {
317-
return repos, err
318-
}
319-
if err = json.Unmarshal(resp, &repos); err != nil {
320-
return repos, JSONError{err}
321-
}
322-
return repos, nil
323-
}
324-
325297
func setLabels(labels []Label) error {
326298
var payload struct {
327299
Labels []Label `json:"labels"`

internal/connect/client.go

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,14 @@ package connect
33
import (
44
"encoding/json"
55
"fmt"
6+
"net/http"
67
"os"
78
"path/filepath"
89

910
cred "github.com/SUSE/connect-ng/internal/credentials"
1011
"github.com/SUSE/connect-ng/internal/util"
1112
"github.com/SUSE/connect-ng/internal/zypper"
13+
"github.com/SUSE/connect-ng/pkg/connection"
1214
"github.com/SUSE/connect-ng/pkg/registration"
1315
)
1416

@@ -320,10 +322,30 @@ func IsRegistered() bool {
320322
return err == nil
321323
}
322324

323-
// UpToDate Checks if API endpoint is up-to-date,
324-
// useful when dealing with RegistrationProxy errors
325-
func UpToDate() bool {
326-
return upToDate()
325+
// Returns true if the current system is targetting an old registration proxy.
326+
func IsOutdatedRegProxy(opts *Options) bool {
327+
// This is not a registration proxy, bail out.
328+
if opts.IsScc() {
329+
return false
330+
}
331+
332+
// The trick is to check on an API endpoint which is not supported by SMT.
333+
wrapper := New(opts)
334+
req, err := wrapper.Connection.BuildRequest("GET", "/connect/repositories/installer", nil)
335+
if err != nil {
336+
return false
337+
}
338+
339+
_, err = wrapper.Connection.Do(req)
340+
if err == nil {
341+
return false
342+
}
343+
if ae, ok := err.(*connection.ApiError); ok {
344+
if ae.Code == http.StatusUnprocessableEntity {
345+
return true
346+
}
347+
}
348+
return true
327349
}
328350

329351
// Print the given message plus some extra registration information that might
@@ -422,9 +444,25 @@ func DeregisterSystem() error {
422444
return deregisterSystem()
423445
}
424446

425-
// InstallerUpdates returns an array of Installer-Updates repositories for the given product
426-
func InstallerUpdates(product Product) ([]zypper.Repository, error) {
427-
return installerUpdates(product)
447+
// Returns the zypper repositories for the installer updates endpoint.
448+
func InstallerUpdates(opts *Options, product Product) ([]zypper.Repository, error) {
449+
repos := make([]zypper.Repository, 0)
450+
451+
wrapper := New(opts)
452+
req, err := wrapper.Connection.BuildRequest("GET", "/connect/repositories/installer", nil)
453+
if err != nil {
454+
return repos, err
455+
}
456+
req = connection.AddQuery(req, product.toQuery())
457+
458+
resp, err := wrapper.Connection.Do(req)
459+
if err != nil {
460+
return repos, err
461+
}
462+
if err = json.Unmarshal(resp, &repos); err != nil {
463+
return repos, JSONError{err}
464+
}
465+
return repos, nil
428466
}
429467

430468
// SyncProducts synchronizes activated system products to the registration server

pkg/connection/query.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package connection
2+
3+
import "net/http"
4+
5+
// Adds the given request query to the existing HTTP request object.
6+
func AddQuery(req *http.Request, query map[string]string) *http.Request {
7+
values := req.URL.Query()
8+
for n, v := range query {
9+
values.Add(n, v)
10+
}
11+
req.URL.RawQuery = values.Encode()
12+
13+
return req
14+
}

third_party/libsuseconnect/libsuseconnect.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,14 +385,14 @@ func reload_certificates() *C.char {
385385

386386
//export list_installer_updates
387387
func list_installer_updates(clientParams, product *C.char) *C.char {
388-
_ = loadConfig(C.GoString(clientParams))
388+
opts := loadConfig(C.GoString(clientParams))
389389

390390
var productQuery connect.Product
391391
err := json.Unmarshal([]byte(C.GoString(product)), &productQuery)
392392
if err != nil {
393393
return C.CString(errorToJSON(connect.JSONError{Err: err}))
394394
}
395-
repos, err := connect.InstallerUpdates(productQuery)
395+
repos, err := connect.InstallerUpdates(opts, productQuery)
396396
if err != nil {
397397
return C.CString(errorToJSON(err))
398398
}

0 commit comments

Comments
 (0)