diff --git a/.gitignore b/.gitignore index 9f48329..0ad7583 100644 --- a/.gitignore +++ b/.gitignore @@ -7,3 +7,4 @@ BUILDROOT RPMS SOURCES SRPMS +.idea diff --git a/main.go b/main.go index 18fd2e2..995cc26 100644 --- a/main.go +++ b/main.go @@ -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, diff --git a/rhsm.go b/rhsm.go index b0024e2..6fb8f00 100644 --- a/rhsm.go +++ b/rhsm.go @@ -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 { @@ -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, environmentType string, serverURL string) ([]string, error) { var orgs []string if serverURL != "" { if err := configureRHSM(serverURL); err != nil { @@ -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"] = environmentType + + } + options["enable_content"] = "true" + if err := privConn.Object( "com.redhat.RHSM1", "/com/redhat/RHSM1/Register").Call( @@ -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 { @@ -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, environmentType string, serverURL string) error { if serverURL != "" { if err := configureRHSM(serverURL); err != nil { return fmt.Errorf("cannot configure RHSM: %w", err) @@ -229,6 +239,13 @@ 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"] = environmentType + + } + if err := privConn.Object( "com.redhat.RHSM1", "/com/redhat/RHSM1/Register").Call( @@ -236,7 +253,7 @@ func registerActivationKey(orgID string, activationKeys []string, serverURL stri dbus.Flags(0), orgID, activationKeys, - map[string]string{}, + options, map[string]string{}, locale).Err; err != nil { return unpackRHSMError(err) @@ -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 == "" { @@ -440,13 +458,15 @@ func registerRHSM(ctx *cli.Context) (string, error) { err = registerActivationKey( organization, ctx.StringSlice("activation-key"), + contentTemplates, + EnvTypeContentTemplate, ctx.String("server")) } else { var orgs []string if organization != "" { - _, err = registerUsernamePassword(username, password, organization, ctx.String("server")) + _, err = registerUsernamePassword(username, password, organization, contentTemplates, EnvTypeContentTemplate, ctx.String("server")) } else { - orgs, err = registerUsernamePassword(username, password, "", ctx.String("server")) + orgs, err = registerUsernamePassword(username, password, "", contentTemplates, EnvTypeContentTemplate, 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. */ @@ -481,7 +501,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, EnvTypeContentTemplate, ctx.String("server")) } } }