Created
June 27, 2018 10:58
-
-
Save manzanit0/9ce60b9fb8e7e762356311f81033b80a to your computer and use it in GitHub Desktop.
Sandbox manager to create and refresh sandboxes.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SandboxInfo | |
{ | |
public SandboxInfo() | |
{ | |
AutoActivate = true; | |
LicenseType = "DEVELOPER"; | |
Description = "Sandbox created via API"; | |
} | |
public string SandboxName { get; set; } | |
public string Status { get; set; } | |
public string Description { get; set; } | |
public string LicenseType { get; set; } | |
public string ApexClassId { get; set; } | |
public bool AutoActivate { get; set; } | |
public string ToJSON() | |
{ | |
return $@" | |
{{ | |
""AutoActivate"": {AutoActivate.ToString().ToLower()}, | |
""SandboxName"": ""{SandboxName}"", | |
""Description"": ""{Description}"", | |
""LicenseType"": ""{LicenseType}"", | |
""ApexClassId"": ""{ApexClassId}"" | |
}}"; | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SandboxManager | |
{ | |
private const string SANDBOX_INFO_ENDPOINT = | |
"/services/data/v42.0/tooling/sobjects/SandboxInfo/"; | |
private const string SANDBOX_PROCESS_ENDPOINT = | |
"/services/data/v42.0/tooling/sobjects/SandboxProcess/"; | |
private const string QUERY_SANDBOX_INFO = | |
"/services/data/v42.0/tooling/query?q=SELECT+Id,SandboxName+FROM+SandboxInfo+WHERE+SandboxName+in+('{name}')"; | |
private const string QUERY_SANDBOX_PROCESS = | |
"/services/data/v42.0/tooling/query?q=SELECT+Id,Status+FROM+SandboxProcess+WHERE+SandboxName+in+('{name}')"; | |
public SandboxManager(ForceClient client) | |
{ | |
Client = client; | |
} | |
private ForceClient Client { get; } | |
public void CreateSandbox(SandboxInfo info) | |
{ | |
var response = Client.Post(SANDBOX_INFO_ENDPOINT, info.ToJSON()); | |
var stringResponse = response.Content.ReadAsStringAsync().Result; | |
Console.WriteLine("\n:: Sandbox created :: \n" + stringResponse); | |
} | |
public void RefreshSandbox(SandboxInfo info) | |
{ | |
var response = Client.Get(QUERY_SANDBOX_INFO.Replace("{name}", info.SandboxName)); | |
var stringResponse = response.Content.ReadAsStringAsync().Result; | |
Console.WriteLine("\n:: Queried Sandbox :: \n" + stringResponse); | |
dynamic queriedSandbox = JObject.Parse(stringResponse); | |
string id = queriedSandbox.records[0].Id; | |
response = Client.Patch(SANDBOX_INFO_ENDPOINT + $"{id}/", info.ToJSON()); | |
stringResponse = response.Content.ReadAsStringAsync().Result; | |
Console.WriteLine("\n:: Refreshed Sandbox :: \n" + stringResponse); | |
} | |
public string GetSandboxStatus(SandboxInfo info) | |
{ | |
var response = Client.Get(QUERY_SANDBOX_PROCESS.Replace("{name}", info.SandboxName)); | |
var stringResponse = response.Content.ReadAsStringAsync().Result; | |
dynamic sandboxes = JObject.Parse(stringResponse); | |
foreach (var record in sandboxes.records) | |
if (record.Status == info.Status) | |
return Client.Get("" + record.attributes.url).Content.ReadAsStringAsync().Result; | |
return $@"{{""Message"": ""A sandbox with the name {info.SandboxName} and status { | |
info.Status | |
} does not exist.""}}"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment