github.com/mook-as/cf-cli@v7.0.0-beta.28.0.20200120190804-b91c115fae48+incompatible/cf/commands/domain/create_shared_domain.go (about)

     1  package domain
     2  
     3  import (
     4  	"errors"
     5  	"fmt"
     6  
     7  	"code.cloudfoundry.org/cli/cf/api"
     8  	"code.cloudfoundry.org/cli/cf/commandregistry"
     9  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/models"
    13  	"code.cloudfoundry.org/cli/cf/requirements"
    14  	"code.cloudfoundry.org/cli/cf/terminal"
    15  )
    16  
    17  type CreateSharedDomain struct {
    18  	ui             terminal.UI
    19  	config         coreconfig.Reader
    20  	domainRepo     api.DomainRepository
    21  	routingAPIRepo api.RoutingAPIRepository
    22  }
    23  
    24  func init() {
    25  	commandregistry.Register(&CreateSharedDomain{})
    26  }
    27  
    28  func (cmd *CreateSharedDomain) MetaData() commandregistry.CommandMetadata {
    29  	fs := make(map[string]flags.FlagSet)
    30  	fs["router-group"] = &flags.StringFlag{Name: "router-group", Usage: T("Routes for this domain will be configured only on the specified router group")}
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "create-shared-domain",
    33  		Description: T("Create a domain that can be used by all orgs (admin-only)"),
    34  		Usage: []string{
    35  			T("CF_NAME create-shared-domain DOMAIN [--router-group ROUTER_GROUP]"),
    36  		},
    37  		Flags: fs,
    38  	}
    39  }
    40  
    41  func (cmd *CreateSharedDomain) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 1 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires DOMAIN as an argument\n\n") + commandregistry.Commands.CommandUsage("create-shared-domain"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    45  	}
    46  
    47  	reqs := []requirements.Requirement{
    48  		requirementsFactory.NewLoginRequirement(),
    49  	}
    50  
    51  	return reqs, nil
    52  }
    53  
    54  func (cmd *CreateSharedDomain) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    55  	cmd.ui = deps.UI
    56  	cmd.config = deps.Config
    57  	cmd.domainRepo = deps.RepoLocator.GetDomainRepository()
    58  	cmd.routingAPIRepo = deps.RepoLocator.GetRoutingAPIRepository()
    59  	return cmd
    60  }
    61  
    62  func (cmd *CreateSharedDomain) Execute(c flags.FlagContext) error {
    63  	var routerGroup models.RouterGroup
    64  	domainName := c.Args()[0]
    65  	routerGroupName := c.String("router-group")
    66  
    67  	if routerGroupName != "" {
    68  		var routerGroupFound bool
    69  		err := cmd.routingAPIRepo.ListRouterGroups(func(group models.RouterGroup) bool {
    70  			if group.Name == routerGroupName {
    71  				routerGroup = group
    72  				routerGroupFound = true
    73  				return false
    74  			}
    75  
    76  			return true
    77  		})
    78  
    79  		if err != nil {
    80  			return err
    81  		}
    82  		if !routerGroupFound {
    83  			return errors.New(T("Router group {{.RouterGroup}} not found", map[string]interface{}{
    84  				"RouterGroup": routerGroupName,
    85  			}))
    86  		}
    87  	}
    88  
    89  	cmd.ui.Say(T("Creating shared domain {{.DomainName}} as {{.Username}}...",
    90  		map[string]interface{}{
    91  			"DomainName": terminal.EntityNameColor(domainName),
    92  			"Username":   terminal.EntityNameColor(cmd.config.Username())}))
    93  
    94  	err := cmd.domainRepo.CreateSharedDomain(domainName, routerGroup.GUID)
    95  	if err != nil {
    96  		return err
    97  	}
    98  
    99  	cmd.ui.Ok()
   100  	return nil
   101  }