forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
purge_service_offering.go
87 lines (72 loc) · 3.32 KB
/
purge_service_offering.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package service
import (
"github.com/cloudfoundry/cli/cf/api"
"github.com/cloudfoundry/cli/cf/command_registry"
"github.com/cloudfoundry/cli/cf/errors"
. "github.com/cloudfoundry/cli/cf/i18n"
"github.com/cloudfoundry/cli/cf/requirements"
"github.com/cloudfoundry/cli/cf/terminal"
"github.com/simonleung8/flags"
"github.com/simonleung8/flags/flag"
)
type PurgeServiceOffering struct {
ui terminal.UI
serviceRepo api.ServiceRepository
}
func init() {
command_registry.Register(&PurgeServiceOffering{})
}
func (cmd *PurgeServiceOffering) MetaData() command_registry.CommandMetadata {
fs := make(map[string]flags.FlagSet)
fs["f"] = &cliFlags.BoolFlag{Name: "f", Usage: T("Force deletion without confirmation")}
fs["p"] = &cliFlags.StringFlag{Name: "p", Usage: T("Provider")}
return command_registry.CommandMetadata{
Name: "purge-service-offering",
Description: T("Recursively remove a service and child objects from Cloud Foundry database without making requests to a service broker"),
Usage: T("CF_NAME purge-service-offering SERVICE [-p PROVIDER]") + "\n\n" + scaryWarningMessage(),
Flags: fs,
}
}
func (cmd *PurgeServiceOffering) Requirements(requirementsFactory requirements.Factory, fc flags.FlagContext) (reqs []requirements.Requirement, err error) {
if len(fc.Args()) != 1 {
cmd.ui.Failed(T("Incorrect Usage. Requires an argument\n\n") + command_registry.Commands.CommandUsage("purge-service-offering"))
}
reqs = []requirements.Requirement{requirementsFactory.NewLoginRequirement()}
return
}
func (cmd *PurgeServiceOffering) SetDependency(deps command_registry.Dependency, pluginCall bool) command_registry.Command {
cmd.ui = deps.Ui
cmd.serviceRepo = deps.RepoLocator.GetServiceRepository()
return cmd
}
func scaryWarningMessage() string {
return T(`WARNING: This operation assumes that the service broker responsible for this service offering is no longer available, and all service instances have been deleted, leaving orphan records in Cloud Foundry's database. All knowledge of the service will be removed from Cloud Foundry, including service instances and service bindings. No attempt will be made to contact the service broker; running this command without destroying the service broker will cause orphan service instances. After running this command you may want to run either delete-service-auth-token or delete-service-broker to complete the cleanup.`)
}
func (cmd *PurgeServiceOffering) Execute(c flags.FlagContext) {
serviceName := c.Args()[0]
offering, apiErr := cmd.serviceRepo.FindServiceOfferingByLabelAndProvider(serviceName, c.String("p"))
switch apiErr.(type) {
case nil:
case *errors.ModelNotFoundError:
cmd.ui.Warn(T("Service offering does not exist\nTIP: If you are trying to purge a v1 service offering, you must set the -p flag."))
return
default:
cmd.ui.Failed(apiErr.Error())
}
confirmed := c.Bool("f")
if !confirmed {
cmd.ui.Warn(scaryWarningMessage())
confirmed = cmd.ui.Confirm(T("Really purge service offering {{.ServiceName}} from Cloud Foundry?",
map[string]interface{}{"ServiceName": serviceName},
))
}
if !confirmed {
return
}
cmd.ui.Say(T("Purging service {{.ServiceName}}...", map[string]interface{}{"ServiceName": serviceName}))
err := cmd.serviceRepo.PurgeServiceOffering(offering)
if err != nil {
cmd.ui.Failed(err.Error())
}
cmd.ui.Ok()
}