|
| 1 | +#!/usr/bin/env python |
| 2 | +# |
| 3 | +# This script demonstrates how to use the `get_labels` function to retrieve metadata |
| 4 | +# about label names and render the output as a table. |
| 5 | +# |
| 6 | + |
| 7 | +import sys |
| 8 | +from sdcclient import SdcClient |
| 9 | + |
| 10 | +def render_labels_as_table(labels): |
| 11 | + if not labels: |
| 12 | + print("No labels found.") |
| 13 | + return |
| 14 | + |
| 15 | + # Calculate the maximum width for the label column |
| 16 | + max_width = max(len(label) for label in labels) |
| 17 | + |
| 18 | + # Create a horizontal separator |
| 19 | + separator = "+" + "-" * (max_width + 2) + "+" |
| 20 | + |
| 21 | + # Create the header row |
| 22 | + header = f"| {'Label'.ljust(max_width)} |" |
| 23 | + |
| 24 | + # Create the rows for each label |
| 25 | + rows = [f"| {label.ljust(max_width)} |" for label in labels] |
| 26 | + |
| 27 | + # Combine everything into a table |
| 28 | + print(f"{separator}\n{header}\n{separator}\n" + "\n".join(rows) + f"\n{separator}") |
| 29 | + |
| 30 | + |
| 31 | +# |
| 32 | +# Parse arguments |
| 33 | +# |
| 34 | +if len(sys.argv) != 2: |
| 35 | + print(('usage: %s <sysdig-token>' % sys.argv[0])) |
| 36 | + print('You can find your token at https://app.sysdigcloud.com/#/settings/user') |
| 37 | + sys.exit(1) |
| 38 | + |
| 39 | +sdc_token = sys.argv[1] |
| 40 | + |
| 41 | +sdclient = SdcClient(sdc_token) |
| 42 | + |
| 43 | +# |
| 44 | +# Optional matchers to filter the labels |
| 45 | +# |
| 46 | +match = [ |
| 47 | + 'up' |
| 48 | +] # Replace with a list of matchers if needed |
| 49 | + |
| 50 | +# |
| 51 | +# Optional limit |
| 52 | +# |
| 53 | +limit = 10 # Set to None to disable the limit |
| 54 | + |
| 55 | +# |
| 56 | +# Fetch labels |
| 57 | +# |
| 58 | +ok, response_json = sdclient.get_labels(match=match, limit=limit) |
| 59 | + |
| 60 | +# |
| 61 | +# Show the result |
| 62 | +# |
| 63 | +if ok: |
| 64 | + # |
| 65 | + # Read the response. The JSON looks like this: |
| 66 | + # |
| 67 | + # { |
| 68 | + # "status": "success", |
| 69 | + # "data": [ |
| 70 | + # "agent_id", |
| 71 | + # "k8s_app", |
| 72 | + # "kube_pod_uid", |
| 73 | + # "kubernetes_io_cluster_service", |
| 74 | + # "container_image_tag", |
| 75 | + # "cloud_provider_tag_team", |
| 76 | + # "cloud_provider_tag_expirationDate", |
| 77 | + # "kube_pod_label_kubernetes_azure_com_managedby", |
| 78 | + # ] |
| 79 | + # } |
| 80 | + # |
| 81 | + labels = response_json.get("data", []) |
| 82 | + render_labels_as_table(labels) |
| 83 | +else: |
| 84 | + print("Error retrieving labels:", response_json) |
0 commit comments