federated-wordpress-customi.../rest-api/provisioner_rest_api_extensions.php

558 lines
16 KiB
PHP
Raw Permalink Normal View History

2024-05-02 17:51:32 +00:00
<?php
2024-10-17 20:39:34 +00:00
if( ! defined( 'ABSPATH' ) ){
exit; // Exit if accessed directly.
}
2024-05-02 17:51:32 +00:00
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_provision_status_failed_register');
function federated_route_update_provision_status_failed_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'provision-failed',
array(
'methods' => 'POST',
'callback' => 'federated_update_provision_status_failed_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_provision_status_failed_subscriptions($request){
$subscription_id = $request['subscription_id'];
$error_message = $request['error_message'];
update_field('provision_status', 'provision_failed', $subscription_id);
update_field('error_message', $error_message, $subscription_id);
2024-05-09 19:01:13 +00:00
$email_message = 'Provision failed for subscription #: ' . $subscription_id . '. Error message: ' . $error_message;
wp_mail('support@federated.computer', 'ALERT: Provision Failed', $email_message);
$output = array();
2024-05-02 17:51:32 +00:00
$output['message'] = 'Marked as failed';
$output['success'] = true;
2024-05-09 19:01:13 +00:00
2024-05-02 17:51:32 +00:00
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_provision_status_removed_register');
function federated_route_update_provision_status_removed_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'set-to-removed',
array(
'methods' => 'POST',
'callback' => 'federated_update_provision_status_removed_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_provision_status_removed_subscriptions($request){
$subscription_id = $request['subscription_id'];
$subscription = wcs_get_subscription($subscription_id);
update_field('provision_status', 'removed', $subscription_id);
$output = array();
$output['message'] = 'Removed';
$output['success'] = true;
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_provision_status_provisioned_register');
function federated_route_update_provision_status_provisioned_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'set-to-provisioned',
array(
'methods' => 'POST',
'callback' => 'federated_update_provision_status_provisioned_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_provision_status_provisioned_subscriptions($request){
$subscription_id = $request['subscription_id'];
$subscription = wcs_get_subscription($subscription_id);
update_field('provision_status', 'provisioned', $subscription_id);
$output = array();
$output['message'] = 'Provisioned';
$output['success'] = true;
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_backup_key_register');
function federated_route_update_backup_key_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-backup-key',
array(
'methods' => 'POST',
'callback' => 'federated_update_backup_key_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_backup_key_subscriptions($request){
$subscription_id = $request['subscription_id'];
$backup_key = $request['key'];
$subscription = wcs_get_subscription($subscription_id);
update_field('backup_gpg_key', $backup_key, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_backup_directory_register');
function federated_route_update_backup_directory_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-backup-directory',
array(
'methods' => 'POST',
'callback' => 'federated_update_backup_directory_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_backup_directory_subscriptions($request){
$subscription_id = $request['subscription_id'];
$backup_directory = $request['directory'];
$subscription = wcs_get_subscription($subscription_id);
update_field('backup_directory', $backup_directory, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_mount_point_register');
function federated_route_update_mount_point_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-mount-point',
array(
'methods' => 'POST',
'callback' => 'federated_update_mount_point_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_mount_point_subscriptions($request){
$subscription_id = $request['subscription_id'];
$mount_point = $request['mount_point'];
$subscription = wcs_get_subscription($subscription_id);
update_field('nfs_mount_point', $mount_point, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_internal_ip_register');
function federated_route_update_internal_ip_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-internal-ip',
array(
'methods' => 'POST',
'callback' => 'federated_update_internal_ip_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_internal_ip_subscriptions($request){
$subscription_id = $request['subscription_id'];
$internal_ip = $request['address'];
$subscription = wcs_get_subscription($subscription_id);
update_field('internal_ip_address', $internal_ip, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_external_ip_register');
function federated_route_update_external_ip_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-external-ip',
array(
'methods' => 'POST',
'callback' => 'federated_update_external_ip_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_external_ip_subscriptions($request){
$subscription_id = $request['subscription_id'];
$external_ip = $request['address'];
$subscription = wcs_get_subscription($subscription_id);
update_field('external_ip_address', $external_ip, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_initial_admin_password_register');
function federated_route_update_initial_admin_password_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-admin-pass',
array(
'methods' => 'POST',
'callback' => 'federated_update_initial_admin_password_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_initial_admin_password_subscriptions($request){
$subscription_id = $request['subscription_id'];
$pass = $request['password'];
$subscription = wcs_get_subscription($subscription_id);
update_field('initial_admin_password', $pass, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_domain_register');
function federated_route_update_domain_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-domain',
array(
'methods' => 'POST',
'callback' => 'federated_update_domain_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_domain_subscriptions($request){
$subscription_id = $request['subscription_id'];
$domain = $request['domain'];
$subscription = wcs_get_subscription($subscription_id);
update_field('domain', $domain, $subscription_id);
$vm_id = get_field('vm_id', $subscription_id);
if($domain !== $vm_id){
update_field('domain_status', 'custom', $subscription_id);
}
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_update_vm_id_register');
function federated_route_update_vm_id_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'update-vm-id',
array(
'methods' => 'POST',
'callback' => 'federated_update_vm_id_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_update_vm_id_subscriptions($request){
$subscription_id = $request['subscription_id'];
$vm_id = $request['vm_id'];
$subscription = wcs_get_subscription($subscription_id);
update_field('vm_id', $vm_id, $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['result'] = 'Success';
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_start_provision_register');
function federated_route_start_provision_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'set-to-provisioning',
array(
'methods' => 'POST',
'callback' => 'federated_start_provision_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_start_provision_subscriptions($request){
$subscription_id = $request['subscription_id'];
$subscription = wcs_get_subscription($subscription_id);
update_field('provision_status', 'provisioning', $subscription_id);
$output = array();
$output['subscription_id'] = $subscription_id;
$output['tier'] = get_subscription_tier($subscription);
$output['email'] = $subscription->get_user()->data->user_email;
$output['country_code'] = $subscription->get_billing_country();
return $output;
}
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_needs_removal_register');
function federated_route_needs_removal_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'needs-removal',
array(
'methods' => 'GET',
'callback' => 'federated_needs_removal_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_needs_removal_subscriptions(){
$args = array(
'numberposts' => -1,
'post_type' => 'shop_subscription',
'post_status' => 'wc-cancelled',
'meta_key' => 'provision_status',
'meta_value' => 'needs_removal'
);
$posts = get_posts($args);
$output = array();
foreach($posts as $post){
//Get data
$post_id = $post->ID;
$vm_id = get_field('vm_id', $post_id);
//Create object
$data = array();
$data['subscription_id'] = $post_id;
$data['vm_id'] = $vm_id;
//Add to output
array_push($output, $data);
}
return $output;
}
2024-10-08 15:42:39 +00:00
//Get needs set to removal
add_action('rest_api_init', 'federated_route_needs_set_to_removal_register');
function federated_route_needs_set_to_removal_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
2024-10-08 15:45:22 +00:00
'needs-set-to-removal',
2024-10-08 15:42:39 +00:00
array(
'methods' => 'GET',
'callback' => 'federated_needs_set_to_removal_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_needs_set_to_removal_subscriptions(){
$cancelled_args = array(
'numberposts' => -1,
'post_type' => 'shop_subscription',
'post_status' => 'wc-cancelled',
'meta_key' => 'provision_status',
'meta_value' => 'provisioned'
);
$cancelled_posts = get_posts($cancelled_args);
$pending_args = array(
'numberposts' => -1,
'post_type' => 'shop_subscription',
'post_status' => 'wc-pending-cancel',
'meta_key' => 'provision_status',
'meta_value' => 'provisioned'
);
$pending_posts = get_posts($pending_args);
2024-10-08 15:45:22 +00:00
2024-10-08 15:42:39 +00:00
$posts = array_merge($cancelled_posts, $pending_posts);
$output = array();
foreach($posts as $post){
//Get data
$post_id = $post->ID;
$vm_id = get_field('vm_id', $post_id);
//Create object
$data = array();
$data['subscription_id'] = $post_id;
$data['vm_id'] = $vm_id;
//Add to output
array_push($output, $data);
}
return $output;
}
2024-05-02 17:51:32 +00:00
//Get needs provision, get needs removal
add_action('rest_api_init', 'federated_route_needs_provision_register');
function federated_route_needs_provision_register(){
//register routes here
register_rest_route(
'federated-rest/v1',
'needs-provision',
array(
'methods' => 'GET',
'callback' => 'federated_needs_provision_subscriptions',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_needs_provision_subscriptions(){
$args = array(
2024-05-10 15:52:59 +00:00
'numberposts' => -1,
'post_type' => 'shop_subscription',
'post_status' => 'wc-active',
'meta_key' => 'provision_status',
'meta_value' => 'needs_provision'
);
$posts = get_posts($args);
return array_map('get_subscription_id', $posts);
}
//Get by VM ID
add_action('rest_api_init', 'federated_route_get_subscription_by_vm_id');
function federated_route_get_subscription_by_vm_id(){
//register routes here
register_rest_route(
'federated-rest/v1',
'get-subscription-id-by-vm-id',
array(
'methods' => 'POST',
'callback' => 'federated_get_subscription_by_vm_id',
'permissions_callback' => 'check_federated_permissions'
)
);
}
function federated_get_subscription_by_vm_id($request){
$vm_id = $request['vm_id'];
$args = array(
'numberposts' => -1,
'post_type' => 'shop_subscription',
'post_status' => 'wc-active',
'meta_key' => 'vm_id',
'meta_value' => $vm_id
);
2024-05-02 17:51:32 +00:00
$posts = get_posts($args);
2024-05-10 16:09:41 +00:00
2024-05-10 15:52:59 +00:00
$output = array();
2024-05-02 17:51:32 +00:00
2024-05-10 15:52:59 +00:00
foreach($posts as $post) {
$subscription_id = get_subscription_id($post);
$subscription_vm_id = get_field('vm_id', $subscription_id);
if($subscription_vm_id == $vm_id) {
$output['subscription_id'] = $subscription_id;
return $output;
}
}
$output['subscription_id'] = 0;
return $output;
2024-05-02 17:51:32 +00:00
}
2024-05-10 15:52:59 +00:00
2024-05-02 17:51:32 +00:00
//Shared Functions
function get_subscription_id($subscription){
return $subscription->ID;
}
function check_federated_permissions(){
if(current_user_can('administrator')){
return true;
} else {
return new WP_Error( 'rest_forbidden', esc_html__( 'OMG you can not view private data.', 'my-text-domain' ), array( 'status' => 401 ) );
}
}