1+ import click
2+ import json
3+ from utils import display_paginated_results , handle_api_error , format_item_details
4+
5+ @click .group ()
6+ def integrations ():
7+ """Commands for managing PagerTree integrations."""
8+ pass
9+
10+ @integrations .command (name = "show" )
11+ @click .argument ("integration_id" , required = True )
12+ @click .pass_context
13+ def show_integration_cmd (ctx , integration_id ):
14+ """Show details of a specific integration in PagerTree."""
15+ try :
16+ client = ctx .obj .client # Get PagerTreeClient from context
17+ integration = client .show_integration (integration_id )
18+ fields = {
19+ "id" : "ID" ,
20+ "name" : "Name" ,
21+ "integration_type.name" : "Type" ,
22+ "enabled" : "Enabled" ,
23+ "created_at" : "Created At" ,
24+ "updated_at" : "Updated At"
25+ }
26+ format_item_details (integration , fields )
27+ except Exception as e :
28+ handle_api_error (e , "showing integration" )
29+
30+ @integrations .command (name = "list" )
31+ @click .option ("--limit" , default = 10 , type = click .IntRange (1 , 100 ), help = "Number of integrations per page" )
32+ @click .option ("--offset" , default = 0 , type = click .IntRange (0 ), help = "Starting point for pagination" )
33+ @click .option ("--search" , help = "Search for integrations by name or type" )
34+ @click .option ("--enabled" , is_flag = True , help = "Filter for enabled integrations" , default = None )
35+ @click .option ("--disabled" , is_flag = True , help = "Filter for disabled integrations" )
36+ @click .pass_context
37+ def list_integrations_cmd (ctx , limit , offset , search , enabled , disabled ):
38+ """List integrations in PagerTree with pagination."""
39+ try :
40+ # Ensure --enabled and --disabled are mutually exclusive
41+ if enabled and disabled :
42+ click .echo ("Error: --enabled and --disabled cannot be used together." )
43+ return
44+
45+ # Set enabled parameter: True for --enabled, False for --disabled, None if neither
46+ enabled_param = 1 if enabled else 0 if disabled else None
47+
48+ client = ctx .obj .client # Get PagerTreeClient from context
49+ logger = ctx .obj .logger # Get logger from context
50+ logger .debug (f"Listing integrations with limit={ limit } , offset={ offset } , search={ search } , enabled={ enabled_param } " )
51+ result = client .list_integrations (limit = limit , offset = offset , search = search , enabled = enabled_param )
52+ logger .debug (f"Full response: { json .dumps (result , indent = 2 )} " )
53+ integrations_list = result ["data" ]
54+ total = result ["total" ]
55+ # Prepare table data
56+ headers = ["ID" , "Name" , "Type" , "Enabled" ]
57+ table_data = [[integration .get ("id" ), integration .get ("name" ), integration .get ("integration_type" ).get ("name" ), integration .get ("enabled" )] for integration in integrations_list ]
58+ display_paginated_results (integrations_list , total , limit , offset , "integration" , headers , table_data )
59+ except Exception as e :
60+ logger .error (f"Error listing integrations: { str (e )} " )
61+ handle_api_error (e , action = "listing integrations" )
62+
63+ @integrations .command (name = "enable" )
64+ @click .argument ("integration_id" , required = True )
65+ @click .pass_context
66+ def enable_integration_cmd (ctx , integration_id ):
67+ """Enable an integration in PagerTree."""
68+ try :
69+ client = ctx .obj .client # Get PagerTreeClient from context
70+ result = client .update_integration (integration_id , enabled = True )
71+ click .echo (f"Integration enabled successfully: { result .get ('id' )} " )
72+ except Exception as e :
73+ handle_api_error (e , action = "enabling integration" )
74+
75+ @integrations .command (name = "disable" )
76+ @click .argument ("integration_id" , required = True )
77+ @click .pass_context
78+ def disable_integration_cmd (ctx , integration_id ):
79+ """Disable an integration in PagerTree."""
80+ try :
81+ client = ctx .obj .client # Get PagerTreeClient from context
82+ result = client .update_integration (integration_id , enabled = False )
83+ click .echo (f"Integration disabled successfully: { result .get ('id' )} " )
84+ except Exception as e :
85+ handle_api_error (e , action = "disabling integration" )
0 commit comments