-
Notifications
You must be signed in to change notification settings - Fork 34
feat: 新增幂等创建接口 #262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
feat: 新增幂等创建接口 #262
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
8e24d2d
feat: 增加幂等创建接口
xiaoj655 55f7157
chore
xiaoj655 92945e9
update migrations
xiaoj655 f8bf2b0
update CHANGES.md
xiaoj655 af11194
chore
xiaoj655 f5b6203
refactor
xiaoj655 11cdab8
s
xiaoj655 29ebf3e
chore: update CHANGES.md
xiaoj655 047d613
chore
xiaoj655 ac3ae64
chore
xiaoj655 64dc3e2
update CHANGES.md
xiaoj655 fe856e1
chore
xiaoj655 7dee8f0
chore
xiaoj655 cb0acd8
chore
xiaoj655 8878562
chore
xiaoj655 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,86 @@ | ||
| # -*- coding: utf-8 -*- | ||
| """ | ||
| TencentBlueKing is pleased to support the open source community by making | ||
| 蓝鲸智云 - PaaS 平台 (BlueKing - PaaS System) available. | ||
| Copyright (C) 2017 THL A29 Limited, a Tencent company. All rights reserved. | ||
| Licensed under the MIT License (the "License"); you may not use this file except | ||
| in compliance with the License. You may obtain a copy of the License at | ||
|
|
||
| http://opensource.org/licenses/MIT | ||
|
|
||
| Unless required by applicable law or agreed to in writing, software distributed under | ||
| the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, | ||
| either express or implied. See the License for the specific language governing permissions and | ||
| limitations under the License. | ||
|
|
||
| We undertake not to change the open source license (MIT license) applicable | ||
| to the current version of the project delivered to anyone in the future. | ||
| """ | ||
| import json | ||
| from typing import Callable | ||
|
|
||
| from django.db import IntegrityError, transaction | ||
|
|
||
| from paas_service.base_vendor import BaseProvider, get_provider_cls | ||
| from paas_service.models import Plan, Service | ||
|
|
||
| from .constants import ProvisionRecordStatus | ||
| from .models import ProvisionRecord, ServiceInstance | ||
|
|
||
|
|
||
| def idempotent_provision_instance( | ||
| provision_key: str, | ||
| service: Service, | ||
| plan: Plan, | ||
| params: dict, | ||
| provider_cls_getter: Callable[[], type[BaseProvider]] = get_provider_cls, | ||
| ) -> tuple[ServiceInstance | None, bool]: | ||
| """Create or reuse instance by provision key | ||
|
|
||
| :returns: (service_instance, created) `service_instance=None` when provisioning | ||
| """ | ||
|
|
||
| try: | ||
| with transaction.atomic(): | ||
| record = ProvisionRecord.objects.create( | ||
| provision_key=provision_key, | ||
| plan_id=plan.uuid, | ||
| status=ProvisionRecordStatus.PROVISIONING, | ||
| ) | ||
| acquired = True | ||
| except IntegrityError: | ||
| acquired = False | ||
|
|
||
| if not acquired: | ||
| record = ProvisionRecord.objects.select_related('service_instance').get( | ||
| provision_key=provision_key, | ||
| ) | ||
| if record.status == ProvisionRecordStatus.SUCCESS: | ||
| return record.service_instance, False | ||
| if record.status == ProvisionRecordStatus.PROVISIONING: | ||
| return None, False | ||
|
xiaoj655 marked this conversation as resolved.
|
||
| raise Exception(f"Provision record with key {provision_key} is in unexpected status {record.status}") | ||
|
xiaoj655 marked this conversation as resolved.
|
||
|
|
||
| provider_cls = provider_cls_getter() | ||
| plan_config = json.loads(plan.config) | ||
| try: | ||
| instance_data = provider_cls(**plan_config).create(params=params) | ||
| except Exception: | ||
| record.delete() | ||
| raise | ||
|
|
||
| service_instance = ServiceInstance.objects.create( | ||
| service=service, | ||
| plan=plan, | ||
| config=instance_data.config, | ||
| credentials=json.dumps(instance_data.credentials), | ||
| tenant_id=plan.tenant_id, | ||
| ) | ||
| mark_record_success(record, service_instance) | ||
|
xiaoj655 marked this conversation as resolved.
|
||
| return service_instance, True | ||
|
|
||
|
|
||
| def mark_record_success(record: ProvisionRecord, service_instance: ServiceInstance): | ||
| record.service_instance = service_instance | ||
| record.status = ProvisionRecordStatus.SUCCESS | ||
| record.save(update_fields=["service_instance", "status"]) | ||
30 changes: 30 additions & 0 deletions
30
sdks/paas-service/paas_service/migrations/0010_provisionrecord.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| # Generated by Django 4.2.27 on 2026-04-22 02:53 | ||
|
|
||
| from django.db import migrations, models | ||
| import django.db.models.deletion | ||
| import uuid | ||
|
|
||
|
|
||
| class Migration(migrations.Migration): | ||
|
|
||
| dependencies = [ | ||
| ('paas_service', '0009_alter_plan_unique_together_plan_tenant_id_and_more'), | ||
| ] | ||
|
|
||
| operations = [ | ||
| migrations.CreateModel( | ||
| name='ProvisionRecord', | ||
| fields=[ | ||
| ('uuid', models.UUIDField(auto_created=True, default=uuid.uuid4, editable=False, primary_key=True, serialize=False, unique=True, verbose_name='UUID')), | ||
| ('created', models.DateTimeField(auto_now_add=True)), | ||
| ('updated', models.DateTimeField(auto_now=True)), | ||
| ('provision_key', models.CharField(max_length=64, unique=True, verbose_name='幂等分配键')), | ||
| ('plan_id', models.UUIDField(verbose_name='方案 ID')), | ||
| ('status', models.CharField(max_length=16, verbose_name='状态')), | ||
| ('service_instance', models.OneToOneField(db_constraint=False, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='record', to='paas_service.serviceinstance', verbose_name='实例')), | ||
| ], | ||
| options={ | ||
| 'abstract': False, | ||
| }, | ||
| ), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,7 +8,7 @@ classifiers = [ | |
| # PEP 621 project metadata | ||
| # See https://www.python.org/dev/peps/pep-0621/ | ||
| name = "paas_service" | ||
| version = "2.0.3" | ||
| version = "2.0.4" | ||
| description = "A Django application for developing BK-PaaS add-on services." | ||
| readme = "README.md" | ||
| authors = [{ name = "blueking", email = "[email protected]" }] | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.