Skip to content

Commit 1160bfd

Browse files
committed
SMT requires a distro_target in the payload.
1 parent d445135 commit 1160bfd

File tree

8 files changed

+68
-29
lines changed

8 files changed

+68
-29
lines changed

internal/connect/client.go

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,28 +53,27 @@ 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, opts.InstanceDataFile, opts.EnableSystemUptimeTracking); err != nil {
57-
return err
58-
}
59-
6056
installReleasePkg := true
61-
product := opts.Product
62-
if product.IsEmpty() {
57+
if opts.Product.IsEmpty() {
6358
base, err := zypper.BaseProduct()
6459
if err != nil {
6560
return err
6661
}
67-
product = base
62+
opts.Product = base
6863
installReleasePkg = false
6964
}
7065

71-
if service, err := registerProduct(conn, opts, product, installReleasePkg); err == nil {
66+
if err := api.RegisterOrKeepAlive(opts); err != nil {
67+
return err
68+
}
69+
70+
if service, err := registerProduct(conn, opts, opts.Product, installReleasePkg); err == nil {
7271
out.Products = append(out.Products, ProductService{
7372
Product: ProductOut{
74-
Name: product.Name,
75-
Identifier: product.Identifier,
76-
Version: product.Version,
77-
Arch: product.Arch,
73+
Name: opts.Product.Name,
74+
Identifier: opts.Product.Identifier,
75+
Version: opts.Product.Version,
76+
Arch: opts.Product.Arch,
7877
},
7978
Service: ServiceOut{
8079
Id: service.ID,
@@ -86,8 +85,8 @@ func Register(api WrappedAPI, opts *Options) error {
8685
return err
8786
}
8887

89-
if product.IsBase {
90-
p, err := registration.FetchProductInfo(conn, product.Identifier, product.Version, product.Arch)
88+
if opts.Product.IsBase {
89+
p, err := registration.FetchProductInfo(conn, opts.Product.Identifier, opts.Product.Version, opts.Product.Arch)
9190
if err != nil {
9291
return err
9392
}

internal/connect/wrapper.go

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ import (
2020
// when unit testing
2121
type WrappedAPI interface {
2222
KeepAlive(uptimeTracking bool) error
23-
Register(regcode, instanceDataFile string) error
24-
RegisterOrKeepAlive(regcode, instanceDataFile string, uptimeTracking bool) error
23+
Register(opts *Options) error
24+
RegisterOrKeepAlive(opts *Options) error
2525
IsRegistered() bool
2626
AssignLabels(labels []string) ([]labels.Label, error)
2727

@@ -145,7 +145,7 @@ func (w Wrapper) KeepAlive(uptimeTracking bool) error {
145145
return err
146146
}
147147

148-
func (w Wrapper) Register(regcode, instanceDataFile string) error {
148+
func (w Wrapper) Register(opts *Options) error {
149149
arch, _ := collectors.DetectArchitecture()
150150
hwinfo, err := FetchSystemInformation(arch)
151151
if err != nil {
@@ -157,8 +157,8 @@ func (w Wrapper) Register(regcode, instanceDataFile string) error {
157157
// "extra" data. This will be used inside of the `registration.Register`
158158
// code.
159159
extraData := registration.ExtraData{}
160-
if instanceDataFile != "" {
161-
data, err := os.ReadFile(instanceDataFile)
160+
if opts.InstanceDataFile != "" {
161+
data, err := os.ReadFile(opts.InstanceDataFile)
162162
if err != nil {
163163
return err
164164
}
@@ -170,7 +170,10 @@ func (w Wrapper) Register(regcode, instanceDataFile string) error {
170170
profileData, err := FetchSystemProfiles(arch, true)
171171
extraData["system_profiles"] = profileData
172172

173-
_, err = registration.Register(w.Connection, regcode, hostname, hwinfo, extraData)
173+
// add distro_target to extra data
174+
extraData["distro_target"] = opts.Product.DistroTarget()
175+
176+
_, err = registration.Register(w.Connection, opts.Token, hostname, hwinfo, extraData)
174177
if err != nil {
175178
profiles.DeleteProfileCache("*-profile-id")
176179
}
@@ -179,11 +182,11 @@ func (w Wrapper) Register(regcode, instanceDataFile string) error {
179182

180183
// RegisterOrKeepAlive calls either `Register` or `KeepAlive` depending on
181184
// whether the current system is registered or not.
182-
func (w Wrapper) RegisterOrKeepAlive(regcode, instanceDataFile string, uptimeTracking bool) error {
185+
func (w Wrapper) RegisterOrKeepAlive(opts *Options) error {
183186
if w.Registered {
184-
return w.KeepAlive(uptimeTracking)
187+
return w.KeepAlive(opts.EnableSystemUptimeTracking)
185188
}
186-
return w.Register(regcode, instanceDataFile)
189+
return w.Register(opts)
187190
}
188191

189192
func (w Wrapper) IsRegistered() bool {

pkg/registration/product.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,15 @@ func (p Product) FindExtension(triplet string) (Product, error) {
149149
return Product{}, fmt.Errorf("extension not found")
150150
}
151151

152+
func (p Product) DistroTarget() string {
153+
identifier := strings.ToLower(p.Identifier)
154+
if strings.HasPrefix(identifier, "sle") {
155+
identifier = "sle"
156+
}
157+
version := strings.Split(p.Version, ".")[0]
158+
return identifier + "-" + version + "-" + p.Arch
159+
}
160+
152161
// Transforms the current product into a list of extensions.
153162
func (p Product) ToExtensionsList() []Product {
154163
res := make([]Product, 0)

pkg/registration/product_test.go

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,27 @@ func TestProductToTriplet(t *testing.T) {
2020
assert.Equal("SLES/15.5/x86_64", product.ToTriplet())
2121
}
2222

23+
func TestProductDistroTargetSle(t *testing.T) {
24+
assert := assert.New(t)
25+
26+
productJson := fixture(t, "pkg/registration/product_tree.json")
27+
product := Product{}
28+
29+
assert.NoError(json.Unmarshal(productJson, &product))
30+
assert.Equal("sle-15-x86_64", product.DistroTarget())
31+
}
32+
33+
func TestProductDistroTargetNotSle(t *testing.T) {
34+
assert := assert.New(t)
35+
36+
productJson := fixture(t, "pkg/registration/product_tree.json")
37+
product := Product{}
38+
39+
assert.NoError(json.Unmarshal(productJson, &product))
40+
product.Identifier = "not-suse-really"
41+
assert.Equal("not-suse-really-15-x86_64", product.DistroTarget())
42+
}
43+
2344
func TestProductTraverseExtensionsFull(t *testing.T) {
2445
assert := assert.New(t)
2546

pkg/registration/register.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ import (
77
)
88

99
type announceRequest struct {
10-
Hostname string `json:"hostname"`
10+
Hostname string `json:"hostname"`
11+
DistroTarget string `json:"distro_target,omitempty"`
1112

1213
requestWithInformation
1314
}
@@ -28,21 +29,27 @@ func Register(conn connection.Connection, regcode, hostname string, systemInform
2829
Hostname: hostname,
2930
}
3031

32+
// distro_target may be data in extra data, but only needed for smt
33+
// and it must be at the same level in the payload as hostname
34+
// so, if it is present add to payload and remove from extraData
35+
if _, ok := extraData["distro_target"]; ok {
36+
payload.DistroTarget = extraData["distro_target"].(string)
37+
delete(extraData, "distro_target")
38+
}
39+
3140
enrichWithSystemInformation(&payload.requestWithInformation, systemInformation)
3241
enrichErr := enrichWithExtraData(&payload.requestWithInformation, extraData)
3342
if enrichErr != nil {
3443
return 0, enrichErr
3544
}
3645

3746
creds := conn.GetCredentials()
38-
3947
request, buildErr := conn.BuildRequest("POST", "/connect/subscriptions/systems", payload)
4048
if buildErr != nil {
4149
return 0, buildErr
4250
}
4351

4452
connection.AddRegcodeAuth(request, regcode)
45-
4653
response, doErr := conn.Do(request)
4754
if doErr != nil {
4855
return 0, doErr
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"hostname":"hostname","hwinfo":{},"instance_data":"<document>{\"instanceId\": \"dummy_instance_data\"}</document>","namespace":"staging-sles","online_at":["12122025:000000000000000000000000","11122025:000000000000000000000000","10122025:000000000000000000000000"]}
1+
{"hostname":"hostname","distro_target":"","hwinfo":{},"instance_data":"<document>{\"instanceId\": \"dummy_instance_data\"}</document>","namespace":"staging-sles","online_at":["12122025:000000000000000000000000","11122025:000000000000000000000000","10122025:000000000000000000000000"]}
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
{"hostname":"hostname","hwinfo":{"cpus":3,"sap":[{"instance_types":["ASCS"],"system_id":"DEV"}],"sockets":3}}
1+
{"hostname":"hostname","distro_target":"","hwinfo":{"cpus":3,"sap":[{"instance_types":["ASCS"],"system_id":"DEV"}],"sockets":3}}

third_party/libsuseconnect/libsuseconnect.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func announce_system(clientParams, distroTarget *C.char) *C.char {
6868
opts := loadConfig(C.GoString(clientParams))
6969
api := connect.NewWrappedAPI(opts)
7070

71-
if err := api.Register(opts.Token, opts.InstanceDataFile); err != nil {
71+
if err := api.Register(opts); err != nil {
7272
return C.CString(errorToJSON(err))
7373
}
7474

0 commit comments

Comments
 (0)