public async Task<string> SubstituteTokensInString(RuntimeContext runtimeContext, TaskContext taskContext, string input)
{
string output = input;
var matches = ImageRegex.Matches(input);
if (matches.Count > 0)
{
foreach (Match match in matches)
{
string imageType = match.Groups[1].Value;
string location = match.Groups[2].Value;
string imageName = match.Groups[3].Value;
using (var client = HttpClientFactory.GetClient(runtimeContext.AccountDetails, "text/xml"))
{
var url = (imageType == "customerImage")
? string.Format(ImageUrl, runtimeContext.AccountDetails.OrgId, imageName)
: string.Format(ImageUrl, "base", imageName);
var response = await client.GetAsync(runtimeContext.AccountDetails.BaseUrl + url);
response.ThrowForHttpFailure();
var responseBody = await response.Content.ReadAsStringAsync();
var document = XDocument.Parse(responseBody);
var imageId = document.Root
.Elements(ServerNamespace + "image")
.Where(e => e.Attribute("location").Value == location)
.Select(e => e.Attribute("id").Value)
.FirstOrDefault();
if (imageId == null)
{
throw new TemplateParserException($"Image '{imageName}' not found in datacenter '{location}'.");
}
output = output.Replace(match.Groups[0].Value, imageId);
}
}
}
return output;
}
}