github.com/loafoe/cli@v7.1.0+incompatible/command/v7/create_org_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccerror"
     5  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     6  	"code.cloudfoundry.org/cli/command/flag"
     7  )
     8  
     9  type CreateOrgCommand struct {
    10  	BaseCommand
    11  
    12  	RequiredArgs    flag.Organization `positional-args:"yes"`
    13  	Quota           string            `short:"q" long:"quota" description:"Quota to assign to the newly created org"`
    14  	usage           interface{}       `usage:"CF_NAME create-org ORG [-q ORG_QUOTA]"`
    15  	relatedCommands interface{}       `related_commands:"create-space, orgs, org-quotas, set-org-role"`
    16  }
    17  
    18  func (cmd CreateOrgCommand) Execute(args []string) error {
    19  	err := cmd.SharedActor.CheckTarget(false, false)
    20  	if err != nil {
    21  		return err
    22  	}
    23  
    24  	user, err := cmd.Config.CurrentUser()
    25  	if err != nil {
    26  		return err
    27  	}
    28  
    29  	orgName := cmd.RequiredArgs.Organization
    30  
    31  	cmd.UI.DisplayTextWithFlavor("Creating org {{.Organization}} as {{.User}}...",
    32  		map[string]interface{}{
    33  			"User":         user.Name,
    34  			"Organization": orgName,
    35  		})
    36  
    37  	org, warnings, err := cmd.Actor.CreateOrganization(orgName)
    38  
    39  	cmd.UI.DisplayWarnings(warnings)
    40  	if err != nil {
    41  		if _, ok := err.(ccerror.OrganizationNameTakenError); ok {
    42  			cmd.UI.DisplayText(err.Error())
    43  			cmd.UI.DisplayOK()
    44  			return nil
    45  		}
    46  		return err
    47  	}
    48  	cmd.UI.DisplayOK()
    49  
    50  	if cmd.Quota != "" {
    51  		cmd.UI.DisplayTextWithFlavor("Setting org quota {{.Quota}} to org {{.Organization}} as {{.User}}...",
    52  			map[string]interface{}{
    53  				"Quota":        cmd.Quota,
    54  				"Organization": orgName,
    55  				"User":         user.Name,
    56  			})
    57  		warnings, err = cmd.Actor.ApplyOrganizationQuotaByName(cmd.Quota, org.GUID)
    58  		cmd.UI.DisplayWarnings(warnings)
    59  		if err != nil {
    60  			return err
    61  		}
    62  		cmd.UI.DisplayOK()
    63  	}
    64  
    65  	cmd.UI.DisplayTextWithFlavor("Assigning role OrgManager to user {{.User}} in org {{.Organization}} as {{.User}}...",
    66  		map[string]interface{}{
    67  			"User":         user.Name,
    68  			"Organization": orgName,
    69  		})
    70  	warnings, err = cmd.Actor.CreateOrgRole(constant.OrgManagerRole, org.GUID, user.Name, user.Origin, user.IsClient)
    71  	cmd.UI.DisplayWarnings(warnings)
    72  	if err != nil {
    73  		return err
    74  	}
    75  	cmd.UI.DisplayOK()
    76  
    77  	cmd.UI.DisplayText(`TIP: Use 'cf target -o "{{.Organization}}"' to target new org`,
    78  		map[string]interface{}{
    79  			"Organization": orgName,
    80  		})
    81  
    82  	return nil
    83  }