Skip to content

Commit

Permalink
Add "update sources" and "update destinations" to create yaml files f…
Browse files Browse the repository at this point in the history
…or all existing sources and destinations
  • Loading branch information
danieldiamond committed May 29, 2022
1 parent 7748d63 commit 58303ea
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Configuration for {{ definition.docker_repository }}
# Documentation about this connector can be found at {{ definition.documentation_url }}
resource_name: {{ resource_name}}
resource_name: "{{ resource_name}}"
definition_type: {{ definition.type}}
definition_id: {{ definition.id }}
definition_image: {{ definition.docker_repository }}
Expand Down Expand Up @@ -32,7 +32,7 @@ definition_version: {{ definition.docker_image_tag }}


{%- macro render_one_of(field) %}
{{ field.name }}:
{{ field.name }}:
{%- for one_of_value in field.one_of_values %}
{%- if loop.first %}
## -------- Pick one valid structure among the examples below: --------
Expand All @@ -41,17 +41,17 @@ definition_version: {{ definition.docker_image_tag }}
## -------- Another valid structure for {{ field.name }}: --------
{{- render_sub_fields(one_of_value, True)|indent(2, False) }}
{%- endif %}
{%- endfor %}
{%- endfor %}
{%- endmacro %}

{%- macro render_object_field(field) %}
{{ field.name }}:
{{- render_sub_fields(field.object_properties, is_commented=False)|indent(2, False)}}
{{ field.name }}:
{{- render_sub_fields(field.object_properties, is_commented=False)|indent(2, False)}}
{%- endmacro %}

{%- macro render_array_of_objects(field) %}
{{ field.name }}:
{{- render_array_sub_fields(field.array_items, is_commented=False)|indent(2, False)}}
{{ field.name }}:
{{- render_array_sub_fields(field.array_items, is_commented=False)|indent(2, False)}}
{%- endmacro %}

{%- macro render_root(root, is_commented) %}
Expand Down
46 changes: 45 additions & 1 deletion octavia-cli/octavia_cli/update/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
import click
from octavia_cli.base_commands import OctaviaCommand
from octavia_cli.generate import definitions
from octavia_cli.generate.commands import generate_source_or_destination
from octavia_cli.generate.renderers import ConnectorSpecificationRenderer
from octavia_cli.get.resources import Connection, Destination, Source
from octavia_cli.list.listings import Destinations, Sources


@click.group("update", help="Update configurations in a YAML spec for a source, destination or a connection.")
Expand All @@ -17,12 +19,24 @@ def update(ctx: click.Context):
pass


def list_resource(resource_type, api_client, workspace_id):
resources = resource_type(api_client, workspace_id)
return {resource_id: resource_name for resource_name, _, resource_id in resources.get_listing()}


def get_resource_definition_id(resource_type, api_client, workspace_id, resource_id):
resource = resource_type(api_client, workspace_id, resource_id)
config = resource.get_config()
return config[f"{resource_type.name}_definition_id"]


def update_resource(resource_type, api_client, workspace_id, resource_id):
resource = resource_type(api_client, workspace_id, resource_id)
definition_type = resource_type.name
config = resource.get_config()
definition_id = config[f"{resource_type.name}_definition_id"]
resource_name = config[f"{resource_type.name}_name"]
# Schema varies across resources, e.g. pull "name" else default to "source_name"
resource_name = config.get("name") or config[f"{resource_type.name}_name"]

definition = definitions.factory(definition_type, api_client, workspace_id, definition_id)
renderer = ConnectorSpecificationRenderer(resource_name, definition)
Expand All @@ -39,6 +53,36 @@ def source(ctx: click.Context, resource_id: str):
update_resource(Source, ctx.obj["API_CLIENT"], ctx.obj["WORKSPACE_ID"], resource_id)


@update.command(cls=OctaviaCommand, name="sources", help="Get YAML for a source")
@click.pass_context
def sources(ctx: click.Context):
api_client = ctx.obj["API_CLIENT"]
workspace_id = ctx.obj["WORKSPACE_ID"]
sources = list_resource(Sources, api_client, workspace_id)

for source_id, source_name in sources.items():
definition_id = get_resource_definition_id(Source, api_client, workspace_id, source_id)

generate_source_or_destination("source", api_client, workspace_id, definition_id, source_name)

update_resource(Source, api_client, workspace_id, source_id)


@update.command(cls=OctaviaCommand, name="destinations", help="Get YAML for a destination")
@click.pass_context
def destinations(ctx: click.Context):
api_client = ctx.obj["API_CLIENT"]
workspace_id = ctx.obj["WORKSPACE_ID"]
destinations = list_resource(Destinations, api_client, workspace_id)

for destination_id, destination_name in destinations.items():
definition_id = get_resource_definition_id(Destination, api_client, workspace_id, destination_id)

generate_source_or_destination("destination", api_client, workspace_id, definition_id, destination_name)

update_resource(Destination, api_client, workspace_id, destination_id)


@update.command(cls=OctaviaCommand, name="destination", help="Get YAML for a destination")
@click.argument("resource_id", type=click.STRING)
@click.pass_context
Expand Down

0 comments on commit 58303ea

Please sign in to comment.