github.com/niteshexa/cloudfoundry_cli@v7.1.0+incompatible/command/v7/create_space_command.go (about)

     1  package v7
     2  
     3  import (
     4  	"code.cloudfoundry.org/cli/actor/actionerror"
     5  	"code.cloudfoundry.org/cli/actor/v7action"
     6  	"code.cloudfoundry.org/cli/api/cloudcontroller/ccv3/constant"
     7  	"code.cloudfoundry.org/cli/command/flag"
     8  	"code.cloudfoundry.org/cli/resources"
     9  )
    10  
    11  type CreateSpaceActor interface {
    12  	CreateSpace(spaceName, orgGUID string) (resources.Space, v7action.Warnings, error)
    13  	CreateSpaceRole(roleType constant.RoleType, orgGUID string, spaceGUID string, userNameOrGUID string, userOrigin string, isClient bool) (v7action.Warnings, error)
    14  	GetOrganizationByName(orgName string) (resources.Organization, v7action.Warnings, error)
    15  	ApplySpaceQuotaByName(quotaName string, spaceGUID string, orgGUID string) (v7action.Warnings, error)
    16  }
    17  
    18  type CreateSpaceCommand struct {
    19  	BaseCommand
    20  
    21  	RequiredArgs    flag.Space  `positional-args:"yes"`
    22  	Organization    string      `short:"o" description:"Organization"`
    23  	Quota           string      `long:"quota" short:"q" description:"Quota to assign to the newly created space"`
    24  	usage           interface{} `usage:"CF_NAME create-space SPACE [-o ORG] [-q QUOTA]"`
    25  	relatedCommands interface{} `related_commands:"set-space-isolation-segment, space-quotas, spaces, target"`
    26  }
    27  
    28  func (cmd CreateSpaceCommand) Execute(args []string) error {
    29  	err := cmd.SharedActor.CheckTarget(false, false)
    30  	if err != nil {
    31  		return err
    32  	}
    33  
    34  	var (
    35  		orgName, orgGUID string
    36  	)
    37  
    38  	if cmd.Organization == "" {
    39  		_, err = cmd.SharedActor.RequireTargetedOrg()
    40  		if err != nil {
    41  			return err
    42  		}
    43  		orgName = cmd.Config.TargetedOrganization().Name
    44  		orgGUID = cmd.Config.TargetedOrganization().GUID
    45  	} else {
    46  		orgName = cmd.Organization
    47  		org, warnings, err := cmd.Actor.GetOrganizationByName(orgName)
    48  		cmd.UI.DisplayWarnings(warnings)
    49  		if err != nil {
    50  			return err
    51  		}
    52  		orgGUID = org.GUID
    53  	}
    54  
    55  	user, err := cmd.Config.CurrentUser()
    56  	if err != nil {
    57  		return err
    58  	}
    59  
    60  	spaceName := cmd.RequiredArgs.Space
    61  
    62  	cmd.UI.DisplayTextWithFlavor("Creating space {{.Space}} in org {{.Organization}} as {{.User}}...",
    63  		map[string]interface{}{
    64  			"User":         user.Name,
    65  			"Space":        spaceName,
    66  			"Organization": orgName,
    67  		})
    68  	space, warnings, err := cmd.Actor.CreateSpace(spaceName, orgGUID)
    69  	cmd.UI.DisplayWarnings(warnings)
    70  
    71  	if _, ok := err.(actionerror.SpaceAlreadyExistsError); ok {
    72  		cmd.UI.DisplayText(err.Error())
    73  		cmd.UI.DisplayOK()
    74  		return nil
    75  	} else if err != nil {
    76  		return err
    77  	}
    78  
    79  	cmd.UI.DisplayOK()
    80  
    81  	if cmd.Quota != "" {
    82  		cmd.UI.DisplayTextWithFlavor("Setting space quota {{.Quota}} to space {{.Space}} as {{.User}}...",
    83  			map[string]interface{}{
    84  				"Quota": cmd.Quota,
    85  				"Space": spaceName,
    86  				"User":  user.Name,
    87  			})
    88  
    89  		warnings, err = cmd.Actor.ApplySpaceQuotaByName(cmd.Quota, space.GUID, orgGUID)
    90  		cmd.UI.DisplayWarnings(warnings)
    91  		if err != nil {
    92  			return err
    93  		}
    94  
    95  		cmd.UI.DisplayOK()
    96  	}
    97  
    98  	cmd.UI.DisplayTextWithFlavor("Assigning role SpaceManager to user {{.User}} in org {{.Organization}} / space {{.Space}} as {{.User}}...",
    99  		map[string]interface{}{
   100  			"User":         user.Name,
   101  			"Space":        spaceName,
   102  			"Organization": orgName,
   103  		})
   104  	warnings, err = cmd.Actor.CreateSpaceRole(constant.SpaceManagerRole, orgGUID, space.GUID, user.Name, user.Origin, user.IsClient)
   105  	cmd.UI.DisplayWarnings(warnings)
   106  	if err != nil {
   107  		return err
   108  	}
   109  	cmd.UI.DisplayOK()
   110  
   111  	cmd.UI.DisplayTextWithFlavor("Assigning role SpaceDeveloper to user {{.User}} in org {{.Organization}} / space {{.Space}} as {{.User}}...",
   112  		map[string]interface{}{
   113  			"User":         user.Name,
   114  			"Space":        spaceName,
   115  			"Organization": orgName,
   116  		})
   117  	warnings, err = cmd.Actor.CreateSpaceRole(constant.SpaceDeveloperRole, orgGUID, space.GUID, user.Name, user.Origin, user.IsClient)
   118  	cmd.UI.DisplayWarnings(warnings)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	cmd.UI.DisplayOK()
   123  
   124  	cmd.UI.DisplayText(`TIP: Use 'cf target -o "{{.Organization}}" -s "{{.Space}}"' to target new space`,
   125  		map[string]interface{}{
   126  			"Organization": orgName,
   127  			"Space":        spaceName,
   128  		})
   129  
   130  	return nil
   131  }