Skip to content

Commit

Permalink
feat: register with content templates
Browse files Browse the repository at this point in the history
    * card ID: CCT-769
    * register to templates with --content-template <name>
  • Loading branch information
rverdile committed Jan 13, 2025
1 parent acd0726 commit 4b28a76
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ BUILD
BUILDROOT
RPMS
SOURCES
SRPMS
SRPMS
5 changes: 5 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ func main() {
Usage: "register with `KEY`",
Aliases: []string{"a"},
},
&cli.StringSliceFlag{
Name: "content-template",
Usage: "register with `CONTENT_TEMPLATE`",
Aliases: []string{"c"},
},
&cli.StringFlag{
Name: "server",
Hidden: true,
Expand Down
33 changes: 26 additions & 7 deletions rhsm.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ import (
"github.com/godbus/dbus/v5"
)

const EnvTypeContentTemplate = "content-template"

func getConsumerUUID() (string, error) {
conn, err := dbus.SystemBus()
if err != nil {
Expand Down Expand Up @@ -85,7 +87,7 @@ func unpackOrgs(s string) ([]string, error) {
// registerUsernamePassword tries to register system against candlepin server (Red Hat Management Service)
// username and password are mandatory. When organization is not obtained, then this method
// returns list of available organization and user can select one organization from the list.
func registerUsernamePassword(username, password, organization, serverURL string) ([]string, error) {
func registerUsernamePassword(username, password, organization string, environments []string, serverURL string) ([]string, error) {
var orgs []string
if serverURL != "" {
if err := configureRHSM(serverURL); err != nil {
Expand Down Expand Up @@ -132,6 +134,14 @@ func registerUsernamePassword(username, password, organization, serverURL string
return orgs, err
}

options := make(map[string]string)
if len(environments) != 0 {
options["environment_names"] = strings.Join(environments, ",")
options["environment_type"] = EnvTypeContentTemplate

}
options["enable_content"] = "true"

if err := privConn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Register").Call(
Expand All @@ -140,7 +150,7 @@ func registerUsernamePassword(username, password, organization, serverURL string
organization,
username,
password,
map[string]string{"enable_content": "true"},
options,
map[string]string{},
locale).Err; err != nil {

Expand Down Expand Up @@ -183,7 +193,7 @@ func registerUsernamePassword(username, password, organization, serverURL string
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 @@ -229,14 +239,21 @@ func registerActivationKey(orgID string, activationKeys []string, serverURL stri
return err
}

options := make(map[string]string)
if len(environments) != 0 {
options["environment_names"] = strings.Join(environments, ",")
options["environment_type"] = EnvTypeContentTemplate

}

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 @@ -407,6 +424,7 @@ func registerRHSM(ctx *cli.Context) (string, error) {
password := ctx.String("password")
organization := ctx.String("organization")
activationKeys := ctx.StringSlice("activation-key")
contentTemplates := ctx.StringSlice("content-template")

if len(activationKeys) == 0 {
if username == "" {
Expand Down Expand Up @@ -440,13 +458,14 @@ func registerRHSM(ctx *cli.Context) (string, error) {
err = registerActivationKey(
organization,
ctx.StringSlice("activation-key"),
contentTemplates,
ctx.String("server"))
} else {
var orgs []string
if organization != "" {
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
_, err = registerUsernamePassword(username, password, organization, contentTemplates, ctx.String("server"))
} else {
orgs, err = registerUsernamePassword(username, password, "", ctx.String("server"))
orgs, err = registerUsernamePassword(username, password, "", contentTemplates, ctx.String("server"))
/* When organization was not specified using CLI option --organization, and it is
required, because user is member of more than one organization, then ask for
the organization. */
Expand Down Expand Up @@ -481,7 +500,7 @@ func registerRHSM(ctx *cli.Context) (string, error) {
}

// Try to register once again with given organization
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
_, err = registerUsernamePassword(username, password, organization, contentTemplates, ctx.String("server"))
}
}
}
Expand Down

0 comments on commit 4b28a76

Please sign in to comment.