Skip to content

Commit

Permalink
handle activation key and missing GetEnvironments() method
Browse files Browse the repository at this point in the history
  • Loading branch information
rverdile committed Nov 14, 2024
1 parent 4ebc91b commit ef842be
Showing 1 changed file with 23 additions and 10 deletions.
33 changes: 23 additions & 10 deletions rhsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,17 +136,26 @@ func registerUsernamePassword(username, password, organization string, environme
options["enable_content"] = "true"

if len(environments) > 0 && organization != "" {
var useEnvIDs = false
envList, err := getEnvironmentsList(privConn, username, password, organization)
if err != nil {
return orgs, err
}
if strings.Contains(err.Error(), "org.freedesktop.DBus.Error.UnknownMethod") {
useEnvIDs = true
} else {
return orgs, err

envIDs, err := mapEnvironmentNamesToIDs(environments, envList, true)
if err != nil {
return orgs, err
}
}

options["environments"] = strings.Join(envIDs, ",")
if useEnvIDs {
options["environments"] = strings.Join(environments, ",")
} else {
envNames, err := mapEnvironmentIDsToNames(environments, envList, true)
if err != nil {
return orgs, err
}
options["environments"] = strings.Join(envNames, ",")
}
}

if err := privConn.Object(
Expand Down Expand Up @@ -200,7 +209,7 @@ func registerUsernamePassword(username, password, organization string, environme
return orgs, nil
}

func registerActivationKey(orgID string, activationKeys []string, serverURL string) error {
func registerActivationKey(orgID string, activationKeys []string, environments []string, serverURL string) error {
if serverURL != "" {
if err := configureRHSM(serverURL); err != nil {
return fmt.Errorf("cannot configure RHSM: %w", err)
Expand Down Expand Up @@ -246,14 +255,17 @@ func registerActivationKey(orgID string, activationKeys []string, serverURL stri
return err
}

options := make(map[string]string)
options["environments"] = strings.Join(environments, ",")

if err := privConn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Register").Call(
"com.redhat.RHSM1.Register.RegisterWithActivationKeys",
dbus.Flags(0),
orgID,
activationKeys,
map[string]string{},
options,
map[string]string{},
locale).Err; err != nil {
return unpackRHSMError(err)
Expand Down Expand Up @@ -458,6 +470,7 @@ func registerRHSM(ctx *cli.Context) (string, error) {
err = registerActivationKey(
organization,
ctx.StringSlice("activation-key"),
contentTemplates,
ctx.String("server"))
} else {
var orgs []string
Expand Down Expand Up @@ -539,7 +552,7 @@ func getEnvironmentsList(conn *dbus.Conn, username, password, organization strin
obj := conn.Object("com.redhat.RHSM1.Register", "/com/redhat/RHSM1/Register")
var envString string
if err := obj.Call(
"com.redhat.RHSM1.Register.GetEnvironments",
"com.redhat.RHSM1.Register.NotGetEnvironments",
dbus.Flags(0),
username,
password,
Expand All @@ -557,7 +570,7 @@ func getEnvironmentsList(conn *dbus.Conn, username, password, organization strin
return environments, nil
}

func mapEnvironmentNamesToIDs(names []string, environmentList []Environment, contentTemplates bool) ([]string, error) {
func mapEnvironmentIDsToNames(names []string, environmentList []Environment, contentTemplates bool) ([]string, error) {
var ids []string

for _, name := range names {
Expand Down

0 comments on commit ef842be

Please sign in to comment.