github.com/swisscom/cloudfoundry-cli@v7.1.0+incompatible/cf/commands/domain/delete_shared_domain.go (about)

     1  package domain
     2  
     3  import (
     4  	"fmt"
     5  
     6  	"code.cloudfoundry.org/cli/cf/api"
     7  	"code.cloudfoundry.org/cli/cf/commandregistry"
     8  	"code.cloudfoundry.org/cli/cf/configuration/coreconfig"
     9  	"code.cloudfoundry.org/cli/cf/errors"
    10  	"code.cloudfoundry.org/cli/cf/flags"
    11  	. "code.cloudfoundry.org/cli/cf/i18n"
    12  	"code.cloudfoundry.org/cli/cf/requirements"
    13  	"code.cloudfoundry.org/cli/cf/terminal"
    14  )
    15  
    16  type DeleteSharedDomain struct {
    17  	ui         terminal.UI
    18  	config     coreconfig.Reader
    19  	orgReq     requirements.TargetedOrgRequirement
    20  	domainRepo api.DomainRepository
    21  }
    22  
    23  func init() {
    24  	commandregistry.Register(&DeleteSharedDomain{})
    25  }
    26  
    27  func (cmd *DeleteSharedDomain) MetaData() commandregistry.CommandMetadata {
    28  	fs := make(map[string]flags.FlagSet)
    29  	fs["f"] = &flags.BoolFlag{ShortName: "f", Usage: T("Force deletion without confirmation")}
    30  
    31  	return commandregistry.CommandMetadata{
    32  		Name:        "delete-shared-domain",
    33  		Description: T("Delete a shared domain"),
    34  		Usage: []string{
    35  			T("CF_NAME delete-shared-domain DOMAIN [-f]"),
    36  		},
    37  		Flags: fs,
    38  	}
    39  }
    40  
    41  func (cmd *DeleteSharedDomain) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) ([]requirements.Requirement, error) {
    42  	if len(fc.Args()) != 1 {
    43  		cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + commandregistry.Commands.CommandUsage("delete-shared-domain"))
    44  		return nil, fmt.Errorf("Incorrect usage: %d arguments of %d required", len(fc.Args()), 1)
    45  	}
    46  
    47  	loginReq := requirementsFactory.NewLoginRequirement()
    48  	cmd.orgReq = requirementsFactory.NewTargetedOrgRequirement()
    49  
    50  	reqs := []requirements.Requirement{
    51  		loginReq,
    52  		cmd.orgReq,
    53  	}
    54  
    55  	return reqs, nil
    56  }
    57  
    58  func (cmd *DeleteSharedDomain) SetDependency(deps commandregistry.Dependency, pluginCall bool) commandregistry.Command {
    59  	cmd.ui = deps.UI
    60  	cmd.config = deps.Config
    61  	cmd.domainRepo = deps.RepoLocator.GetDomainRepository()
    62  	return cmd
    63  }
    64  
    65  func (cmd *DeleteSharedDomain) Execute(c flags.FlagContext) error {
    66  	domainName := c.Args()[0]
    67  	force := c.Bool("f")
    68  
    69  	cmd.ui.Say(T("Deleting domain {{.DomainName}} as {{.Username}}...",
    70  		map[string]interface{}{
    71  			"DomainName": terminal.EntityNameColor(domainName),
    72  			"Username":   terminal.EntityNameColor(cmd.config.Username())}))
    73  
    74  	domain, err := cmd.domainRepo.FindByNameInOrg(domainName, cmd.orgReq.GetOrganizationFields().GUID)
    75  	switch err.(type) {
    76  	case nil:
    77  		if !domain.Shared {
    78  			return errors.New(T("domain {{.DomainName}} is an owned domain, not a shared domain.",
    79  				map[string]interface{}{"DomainName": domainName}))
    80  		}
    81  	case *errors.ModelNotFoundError:
    82  		cmd.ui.Ok()
    83  		cmd.ui.Warn(err.Error())
    84  		return nil
    85  	default:
    86  		return errors.New(T("Error finding domain {{.DomainName}}\n{{.Err}}",
    87  			map[string]interface{}{
    88  				"DomainName": domainName,
    89  				"Err":        err.Error()}))
    90  	}
    91  
    92  	if !force {
    93  		answer := cmd.ui.Confirm(T("This action impacts all orgs using this domain.\nDeleting it will remove associated routes and could make any app with this domain, in any org, unreachable.\nAre you sure you want to delete the domain {{.DomainName}}? ", map[string]interface{}{"DomainName": domainName}))
    94  
    95  		if !answer {
    96  			return nil
    97  		}
    98  	}
    99  
   100  	err = cmd.domainRepo.DeleteSharedDomain(domain.GUID)
   101  	if err != nil {
   102  		return errors.New(T("Error deleting domain {{.DomainName}}\n{{.Err}}",
   103  			map[string]interface{}{"DomainName": domainName, "Err": err.Error()}))
   104  	}
   105  
   106  	cmd.ui.Ok()
   107  	return nil
   108  }