|
32 | 32 | from .controller import Controller, ConnectedController |
33 | 33 | from .delta import get_entity_class, get_entity_delta |
34 | 34 | from .errors import JujuAPIError, JujuError, JujuModelConfigError, JujuBackupError |
35 | | -from .errors import JujuModelError, JujuAppError, JujuUnitError, JujuAgentError, JujuMachineError, PylibjujuError |
| 35 | +from .errors import JujuModelError, JujuAppError, JujuUnitError, JujuAgentError, JujuMachineError, PylibjujuError, JujuNotSupportedError |
36 | 36 | from .exceptions import DeadEntityException |
37 | 37 | from .names import is_valid_application |
38 | 38 | from .offerendpoints import ParseError as OfferParseError |
39 | 39 | from .offerendpoints import parse_local_endpoint, parse_offer_url |
40 | 40 | from .origin import Channel, Source |
41 | 41 | from .placement import parse as parse_placement |
| 42 | +from .secrets import create_secret_data, read_secret_data |
42 | 43 | from .tag import application as application_tag |
43 | 44 | from .url import URL, Schema |
44 | 45 | from .version import DEFAULT_ARCHITECTURE |
@@ -2613,15 +2614,51 @@ async def export_bundle(self, filename=None): |
2613 | 2614 | except IOError: |
2614 | 2615 | raise |
2615 | 2616 |
|
| 2617 | + async def add_secret(self, name, dataArgs, file="", info=""): |
| 2618 | + """Adds a secret with a list of key values |
| 2619 | +
|
| 2620 | + Equivalent to the cli command: |
| 2621 | + juju add-secret [options] <name> [key[#base64|#file]=value...] |
| 2622 | +
|
| 2623 | + :param name str: The name of the secret to be added. |
| 2624 | + :param dataArgs []str: The key value pairs to be added into the secret. |
| 2625 | + :param file str: A path to a yaml file containing secret key values. |
| 2626 | + :param info str: The secret description. |
| 2627 | + """ |
| 2628 | + data = create_secret_data(dataArgs) |
| 2629 | + |
| 2630 | + if file: |
| 2631 | + data_from_file = read_secret_data(file) |
| 2632 | + for k, v in data_from_file.items(): |
| 2633 | + # Caution: key/value pairs in files overwrite the ones in the args. |
| 2634 | + data[k] = v |
| 2635 | + |
| 2636 | + if client.SecretsFacade.best_facade_version(self.connection()) < 2: |
| 2637 | + raise JujuNotSupportedError("user secrets") |
| 2638 | + |
| 2639 | + secretsFacade = client.SecretsFacade.from_connection(self.connection()) |
| 2640 | + results = await secretsFacade.CreateSecrets([{ |
| 2641 | + 'content': {'data': data}, |
| 2642 | + 'description': info, |
| 2643 | + 'label': name, |
| 2644 | + }]) |
| 2645 | + if len(results.results) != 1: |
| 2646 | + raise JujuAPIError(f"expected 1 result, got {len(results.results)}") |
| 2647 | + result = results.results[0] |
| 2648 | + if result.error is not None: |
| 2649 | + raise JujuAPIError(result.error.message) |
| 2650 | + return result.result |
| 2651 | + |
2616 | 2652 | async def list_secrets(self, filter="", show_secrets=False): |
2617 | 2653 | """ |
2618 | 2654 | Returns the list of available secrets. |
2619 | 2655 | """ |
2620 | 2656 | facade = client.SecretsFacade.from_connection(self.connection()) |
2621 | | - return await facade.ListSecrets({ |
| 2657 | + results = await facade.ListSecrets({ |
2622 | 2658 | 'filter': filter, |
2623 | 2659 | 'show-secrets': show_secrets, |
2624 | 2660 | }) |
| 2661 | + return results.results |
2625 | 2662 |
|
2626 | 2663 | async def _get_source_api(self, url, controller_name=None): |
2627 | 2664 | controller = Controller() |
|
0 commit comments