Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Added support for environments during registration #121

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 38 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ func registerRHSM(ctx *cli.Context) (string, error) {
password := ctx.String("password")
organization := ctx.String("organization")
activationKeys := ctx.StringSlice("activation-key")
environments := ctx.StringSlice("environment")

if len(activationKeys) == 0 {
if username == "" {
Expand Down Expand Up @@ -221,9 +222,9 @@ func registerRHSM(ctx *cli.Context) (string, error) {
} else {
var orgs []string
if organization != "" {
_, err = registerUsernamePassword(username, password, organization, ctx.String("server"))
_, err = registerUsernamePassword(username, password, organization, environments, ctx.String("server"))
} else {
orgs, err = registerUsernamePassword(username, password, "", ctx.String("server"))
orgs, err = registerUsernamePassword(username, password, "", environments, 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 @@ -255,7 +256,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, environments, ctx.String("server"))
}
}
}
Expand All @@ -269,9 +270,35 @@ func registerRHSM(ctx *cli.Context) (string, error) {
return successMsg, nil
}

// beforeConnectAction tries to check valid combination CLI options, when
// connect command is used
func beforeConnectAction(ctx *cli.Context) error {
username := ctx.String("username")
organization := ctx.String("organization")
activationKeys := ctx.StringSlice("activation-key")
environments := ctx.StringSlice("environment")

if username != "" && len(activationKeys) > 0 {
err := fmt.Errorf("error: activation keys do not require user credentials")
return cli.Exit(err, 1)
}

if len(activationKeys) > 0 && organization == "" {
err := fmt.Errorf("error: activation keys require organization")
return cli.Exit(err, 1)
}

if len(activationKeys) > 0 && len(environments) > 0 {
err := fmt.Errorf("error: activation keys do not allow environments to be specified")
return cli.Exit(err, 1)
}

return nil
}

// connectAction tries to register system against Red Hat Subscription Management,
// gather the profile information that the system will configure
// connect system to Red Hat Insights and it also tries to start rhcd service
// connect system to Red Hat Insights, and it also tries to start yggdrasil service
func connectAction(ctx *cli.Context) error {
uid := os.Getuid()
if uid != 0 {
Expand Down Expand Up @@ -759,9 +786,14 @@ func main() {
},
&cli.StringSliceFlag{
Name: "activation-key",
Usage: "register with `KEY`",
Usage: "register with `KEY`(s)",
Aliases: []string{"a"},
},
&cli.StringSliceFlag{
Name: "environment",
Usage: "register with `ENVIRONMENT`(s)",
Aliases: []string{"e"},
},
&cli.StringFlag{
Name: "server",
Hidden: true,
Expand All @@ -771,6 +803,7 @@ func main() {
Usage: "Connects the system to " + Provider,
UsageText: fmt.Sprintf("%v connect [command options]", app.Name),
Description: fmt.Sprintf("The connect command connects the system to Red Hat Subscription Management, Red Hat Insights and %v and activates the %v service that enables %v to interact with the system. For details visit: https://red.ht/connector", Provider, ServiceName, Provider),
Before: beforeConnectAction,
Action: connectAction,
},
{
Expand Down
11 changes: 9 additions & 2 deletions register.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"io"
"net/url"
"os"
"strings"

"github.com/godbus/dbus/v5"
)
Expand Down Expand Up @@ -78,7 +79,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 @@ -125,6 +126,12 @@ func registerUsernamePassword(username, password, organization, serverURL string
return orgs, err
}

var options = make(map[string]string)
options["enable_content"] = "true"
if len(environments) > 0 {
options["environments"] = strings.Join(environments, ",")
}

if err := privConn.Object(
"com.redhat.RHSM1",
"/com/redhat/RHSM1/Register").Call(
Expand All @@ -133,7 +140,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
Loading