44
55from openbrokerapi .catalog import ServicePlan
66from openbrokerapi .errors import ErrInstanceAlreadyExists , ErrAsyncRequired , \
7- ErrBindingAlreadyExists , ErrBadRequest , ErrInstanceDoesNotExist
7+ ErrBindingAlreadyExists , ErrBadRequest , ErrInstanceDoesNotExist , \
8+ ServiceException
89from openbrokerapi .service_broker import ServiceBroker , Service , \
910 ProvisionDetails , ProvisionedServiceSpec , ProvisionState , GetBindingSpec , \
1011 BindDetails , Binding , BindState , UnbindDetails , UnbindSpec , \
@@ -73,15 +74,18 @@ def bind(self,
7374 if not (instance_meta and
7475 instance_meta ['last_operation' ]['state' ] == 'succeeded' ):
7576 raise ErrBadRequest (msg = "This instance %s is not ready" % instance_id ) # noqa
76- # if not async_allowed:
77- # raise ErrAsyncRequired()
7877 instance_path = get_instance_path (instance_id )
79- if os .path .exists (f'{ instance_path } /bind.yaml ' ):
78+ if os .path .exists (f'{ instance_path } /bind.json ' ):
8079 raise ErrBindingAlreadyExists ()
8180 chart_path , plan_path = get_chart_path (instance_id ), get_plan_path (instance_id ) # noqa
8281 shutil .copy (f'{ plan_path } /bind.yaml' , f'{ chart_path } /templates' )
83- bind .delay (instance_id , binding_id , details , async_allowed , ** kwargs )
84- return Binding (state = BindState .IS_ASYNC )
82+ bind (instance_id , binding_id , details , async_allowed , ** kwargs )
83+ data = load_binding_meta (instance_id )
84+ if data ["last_operation" ]["state" ] == OperationState .SUCCEEDED .value :
85+ return Binding (state = BindState .SUCCESSFUL_BOUND ,
86+ credentials = data ["credentials" ])
87+ else :
88+ raise ServiceException (data ["last_operation" ]["description" ])
8589
8690 def unbind (self ,
8791 instance_id : str ,
@@ -92,7 +96,8 @@ def unbind(self,
9296 ) -> UnbindSpec :
9397 instance_path = get_instance_path (instance_id )
9498 binding_info = f'{ instance_path } /binding.json'
95- shutil .rmtree (binding_info , ignore_errors = True )
99+ if os .path .exists (binding_info ):
100+ os .remove (binding_info )
96101 return UnbindSpec (is_async = False )
97102
98103 def update (self ,
@@ -104,17 +109,18 @@ def update(self,
104109 instance_path = get_instance_path (instance_id )
105110 if not os .path .exists (instance_path ):
106111 raise ErrBadRequest (msg = "Instance %s does not exist" % instance_id )
107- is_plan_updateable = get_addon_updateable (instance_id )
112+ is_plan_updateable = get_addon_updateable (details . service_id )
108113 if not is_plan_updateable :
109114 raise ErrBadRequest (msg = "Instance %s does not updateable" % instance_id ) # noqa
110115 if not async_allowed :
111116 raise ErrAsyncRequired ()
112- plan_path = get_plan_path (instance_id )
113- # delete the pre plan
114- shutil .rmtree (plan_path , ignore_errors = True )
115- _ , addon_plan_path = get_addon_path (details .service_id , details .plan_id ) # noqa
116- # add the new plan
117- shutil .copytree (addon_plan_path , plan_path )
117+ if details .plan_id is not None :
118+ plan_path = get_plan_path (instance_id )
119+ # delete the pre plan
120+ shutil .rmtree (plan_path , ignore_errors = True )
121+ _ , addon_plan_path = get_addon_path (details .service_id , details .plan_id ) # noqa
122+ # add the new plan
123+ shutil .copytree (addon_plan_path , plan_path )
118124 update .delay (instance_id , details )
119125 return UpdateServiceSpec (is_async = True )
120126
0 commit comments