Example #1
0
func (repo CloudControllerServiceRepository) FindInstanceByName(name string) (instance cf.ServiceInstance, apiErr *net.ApiError) {
	path := fmt.Sprintf("%s/v2/spaces/%s/service_instances?return_user_provided_service_instances=true&q=name%s&inline-relations-depth=1", repo.config.Target, repo.config.Space.Guid, "%3A"+name)
	request, apiErr := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	response := new(ServiceInstancesApiResponse)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, response)
	if apiErr != nil {
		return
	}

	if len(response.Resources) == 0 {
		apiErr = net.NewApiErrorWithMessage("Service %s not found", name)
		return
	}

	resource := response.Resources[0]
	instance.Guid = resource.Metadata.Guid
	instance.Name = resource.Entity.Name
	instance.ServiceBindings = []cf.ServiceBinding{}

	for _, bindingResource := range resource.Entity.ServiceBindings {
		newBinding := cf.ServiceBinding{
			Url:     bindingResource.Metadata.Url,
			Guid:    bindingResource.Metadata.Guid,
			AppGuid: bindingResource.Entity.AppGuid,
		}
		instance.ServiceBindings = append(instance.ServiceBindings, newBinding)
	}

	return
}
Example #2
0
File: routes.go Project: jbayer/cli
func (repo CloudControllerRouteRepository) FindByHost(host string) (route cf.Route, apiErr *net.ApiError) {
	path := fmt.Sprintf("%s/v2/routes?q=host%s", repo.config.Target, "%3A"+host)

	request, apiErr := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	response := new(ApiResponse)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, response)
	if apiErr != nil {
		return
	}

	if len(response.Resources) == 0 {
		apiErr = net.NewApiErrorWithMessage("Route not found")
		return
	}

	resource := response.Resources[0]
	route.Guid = resource.Metadata.Guid
	route.Host = resource.Entity.Host

	return
}
Example #3
0
func (repo *FakeApplicationRepository) Start(app cf.Application) (apiErr *net.ApiError) {
	repo.StartedApp = app
	if repo.StartAppErr {
		apiErr = net.NewApiErrorWithMessage("Error starting application")
	}
	return
}
Example #4
0
func (repo *FakeSpaceRepository) FindByName(name string) (space cf.Space, apiErr *net.ApiError) {
	repo.SpaceName = name
	if repo.SpaceByNameErr {
		apiErr = net.NewApiErrorWithMessage("Error finding space by name.")
	}
	return repo.SpaceByName, apiErr
}
Example #5
0
func validateApplication(app cf.Application) (apiErr *net.ApiError) {
	reg := regexp.MustCompile("^[0-9a-zA-Z\\-_]*$")
	if !reg.MatchString(app.Name) {
		apiErr = net.NewApiErrorWithMessage("Application name is invalid. Name can only contain letters, numbers, underscores and hyphens.")
	}

	return
}
Example #6
0
func (repo *FakeRouteRepository) FindAll() (routes []cf.Route, apiErr *net.ApiError) {
	if repo.FindAllErr {
		apiErr = net.NewApiErrorWithMessage("Error finding all routes")
	}

	routes = repo.FindAllRoutes
	return
}
Example #7
0
func (repo *FakeOrgRepository) FindByName(name string) (org cf.Organization, apiErr *net.ApiError) {
	repo.FindByNameName = name

	if repo.FindByNameErr {
		apiErr = net.NewApiErrorWithMessage("Error finding organization by name.")
	}
	return repo.FindByNameOrganization, apiErr
}
Example #8
0
func (repo *FakeApplicationRepository) SetEnv(app cf.Application, envVars map[string]string) (apiErr *net.ApiError) {
	repo.SetEnvApp = app
	repo.SetEnvVars = envVars

	if repo.SetEnvErr {
		apiErr = net.NewApiErrorWithMessage("Failed setting env")
	}
	return
}
Example #9
0
func (repo *FakeRouteRepository) FindByHost(host string) (route cf.Route, apiErr *net.ApiError) {
	repo.FindByHostHost = host

	if repo.FindByHostErr {
		apiErr = net.NewApiErrorWithMessage("Route not found")
	}

	route = repo.FindByHostRoute
	return
}
Example #10
0
func (repo *FakeApplicationRepository) FindByName(name string) (app cf.Application, apiErr *net.ApiError) {
	repo.AppName = name
	if repo.AppByNameErr {
		apiErr = net.NewApiErrorWithMessage("Error finding app by name.")
	}
	if repo.AppByNameAuthErr {
		apiErr = net.NewApiError("Authentication failed.", "1000", 401)
	}
	return repo.AppByName, apiErr
}
Example #11
0
func (repo CloudControllerApplicationRepository) FindByName(name string) (app cf.Application, apiErr *net.ApiError) {
	path := fmt.Sprintf("%s/v2/spaces/%s/apps?q=name%s&inline-relations-depth=1", repo.config.Target, repo.config.Space.Guid, "%3A"+name)
	request, apiErr := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	findResponse := new(ApplicationsApiResponse)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, findResponse)
	if apiErr != nil {
		return
	}

	if len(findResponse.Resources) == 0 {
		apiErr = net.NewApiErrorWithMessage(fmt.Sprintf("Application %s not found", name))
		return
	}

	res := findResponse.Resources[0]
	path = fmt.Sprintf("%s/v2/apps/%s/summary", repo.config.Target, res.Metadata.Guid)
	request, apiErr = repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	summaryResponse := new(ApplicationSummary)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, summaryResponse)
	if apiErr != nil {
		return
	}

	urls := []string{}
	// This is a little wonky but we made a concious effort
	// to keep the domain very separate from the API repsonses
	// to maintain flexibility.
	domainRoute := cf.Route{}
	for _, route := range summaryResponse.Routes {
		domainRoute.Domain = cf.Domain{Name: route.Domain.Name}
		domainRoute.Host = route.Host
		urls = append(urls, domainRoute.URL())
	}

	app = cf.Application{
		Name:             summaryResponse.Name,
		Guid:             summaryResponse.Guid,
		Instances:        summaryResponse.Instances,
		RunningInstances: summaryResponse.RunningInstances,
		Memory:           summaryResponse.Memory,
		EnvironmentVars:  res.Entity.EnvironmentJson,
		Urls:             urls,
		State:            strings.ToLower(summaryResponse.State),
	}

	return
}
Example #12
0
func (repo CloudControllerServiceRepository) DeleteService(instance cf.ServiceInstance) (apiErr *net.ApiError) {
	if len(instance.ServiceBindings) > 0 {
		return net.NewApiErrorWithMessage("Cannot delete service instance, apps are still bound to it")
	}

	path := fmt.Sprintf("%s/v2/service_instances/%s", repo.config.Target, instance.Guid)
	request, apiErr := repo.gateway.NewRequest("DELETE", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	apiErr = repo.gateway.PerformRequest(request)
	return
}
Example #13
0
File: spaces.go Project: jbayer/cli
func (repo CloudControllerSpaceRepository) FindByName(name string) (space cf.Space, apiErr *net.ApiError) {
	spaces, apiErr := repo.FindAll()
	lowerName := strings.ToLower(name)

	if apiErr != nil {
		return
	}

	for _, s := range spaces {
		if strings.ToLower(s.Name) == lowerName {
			return s, nil
		}
	}

	apiErr = net.NewApiErrorWithMessage("Space not found")
	return
}
Example #14
0
func (repo CloudControllerOrganizationRepository) FindByName(name string) (org cf.Organization, apiErr *net.ApiError) {
	orgs, apiErr := repo.FindAll()
	lowerName := strings.ToLower(name)

	if apiErr != nil {
		return
	}

	for _, o := range orgs {
		if strings.ToLower(o.Name) == lowerName {
			return o, nil
		}
	}

	apiErr = net.NewApiErrorWithMessage("Organization not found")
	return
}
Example #15
0
func (repo CloudControllerDomainRepository) FindByName(name string) (domain cf.Domain, apiErr *net.ApiError) {
	domains, apiErr := repo.FindAll()

	if apiErr != nil {
		return
	}

	if name == "" {
		domain = domains[0]
	} else {
		apiErr = net.NewApiErrorWithMessage("Could not find domain with name %s", name)

		for _, d := range domains {
			if d.Name == strings.ToLower(name) {
				domain = d
				apiErr = nil
			}
		}
	}

	return
}
Example #16
0
func (repo CloudControllerServiceRepository) UnbindService(instance cf.ServiceInstance, app cf.Application) (apiErr *net.ApiError) {
	var path string

	for _, binding := range instance.ServiceBindings {
		if binding.AppGuid == app.Guid {
			path = repo.config.Target + binding.Url
			break
		}
	}

	if path == "" {
		apiErr = net.NewApiErrorWithMessage("Error finding service binding")
		return
	}

	request, apiErr := repo.gateway.NewRequest("DELETE", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	apiErr = repo.gateway.PerformRequest(request)
	return
}
Example #17
0
File: stacks.go Project: jbayer/cli
func (repo CloudControllerStackRepository) FindByName(name string) (stack cf.Stack, apiErr *net.ApiError) {
	path := fmt.Sprintf("%s/v2/stacks?q=name%s", repo.config.Target, "%3A"+name)
	request, apiErr := repo.gateway.NewRequest("GET", path, repo.config.AccessToken, nil)
	if apiErr != nil {
		return
	}

	findResponse := new(ApiResponse)
	apiErr = repo.gateway.PerformRequestForJSONResponse(request, findResponse)
	if apiErr != nil {
		return
	}

	if len(findResponse.Resources) == 0 {
		apiErr = net.NewApiErrorWithMessage("Stack %s not found", name)
		return
	}

	res := findResponse.Resources[0]
	stack.Guid = res.Metadata.Guid
	stack.Name = res.Entity.Name

	return
}