Esempio n. 1
0
File: push.go Progetto: pmuellr/cli
func (cmd *Push) domain(c *cli.Context, domainName string) (domain cf.Domain) {
	var apiResponse net.ApiResponse

	if domainName != "" {
		domain, apiResponse = cmd.domainRepo.FindByNameInCurrentSpace(domainName)
		if apiResponse.IsNotSuccessful() {
			cmd.ui.Failed(apiResponse.Message)
		}
	} else {
		stopChan := make(chan bool, 1)
		domainsChan, statusChan := cmd.domainRepo.ListDomainsForOrg(cmd.config.OrganizationFields.Guid, stopChan)

		for domainsChunk := range domainsChan {
			for _, d := range domainsChunk {
				if d.Shared {
					domain = d
					stopChan <- true
					break
				}
			}
		}

		apiResponse, ok := <-statusChan
		if (domain.Guid == "") && ok && apiResponse.IsNotSuccessful() {
			cmd.ui.Failed(apiResponse.Message)
		} else if domain.Guid == "" {
			cmd.ui.Failed("No default domain exists")
		}
	}

	return
}
Esempio n. 2
0
func (cmd *CreateRoute) CreateRoute(hostName string, domain cf.Domain, space cf.Space) (route cf.Route, apiResponse net.ApiResponse) {
	routeToCreate := cf.Route{Host: hostName, Domain: domain}

	cmd.ui.Say("Creating route %s for org %s / space %s as %s...",
		terminal.EntityNameColor(routeToCreate.URL()),
		terminal.EntityNameColor(cmd.config.Organization.Name),
		terminal.EntityNameColor(space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	route, apiResponse = cmd.routeRepo.CreateInSpace(routeToCreate, domain, space)
	if apiResponse.IsNotSuccessful() {
		var findApiResponse net.ApiResponse
		route, findApiResponse = cmd.routeRepo.FindByHostAndDomain(hostName, domain.Name)

		if findApiResponse.IsNotSuccessful() ||
			route.Space.Guid != space.Guid ||
			route.Domain.Guid != domain.Guid ||
			route.Host != hostName {
			return
		}

		apiResponse = net.NewSuccessfulApiResponse()
		cmd.ui.Ok()
		cmd.ui.Warn("Route %s already exists", route.URL())
		return
	}

	cmd.ui.Ok()
	return
}
Esempio n. 3
0
func (cmd *CreateRoute) CreateRoute(hostName string, domain models.DomainFields, space models.SpaceFields) (route models.Route, apiResponse net.ApiResponse) {
	cmd.ui.Say("Creating route %s for org %s / space %s as %s...",
		terminal.EntityNameColor(domain.UrlForHost(hostName)),
		terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
		terminal.EntityNameColor(space.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	route, apiResponse = cmd.routeRepo.CreateInSpace(hostName, domain.Guid, space.Guid)
	if apiResponse.IsNotSuccessful() {
		var findApiResponse net.ApiResponse
		route, findApiResponse = cmd.routeRepo.FindByHostAndDomain(hostName, domain.Name)

		if findApiResponse.IsNotSuccessful() ||
			route.Space.Guid != space.Guid ||
			route.Domain.Guid != domain.Guid {
			return
		}

		apiResponse = net.NewSuccessfulApiResponse()
		cmd.ui.Ok()
		cmd.ui.Warn("Route %s already exists", route.URL())
		return
	}

	cmd.ui.Ok()
	return
}
Esempio n. 4
0
func (cmd Push) Run(c *cli.Context) {
	var (
		apiResponse net.ApiResponse
	)

	if len(c.Args()) != 1 {
		cmd.ui.FailWithUsage(c, "push")
		return
	}

	app, didCreate := cmd.getApp(c)

	domain := cmd.domain(c)
	hostName := cmd.hostName(app, c)
	cmd.bindAppToRoute(app, domain, hostName, didCreate, c)

	cmd.ui.Say("Uploading %s...", terminal.EntityNameColor(app.Name))

	dir := cmd.path(c)
	apiResponse = cmd.appBitsRepo.UploadApp(app, dir)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("")

	cmd.restart(app, c)
}
Esempio n. 5
0
func (cmd MarketplaceServices) Run(c *cli.Context) {
	var (
		serviceOfferings models.ServiceOfferings
		apiResponse      net.ApiResponse
	)

	if cmd.config.HasSpace() {
		cmd.ui.Say("Getting services from marketplace in org %s / space %s as %s...",
			terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
			terminal.EntityNameColor(cmd.config.SpaceFields().Name),
			terminal.EntityNameColor(cmd.config.Username()),
		)
		serviceOfferings, apiResponse = cmd.serviceRepo.GetServiceOfferingsForSpace(cmd.config.SpaceFields().Guid)
	} else if !cmd.config.IsLoggedIn() {
		cmd.ui.Say("Getting all services from marketplace...")
		serviceOfferings, apiResponse = cmd.serviceRepo.GetAllServiceOfferings()
	} else {
		cmd.ui.Failed("Cannot list marketplace services without a targetted space")
	}

	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("")

	if len(serviceOfferings) == 0 {
		cmd.ui.Say("No service offerings found")
		return
	}

	table := [][]string{
		[]string{"service", "plans", "description"},
	}

	sort.Sort(serviceOfferings)
	for _, offering := range serviceOfferings {
		planNames := ""

		for _, plan := range offering.Plans {
			if plan.Name == "" {
				continue
			}
			planNames = planNames + ", " + plan.Name
		}

		planNames = strings.TrimPrefix(planNames, ", ")

		table = append(table, []string{
			offering.Label,
			planNames,
			offering.Description,
		})
	}

	cmd.ui.DisplayTable(table)
	return
}
Esempio n. 6
0
func (cmd *Push) updateApp(app models.Application, appParams models.AppParams) (updatedApp models.Application) {
	cmd.ui.Say("Updating app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields().Name),
		terminal.EntityNameColor(cmd.config.SpaceFields().Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	if appParams.EnvironmentVars != nil {
		for key, val := range app.EnvironmentVars {
			if _, ok := (*appParams.EnvironmentVars)[key]; !ok {
				(*appParams.EnvironmentVars)[key] = val
			}
		}
	}

	var apiResponse net.ApiResponse
	updatedApp, apiResponse = cmd.appRepo.Update(app.Guid, appParams)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("")

	return
}
Esempio n. 7
0
File: push.go Progetto: nsnt/cli
func (cmd *Push) domain(c *cli.Context, domainName string) (domain cf.Domain) {
	var apiResponse net.ApiResponse

	if domainName != "" {
		domain, apiResponse = cmd.domainRepo.FindByNameInCurrentSpace(domainName)
		if apiResponse.IsNotSuccessful() {
			cmd.ui.Failed(apiResponse.Message)
		}
		return
	}

	apiResponse = cmd.domainRepo.ListSharedDomains(api.ListDomainsCallback(func(domains []cf.Domain) bool {
		domain = domains[0]
		return false
	}))

	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
	}

	if domain.Guid == "" {
		cmd.ui.Failed("No default domain exists")
	}

	return
}
Esempio n. 8
0
func (req *domainApiRequirement) Execute() bool {
	var apiResponse net.ApiResponse
	req.domain, apiResponse = req.domainRepo.FindByNameInOrg(req.name, req.config.OrganizationFields().Guid)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 9
0
func (req *serviceInstanceApiRequirement) Execute() (success bool) {
	var apiResponse net.ApiResponse
	req.serviceInstance, apiResponse = req.serviceRepo.FindInstanceByName(req.name)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 10
0
func (req *applicationApiRequirement) Execute() (success bool) {
	var apiResponse net.ApiResponse
	req.application, apiResponse = req.appRepo.Read(req.name)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 11
0
File: domain.go Progetto: nsnt/cli
func (req *domainApiRequirement) Execute() bool {
	var apiResponse net.ApiResponse
	req.domain, apiResponse = req.domainRepo.FindByNameInCurrentSpace(req.name)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 12
0
func (req *userApiRequirement) Execute() (success bool) {
	var apiResponse net.ApiResponse
	req.user, apiResponse = req.userRepo.FindByUsername(req.username)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 13
0
func (req *buildpackApiRequirement) Execute() (success bool) {
	var apiResponse net.ApiResponse
	req.buildpack, apiResponse = req.buildpackRepo.FindByName(req.name)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 14
0
func (req *organizationApiRequirement) Execute() (success bool) {
	var apiResponse net.ApiResponse
	req.org, apiResponse = req.orgRepo.FindByName(req.name)

	if apiResponse.IsNotSuccessful() {
		req.ui.Failed(apiResponse.Message)
		return false
	}

	return true
}
Esempio n. 15
0
func (cmd Login) setSpace(c *cli.Context, userChanged bool) (err error) {
	spaceName := c.String("s")

	if spaceName == "" {
		// If user is changing, clear the space
		if userChanged {
			cmd.config.SetSpaceFields(models.SpaceFields{})
		}
		// Reuse space in config
		if cmd.config.HasSpace() && !userChanged {
			return
		}

		var availableSpaces []models.Space
		apiResponse := cmd.spaceRepo.ListSpaces(func(space models.Space) bool {
			availableSpaces = append(availableSpaces, space)
			return (len(availableSpaces) < maxChoices)
		})

		if apiResponse.IsNotSuccessful() {
			err = errors.New(fmt.Sprintf("Error finding available spaces\n%s", apiResponse.Message))
			cmd.ui.Failed(err.Error())
			return
		}

		// Target only space if possible
		if len(availableSpaces) == 1 {
			return cmd.targetSpace(availableSpaces[0])
		}

		spaceName = cmd.promptForSpaceName(availableSpaces)
	}

	if spaceName == "" {
		cmd.ui.Say("")
		err = errors.New(userSkippedInput)
		return
	}

	var space models.Space
	var apiResponse net.ApiResponse
	space, apiResponse = cmd.spaceRepo.FindByName(spaceName)
	if apiResponse.IsNotSuccessful() {
		err = errors.New(fmt.Sprintf("Error finding space %s\n%s", terminal.EntityNameColor(spaceName), apiResponse.Message))
		cmd.ui.Failed(err.Error())
		return
	}

	err = cmd.targetSpace(space)
	return
}
Esempio n. 16
0
func (cmd Login) setOrganization(c *cli.Context, userChanged bool) (err error) {
	orgName := c.String("o")

	if orgName == "" {
		// If the user is changing, clear out the org
		if userChanged {
			cmd.config.SetOrganizationFields(models.OrganizationFields{})
		}

		// Reuse org in config
		if cmd.config.HasOrganization() && !userChanged {
			return
		}

		availableOrgs := []models.Organization{}
		apiResponse := cmd.orgRepo.ListOrgs(func(o models.Organization) bool {
			availableOrgs = append(availableOrgs, o)
			return len(availableOrgs) < maxChoices
		})

		if apiResponse.IsNotSuccessful() {
			err = errors.New(fmt.Sprintf("Error finding avilable orgs\n%s", apiResponse.Message))
			return
		}

		if len(availableOrgs) == 1 {
			return cmd.targetOrganization(availableOrgs[0])
		}

		orgName = cmd.promptForOrgName(availableOrgs)
	}

	if orgName == "" {
		cmd.ui.Say("")
		err = errors.New(userSkippedInput)
		return
	}

	var org models.Organization
	var apiResponse net.ApiResponse
	org, apiResponse = cmd.orgRepo.FindByName(orgName)
	if apiResponse.IsNotSuccessful() {
		err = errors.New(apiResponse.Message)
		cmd.ui.Failed("Error finding org %s\n%s", terminal.EntityNameColor(orgName), err)
		return
	}

	return cmd.targetOrganization(org)
}
Esempio n. 17
0
func (cmd Push) domain(c *cli.Context) (domain cf.Domain) {
	var apiResponse net.ApiResponse

	domainName := c.String("d")

	if domainName != "" {
		domain, apiResponse = cmd.domainRepo.FindByNameInCurrentSpace(domainName)
	} else {
		domain, apiResponse = cmd.domainRepo.FindDefaultAppDomain()
	}

	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
	}
	return
}
Esempio n. 18
0
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
}
Esempio n. 19
0
func (repo CloudControllerDomainRepository) ListDomainsForOrg(orgGuid string, stop chan bool) (domainsChan chan []cf.Domain, statusChan chan net.ApiResponse) {
	domainsChan = make(chan []cf.Domain, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
		path := "/v2/domains?inline-relations-depth=1"
	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					allDomains      []cf.Domain
					domainsToReturn []cf.Domain
					apiResponse     net.ApiResponse
				)

				allDomains, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(domainsChan)
					close(statusChan)
					return
				}

				for _, d := range allDomains {
					if repo.isOrgDomain(orgGuid, d.DomainFields) {
						domainsToReturn = append(domainsToReturn, d)
					}
				}

				if len(domainsToReturn) > 0 {
					domainsChan <- domainsToReturn
				}
			}
		}
		close(domainsChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}
Esempio n. 20
0
File: push.go Progetto: nsnt/cli
func (cmd *Push) updateApp(app *cf.Application, appParams cf.AppParams) {
	cmd.ui.Say("Updating app %s in org %s / space %s as %s...",
		terminal.EntityNameColor(app.Name),
		terminal.EntityNameColor(cmd.config.OrganizationFields.Name),
		terminal.EntityNameColor(cmd.config.SpaceFields.Name),
		terminal.EntityNameColor(cmd.config.Username()),
	)

	var apiResponse net.ApiResponse
	*app, apiResponse = cmd.appRepo.Update(app.Guid, appParams)
	if apiResponse.IsNotSuccessful() {
		cmd.ui.Failed(apiResponse.Message)
		return
	}

	cmd.ui.Ok()
	cmd.ui.Say("")

	return
}
Esempio n. 21
0
func (cmd *Push) domain(c *cli.Context, domainName string) (domain models.DomainFields) {
	var apiResponse net.ApiResponse

	if domainName != "" {
		domain, apiResponse = cmd.domainRepo.FindByNameInOrg(domainName, cmd.config.OrganizationFields().Guid)
		if apiResponse.IsNotSuccessful() {
			cmd.ui.Failed(apiResponse.Message)
		}
		return
	}

	domain, err := cmd.findDefaultDomain()

	if err != nil {
		cmd.ui.Failed(err.Error())
	}

	if domain.Guid == "" {
		cmd.ui.Failed("No default domain exists")
	}

	return
}
Esempio n. 22
0
func (repo CloudControllerServiceBrokerRepository) ListServiceBrokers(stop chan bool) (serviceBrokersChan chan []cf.ServiceBroker, statusChan chan net.ApiResponse) {
	serviceBrokersChan = make(chan []cf.ServiceBroker, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
		path := "/v2/service_brokers"

	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					serviceBrokers []cf.ServiceBroker
					apiResponse    net.ApiResponse
				)
				serviceBrokers, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(serviceBrokersChan)
					close(statusChan)
					return
				}

				if len(serviceBrokers) > 0 {
					serviceBrokersChan <- serviceBrokers
				}
			}
		}
		close(serviceBrokersChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}
Esempio n. 23
0
File: routes.go Progetto: nsnt/cli
func (repo CloudControllerRouteRepository) ListRoutes(stop chan bool) (routesChan chan []cf.Route, statusChan chan net.ApiResponse) {
	routesChan = make(chan []cf.Route, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
		path := fmt.Sprintf("/v2/routes?inline-relations-depth=1")

	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					routes      []cf.Route
					apiResponse net.ApiResponse
				)
				routes, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(routesChan)
					close(statusChan)
					return
				}

				if len(routes) > 0 {
					routesChan <- routes
				}
			}
		}
		close(routesChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}
Esempio n. 24
0
func (repo CloudControllerBuildpackRepository) ListBuildpacks(stop chan bool) (buildpacksChan chan []cf.Buildpack, statusChan chan net.ApiResponse) {
	buildpacksChan = make(chan []cf.Buildpack, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
		path := buildpacks_path

	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					buildpacks  []cf.Buildpack
					apiResponse net.ApiResponse
				)
				buildpacks, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(buildpacksChan)
					close(statusChan)
					return
				}

				if len(buildpacks) > 0 {
					buildpacksChan <- buildpacks
				}
			}
		}
		close(buildpacksChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}
Esempio n. 25
0
func (repo CloudControllerOrganizationRepository) ListOrgs(stop chan bool) (orgsChan chan []cf.Organization, statusChan chan net.ApiResponse) {
	orgsChan = make(chan []cf.Organization, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
		path := "/v2/organizations"

	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					organizations []cf.Organization
					apiResponse   net.ApiResponse
				)
				organizations, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(orgsChan)
					close(statusChan)
					return
				}

				if len(organizations) > 0 {
					orgsChan <- organizations
				}
			}
		}
		close(orgsChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}
