func (repo CloudControllerServiceRepository) CreateServiceInstance(name, planGuid string) (err error) { path := fmt.Sprintf("%s/v2/service_instances", repo.config.ApiEndpoint()) data := fmt.Sprintf( `{"name":"%s","service_plan_guid":"%s","space_guid":"%s", "async": true}`, name, planGuid, repo.config.SpaceFields().Guid, ) err = repo.gateway.CreateResource(path, strings.NewReader(data)) if httpErr, ok := err.(errors.HttpError); ok && httpErr.ErrorCode() == errors.SERVICE_INSTANCE_NAME_TAKEN { serviceInstance, findInstanceErr := repo.FindInstanceByName(name) if findInstanceErr == nil && serviceInstance.ServicePlan.Guid == planGuid { return errors.NewServiceInstanceAlreadyExistsError(name) } } return }
Expect(serviceRepo.CreateServiceInstanceArgs.Name).To(Equal("my-cleardb-service")) Expect(serviceRepo.CreateServiceInstanceArgs.PlanGuid).To(Equal("cleardb-spark-guid")) }) It("warns the user when the service already exists with the same service plan", func() { offering := models.ServiceOffering{} offering.Label = "cleardb" plan := models.ServicePlanFields{} plan.Name = "spark" plan.Guid = "cleardb-spark-guid" offering.Plans = []models.ServicePlanFields{plan} offering2 := models.ServiceOffering{} offering2.Label = "postgres" serviceRepo := &testapi.FakeServiceRepo{} serviceRepo.CreateServiceInstanceReturns.Error = errors.NewServiceInstanceAlreadyExistsError("my-cleardb-service") serviceRepo.FindServiceOfferingsForSpaceByLabelReturns.ServiceOfferings = []models.ServiceOffering{offering, offering2} ui := callCreateService([]string{"cleardb", "spark", "my-cleardb-service"}, []string{}, serviceRepo, ) testassert.SliceContains(ui.Outputs, testassert.Lines{ {"Creating service", "my-cleardb-service"}, {"OK"}, {"my-cleardb-service", "already exists"}, }) Expect(serviceRepo.CreateServiceInstanceArgs.Name).To(Equal("my-cleardb-service")) Expect(serviceRepo.CreateServiceInstanceArgs.PlanGuid).To(Equal("cleardb-spark-guid")) })