Skip to content

Commit e7280c0

Browse files
author
lijianguo
committed
chore(helmbroker): update bind and update
1 parent de72ec5 commit e7280c0

3 files changed

Lines changed: 42 additions & 38 deletions

File tree

rootfs/helmbroker/broker.py

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
from openbrokerapi.catalog import ServicePlan
66
from openbrokerapi.errors import ErrInstanceAlreadyExists, ErrAsyncRequired, \
7-
ErrBindingAlreadyExists, ErrBadRequest, ErrInstanceDoesNotExist
7+
ErrBindingAlreadyExists, ErrBadRequest, ErrInstanceDoesNotExist, \
8+
ServiceException
89
from 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

rootfs/helmbroker/tasks.py

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -57,18 +57,18 @@ def provision(instance_id: str, details: ProvisionDetails):
5757

5858
@app.task(serializer='pickle')
5959
def update(instance_id: str, details: UpdateDetails):
60-
data = {
61-
"id": instance_id,
62-
"details": {
63-
"service_id": details.service_id,
64-
"plan_id": details.plan_id,
65-
"context": details.context,
66-
"parameters": details.parameters,
67-
},
68-
"last_operation": {
69-
"state": OperationState.IN_PROGRESS.value,
70-
"description": "update %s in progress at %s" % (instance_id, time.time()) # noqa
71-
}
60+
data = load_instance_meta(instance_id)
61+
if details.service_id:
62+
data['details']['service_id'] = details.service_id
63+
if details.plan_id:
64+
data['details']['service_id'] = details.plan_id
65+
if details.context:
66+
data['details']['context'] = details.context
67+
if details.parameters:
68+
data['details']['service_id'] = details.parameters
69+
data['last_operation'] = {
70+
"state": OperationState.IN_PROGRESS.value,
71+
"description": "update %s in progress at %s" % (instance_id, time.time()) # noqa
7272
}
7373
dump_instance_meta(instance_id, data)
7474
chart_path = get_chart_path(instance_id)
@@ -107,8 +107,7 @@ def bind(instance_id: str,
107107
**kwargs):
108108
data = {
109109
"binding_id": binding_id,
110-
"credential": {
111-
},
110+
"credentials": {},
112111
"last_operation": {
113112
"state": OperationState.IN_PROGRESS.value,
114113
"description": "binding %s in progress at %s" % (binding_id, time.time()) # noqa
@@ -141,7 +140,7 @@ def bind(instance_id: str,
141140
success_flag = False
142141
errors.append(val)
143142
else:
144-
data['credential'][_['name']] = val
143+
data['credentials'][_['name']] = val
145144
if success_flag:
146145
data['last_operation'] = {
147146
'state': OperationState.SUCCEEDED.value,
@@ -154,7 +153,8 @@ def bind(instance_id: str,
154153
}
155154
dump_binding_meta(instance_id, data)
156155
bind_yaml = f'{chart_path}/templates/bind.yaml'
157-
shutil.rmtree(bind_yaml, ignore_errors=True)
156+
if os.path.exists(bind_yaml):
157+
os.remove(bind_yaml)
158158

159159

160160
@app.task()

rootfs/helmbroker/utils.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,15 @@ def command(cmd, *args, output_type="text"):
2121
get_plan_path = lambda instance_id: os.path.join(get_instance_path(instance_id), "plan") # noqa
2222

2323

24-
def get_addon_path(service_id, plan_id):
24+
def get_addon_meta(service_id):
2525
services = load_addons_meta()
2626
service = [addon for addon in [addons for _, addons in services.items()]
2727
if addon['id'] == service_id][0]
28+
return service
29+
30+
31+
def get_addon_path(service_id, plan_id):
32+
service = get_addon_meta(service_id)
2833
plan = [plan for plan in service['plans'] if plan['id'] == plan_id][0]
2934
service_name = f'{service["name"]}-{service["version"]}'
3035
plan_name = plan['name']
@@ -33,13 +38,6 @@ def get_addon_path(service_id, plan_id):
3338
return service_path, plan_path
3439

3540

36-
def get_addon_meta(service_id):
37-
services = load_addons_meta()
38-
service = [addon for addon in [addons for _, addons in services.items()]
39-
if addon['id'] == service_id][0]
40-
return service
41-
42-
4341
def get_addon_name(service_id):
4442
service = get_addon_meta(service_id)
4543
return service['name']

0 commit comments

Comments
 (0)