Esempio n. 26
0
File: spaces.go Progetto: nsnt/cli
func (repo CloudControllerSpaceRepository) ListSpaces(stop chan bool) (spacesChan chan []cf.Space, statusChan chan net.ApiResponse) {
	spacesChan = make(chan []cf.Space, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
		path := fmt.Sprintf("/v2/organizations/%s/spaces", repo.config.OrganizationFields.Guid)

	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					spaces      []cf.Space
					apiResponse net.ApiResponse
				)
				spaces, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(spacesChan)
					close(statusChan)
					return
				}

				if len(spaces) > 0 {
					spacesChan <- spaces
				}
			}
		}
		close(spacesChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}
Esempio n. 27
0
File: users.go Progetto: pmuellr/cli
func (repo CloudControllerUserRepository) listUsersForRole(path string, roleName string, stop chan bool) (usersChan chan []cf.UserFields, statusChan chan net.ApiResponse) {
	usersChan = make(chan []cf.UserFields, 4)
	statusChan = make(chan net.ApiResponse, 1)

	go func() {
	loop:
		for path != "" {
			select {
			case <-stop:
				break loop
			default:
				var (
					users       []cf.UserFields
					apiResponse net.ApiResponse
				)

				users, path, apiResponse = repo.findNextWithPath(path)
				if apiResponse.IsNotSuccessful() {
					statusChan <- apiResponse
					close(usersChan)
					close(statusChan)
					return
				}

				if len(users) > 0 {
					usersChan <- users
				}
			}
		}
		close(usersChan)
		close(statusChan)
		cf.WaitForClose(stop)
	}()

	return
}