forked from cloudfoundry/cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
domain_mapper.go
103 lines (88 loc) · 2.42 KB
/
domain_mapper.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package domain
import (
"cf"
"cf/api"
"cf/configuration"
"cf/net"
"cf/requirements"
"cf/terminal"
"errors"
"github.com/codegangsta/cli"
)
type DomainMapper struct {
ui terminal.UI
config *configuration.Configuration
domainRepo api.DomainRepository
spaceReq requirements.SpaceRequirement
orgReq requirements.TargetedOrgRequirement
bind bool
}
func NewDomainMapper(ui terminal.UI, config *configuration.Configuration, domainRepo api.DomainRepository, bind bool) (cmd *DomainMapper) {
cmd = new(DomainMapper)
cmd.ui = ui
cmd.config = config
cmd.domainRepo = domainRepo
cmd.bind = bind
return
}
func (cmd *DomainMapper) GetRequirements(reqFactory requirements.Factory, c *cli.Context) (reqs []requirements.Requirement, err error) {
if len(c.Args()) != 2 {
err = errors.New("Incorrect Usage")
if cmd.bind {
cmd.ui.FailWithUsage(c, "map-domain")
} else {
cmd.ui.FailWithUsage(c, "unmap-domain")
}
return
}
spaceName := c.Args()[0]
cmd.spaceReq = reqFactory.NewSpaceRequirement(spaceName)
loginReq := reqFactory.NewLoginRequirement()
cmd.orgReq = reqFactory.NewTargetedOrgRequirement()
reqs = []requirements.Requirement{
loginReq,
cmd.orgReq,
cmd.spaceReq,
}
return
}
func (cmd *DomainMapper) Run(c *cli.Context) {
var (
apiResponse net.ApiResponse
domain cf.Domain
)
domainName := c.Args()[1]
space := cmd.spaceReq.GetSpace()
org := cmd.orgReq.GetOrganization()
if cmd.bind {
cmd.ui.Say("Mapping domain %s to org %s / space %s as %s...",
terminal.EntityNameColor(domainName),
terminal.EntityNameColor(cmd.config.Organization.Name),
terminal.EntityNameColor(space.Name),
terminal.EntityNameColor(cmd.config.Username()),
)
} else {
cmd.ui.Say("Unmapping domain %s from org %s / space %s as %s...",
terminal.EntityNameColor(domainName),
terminal.EntityNameColor(cmd.config.Organization.Name),
terminal.EntityNameColor(space.Name),
terminal.EntityNameColor(cmd.config.Username()),
)
}
domain, apiResponse = cmd.domainRepo.FindByNameInOrg(domainName, org)
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed("Error finding domain %s\n%s", terminal.EntityNameColor(domainName), apiResponse.Message)
return
}
if cmd.bind {
apiResponse = cmd.domainRepo.Map(domain, space)
} else {
apiResponse = cmd.domainRepo.Unmap(domain, space)
}
if apiResponse.IsNotSuccessful() {
cmd.ui.Failed(apiResponse.Message)
return
}
cmd.ui.Ok()
return
}