Skip to content

Instantly share code, notes, and snippets.

@mwrock
Created January 28, 2015 20:42
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mwrock/69b89d399a07ea87eed6 to your computer and use it in GitHub Desktop.
Save mwrock/69b89d399a07ea87eed6 to your computer and use it in GitHub Desktop.
clc-powershell
function Assert-Authentication {
[CmdletBinding()]
param(
[switch]$Force
)
if(!$script:AuthSession -Or $Force) {
$creds = Get-CLCAPICredentials -ActiveCredential
if(!$Creds){
throw "You have not set your API Credentials. Use Set-CLCAPICredentials to store them."
}
$script:AuthSession = Get-AuthenticatedSession -URI $creds.URIHost -APIKey $creds.APIKey -Password $creds.Password
$script:UriHost = $creds.URIHost
}
}
function Get-AuthenticatedSession {
[CmdletBinding()]
param(
[String]$URIHost = "api.CenturyLinkCloud.com",
[parameter(Mandatory=$true)]
[GUID]$APIKey,
[parameter(Mandatory=$true)]
[string]$Password
)
$strAPIKey = $APIKey.ToString("N")
$body = @"
{
"request": {
"APIKey": "$strAPIKey",
"Password":"$Password"
}
}
"@
$Result = Invoke-RestMethod -Method Post -Uri https://$URIHost/$($CLCRoutes.Logon)/ -ContentType "application/json" -body $body -SessionVariable authSession
if(!$result.Success){
throw $result.Message
}
return $authSession
}
function Invoke-CLCRestMethod {
[CmdletBinding()]
param(
[parameter(Mandatory=$true)]
[string]$Path,
[Object]$JsonValues,
[String]$LocationAlias
)
Assert-Authentication
$body = $JsonValues | ConvertTo-Json
$uri = "https://$($script:UriHost)/$Path"
if($LocationAlias) {
$uri = $uri.Replace("https://api.", "https://api-$LocationAlias.")
}
Write-Verbose "Sending $body to $uri"
$result = Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -body $body -WebSession $script:AuthSession
if(!$result.Success){
if($result.StatusCode -eq 100){
Assert-Authentication -Force
$result = Invoke-RestMethod -Method Post -Uri $uri -ContentType "application/json" -body $body -WebSession $script:AuthSession
}
else{
throw $result.Message
}
}
return $result
}
function New-CLCServer {
[CmdletBinding()]
param(
[string]$AccountAlias,
[string]$LocationAlias,
[parameter(Mandatory=$true)]
[string]$Template,
[parameter(Mandatory=$true)]
[string]$Alias,
[string]$Description,
[parameter(Mandatory=$true)]
[int]$HardwareGroupID,
[int]$ServerType = 1,
[int]$ServiceLevel = 2,
[int]$Cpu = 2,
[int]$MemoryGB = 4,
[int]$ExtraDriveGB = 0,
[string]$PrimaryDns,
[string]$SecondaryDns,
[parameter(Mandatory=$true)]
[string]$Network,
[string]$Password
)
if(!$PSBoundParameters.ContainsKey("ServerType")) { $PSBoundParameters.ServerType = $ServerType}
if(!$PSBoundParameters.ContainsKey("ServiceLevel")) { $PSBoundParameters.ServiceLevel = $ServiceLevel}
if(!$PSBoundParameters.ContainsKey("Cpu")) { $PSBoundParameters.Cpu = $Cpu}
if(!$PSBoundParameters.ContainsKey("MemoryGB")) { $PSBoundParameters.MemoryGB = $MemoryGB}
if(!$PSBoundParameters.ContainsKey("ExtraDriveGB")) { $PSBoundParameters.ExtraDriveGB = $ExtraDriveGB}
Invoke-CLCRestMethod -Path $CLCRoutes.CreateServer -JsonValues $PSBoundParameters
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment