如何使用Softlayer Python API重新启动虚拟机

时间:2016-04-22 09:29:16

标签: python api ibm-cloud-infrastructure

我找不到使用SoftLayer Python API VSManager重启或power-off/on虚拟机实例的位置。

XMLRPC API at:

中描述了这些操作

http://developer.softlayer.com/reference/services/SoftLayer_Virtual_Guest

但我找不到相应的:

http://softlayer-python.readthedocs.org/en/latest/api/managers/vs.html

2 个答案:

答案 0 :(得分:3)

事实上,经理没有这个实现,你必须在这里做一些api调用:

static void Perform_Deletions(List<UserAccountObject> User_List, string directory)
    {
        string ldapServer = null;
        string parentOU = null;
        string userCN = null;
        string ldapDirectory = null;
        string userName = null;
        string passWord = null;

        // REGEX value to only return OU path portion of User DN
        string dnSuffixRegex = @"ou.*";
        Regex myDNRegex = new Regex(dnSuffixRegex, RegexOptions.IgnoreCase);

        // REGEX to only Return the CN portion of User DN
        string cnRegex = @"^([^,]+)";
        Regex myCNRegex = new Regex(cnRegex, RegexOptions.IgnoreCase);

        switch (directory)
        {
            case "AD1":
                {
                    ldapDirectory = "LDAP://ad1.contosoe.com/";
                    userName = "Admin";
                    passWord = @"P@$$W0rd1";

                    break;
                }
            case "AD2":
                {
                    ldapDirectory = "LDAP://ad2.contosof.com/";
                    userName = "Admin";
                    passWord = @"P@$$W0rd1";

                    break;
                }
            case "EDIR1":
                {
                    ldapDirectory = "LDAP://edirectory1.contosoc.com/";
                    userName = @"cn=Admin,o=Root";
                    passWord = @"P@$$W0rd1";

                    break;
                }
            case "AD3":
                {
                    ldapDirectory = "LDAP://ad3.contosod.com/";
                    userName = "Admin";
                    passWord = @"P@$$W0rd1";

                    break;
                }
            case "EDIR2":
                {
                    ldapDirectory = "LDAP://edirectory2.contosob.com/";
                    userName = @"cn=Admin,o=Root";
                    passWord = @"P@$$W0rd1";

                    break;
                }
            case "EDIR3":
                {
                    ldapDirectory = "LDAP://edirectory3.contosoa.com/";
                    userName = @"cn=Admin,o=Root";
                    passWord = @"P@$$W0rd1";

                    break;
                }
            default:
                {
                    break;
                }
        }

        foreach (UserAccountObject user in User_List)
        {
            foreach (Match cnMatch in myCNRegex.Matches(user.Distinguished_Name))
            {
                userCN = cnMatch.ToString();
            }

            foreach (Match dnMatch in myDNRegex.Matches(user.Distinguished_Name))
            {
                parentOU = dnMatch.ToString();
            }

            ldapServer = ldapDirectory + parentOU;

            try
            {
                DirectoryEntry myLdapconnection = new DirectoryEntry(ldapServer, userName, passWord, AuthenticationTypes.ServerBind);
                DirectoryEntry userToDelete = myLdapconnection.Children.Find(userCN);
                myLdapconnection.RefreshCache();
                myLdapconnection.Children.Remove(userToDelete);
                myLdapconnection.CommitChanges();
                myLdapconnection.Close();
                myLdapconnection.Dispose();
                user.Deletion_Status = "SUCCEEDED";
            }
            catch (Exception e)
            {
                user.Deletion_Status = "FAILED";
                Console.WriteLine("Exception Caught:\n\n{0}", e.ToString());
            }
        }
    }

-

"""
Power off Guest

The scripts will look for a VSI which has an specific
hostname and the it powers off the VSI by making a single call
to the SoftLayer_Virtual_Guest::powerOff method.

Important manual pages:
http://sldn.softlayer.com/reference/services/SoftLayer_Acount/
http://sldn.softlayer.com/reference/services/SoftLayer_Acount/getVirtualGuests
http://sldn.softlayer.com/reference/services/SoftLayer_Virtual_Guest/setTags

License: http://sldn.softlayer.com/article/License
Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
"""
import SoftLayer

"""
# Your SoftLayer API username and key.
#
# Generate an API key at the SoftLayer Customer Portal:
# https://manage.softlayer.com/Administrative/apiKeychain
"""
username = 'set me'
key = 'set me'

# The name of the machine you wish to power off
virtualGuestName = 'rctest'

# Declare a new API service object
client = SoftLayer.Client(username=username, api_key=key)


try:
    # Getting all virtual guest that the account has:
    virtualGuests = client['SoftLayer_Account'].getVirtualGuests()
except SoftLayer.SoftLayerAPIError as e:
    """
    If there was an error returned from the SoftLayer API then bomb out with the
    error message.
    """
    print("Unable to retrieve hardware. "
          % (e.faultCode, e.faultString))

# Looking for the virtual guest
virtualGuestId = ''
for virtualGuest in virtualGuests:
    if virtualGuest['hostname'] == virtualGuestName:
        virtualGuestId = virtualGuest['id']

try:
    # Power off the virtual guest
    virtualMachines = client['SoftLayer_Virtual_Guest'].powerOff(id=virtualGuestId)
    print ("powered off")
except SoftLayer.SoftLayerAPIError as e:
    """
    If there was an error returned from the SoftLayer API then bomb out with the
    error message.
    """
    print("Unable to power off the virtual guest"
          % (e.faultCode, e.faultString))

此致

答案 1 :(得分:1)

slcli vs power_offslcli vs power_on应该可以解决问题。 Working with Virtual Servers

也许这也有帮助。 Source of the reboot functionality from the cli