Skip to content

Instantly share code, notes, and snippets.

@softlayer
Created September 20, 2010 16:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save softlayer/588164 to your computer and use it in GitHub Desktop.
Save softlayer/588164 to your computer and use it in GitHub Desktop.
// Retrieve account VLAN and subnet information per server.
//
// Retrieve a list of all servers associated with a SoftLayer customer account
// and print a simple report detailing their VLANs and the subnets assigned to
// them. We do this with a call to the getHardare() method in the
// SoftLayer_Account API service using an object mask to retrieve the hardware,
// VLANs, VLANs' associated subnets and primary router records. See below for
// more details.
//
// Backend code to call the SoftLayer API was generated with the wsdl.exe
// utility:
//
// wsdl.exe /language:CS /out:"C:\path\to\project\SoftLayer_API.cs" /sharetypes https://api.softlayer.com/soap/v3/SoftLayer_Account?wsdl
//
// Important manual pages:
// http://sldn.softlayer.com/article/C-Sharp
// http://sldn.softlayer.com/reference/services/SoftLayer_Account
// http://sldn.softlayer.com/reference/datatypes/SoftLayer_Hardware_Server
// http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Vlan
// http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Subnet
// http://sldn.softlayer.com/reference/datatypes/SoftLayer_Network_Subnet_IpAddress
// http://sldn.softlayer.com/reference/services/SoftLayer_Account/getHardware
//
// License: http://sldn.softlayer.com/article/License
// Author: SoftLayer Technologies, Inc. <sldn@softlayer.com>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// Your SoftLayer API username.
string username = "set me";
// Your SoftLayer API key.
//
// Generate one at https://manage.softlayer.com/Administrative/apiKeychain
string apiKey = "set me too!";
// Create a connection to the SoftLayer_Account API service and
// bind our API username and key to it.
authenticate authenticate = new authenticate();
authenticate.username = username;
authenticate.apiKey = apiKey;
SoftLayer_AccountService accountService = new SoftLayer_AccountService();
accountService.authenticateValue = authenticate;
// Bind an object mask to our API call that retrieves our servers,
// VLANs' primary router, subnets, and optionally individual IP
// address records.
SoftLayer_AccountObjectMask objectMask = new SoftLayer_AccountObjectMask();
objectMask.mask = new SoftLayer_Account();
objectMask.mask.hardware = new SoftLayer_Hardware[1];
objectMask.mask.hardware[0] = new SoftLayer_Hardware();
objectMask.mask.hardware[0].networkVlans= new SoftLayer_Network_Vlan[1];
objectMask.mask.hardware[0].networkVlans[0] = new SoftLayer_Network_Vlan();
objectMask.mask.hardware[0].networkVlans[0].primaryRouter = new SoftLayer_Hardware_Router();
objectMask.mask.hardware[0].networkVlans[0].subnets = new SoftLayer_Network_Subnet[1];
objectMask.mask.hardware[0].networkVlans[0].subnets[0] = new SoftLayer_Network_Subnet();
// Uncomment these two lines if you wish to retrieve individual
// records for each of your account's subnets.
//objectMask.mask.hardware[0].networkVlans[0].subnets[0].ipAddresses = new SoftLayer_Network_Subnet_IpAddress[1];
//objectMask.mask.hardware[0].networkVlans[0].subnets[0].ipAddresses[0] = new SoftLayer_Network_Subnet_IpAddress();
accountService.SoftLayer_AccountObjectMaskValue = objectMask;
try
{
// Call getHardware() from the SoftLayer API to get our server
// records.
SoftLayer_Hardware[] hardware = accountService.getHardware();
// Loop through our servers, print their id numbers and FQDNs.
foreach (SoftLayer_Hardware_Server server in hardware)
{
Console.WriteLine("Server " + server.id.ToString() + ": " + server.fullyQualifiedDomainName);
Console.WriteLine("==========");
// Loop through our VLANs, print their VLAN numbers and the
// name of the router they're associated with.
foreach (SoftLayer_Network_Vlan vlan in server.networkVlans)
{
Console.WriteLine("VLAN " + vlan.vlanNumber.ToString() + " - " + vlan.primaryRouter.fullyQualifiedDomainName);
Console.WriteLine("----------");
// Loop through the subnets in our VLANs, printing the
// subnet's network id and network mask in CIDR
// format.
foreach (SoftLayer_Network_Subnet subnet in vlan.subnets)
{
Console.WriteLine(subnet.networkIdentifier + "/" + subnet.cidr.ToString());
}
Console.WriteLine();
}
}
}
catch (Exception e)
{
Console.WriteLine("Unable to retrieve hardware information: " + e.Message);
}
// Hit <enter> to exit.
Console.ReadLine();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment