Exemplo n.º 1
0
func (repo CloudControllerServiceRepository) FindServicePlanByDescription(planDescription resources.ServicePlanDescription) (string, error) {
	path := fmt.Sprintf("/v2/services?inline-relations-depth=1&q=%s",
		url.QueryEscape("label:"+planDescription.ServiceLabel+";provider:"+planDescription.ServiceProvider))

	var planGuid string
	offerings, apiErr := repo.getServiceOfferings(path)
	if apiErr != nil {
		return planGuid, apiErr
	}

	for _, serviceOfferingResource := range offerings {
		for _, servicePlanResource := range serviceOfferingResource.Plans {
			if servicePlanResource.Name == planDescription.ServicePlanName {
				planGuid := servicePlanResource.Guid
				return planGuid, apiErr
			}
		}
	}

	apiErr = errors.NewModelNotFoundError("Plan", planDescription.String())

	return planGuid, apiErr
}
Exemplo n.º 2
0
Arquivo: services.go Projeto: julz/cli
func (repo CloudControllerServiceRepository) FindServicePlanByDescription(planDescription resources.ServicePlanDescription) (planGuid string, apiErr error) {
	path := fmt.Sprintf("%s/v2/services?inline-relations-depth=1&q=%s",
		repo.config.ApiEndpoint(),
		url.QueryEscape("label:"+planDescription.ServiceLabel+";provider:"+planDescription.ServiceProvider))

	response := new(resources.PaginatedServiceOfferingResources)
	apiErr = repo.gateway.GetResource(path, response)
	if apiErr != nil {
		return
	}

	for _, serviceOfferingResource := range response.Resources {
		for _, servicePlanResource := range serviceOfferingResource.Entity.ServicePlans {
			if servicePlanResource.Entity.Name == planDescription.ServicePlanName {
				planGuid = servicePlanResource.Metadata.Guid
				return
			}
		}
	}

	apiErr = errors.NewModelNotFoundError("Plan", planDescription.String())

	return
}
Exemplo n.º 3
0
func (cmd *MigrateServiceInstances) Run(c *cli.Context) {
	v1 := resources.ServicePlanDescription{
		ServiceLabel:    c.Args()[0],
		ServiceProvider: c.Args()[1],
		ServicePlanName: c.Args()[2],
	}
	v2 := resources.ServicePlanDescription{
		ServiceLabel:    c.Args()[3],
		ServicePlanName: c.Args()[4],
	}
	force := c.Bool("f")

	v1Guid, apiErr := cmd.serviceRepo.FindServicePlanByDescription(v1)
	switch apiErr.(type) {
	case nil:
	case *errors.ModelNotFoundError:
		cmd.ui.Failed("Plan %s cannot be found", terminal.EntityNameColor(v1.String()))
		return
	default:
		cmd.ui.Failed(apiErr.Error())
		return
	}

	v2Guid, apiErr := cmd.serviceRepo.FindServicePlanByDescription(v2)
	switch apiErr.(type) {
	case nil:
	case *errors.ModelNotFoundError:
		cmd.ui.Failed("Plan %s cannot be found", terminal.EntityNameColor(v2.String()))
		return
	default:
		cmd.ui.Failed(apiErr.Error())
		return
	}

	count, apiErr := cmd.serviceRepo.GetServiceInstanceCountForServicePlan(v1Guid)
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
		return
	} else if count == 0 {
		cmd.ui.Failed("Plan %s has no service instances to migrate", terminal.EntityNameColor(v1.String()))
		return
	}

	cmd.ui.Warn("WARNING: This operation is internal to Cloud Foundry; service brokers will not be contacted and" +
		" resources for service instances will not be altered. The primary use case for this operation is" +
		" to replace a service broker which implements the v1 Service Broker API with a broker which" +
		" implements the v2 API by remapping service instances from v1 plans to v2 plans.  We recommend" +
		" making the v1 plan private or shutting down the v1 broker to prevent additional instances from" +
		" being created. Once service instances have been migrated, the v1 services and plans can be" +
		" removed from Cloud Foundry.")

	serviceInstancesPhrase := pluralizeServiceInstances(count)

	if !force {
		response := cmd.ui.Confirm("Really migrate %s from plan %s to %s?>",
			serviceInstancesPhrase,
			terminal.EntityNameColor(v1.String()),
			terminal.EntityNameColor(v2.String()),
		)
		if !response {
			return
		}
	}

	cmd.ui.Say("Attempting to migrate %s...", serviceInstancesPhrase)

	changedCount, apiErr := cmd.serviceRepo.MigrateServicePlanFromV1ToV2(v1Guid, v2Guid)
	if apiErr != nil {
		cmd.ui.Failed(apiErr.Error())
	}

	cmd.ui.Say("%s migrated.", pluralizeServiceInstances(changedCount))
	cmd.ui.Ok()

	return
}