The Community Forums

Interact with an entire community of cPanel & WHM users!
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

XML-API Help - Delete Sub-Domains/MySQL User/DB

Discussion in 'cPanel Developers' started by rezman, Mar 13, 2012.

  1. rezman

    rezman Well-Known Member

    Joined:
    Feb 3, 2011
    Messages:
    45
    Likes Received:
    0
    Trophy Points:
    6
    Location:
    USA
    cPanel Access Level:
    Root Administrator
    Working on setting up a way to delete a sub-domain, MySQL User and MySQL DB using the API.

    Reading over the API1 module for 'subdomain' (WebHome < ApiDocs/Api1 < TWiki) it appears it doesn't support deleting. So I resorted to using the API2 module for this.

    Code:
    $result = $cp->api2_query( $cp_user, 'SubDomain', 'delsubdomain', array( "domain" => $subDomain ) );
    
    if ( $result->data->result[0] == 1 ) :
      // SUCCESS
      redir( "some URL" );
    else :
      // FAIL
      echo $result->data->reason[0];
    endif;
    
    This works!

    Now when I go to delete a MySQL user & DB it doesn't work. Strange how the API2 MysqlFE module (ApiMysqlFE < ApiDocs/Api2 < TWiki) doesn't seem to support deleting so I'm falling back to using the API1 module (ApiMysql < ApiDocs/Api1 < TWiki).

    Code:
    
    $ID = $_GET['id'];
    $db_username = $cp_user ."_". $ID
    
    // DELETE A USER
    $result = $cp->api1_query( $cp_user, 'Mysql', 'deluser', array( "dbuser" => $db_username ) );
    
    // DELETE A DB
    $result = $cp->api1_query( $cp_user, 'Mysql', 'deldb', array( "dbname" => $db_username ) );
    Neither of those work and because nothing is returned I can't seem to see what if anything is wrong.

    Anyone have any input on this to help me out?
     
  2. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    Not sure why they changed their documentation to seem like API1 functions have named values. They do not. To pass values to API1 functions, you have to pass in 'arg-0', 'arg-1', etc. Updated calls below.


    Code:
    // DELETE A USER
    $result = $cp->api1_query( $cp_user, 'Mysql', 'deluser', array( "arg-0" => $db_username ) );
    
    // DELETE A DB
    $result = $cp->api1_query( $cp_user, 'Mysql', 'deldb', array( "arg-0" => $db_username ) );
    
     
  3. cPanelDavidN

    cPanelDavidN Integration Developer
    Staff Member

    Joined:
    Dec 17, 2009
    Messages:
    571
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    Houston, TX
    cPanel Access Level:
    Root Administrator
    Hi rezman,

    KostonConsulting definitely has the right idea, however I think some refinement is needed.

    It looks like you're probably using the PHP client class (xmlapi.php). When making API1 calls with that class you should pass an ordinal array (where the indices correlating with the argument listing in the documentation) for the module/function args:
    PHP:
    $args = array( $db_username );
    // OR array( '0' => $db_username );
    $cp->api1_query$cp_user'Mysql''deluser'$args );
    The PHP class will prepend the 'arg-' accordingly for you (when it constructs the query URL and URL params). If you were not using the PHP class, then you'd have to ensure that your the URL params are 'arg-n' where 'n' references a positional argument. The documentation does not explicitly state the order, however, the order in which the arguments are listed is what is expected. For example, the PHP args array for API1 SubDomain::addsubdomain would be:
    PHP:
    $args = array( $domain$rootdomain$usecanonicalname$disallowdot$dir);
    For API2, you use an associative array:
    PHP:
    $args = array( 'db' => $dbname'user' => $db_username );
    $cp->api2_query$cp_user'MysqlFE''userdbprivs'$args ); 
    And yes, API1 and API2 do not provide the same functionality. There are several functions within modules, and even whole modules, that are only available in one or the other...and not both. Simply put: API1 and API2 are 2 entirely different APIs; they are NOT different versions of the same API.

    The API2 MysqlFE module does not provide any add/delete operations for Mysql databases or database users. Those are currently only available from the Mysql API1 module. Likewise API1 'SubDomain' was not provisioned the delete subdomain operation. Unfortunately, this is the way that they've been for a long while now; it's not likely to change. (though, you're welcome to make/add a post in the feature request forum).

    Regards,
    -DavidN
     
  4. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Hey David,

    I'm hoping you can help me here: I have no idea why this isnt working:

    Code:
            my $sqlprivs = $cp->cpanel_api2_request('whostmgr', { 'module' => 'MysqlFE', 'func' => 'userdbprivs', 'user' => $user },
                    {
                            'user' => $dbuser,
                            'db' => $dbname,
                    }
            );
            print Dumper $sqlprivs;
    
    There is nothing returned into $sqlprivs so it's kind of hard to troubleshoot, cPanel access log shows the following:


    127.0.0.1 - root [04/19/2012:15:35:35 -0000] "GET /json-api/cpanel?db=*****&user=*****&cpanel_jsonapi_module=MysqlFE&cpanel_jsonapi_user=*****&cpanel_jsonapi_apiversion=2&cpanel_jsonapi_func=userdbprivs HTTP/1.1" 200 0 "" "Cpanel::PublicAPI (perl) 1.1"

    The users and db's have been edited out for privacy
     
  5. KostonConsulting

    KostonConsulting Well-Known Member

    Joined:
    Jun 17, 2010
    Messages:
    255
    Likes Received:
    1
    Trophy Points:
    18
    Location:
    San Francisco, CA
    cPanel Access Level:
    Root Administrator
    I believe this should be running through the 'cpanel' service (in bold below). Also bolded where you can set the specific output format to xml or json.

    Code:
    
    my $sqlprivs = $cp->cpanel_api2_request([B]'cpanel'[/B], { 'module' => 'MysqlFE', 'func' => 'userdbprivs', 'user' => $user },
                    {
                            'user' => $dbuser,
                            'db' => $dbname,
                    }, [B]'json'[/B]
            );
            print Dumper $sqlprivs;
    
     
  6. morissette

    morissette Well-Known Member

    Joined:
    May 24, 2009
    Messages:
    119
    Likes Received:
    0
    Trophy Points:
    16
    Location:
    Austin, TX
    cPanel Access Level:
    Root Administrator
    Using the cpanel service, as my publicapi object is through root do I need to use the users accesshash and create a new object?
     
Loading...

Share This Page