copy smtp to new instance
This commit is contained in:
@@ -1510,4 +1510,81 @@ Thank you for choosing DocuPulse!
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error sending completion email: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
|
||||
@launch_api.route('/copy-smtp-settings', methods=['POST'])
|
||||
@csrf.exempt
|
||||
def copy_smtp_settings():
|
||||
"""Copy SMTP settings from master instance to launched instance"""
|
||||
try:
|
||||
if not request.is_json:
|
||||
return jsonify({'error': 'Request must be JSON'}), 400
|
||||
|
||||
data = request.get_json()
|
||||
if 'instance_url' not in data:
|
||||
return jsonify({'error': 'Missing instance_url parameter'}), 400
|
||||
|
||||
instance_url = data['instance_url']
|
||||
|
||||
# Get SMTP settings from master instance
|
||||
smtp_settings = KeyValueSettings.get_value('smtp_settings')
|
||||
if not smtp_settings:
|
||||
return jsonify({'error': 'SMTP settings not configured on master instance'}), 400
|
||||
|
||||
# Get the instance from database to get the connection token
|
||||
instance = Instance.query.filter_by(main_url=instance_url).first()
|
||||
|
||||
if not instance:
|
||||
return jsonify({'error': 'Instance not found in database'}), 404
|
||||
|
||||
if not instance.connection_token:
|
||||
return jsonify({'error': 'Instance not authenticated'}), 400
|
||||
|
||||
# Get JWT token from the launched instance using management API key
|
||||
jwt_response = requests.post(
|
||||
f"{instance_url.rstrip('/')}/api/admin/management-token",
|
||||
headers={
|
||||
'X-API-Key': instance.connection_token,
|
||||
'Accept': 'application/json'
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if jwt_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to get JWT token: {jwt_response.text}'}), 400
|
||||
|
||||
jwt_data = jwt_response.json()
|
||||
jwt_token = jwt_data.get('token')
|
||||
|
||||
if not jwt_token:
|
||||
return jsonify({'error': 'No JWT token received'}), 400
|
||||
|
||||
# Copy SMTP settings to the launched instance
|
||||
smtp_response = requests.post(
|
||||
f"{instance_url.rstrip('/')}/api/admin/key-value",
|
||||
headers={
|
||||
'Authorization': f'Bearer {jwt_token}',
|
||||
'Accept': 'application/json',
|
||||
'Content-Type': 'application/json'
|
||||
},
|
||||
json={
|
||||
'key': 'smtp_settings',
|
||||
'value': smtp_settings
|
||||
},
|
||||
timeout=10
|
||||
)
|
||||
|
||||
if smtp_response.status_code != 200:
|
||||
return jsonify({'error': f'Failed to copy SMTP settings: {smtp_response.text}'}), 400
|
||||
|
||||
# Log the SMTP settings copy
|
||||
current_app.logger.info(f"SMTP settings copied to instance {instance_url}")
|
||||
|
||||
return jsonify({
|
||||
'message': 'SMTP settings copied successfully',
|
||||
'data': smtp_settings
|
||||
})
|
||||
|
||||
except Exception as e:
|
||||
current_app.logger.error(f"Error copying SMTP settings: {str(e)}")
|
||||
return jsonify({'error': str(e)}), 500
|
||||
Reference in New Issue
Block a user