Skip to content

Commit

Permalink
feat: add builder CRUD (#41)
Browse files Browse the repository at this point in the history
* feat: add builder CRUD

* add validator
  • Loading branch information
smile37773 authored Dec 20, 2021
1 parent 0b3d274 commit 5310038
Show file tree
Hide file tree
Showing 5 changed files with 127 additions and 1 deletion.
28 changes: 28 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_build_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

# pylint: disable=too-few-public-methods, unused-argument, redefined-builtin

import json
from .vendored_sdks.appplatform.v2022_01_01_preview import models as models_20220101preview

DEFAULT_BUILD_SERVICE_NAME = "default"
Expand All @@ -20,3 +21,30 @@ def _update_default_build_agent_pool(cmd, client, resource_group, name, build_po
properties=build_properties)
return client.build_service_agent_pool.begin_update_put(
resource_group, name, DEFAULT_BUILD_SERVICE_NAME, DEFAULT_BUILD_AGENT_POOL_NAME, agent_pool_resource)


def create_or_update_builder(cmd, client, resource_group, service, name, builder_json=None, builder_file=None):
builder = _update_builder(builder_file, builder_json, [])
builder_resource = models_20220101preview.BuilderResource(
properties=builder
)
return client.build_service_builder.begin_create_or_update(resource_group, service, DEFAULT_BUILD_SERVICE_NAME,
name, builder_resource)


def builder_show(cmd, client, resource_group, service, name,):
return client.build_service_builder.get(resource_group, service, DEFAULT_BUILD_SERVICE_NAME, name)


def builder_delete(cmd, client, resource_group, service, name):
return client.build_service_builder.begin_delete(resource_group, service, DEFAULT_BUILD_SERVICE_NAME, name)


def _update_builder(builder_file, builder_json, builder):
if builder_file is not None:
with open(builder_file, 'r') as json_file:
builder = json.load(json_file)

if builder_json is not None:
builder = json.loads(builder_json)
return builder
37 changes: 37 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -655,6 +655,43 @@
text: az spring-cloud application-configuration-service unbind --app MyApp -s MyService -g MyResourceGroup
"""

helps['spring-cloud build-service builder'] = """
type: group
short-summary: (Enterprise Tier Only) Commands to manage Builder Resource
"""

helps['spring-cloud build-service builder create'] = """
type: command
short-summary: Create a builder.
examples:
- name: Create a builder using JSON file.
text: az spring-cloud build-service buildpacks-binding create --name my-builder --builder-json MyJson.json
"""

helps['spring-cloud build-service builder update'] = """
type: command
short-summary: Update a builder.
examples:
- name: Update a builder using JSON file.
text: az spring-cloud build-service buildpacks-binding update --name my-builder --builder-json MyJson.json
"""

helps['spring-cloud build-service builder show'] = """
type: command
short-summary: Show a builder.
examples:
- name: Show a builder.
text: az spring-cloud build-service buildpacks-binding show --name my-builder
"""

helps['spring-cloud build-service builder delete'] = """
type: command
short-summary: Delete a builder.
examples:
- name: Delete a builder.
text: az spring-cloud build-service buildpacks-binding delete --name my-builder
"""

helps['spring-cloud build-service buildpacks-binding'] = """
type: group
short-summary: (Enterprise Tier Only) Commands to manage Buildpacks Binding
Expand Down
20 changes: 19 additions & 1 deletion src/spring-cloud/azext_spring_cloud/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
validate_buildpacks_binding_secrets, only_support_enterprise,
validate_buildpacks_binding_not_exist, validate_buildpacks_binding_exist,
validate_git_uri, validate_acs_patterns, validate_routes, validate_builder,
validate_build_pool_size)
validate_build_pool_size, validate_builder_resource, validate_builder_create,
validate_builder_update)
from ._app_validator import (fulfill_deployment_param, active_deployment_exist, active_deployment_exist_under_app, ensure_not_active_deployment)
from ._utils import ApiType

Expand Down Expand Up @@ -521,6 +522,23 @@ def prepare_logs_argument(c):
c.argument('routes_json', type=str, help="The JSON array of API routes.", validator=validate_routes)
c.argument('routes_file', type=str, help="The file path of JSON array of API routes.", validator=validate_routes)

for scope in ['spring-cloud build-service builder create',
'spring-cloud build-service builder update']:
with self.argument_context(scope) as c:
c.argument('builder_json', type=str, help="The JSON array of builder.", validator=validate_builder_resource)
c.argument('builder_file', type=str, help="The file path of JSON array of builder.", validator=validate_builder_resource)

with self.argument_context('spring-cloud build-service builder create') as c:
c.argument('name', type=str, help="The builder name.", validator=validate_builder_create)

with self.argument_context('spring-cloud build-service builder update') as c:
c.argument('name', type=str, help="The builder name.", validator=validate_builder_update)

for scope in ['spring-cloud build-service builder show',
'spring-cloud build-service builder delete']:
with self.argument_context(scope) as c:
c.argument('name', type=str, help="The builder name.")

for scope in ['spring-cloud build-service buildpacks-binding create',
'spring-cloud build-service buildpacks-binding set']:
with self.argument_context(scope) as c:
Expand Down
29 changes: 29 additions & 0 deletions src/spring-cloud/azext_spring_cloud/_validators_enterprise.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,35 @@ def validate_builder(cmd, namespace):
.format(namespace.builder))


def validate_builder_create(cmd, namespace):
client = get_client(cmd)
try:
builder = client.build_service_builder.get(namespace.resource_group,
namespace.service,
DEFAULT_BUILD_SERVICE_NAME,
namespace.name)
if builder is not None:
raise CLIError('Builder {} already exists.'.format(namespace.name))
except ResourceNotFoundError:
pass


def validate_builder_update(cmd, namespace):
client = get_client(cmd)
try:
client.build_service_builder.get(namespace.resource_group,
namespace.service,
DEFAULT_BUILD_SERVICE_NAME,
namespace.name)
except ResourceNotFoundError:
raise CLIError('Builder {} does not exist.'.format(namespace.name))


def validate_builder_resource(namespace):
if namespace.builder_json is not None and namespace.builder_file is not None:
raise CLIError("You can only specify either --builder-json or --builder-file.")


def validate_build_pool_size(namespace):
if _parse_sku_name(namespace.sku) != 'enterprise':
namespace.build_pool_size = None
14 changes: 14 additions & 0 deletions src/spring-cloud/azext_spring_cloud/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,11 @@ def load_command_table(self, _):
client_factory=cf_spring_cloud_enterprise
)

builder_cmd_group = CliCommandType(
operations_tmpl="azext_spring_cloud._build_service#{}",
client_factory=cf_spring_cloud_enterprise
)

buildpacks_binding_cmd_group = CliCommandType(
operations_tmpl="azext_spring_cloud.buildpacks_binding#{}",
client_factory=cf_spring_cloud_enterprise
Expand Down Expand Up @@ -283,6 +288,15 @@ def load_command_table(self, _):
g.custom_command('bind', 'api_portal_custom_domain_update')
g.custom_command('unbind', 'api_portal_custom_domain_unbind')
g.custom_command('update', 'api_portal_custom_domain_update')

with self.command_group('spring-cloud build-service builder',
custom_command_type=builder_cmd_group,
exception_handler=handle_asc_exception, is_preview=True) as g:
# create and set commands are differentiate by their parameter validators
g.custom_command('create', 'create_or_update_builder')
g.custom_command('update', 'create_or_update_builder')
g.custom_command('show', 'builder_show')
g.custom_command('delete', 'builder_delete')

with self.command_group('spring-cloud build-service buildpacks-binding',
custom_command_type=buildpacks_binding_cmd_group,
Expand Down

0 comments on commit 5310038

Please sign in to comment.