github.com/cloudfoundry-attic/cli-with-i18n@v6.32.1-0.20171002233121-7401370d3b85+incompatible/command/v3/create_isolation_segment_command.go (about) 1 package v3 2 3 import ( 4 "net/http" 5 6 "code.cloudfoundry.org/cli/actor/sharedaction" 7 "code.cloudfoundry.org/cli/actor/v3action" 8 "code.cloudfoundry.org/cli/api/cloudcontroller/ccerror" 9 "code.cloudfoundry.org/cli/api/cloudcontroller/ccversion" 10 "code.cloudfoundry.org/cli/command" 11 "code.cloudfoundry.org/cli/command/flag" 12 "code.cloudfoundry.org/cli/command/translatableerror" 13 "code.cloudfoundry.org/cli/command/v3/shared" 14 ) 15 16 //go:generate counterfeiter . CreateIsolationSegmentActor 17 18 type CreateIsolationSegmentActor interface { 19 CloudControllerAPIVersion() string 20 CreateIsolationSegmentByName(isolationSegment v3action.IsolationSegment) (v3action.Warnings, error) 21 } 22 23 type CreateIsolationSegmentCommand struct { 24 RequiredArgs flag.IsolationSegmentName `positional-args:"yes"` 25 usage interface{} `usage:"CF_NAME create-isolation-segment SEGMENT_NAME\n\nNOTES:\n The isolation segment name must match the placement tag applied to the Diego cell."` 26 relatedCommands interface{} `related_commands:"enable-org-isolation, isolation-segments"` 27 28 UI command.UI 29 Config command.Config 30 SharedActor command.SharedActor 31 Actor CreateIsolationSegmentActor 32 } 33 34 func (cmd *CreateIsolationSegmentCommand) Setup(config command.Config, ui command.UI) error { 35 cmd.UI = ui 36 cmd.Config = config 37 cmd.SharedActor = sharedaction.NewActor(config) 38 39 client, _, err := shared.NewClients(config, ui, true) 40 if err != nil { 41 if v3Err, ok := err.(ccerror.V3UnexpectedResponseError); ok && v3Err.ResponseCode == http.StatusNotFound { 42 return translatableerror.MinimumAPIVersionNotMetError{MinimumVersion: ccversion.MinVersionIsolationSegmentV3} 43 } 44 45 return err 46 } 47 cmd.Actor = v3action.NewActor(nil, client, config) 48 49 return nil 50 } 51 52 func (cmd CreateIsolationSegmentCommand) Execute(args []string) error { 53 err := command.MinimumAPIVersionCheck(cmd.Actor.CloudControllerAPIVersion(), ccversion.MinVersionIsolationSegmentV3) 54 if err != nil { 55 return err 56 } 57 58 err = cmd.SharedActor.CheckTarget(cmd.Config, false, false) 59 if err != nil { 60 return shared.HandleError(err) 61 } 62 63 user, err := cmd.Config.CurrentUser() 64 if err != nil { 65 return err 66 } 67 68 cmd.UI.DisplayTextWithFlavor("Creating isolation segment {{.SegmentName}} as {{.CurrentUser}}...", map[string]interface{}{ 69 "SegmentName": cmd.RequiredArgs.IsolationSegmentName, 70 "CurrentUser": user.Name, 71 }) 72 73 warnings, err := cmd.Actor.CreateIsolationSegmentByName(v3action.IsolationSegment{ 74 Name: cmd.RequiredArgs.IsolationSegmentName, 75 }) 76 cmd.UI.DisplayWarnings(warnings) 77 if _, ok := err.(v3action.IsolationSegmentAlreadyExistsError); ok { 78 cmd.UI.DisplayWarning("Isolation segment {{.IsolationSegmentName}} already exists.", map[string]interface{}{ 79 "IsolationSegmentName": cmd.RequiredArgs.IsolationSegmentName, 80 }) 81 } else if err != nil { 82 return err 83 } 84 85 cmd.UI.DisplayOK() 86 87 return nil 88 }