Here are the examples of the csharp api Newtonsoft.Json.Linq.JToken.Parse(string, Newtonsoft.Json.Linq.JsonLoadSettings) taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.
681 Examples
19
View Source File : GateIoFuturesServer.cs
License : Apache License 2.0
Project Creator : AlexWan
License : Apache License 2.0
Project Creator : AlexWan
public override void SendOrder(Order order)
{
decimal outputVolume = order.Volume;
if (order.Side == Side.Sell)
outputVolume = -1 * order.Volume;
CreateOrderRequst jOrder = new CreateOrderRequst()
{
Contract = order.SecurityNameCode,
Iceberg = 0,
Price = order.Price.ToString(CultureInfo.InvariantCulture),
Size = Convert.ToInt64(outputVolume),
Tif = "gtc",
Text = $"t-{order.NumberUser}"
};
string bodyContent = JsonConvert.SerializeObject(jOrder).Replace(" ", "").Replace(Environment.NewLine, "");
string timeStamp = TimeManager.GetUnixTimeStampSeconds().ToString();
var headers = new Dictionary<string, string>();
headers.Add("Timestamp", timeStamp);
headers.Add("KEY", _publicKey);
headers.Add("SIGN", _signer.GetSignStringRest("POST", _path + _wallet + "/orders", "", bodyContent, timeStamp));
var result = _requestREST.SendPostQuery("POST", _host + _path + _wallet, "/orders", Encoding.UTF8.GetBytes(bodyContent), headers);
CreateOrderResponse orderResponse = JsonConvert.DeserializeObject<CreateOrderResponse>(result);
if (orderResponse.Status == "finished")
{
SendLogMessage($"Order num {order.NumberUser} on exchange.", LogMessageType.Trade);
}
else if (orderResponse.Status == "open")
{
SendLogMessage($"Order num {order.NumberUser} wait to execution on exchange.", LogMessageType.Trade);
}
else
{
//err_msg
dynamic errorData = JToken.Parse(result);
string errorMsg = errorData.err_msg;
SendLogMessage($"Order exchange error num {order.NumberUser} : {errorMsg}", LogMessageType.Error);
order.State = OrderStateType.Fail;
OnOrderEvent(order);
}
}
19
View Source File : GateIoServer.cs
License : Apache License 2.0
Project Creator : AlexWan
License : Apache License 2.0
Project Creator : AlexWan
public override void SendOrder(Order order)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("{0}={1}", "currencyPair", order.SecurityNameCode.ToLower());
builder.Append("&");
builder.AppendFormat("{0}={1}", "rate", order.Price);
builder.Append("&");
builder.AppendFormat("{0}={1}", "amount", order.Volume);
builder.Append("&");
builder.AppendFormat("{0}={1}", "text", "t-" + order.NumberUser);
var headers = new Dictionary<string, string>();
headers.Add("Key", _publicKey);
headers.Add("Sign", SingRestData(_secretKey, builder.ToString()));
var data = Encoding.UTF8.GetBytes(builder.ToString());
var endPoint = order.Side == Side.Buy ? "buy" : "sell";
var result = _restChannel.SendPostQuery(PrivateRestUri, endPoint, data, headers);
var jt = JToken.Parse(result);
var responseData = jt["result"].ToString();
if (responseData == "false")
{
order.State = OrderStateType.Fail;
OnOrderEvent(order);
SendLogMessage("Error placing order: you have insufficient funds", LogMessageType.Trade);
}
}
19
View Source File : GateIoServer.cs
License : Apache License 2.0
Project Creator : AlexWan
License : Apache License 2.0
Project Creator : AlexWan
public override void CancelOrder(Order order)
{
StringBuilder builder = new StringBuilder();
builder.AppendFormat("{0}={1}", "orderNumber", order.NumberMarket);
builder.Append("&");
builder.AppendFormat("{0}={1}", "currencyPair", order.SecurityNameCode.ToLower());
builder.Append("&");
builder.AppendFormat("{0}={1}", "text", "t-" + order.NumberUser);
var headers = new Dictionary<string, string>();
headers.Add("Key", _publicKey);
headers.Add("Sign", SingRestData(_secretKey, builder.ToString()));
var data = Encoding.UTF8.GetBytes(builder.ToString());
var endPoint = "cancelOrder";
var result = _restChannel.SendPostQuery(PrivateRestUri, endPoint, data, headers);
var jt = JToken.Parse(result);
var responseData = jt["result"].ToString();
if (responseData == "false")
{
SendLogMessage(jt["message"].ToString(), LogMessageType.Error);
}
}
19
View Source File : KrakenClient.cs
License : Apache License 2.0
Project Creator : AlexWan
License : Apache License 2.0
Project Creator : AlexWan
public void UpdatePositionOnBoard()
{
var balances = _kraken.GetBalance();
// var ret2 = JsonConvert.DeserializeObject<GetTradeBalanceResponse>(tradeBalance.ToString());
var jProperties = JToken.Parse(balances.ToString()).SelectToken("result");//.Children<JProperty>();
var children = jProperties.Children();
foreach (var portfolio in children)
{
string res = portfolio.ToString().Replace("{", "").Replace("}", "").Replace("\"", "").Replace(" ", "");
string[] resArr = res.Split(':');
PositionOnBoard position = new PositionOnBoard();
position.PortfolioName = "KrakenTradePortfolio";
position.SecurityNameCode = resArr[0];
position.ValueCurrent = resArr[1].ToDecimal();
position.ValueBegin = resArr[1].ToDecimal();
_myPortfolio.SetNewPosition(position);
}
if (NewPortfolioEvent != null)
{
NewPortfolioEvent(_myPortfolio);
}
}
19
View Source File : SongDownloader.cs
License : MIT License
Project Creator : andruzzzhka
License : MIT License
Project Creator : andruzzzhka
public IEnumerator RequestSongByLevelIDCoroutine(string levelId, Action<Song> callback)
{
UnityWebRequest wwwId = GetRequestForUrl($"{Config.Instance.BeatSaverURL}/api/maps/by-hash/" + levelId.ToLower());
wwwId.timeout = 10;
yield return wwwId.SendWebRequest();
if (wwwId.isNetworkError || wwwId.isHttpError)
{
Plugin.log.Error($"Unable to fetch song by hash! {wwwId.error}\nURL:" + $"{Config.Instance.BeatSaverURL}/api/maps/by-hash/" + levelId.ToLower());
}
else
{
JObject jNode = JObject.Parse(wwwId.downloadHandler.text);
if (jNode.Children().Count() == 0)
{
Plugin.log.Error($"Song {levelId} doesn't exist on BeatSaver!");
callback?.Invoke(null);
yield break;
}
Song _tempSong = Song.FromSearchNode(jNode);
callback?.Invoke(_tempSong);
}
}
19
View Source File : SongDownloader.cs
License : MIT License
Project Creator : andruzzzhka
License : MIT License
Project Creator : andruzzzhka
public IEnumerator RequestSongByKeyCoroutine(string key, Action<Song> callback)
{
UnityWebRequest wwwId = GetRequestForUrl($"{Config.Instance.BeatSaverURL}/api/maps/detail/" + key.ToLower());
wwwId.timeout = 10;
yield return wwwId.SendWebRequest();
if (wwwId.isNetworkError || wwwId.isHttpError)
{
Plugin.log.Error(wwwId.error);
}
else
{
JObject node = JObject.Parse(wwwId.downloadHandler.text);
Song _tempSong = new Song((JObject)node, false);
callback?.Invoke(_tempSong);
}
}
19
View Source File : Anki.cs
License : MIT License
Project Creator : AnkiTools
License : MIT License
Project Creator : AnkiTools
private void ReadApkgFile(string path)
{
if (File.Exists(Path.Combine(_path, "collection.db")))
File.Delete(Path.Combine(_path, "collection.db"));
if (File.Exists(Path.Combine(_path, "media")))
File.Delete(Path.Combine(_path, "media"));
ZipFile.ExtractToDirectory(path, _path);
string anki2File = Path.Combine(_path, "collection.anki2");
File.Move(anki2File, _collectionFilePath);
_conn = new SQLiteConnection(@"Data Source=" + _collectionFilePath + ";Version=3;");
try
{
_conn.Open();
Mapper mapper = Mapper.Instance;
var cardMetadatas = Mapper.MapSQLiteReader(_conn, "SELECT id, mod, type, queue, due, ivl, factor, reps, lapses, left, odue, odid FROM cards");
foreach (var cardMetadata in cardMetadatas)
{
_cardsMetadatas.Enqueue(cardMetadata.ToObject<CardMetadata>());
}
SQLiteDataReader reader = SQLiteHelper.ExecuteSQLiteCommandRead(_conn, "SELECT notes.flds, notes.mid FROM notes");
List<double> mids = new List<double>();
string[] splitted = null;
List<string[]> result = new List<string[]>();
while (reader.Read())
{
splitted = reader.GetString(0).Split('\x1f');
var currentMid = reader.GetInt64(1);
if (mids.Contains(currentMid) == false)
mids.Add(currentMid);
result.Add(splitted);
}
reader.Close();
reader = SQLiteHelper.ExecuteSQLiteCommandRead(_conn, "SELECT models FROM col");
JObject models = null;
while (reader.Read())
{
models = JObject.Parse(reader.GetString(0));
}
AddFields(models, mids);
reader.Close();
var revLogMetadatas = Mapper.MapSQLiteReader(_conn, "SELECT * FROM revlog");
foreach (var revLogMetadata in revLogMetadatas)
{
_revLogMetadatas.Add(revLogMetadata.ToObject<RevLogMetadata>());
}
foreach (var res in result)
{
AddItem(res);
}
}
catch (Exception e)
{
throw new Exception(e.Message);
}
finally
{
_conn.Close();
_conn.Dispose();
SQLiteConnection.ClearAllPools();
}
}
19
View Source File : TestHelperExtensions.cs
License : Apache License 2.0
Project Creator : AppMetrics
License : Apache License 2.0
Project Creator : AppMetrics
public static JToken ParseAsJson(this string json)
{
return JToken.Parse(json, new JsonLoadSettings { LineInfoHandling = LineInfoHandling.Ignore, CommentHandling = CommentHandling.Ignore });
}
19
View Source File : D365WebAPIHelper.cs
License : MIT License
Project Creator : arafattehsin
License : MIT License
Project Creator : arafattehsin
public async static Task<string> GenerateLead(Prospect prospect, CRMCredentials crmUser)
{
HttpMessageHandler messageHandler = null;
Version webAPIVersion = new Version(9, 0);
//One message handler for OAuth authentication, and the other for Windows integrated
// authentication. (replacedumes that HTTPS protocol only used for CRM Online.)
if (crmUser.ServiceUrl.StartsWith("https://"))
{
messageHandler = new OAuthMessageHandler(crmUser.ServiceUrl, crmUser.ClientID, crmUser.RedirectUrl, crmUser.AuthpointEnd, crmUser.Key,
new HttpClientHandler());
}
try
{
//Create an HTTP client to send a request message to the CRM Web service.
using (HttpClient httpClient = new HttpClient(messageHandler))
{
//Specify the Web API address of the service and the period of time each request
// has to execute.
httpClient.BaseAddress = new Uri(crmUser.ServiceUrl);
httpClient.Timeout = new TimeSpan(0, 2, 0); //2 minutes
httpClient.DefaultRequestHeaders.Add("OData-MaxVersion", "4.0");
httpClient.DefaultRequestHeaders.Add("OData-Version", "4.0");
httpClient.DefaultRequestHeaders.Accept.Add(
new MediaTypeWithQualityHeaderValue("application/json"));
// Get the current userId of the service account (owner incase of Lead)
HttpResponseMessage whoAmIResponse = await httpClient.GetAsync("api/data/v9.0/WhoAmI");
Guid userId;
if (whoAmIResponse.IsSuccessStatusCode)
{
JObject jWhoAmIResponse =
JObject.Parse(whoAmIResponse.Content.ReadreplacedtringAsync().Result);
userId = (Guid)jWhoAmIResponse["UserId"];
// Populate the fields of prospect customer (lead)
JObject lead = new JObject();
lead.Add("subject", "Lead from the Event Bot");
lead.Add("lastname", prospect.Name);
lead.Add("mobilephone", prospect.PhoneNumber);
lead.Add("emailaddress1", prospect.Email);
lead.Add("[email protected]", $"/systemusers({userId})");
// Create request and response
HttpRequestMessage createRequest = new HttpRequestMessage(HttpMethod.Post, "api/data/v9.0/leads");
createRequest.Content = new StringContent(lead.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage createResponse = await httpClient.SendAsync(createRequest);
// Check if the status is good to go (204)
if (createResponse.StatusCode == HttpStatusCode.NoContent)
{
return Helpers.Common.GenerateReferenceID(6);
}
else
{
return $"Failed to register {createResponse.StatusCode}";
}
}
else
return $"Failed - {whoAmIResponse.StatusCode}";
}
}
catch (Exception ex)
{
throw ex;
}
}
19
View Source File : AddonValidator.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private bool ValidateJsonFiles(C3Addon addon)
{
var isValid = true;
if (addon.Type != PluginType.Effect && addon.Type != PluginType.Theme)
{
//addon.json
isValid = TryAction(() => JObject.Parse(addon.AddonJson));
if (!isValid) { LogManager.CompilerLog.Insert("failed validation on addon.json"); return false; } else { LogManager.CompilerLog.Insert("addon.json is valid json"); }
//validate plugin.js edittime
if (addon.PluginEditTime.Contains("this._info.AddFileDependency()"))
{
LogManager.CompilerLog.Insert("file dependency in plugin.js cannot have empty params");
return false;
}
//aces.json
foreach (var action in addon.Actions.Values)
{
isValid = TryAction(() => JObject.Parse(action.Ace));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on action : {action.Id} ace.json"); return false; } else { LogManager.CompilerLog.Insert($"action : {action.Id} ace.json is valid json"); }
isValid = TryAction(() => FormatHelper.Insatnce.Json(action.Language, true));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on action : {action.Id} lang.json"); return false; } else { LogManager.CompilerLog.Insert($"action : {action.Id} lang.json is valid json"); }
//variadic param type warning
if (action.Ace.Contains("\"type\": \"variadic\""))
{
LogManager.CompilerLog.Insert($"WARNING _ {action.Id} is using a variadic parameter, this feature is not doreplacedented in the C3 SDK => USE AT YOUR OWN RISK!");
}
}
foreach (var condition in addon.Conditions.Values)
{
isValid = TryAction(() => JObject.Parse(condition.Ace));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on condition : {condition.Id} ace.json"); return false; } else { LogManager.CompilerLog.Insert($"condition : {condition.Id} ace.json is valid json"); }
isValid = TryAction(() => FormatHelper.Insatnce.Json(condition.Language, true));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on condition : {condition.Id} lang.json"); return false; } else { LogManager.CompilerLog.Insert($"condition : {condition.Id} lang.json is valid json"); }
}
foreach (var expression in addon.Expressions.Values)
{
isValid = TryAction(() => JObject.Parse(expression.Ace));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on expression : {expression.Id} ace.json"); return false; } else { LogManager.CompilerLog.Insert($"expression : {expression.Id} ace.json is valid json"); }
isValid = TryAction(() => FormatHelper.Insatnce.Json(expression.Language, true));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on expression : {expression.Id} lang.json"); return false; } else { LogManager.CompilerLog.Insert($"expression : {expression.Id} lang.json is valid json"); }
}
//property lang
isValid = TryAction(() => FormatHelper.Insatnce.Json(addon.LanguageProperties, true));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on language properties json"); return false; } else { LogManager.CompilerLog.Insert($"language properties json is valid json"); }
//property categories
isValid = TryAction(() => FormatHelper.Insatnce.Json(addon.LanguageCategories, true));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on language categories json"); return false; } else { LogManager.CompilerLog.Insert($"language categories json is valid json"); }
}
else
{
foreach (var parameter in addon.Effect.Parameters.Values)
{
isValid = TryAction(() => JObject.Parse(parameter.Json));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on effect param : {parameter.Key} addon.json"); return false; } else { LogManager.CompilerLog.Insert($"effect param : {parameter.Key}, addon.json is valid json"); }
isValid = TryAction(() => FormatHelper.Insatnce.Json(parameter.Lang, true));
if (!isValid) { LogManager.CompilerLog.Insert($"failed validation on effect param : {parameter.Key} lang.json"); return false; } else { LogManager.CompilerLog.Insert($"effect param : {parameter.Key} lang.json, is valid json"); }
}
}
return true;
}
19
View Source File : AceParameterHelper.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
public Models.Condition GenerateParam(Models.Condition cnd, string id, string type, string value, string name, string desc, List<string> comboItems)
{
if (cnd.Ace.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value, comboItems);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc, comboItems);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {cnd.Language} }}")[cnd.Id];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(cnd.Code, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(cnd.Code, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, cnd.ScriptName, paramList);
//updates
cnd.Language = $"\"{cnd.Id}\": {langJson.ToString(formatting: Formatting.Indented)} ";
cnd.Ace = FormatHelper.Insatnce.Json(Regex.Replace(cnd.Ace, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
cnd.Code = cnd.Code.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value, comboItems);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc, comboItems);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, cnd.ScriptName);
//updates
cnd.Language = cnd.Language.Replace(@"""
}", langTemplate);
cnd.Ace = FormatHelper.Insatnce.Json(cnd.Ace.Replace("}", aceTemplate));
cnd.Code = cnd.Code.Replace($"{cnd.ScriptName}()", codeTemplate);
}
return cnd;
}
19
View Source File : AddonWindow.xaml.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private void Tab_ChangedEvent(object sender, SelectionChangedEventArgs e)
{
if (AddonJsTab.IsSelected)
{
////save current selection
if (_selectedFile != null)
{
//only update content for readable text files
switch (_selectedFile.Extention?.ToLower() ?? ".txt")
{
case ".js":
case ".html":
case ".css":
case ".txt":
case ".json":
case ".xml":
_selectedFile.Content = FileTextEditor.Text;
break;
default:
_selectedFile.Content = $"BINARY FILE => {_selectedFile.FileName}\nBYTE LENGTH : ({_selectedFile.Bytes.Length})";
break;
}
_selectedFile.C2Folder = C2RuntimeFolder.IsChecked != null && C2RuntimeFolder.IsChecked.Value;
_selectedFile.C3Folder = C3RuntimeFolder.IsChecked != null && C3RuntimeFolder.IsChecked.Value;
_selectedFile.Rootfolder = RootFolder.IsChecked != null && RootFolder.IsChecked.Value;
_selectedFile.Domfolder = DomScript.IsChecked != null && DomScript.IsChecked.Value;
//_selectedFile.Bytes = Encoding.ASCII.GetBytes(FileTextEditor.Text);
_selectedFile.FileType = FileTypeDropDown.Text;
_selectedFile.PluginTemplate = TemplateHelper.ThirdPartyFile(_selectedFile);
_selectedFile.Compress = CompressFile.IsChecked != null && CompressFile.IsChecked.Value;
_selectedFile.PlainText = PlainText.IsChecked != null && PlainText.IsChecked.Value;
}
//update addon.json
try
{
var addon = JObject.Parse(AddonTextEditor.Text);
var fileList = JArray.Parse(addon["file-list"].ToString());
foreach (var thirdPartyFile in _files)
{
//remove all checks
foreach (var item in fileList.Children().ToList())
{
if (item.ToString().Equals("c3runtime/" + thirdPartyFile.FileName.Replace("\\", "/")) ||
item.ToString().Equals("c2runtime/" + thirdPartyFile.FileName.Replace("\\", "/")) ||
item.ToString().Equals(thirdPartyFile.FileName.Replace("\\", "/")))
{
fileList.Remove(item);
}
}
if (thirdPartyFile.C3Folder) fileList.Add("c3runtime/" + thirdPartyFile.FileName.Replace("\\", "/"));
if (thirdPartyFile.C2Folder) fileList.Add("c2runtime/" + thirdPartyFile.FileName.Replace("\\", "/"));
if (thirdPartyFile.Rootfolder) fileList.Add(thirdPartyFile.FileName.Replace("\\", "/"));
addon["file-list"] = fileList;
}
AddonTextEditor.Text = addon.ToString(Formatting.Indented);
}
catch (Exception ex)
{
LogManager.AddErrorLog(ex);
NotificationManager.PublishErrorNotification($"error parseing json, addon.json not updated => {ex.Message}");
}
}
}
19
View Source File : AceParameterHelper.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
public Models.Action GenerateParam(Models.Action action, string id, string type, string value, string name, string desc)
{
var isVariadic = type == "variadic";
if (action.Ace.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {action.Language} }}")[action.Id];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(action.Code, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(action.Code, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, action.ScriptName, isVariadic, paramList);
//updates
action.Language = $"\"{action.Id}\": {langJson.ToString(formatting: Formatting.Indented)} ";
action.Ace = FormatHelper.Insatnce.Json(Regex.Replace(action.Ace, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
action.Code = action.Code.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, action.ScriptName, isVariadic);
//updates
action.Language = action.Language.Replace(@"""
}", langTemplate);
action.Ace = FormatHelper.Insatnce.Json(action.Ace.Replace("}", aceTemplate));
action.Code = action.Code.Replace($"{action.ScriptName}()", codeTemplate);
}
return action;
}
19
View Source File : AceParameterHelper.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
public Models.Action GenerateParam(Models.Action action, string id, string type, string value, string name, string desc, List<string> comboItems)
{
var isVariadic = type == "variadic";
if (action.Ace.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value, comboItems);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc, comboItems);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {action.Language} }}")[action.Id];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(action.Code, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(action.Code, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, action.ScriptName, isVariadic, paramList);
//updates
action.Language = $"\"{action.Id}\": {langJson.ToString(formatting: Formatting.Indented)} ";
action.Ace = FormatHelper.Insatnce.Json(Regex.Replace(action.Ace, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
action.Code = action.Code.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value, comboItems);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc, comboItems);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, action.ScriptName, isVariadic);
//updates
action.Language = action.Language.Replace(@"""
}", langTemplate);
action.Ace = FormatHelper.Insatnce.Json(action.Ace.Replace("}", aceTemplate));
action.Code = action.Code.Replace($"{action.ScriptName}()", codeTemplate);
}
return action;
}
19
View Source File : ActionWindow.xaml.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private void SaveParamButton_Click(object sender, RoutedEventArgs e)
{
try
{
var id = ParamIdText.Text.ToLower().Replace(" ", "-");
var type = ParamTypeDropdown.Text;
var value = ParamValueText.Text;
var name = ParamNameText.Text;
var desc = ParamDescText.Text.Replace("\"", "\\\""); ;
var isVariadic = type == "variadic";
var actionId = _selectedAction.Id;
//todo: duplicated across all aces and AceParameterHelper, need to find better way to consolidate those
//there is at least one param defined
if (AceTextEditor.Text.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {LanguageTextEditor.Text} }}")[actionId];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(CodeTextEditor.Text, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(CodeTextEditor.Text, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, _selectedAction.ScriptName, isVariadic, paramList);
//updates
LanguageTextEditor.Text = $"\"{actionId}\": {langJson.ToString(formatting: Formatting.Indented)} ";
AceTextEditor.Text = FormatHelper.Insatnce.Json(Regex.Replace(AceTextEditor.Text, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
CodeTextEditor.Text = CodeTextEditor.Text.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, _selectedAction.ScriptName, isVariadic);
//updates
LanguageTextEditor.Text = LanguageTextEditor.Text.Replace(@"""
}", langTemplate);
AceTextEditor.Text = FormatHelper.Insatnce.Json(AceTextEditor.Text.Replace("}", aceTemplate));
CodeTextEditor.Text = CodeTextEditor.Text.Replace($"{_selectedAction.ScriptName}()", codeTemplate);
}
}
catch (Exception ex)
{
LogManager.AddErrorLog(ex);
NotificationManager.PublishErrorNotification($"error adding parameter : {ex.Message}");
}
NewParamWindow.IsOpen = false;
}
19
View Source File : AceParameterHelper.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
public Models.Condition GenerateParam(Models.Condition cnd, string id, string type, string value, string name, string desc)
{
if (cnd.Ace.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {cnd.Language} }}")[cnd.Id];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(cnd.Code, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(cnd.Code, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, cnd.ScriptName, paramList);
//updates
cnd.Language = $"\"{cnd.Id}\": {langJson.ToString(formatting: Formatting.Indented)} ";
cnd.Ace = FormatHelper.Insatnce.Json(Regex.Replace(cnd.Ace, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
cnd.Code = cnd.Code.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, cnd.ScriptName);
//updates
cnd.Language = cnd.Language.Replace(@"""
}", langTemplate);
cnd.Ace = FormatHelper.Insatnce.Json(cnd.Ace.Replace("}", aceTemplate));
cnd.Code = cnd.Code.Replace($"{cnd.ScriptName}()", codeTemplate);
}
return cnd;
}
19
View Source File : ExpressionWindow.xaml.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private void SaveParamButton_Click(object sender, RoutedEventArgs e)
{
try
{
var id = ParamIdText.Text.ToLower().Replace(" ", "-");
var type = ParamTypeDropdown.Text;
var value = ParamValueText.Text;
var name = ParamNameText.Text;
var desc = ParamDescText.Text.Replace("\"", "\\\"");
var expressionId = _selectedExpression.Id;
//there is at least one param defined
if (AceTextEditor.Text.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {LanguageTextEditor.Text} }}")[expressionId];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(CodeTextEditor.Text, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(CodeTextEditor.Text, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, _selectedExpression.ScriptName, paramList);
//updates
LanguageTextEditor.Text = $"\"{expressionId}\": {langJson.ToString(formatting: Formatting.Indented)} ";
AceTextEditor.Text = FormatHelper.Insatnce.Json(Regex.Replace(AceTextEditor.Text, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
CodeTextEditor.Text = CodeTextEditor.Text.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, _selectedExpression.ScriptName);
//updates
LanguageTextEditor.Text = LanguageTextEditor.Text.Replace(@"""
}", langTemplate);
AceTextEditor.Text = FormatHelper.Insatnce.Json(AceTextEditor.Text.Replace("}", aceTemplate));
CodeTextEditor.Text = CodeTextEditor.Text.Replace($"{_selectedExpression.ScriptName}()", codeTemplate);
}
}
catch (Exception ex)
{
LogManager.AddErrorLog(ex);
}
NewParamWindow.IsOpen = false;
}
19
View Source File : AceParameterHelper.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
public Models.Expression GenerateParam(Models.Expression exp, string id, string type, string value, string name, string desc)
{
if (exp.Ace.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {exp.Language} }}")[exp.Id];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(exp.Code, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(exp.Code, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, exp.ScriptName, paramList);
//updates
exp.Language = $"\"{exp.Id}\": {langJson.ToString(formatting: Formatting.Indented)} ";
exp.Ace = FormatHelper.Insatnce.Json(Regex.Replace(exp.Ace, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
exp.Code = exp.Code.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, exp.ScriptName);
//updates
exp.Language = exp.Language.Replace(@"""
}", langTemplate);
exp.Ace = FormatHelper.Insatnce.Json(exp.Ace.Replace("}", aceTemplate));
exp.Code = exp.Code.Replace($"{exp.ScriptName}()", codeTemplate);
}
return exp;
}
19
View Source File : AddonWindow.xaml.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private async void AddFile_OnClick(object sender, RoutedEventArgs e)
{
var filename = await WindowManager.ShowInputDialog("New File Name?", "please enter the name for the new javascript file", "javascriptFile.js");
if (string.IsNullOrWhiteSpace(filename)) return;
var tpf = new ThirdPartyFile
{
Content = string.Empty,
FileName = filename,
C3Folder = true,
C2Folder = false,
Rootfolder = false,
FileType = "inline-script",
Compress = false,
PlainText = true,
Domfolder = false
};
tpf.PluginTemplate = TemplateHelper.ThirdPartyFile(tpf);
tpf.Extention = Path.GetExtension(tpf.FileName);
if (string.IsNullOrWhiteSpace(tpf.Extention))
{
tpf.Extention = ".txt";
tpf.FileName = tpf.FileName + tpf.Extention;
}
_files.Add(tpf);
try
{
var addon = JObject.Parse(AddonTextEditor.Text);
var fileList = JArray.Parse(addon["file-list"].ToString());
if (tpf.C3Folder) fileList.Add("c3runtime/" + tpf.FileName);
if (tpf.C2Folder) fileList.Add("c2runtime/" + tpf.FileName);
if (tpf.Rootfolder) fileList.Add(tpf.FileName);
addon["file-list"] = fileList;
AddonTextEditor.Text = addon.ToString(Formatting.Indented);
}
catch (Exception ex)
{
LogManager.AddErrorLog(ex);
NotificationManager.PublishErrorNotification($"error parseing json, addon.json not updated => {ex.Message}");
}
}
19
View Source File : AddonWindow.xaml.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private void RemoveFile_OnClick(object sender, RoutedEventArgs e)
{
if (FileListBox.SelectedIndex == -1)
{
NotificationManager.PublishErrorNotification("failed to remove file, no file selected");
return;
}
_selectedFile = FileListBox.SelectedItem as ThirdPartyFile;
if (_selectedFile != null)
{
var file = _selectedFile;
_files.Remove(_selectedFile);
FileListBox.ItemsSource = _files;
FileListBox.Items.Refresh();
//clear editors
FileTextEditor.Text = string.Empty;
try
{
var addon = JObject.Parse(AddonTextEditor.Text);
var fileList = JArray.Parse(addon["file-list"].ToString());
//remove all checks
foreach (var item in fileList.Children().ToList())
{
if (item.ToString().Equals("c3runtime/" + file.FileName) ||
item.ToString().Equals("c2runtime/" + file.FileName) ||
item.ToString().Equals(file.FileName))
{
fileList.Remove(item);
}
}
addon["file-list"] = fileList;
AddonTextEditor.Text = addon.ToString(Formatting.Indented);
}
catch (Exception ex)
{
LogManager.AddErrorLog(ex);
NotificationManager.PublishErrorNotification($"error parseing json, addon.json not updated => {ex.Message}");
}
_selectedFile = null;
C3RuntimeFolder.IsChecked = false;
C2RuntimeFolder.IsChecked = false;
RootFolder.IsChecked = false;
PlainText.IsChecked = false;
CompressFile.IsChecked = false;
DomScript.IsChecked = false;
}
else
{
NotificationManager.PublishErrorNotification("failed to remove action, no 3rd party files selected");
}
}
19
View Source File : ConditionWindow.xaml.cs
License : GNU General Public License v3.0
Project Creator : armandoalonso
License : GNU General Public License v3.0
Project Creator : armandoalonso
private void SaveParamButton_Click(object sender, RoutedEventArgs e)
{
try
{
var id = ParamIdText.Text.ToLower().Replace(" ", "-");
var type = ParamTypeDropdown.Text;
var value = ParamValueText.Text;
var name = ParamNameText.Text;
var desc = ParamDescText.Text.Replace("\"", "\\\"");
var conditionId = _selectedCondition.Id;
//there is at least one param defined
if (AceTextEditor.Text.Contains("\"params\": ["))
{
//ace param
var aceTemplate = TemplateHelper.AceParam(id, type, value);
//lang param
var langTemplate = TemplateHelper.AceLang(id, type, name, desc);
var newProperty = JObject.Parse(langTemplate);
var langJson = JObject.Parse($"{{ {LanguageTextEditor.Text} }}")[conditionId];
var langParams = langJson["params"];
langParams.Last.AddAfterSelf(newProperty.Property(id));
langJson["params"] = langParams;
//code param
var func = Regex.Match(CodeTextEditor.Text, @"(?:\()(?<param>.*)(?:\))");
var declaration = Regex.Match(CodeTextEditor.Text, @".*(?:\()(?<param>.*)(?:\))").Value;
var paramList = func.Groups["param"].Value.Split(',');
var codeTemplate = TemplateHelper.AceCode(id, _selectedCondition.ScriptName, paramList);
//updates
LanguageTextEditor.Text = $"\"{conditionId}\": {langJson.ToString(formatting: Formatting.Indented)} ";
AceTextEditor.Text = FormatHelper.Insatnce.Json(Regex.Replace(AceTextEditor.Text, @"}(\r\n?|\n|\s*)]", $"{aceTemplate}\r\n]"));
CodeTextEditor.Text = CodeTextEditor.Text.Replace(declaration, codeTemplate);
}
//this will be the first param
else
{
//ace param
var aceTemplate = TemplateHelper.AceParamFirst(id, type, value);
//language param
var langTemplate = TemplateHelper.AceLangFirst(id, type, name, desc);
//code param
var codeTemplate = TemplateHelper.AceCodeFirst(id, _selectedCondition.ScriptName);
//updates
LanguageTextEditor.Text = LanguageTextEditor.Text.Replace(@"""
}", langTemplate);
AceTextEditor.Text = FormatHelper.Insatnce.Json(AceTextEditor.Text.Replace("}", aceTemplate));
CodeTextEditor.Text = CodeTextEditor.Text.Replace($"{_selectedCondition.ScriptName}()", codeTemplate);
}
}
catch (Exception ex)
{
LogManager.AddErrorLog(ex);
}
NewParamWindow.IsOpen = false;
}
19
View Source File : AuthServerTests.cs
License : Apache License 2.0
Project Creator : aspnet
License : Apache License 2.0
Project Creator : aspnet
[Theory, Trait("FunctionalTests", "Security")]
[InlineData(HostType.IIS)]
[InlineData(HostType.HttpListener)]
public void Security_AuthorizeEndpointTests(HostType hostType)
{
using (ApplicationDeployer deployer = new ApplicationDeployer())
{
var applicationUrl = deployer.Deploy(hostType, AuthServerHappyPathConfiguration);
IDisposable clientEndpoint = null;
try
{
clientEndpoint = WebApp.Start(Client_Redirect_Uri, app => app.Run(context => { return context.Response.WriteAsync(context.Request.QueryString.Value); }));
string tokenEndpointUri = applicationUrl + "TokenEndpoint";
var basicClient = new HttpClient();
var headerValue = Convert.ToBase64String(Encoding.Default.GetBytes(string.Format("{0}:{1}", "123", "invalid")));
basicClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", headerValue);
HttpClient httpClient = new HttpClient();
string requestUri = null;
Uri landingUri = null;
Uri applicationUri = new Uri(applicationUrl);
HttpResponseMessage httpResponseMessage = null;
//Happy path - response_type:code
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "code", "123", Client_Redirect_Uri, "scope1", "validstate");
landingUri = httpClient.GetAsync(requestUri).Result.RequestMessage.RequestUri;
replacedert.Equal<string>(Client_Redirect_Uri, landingUri.GetLeftPart(UriPartial.Path));
replacedert.NotNull(landingUri.ParseQueryString()["code"]);
replacedert.Equal<string>("validstate", landingUri.ParseQueryString()["state"]);
//Happy path - response_type:token
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "token", "123", Client_Redirect_Uri, "scope1", "validstate");
landingUri = httpClient.GetAsync(requestUri).Result.RequestMessage.RequestUri;
landingUri = new Uri(landingUri.AbsoluteUri.Replace('#', '?'));
replacedert.Equal<string>(Client_Redirect_Uri, landingUri.GetLeftPart(UriPartial.Path));
replacedert.NotNull(landingUri.ParseQueryString()["access_token"]);
replacedert.NotNull(landingUri.ParseQueryString()["expires_in"]);
replacedert.Equal<string>("bearer", landingUri.ParseQueryString()["token_type"]);
replacedert.Equal<string>("validstate", landingUri.ParseQueryString()["state"]);
//Invalid redirect URI - preplaced error to application
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "code", "123", "invalid_uri_preplacedonerror", "scope1", "validstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
replacedert.Equal<string>("error: invalid_request\r\n", httpResponseMessage.Content.ReadreplacedtringAsync().Result);
replacedert.True(httpResponseMessage.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority).StartsWith(applicationUri.GetLeftPart(UriPartial.Authority)), "Should not be redirected on invalid redirect_uri");
//Invalid redirect URI - Display error by middleware
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "code", "123", "invalid_uri_displayerror", "scope1", "validstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
replacedert.True(httpResponseMessage.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority).StartsWith(applicationUri.GetLeftPart(UriPartial.Authority)), "Should not be redirected on invalid redirect_uri");
replacedert.True(httpResponseMessage.Content.ReadreplacedtringAsync().Result.StartsWith("error: invalid_request"), "Did not receive an error for an invalid redirect_uri");
//What happens if we don't set Validated explicitly. Send an invalid clientId => We don't set Validated for this case.
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "token", "invalidClient", Client_Redirect_Uri, "scope1", "validstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
replacedert.True(httpResponseMessage.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority).StartsWith(applicationUri.GetLeftPart(UriPartial.Authority)), "Should not be redirected on invalid redirect_uri");
replacedert.True(httpResponseMessage.Content.ReadreplacedtringAsync().Result.StartsWith("error: invalid_request"), "Did not receive an error for an invalid redirect_uri");
//OnValidateAuthorizeRequest - Rejecting a request. Send an invalid state as we validate it there. Client should receive all the error code & description that we send
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "code", "123", Client_Redirect_Uri, "scope1", "invalidstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
landingUri = httpResponseMessage.RequestMessage.RequestUri;
replacedert.Equal<string>(Client_Redirect_Uri, landingUri.GetLeftPart(UriPartial.Path));
replacedert.Equal<string>("state.invalid", landingUri.ParseQueryString()["error"]);
replacedert.Equal<string>("state.invaliddescription", landingUri.ParseQueryString()["error_description"]);
replacedert.Equal<string>("state.invaliduri", landingUri.ParseQueryString()["error_uri"]);
replacedert.True(httpResponseMessage.Content.ReadreplacedtringAsync().Result.StartsWith("error=state.invalid&error_description=state.invaliddescription&error_uri=state.invaliduri"), "Did not receive an error when provider did not set Validated");
//Missing response_type
requestUri = AuthZ.CreateAuthZUri(applicationUrl, null, "123", Client_Redirect_Uri, "scope1", "validstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
replacedert.Equal<string>(Client_Redirect_Uri, httpResponseMessage.RequestMessage.RequestUri.GetLeftPart(UriPartial.Path));
replacedert.Equal<string>("invalid_request", httpResponseMessage.RequestMessage.RequestUri.ParseQueryString()["error"]);
//Unsupported response_type
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "invalid_response_type", "123", Client_Redirect_Uri, "scope1", "validstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
replacedert.Equal<string>(Client_Redirect_Uri, httpResponseMessage.RequestMessage.RequestUri.GetLeftPart(UriPartial.Path));
replacedert.Equal<string>("unsupported_response_type", httpResponseMessage.RequestMessage.RequestUri.ParseQueryString()["error"]);
//Missing client_id
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "token", null, Client_Redirect_Uri, "scope1", "validstate");
httpResponseMessage = httpClient.GetAsync(requestUri).Result;
replacedert.True(httpResponseMessage.RequestMessage.RequestUri.GetLeftPart(UriPartial.Authority).StartsWith(applicationUri.GetLeftPart(UriPartial.Authority)), "Should not be redirected on invalid redirect_uri");
replacedert.True(httpResponseMessage.Content.ReadreplacedtringAsync().Result.StartsWith("error: invalid_request"), "Did not receive an error for an invalid redirect_uri");
//Missing state - Should succeed
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "code", "123", Client_Redirect_Uri, "scope1", null);
landingUri = httpClient.GetAsync(requestUri).Result.RequestMessage.RequestUri;
replacedert.Equal<string>(Client_Redirect_Uri, landingUri.GetLeftPart(UriPartial.Path));
replacedert.NotNull(landingUri.ParseQueryString()["code"]);
replacedert.Equal<bool>(false, landingUri.ParseQueryString().ContainsKey("state"));
//Token endpoint tests
//Invalid client (client_id, client_secret) - As form parameters
var formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "invalid") });
var responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
var jToken = JToken.Parse(responseMessage);
replacedert.Equal<string>("invalid_client", jToken.SelectToken("error").Value<string>());
//Invalid client (client_id, client_secret) - As Basic auth headers
responseMessage = basicClient.GetAsync(tokenEndpointUri).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.Equal<string>("invalid_client", jToken.SelectToken("error").Value<string>());
//grant_type=authorization_code - invalid code being sent
formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "secret123"), new kvp("grant_type", "authorization_code"), new kvp("code", "InvalidCode"), new kvp("redirect_uri", Client_Redirect_Uri) });
responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.Equal<string>("invalid_grant", jToken.SelectToken("error").Value<string>());
//grant_type=authorization_code - Full scenario
requestUri = AuthZ.CreateAuthZUri(applicationUrl, "code", "123", Client_Redirect_Uri, "scope1", "validstate");
landingUri = httpClient.GetAsync(requestUri).Result.RequestMessage.RequestUri;
replacedert.Equal<string>(Client_Redirect_Uri, landingUri.GetLeftPart(UriPartial.Path));
replacedert.NotNull(landingUri.ParseQueryString()["code"]);
replacedert.Equal<string>("validstate", landingUri.ParseQueryString()["state"]);
formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "secret123"), new kvp("grant_type", "authorization_code"), new kvp("code", landingUri.ParseQueryString()["code"]), new kvp("redirect_uri", Client_Redirect_Uri) });
responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.NotNull(jToken.SelectToken("access_token").Value<string>());
replacedert.Equal<string>("bearer", jToken.SelectToken("token_type").Value<string>());
replacedert.NotNull(jToken.SelectToken("expires_in").Value<string>());
replacedert.Equal<string>("value1", jToken.SelectToken("param1").Value<string>());
replacedert.Equal<string>("value2", jToken.SelectToken("param2").Value<string>());
replacedert.NotNull(jToken.SelectToken("refresh_token").Value<string>());
//grant_type=preplacedword -- Resource owner credentials -- Invalid credentials
formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "secret123"), new kvp("grant_type", "preplacedword"), new kvp("username", "user1"), new kvp("preplacedword", "invalid"), new kvp("scope", "scope1 scope2 scope3") });
responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.Equal<string>("invalid_grant", jToken.SelectToken("error").Value<string>());
//grant_type=preplacedword -- Resource owner credentials
formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "secret123"), new kvp("grant_type", "preplacedword"), new kvp("username", "user1"), new kvp("preplacedword", "preplacedword1"), new kvp("scope", "scope1 scope2 scope3") });
responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.NotNull(jToken.SelectToken("access_token").Value<string>());
replacedert.Equal<string>("bearer", jToken.SelectToken("token_type").Value<string>());
replacedert.NotNull(jToken.SelectToken("expires_in").Value<string>());
replacedert.Equal<string>("value1", jToken.SelectToken("param1").Value<string>());
replacedert.Equal<string>("value2", jToken.SelectToken("param2").Value<string>());
replacedert.NotNull(jToken.SelectToken("refresh_token").Value<string>());
//grant_type=refresh_token -- Use the refresh token issued on the previous call
formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "secret123"), new kvp("grant_type", "refresh_token"), new kvp("refresh_token", jToken.SelectToken("refresh_token").Value<string>()), new kvp("scope", "scope1 scope2") });
responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.NotNull(jToken.SelectToken("access_token").Value<string>());
replacedert.Equal<string>("bearer", jToken.SelectToken("token_type").Value<string>());
replacedert.NotNull(jToken.SelectToken("expires_in").Value<string>());
replacedert.Equal<string>("value1", jToken.SelectToken("param1").Value<string>());
replacedert.Equal<string>("value2", jToken.SelectToken("param2").Value<string>());
replacedert.NotNull(jToken.SelectToken("refresh_token").Value<string>());
//grant_type=client_credentials - Bug# https://github.com/Katana/katana/issues/562
formContent = AuthZ.CreateTokenEndpointContent(new[] { new kvp("client_id", "123"), new kvp("client_secret", "secret123"), new kvp("grant_type", "client_credentials"), new kvp("scope", "scope1 scope2 scope3") });
responseMessage = httpClient.PostAsync(tokenEndpointUri, formContent).Result.Content.ReadreplacedtringAsync().Result;
jToken = JToken.Parse(responseMessage);
replacedert.NotNull(jToken.SelectToken("access_token").Value<string>());
replacedert.Equal<string>("bearer", jToken.SelectToken("token_type").Value<string>());
replacedert.NotNull(jToken.SelectToken("expires_in").Value<string>());
replacedert.Equal<string>("value1", jToken.SelectToken("param1").Value<string>());
replacedert.Equal<string>("value2", jToken.SelectToken("param2").Value<string>());
}
finally
{
//Finally close the client endpoint
if (clientEndpoint != null)
{
clientEndpoint.Dispose();
}
}
}
}
19
View Source File : AssertExtensions.cs
License : Apache License 2.0
Project Creator : AutomateThePlanet
License : Apache License 2.0
Project Creator : AutomateThePlanet
private static void replacedertJsonSchema(IMeasuredResponse response, JSchema jsonSchema)
{
IList<string> messages;
var trimmedContent = response.Content.TrimStart();
bool isSchemaValid =
trimmedContent.StartsWith("{", StringComparison.Ordinal)
? JObject.Parse(response.Content).IsValid(jsonSchema, out messages)
: JArray.Parse(response.Content).IsValid(jsonSchema, out messages);
if (!isSchemaValid)
{
var sb = new StringBuilder();
sb.AppendLine("JSON Schema is not valid. Error Messages:");
foreach (var errorMessage in messages)
{
sb.AppendLine(errorMessage);
}
throw new ApireplacedertException(sb.ToString());
}
}
19
View Source File : MeasuredResponseExtensions.cs
License : Apache License 2.0
Project Creator : AutomateThePlanet
License : Apache License 2.0
Project Creator : AutomateThePlanet
public static dynamic AsDynamicContent(this IMeasuredResponse response)
{
if (response.Request.RequestFormat == DataFormat.Xml)
{
var doc = XDoreplacedent.Parse(response.Content);
var contentToBeConverted = JsonConvert.SerializeXNode(doc);
return contentToBeConverted;
}
dynamic contentAsDynamic = JObject.Parse(response.Content);
return contentAsDynamic;
}
19
View Source File : AllureWorkflowPlugin.cs
License : Apache License 2.0
Project Creator : AutomateThePlanet
License : Apache License 2.0
Project Creator : AutomateThePlanet
private Status GetAllureTestResultOutput(TestOutcome testOutcome, string consoleMessage)
{
if (testOutcome != TestOutcome.Preplaceded)
{
var allureConfiguration = JObject.Parse(_allureLifecycle.Value.JsonConfiguration);
var allureSection = allureConfiguration["allure"];
try
{
var config = allureSection?.ToObject<AllureExtendedConfiguration>();
if (config?.BrokenTestData != null)
{
foreach (var word in config.BrokenTestData)
{
if (consoleMessage.Contains(word))
{
return Status.broken;
}
}
}
}
catch (Exception)
{
// Ignored
}
switch (testOutcome)
{
case TestOutcome.Inconclusive:
return Status.broken;
case TestOutcome.Aborted:
return Status.skipped;
case TestOutcome.Preplaceded:
return Status.preplaceded;
case TestOutcome.Error:
return Status.broken;
case TestOutcome.Failed:
return Status.failed;
default:
return Status.none;
}
}
return Status.preplaceded;
}
19
View Source File : EMFPipeTests.cs
License : Apache License 2.0
Project Creator : awslabs
License : Apache License 2.0
Project Creator : awslabs
[Fact]
public void ConvertsIISLogs()
{
var logs = new string[]
{
"2017-05-31 06:00:30 W3SVC1 EC2AMAZ-HCNHA1G 10.10.10.10 POST /DoWork - 443 EXAMPLE\\jonsmith 11.11.11.11 HTTP/1.1 SEA-HDFEHW23455/1.0.9/jonsmith - - localhost 500 2 0 1950 348 158",
"2017-05-31 06:00:30 W3SVC1 EC2AMAZ-HCNHA1G ::1 GET / - 80 - ::1 HTTP/1.1 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko - - localhost 200 0 0 950 348 128",
"2017-05-31 06:00:30 W3SVC1 EC2AMAZ-HCNHA1G ::1 GET / - 80 - ::1 HTTP/1.1 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko - - localhost 401 1 0 50 348 150",
"2017-05-31 06:00:30 W3SVC1 EC2AMAZ-HCNHA1G ::1 GET / - 80 - ::1 HTTP/1.1 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko - - localhost 503 7 0 550 348 192",
"2017-05-31 06:00:30 W3SVC1 EC2AMAZ-HCNHA1G ::1 GET /iisstart.png - 80 - ::1 HTTP/1.1 Mozilla/5.0+(Windows+NT+10.0;+WOW64;+Trident/7.0;+rv:11.0)+like+Gecko - http://localhost/ localhost 200 0 0 99960 317 3"
};
var config = TestUtility.GetConfig("Pipes", "IISEMFTestPipe");
var context = new PluginContext(config, NullLogger.Instance, null);
var sink = new MockEventSink(context);
context.ContextData[PluginContext.SINK_TYPE] = sink.GetType();
var pipe = new EMFPipe<IDictionary<string, string>>(context);
pipe.Subscribe(sink);
var records = ParseW3SVCLogs(logs);
foreach (var record in records)
{
pipe.OnNext(new Envelope<IDictionary<string, string>>(record));
}
replacedert.Equal(5, sink.Records.Count);
var jo = JObject.Parse(sink.Records.First());
replacedert.Equal("10.10.10.10", jo["s-ip"].ToString());
replacedert.Equal("POST", jo["cs-method"].ToString());
replacedert.Equal("/DoWork", jo["cs-uri-stem"].ToString());
replacedert.Equal("443", jo["s-port"].ToString());
replacedert.Equal("11.11.11.11", jo["c-ip"].ToString());
replacedert.Equal(@"EXAMPLE\jonsmith", jo["cs-username"].ToString());
replacedert.Equal(@"SEA-HDFEHW23455/1.0.9/jonsmith", jo["cs-User-Agent"].ToString());
replacedert.Equal("500", jo["sc-status"].ToString());
replacedert.Equal("2", jo["sc-substatus"].ToString());
replacedert.Equal("IISNamespace", jo["_aws"]["CloudWatchMetrics"][0]["Namespace"].ToString());
replacedert.Equal("time-taken", jo["_aws"]["CloudWatchMetrics"][0]["Metrics"][0]["Name"].ToString());
}
19
View Source File : EventLogTest.cs
License : Apache License 2.0
Project Creator : awslabs
License : Apache License 2.0
Project Creator : awslabs
[Fact]
public void TestRawEventRecordEnvelope_GivenJsonFormat_FallsBackToEventRecordEnvelopeBehavior()
{
using (var eventReader = new EventLogReader("Application", PathType.LogName))
{
EventLog.WriteEntry(_logSource, "Test message", EventLogEntryType.Information, 0);
EventRecord eventRecord = null;
do
{
Thread.Sleep(100);
eventRecord = eventReader.ReadEvent();
} while (eventRecord == null);
var envelope = new RawEventRecordEnvelope(eventRecord, true, null);
var jsonStr = envelope.GetMessage("json");
var jsonObj = JObject.Parse(jsonStr);
replacedert.NotNull(jsonObj);
replacedert.NotNull(jsonObj["EventId"]);
replacedert.NotNull(jsonObj["LevelDisplayName"]);
replacedert.NotNull(jsonObj["LogName"]);
replacedert.NotNull(jsonObj["MachineName"]);
replacedert.NotNull(jsonObj["ProviderName"]);
replacedert.NotNull(jsonObj["TimeCreated"]);
replacedert.NotNull(jsonObj["Description"]);
replacedert.NotNull(jsonObj["Index"]);
replacedert.NotNull(jsonObj["UserName"]);
replacedert.NotNull(jsonObj["Keywords"]);
}
}
19
View Source File : EMFPipeTests.cs
License : Apache License 2.0
Project Creator : awslabs
License : Apache License 2.0
Project Creator : awslabs
[Fact]
public void ConvertsPowerShellSource()
{
var records = new List<Envelope<JObject>>
{
new Envelope<JObject>(JObject.Parse("{\"ComputerName\":\"MyComputer\",\"Name\":\"TrustedInstaller\",\"Status\":\"Running\"}")),
new Envelope<JObject>(JObject.Parse("{\"ComputerName\":\"MyComputer\",\"Name\":\"WinRM\",\"Status\":\"Stopped\"}"))
};
var config = TestUtility.GetConfig("Pipes", "PSEMFTestPipe");
using (var logger = new MemoryLogger(nameof(EMFPipeTests)))
{
var context = new PluginContext(config, logger, null);
var source = new MockEventSource<JObject>(context);
var sink = new MockEventSink(context);
context.ContextData[PluginContext.SOURCE_TYPE] = source.GetType();
context.ContextData[PluginContext.SOURCE_OUTPUT_TYPE] = source.GetOutputType();
context.ContextData[PluginContext.SINK_TYPE] = sink.GetType();
var pipe = new EMFPipe<JObject>(context);
source.Subscribe(pipe);
pipe.Subscribe(sink);
foreach (var r in records)
source.MockEvent(r.Data);
replacedert.Equal(2, sink.Records.Count);
var jo = JObject.Parse(sink.Records.First());
replacedert.Equal("PSNamespace", jo["_aws"]["CloudWatchMetrics"][0]["Namespace"].ToString());
replacedert.Equal("ServiceStatus", jo["_aws"]["CloudWatchMetrics"][0]["Metrics"][0]["Name"].ToString());
replacedert.Equal(1, jo["ServiceStatus"].ToObject<int>());
replacedert.Equal("Running", jo["Status"].ToString());
replacedert.Equal("TrustedInstaller", jo["Name"].ToString());
var dims = jo["_aws"]["CloudWatchMetrics"][0]["Dimensions"][0].ToArray().Select(i => i.ToString()).ToList();
replacedert.Equal(3, dims.Count);
replacedert.Contains("Name", dims);
replacedert.Contains("ComputerName", dims);
replacedert.Contains("Status", dims);
jo = JObject.Parse(sink.Records.Last());
replacedert.Equal("Stopped", jo["Status"].ToString());
replacedert.Equal("WinRM", jo["Name"].ToString());
}
}
19
View Source File : SingleLineJsonTextParser.cs
License : Apache License 2.0
Project Creator : awslabs
License : Apache License 2.0
Project Creator : awslabs
public async Task ParseRecordsAsync(LogContext context, IList<IEnvelope<JObject>> output,
int recordCount, CancellationToken stopToken = default)
{
using (var stream = new FileStream(context.FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
stream.Position = context.Position;
using (var reader = new LineReader(stream, _encoding))
{
var linesCount = 0;
while (linesCount < recordCount)
{
stopToken.ThrowIfCancellationRequested();
var (line, consumed) = await reader.ReadAsync(stopToken);
_logger.LogTrace("File: '{0}', line: '{1}', bytes: {2}", context.FilePath, line, consumed);
context.Position += consumed;
if (line is null)
{
break;
}
try
{
var jObject = JObject.Parse(line);
var timestamp = _timestampExtrator is null
? DateTime.Now
: _timestampExtrator.GetTimestamp(jObject);
var envelope = new LogEnvelope<JObject>(jObject,
timestamp,
line,
context.FilePath,
context.Position,
context.LineNumber);
output.Add(envelope);
linesCount++;
}
catch (Exception ex)
{
_logger.LogError(ex, "Error processing record '{0}'", line);
continue;
}
}
}
}
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<string[]> GetAllOperationNames(string ApiManagementName, string ResourceGroupName, string ApiName)
{
JObject oOperations = new JObject();
int numOfOps = 0;
List<string> operationNames = new List<string>();
do
{
(string azToken, string azSubId) = await auth.GetAccessToken();
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}/operations?$skip={5}&api-version={6}",
baseUrl, azSubId, ResourceGroupName, ApiManagementName, ApiName, numOfOps, GlobalConstants.APIVersion);
numOfOps += GlobalConstants.NumOfRecords;
string operations = await CallApiManagementAsync(azToken, requestUrl);
oOperations = JObject.Parse(operations);
foreach (var item in oOperations["value"])
{
operationNames.Add(((JValue)item["name"]).Value.ToString());
}
}
while (oOperations["nextLink"] != null);
return operationNames.ToArray();
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<string> GetAPIServiceUrl(string ApiManagementName, string ResourceGroupName, string ApiName)
{
(string azToken, string azSubId) = await auth.GetAccessToken();
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis/{4}?api-version={5}",
baseUrl, azSubId, ResourceGroupName, ApiManagementName, ApiName, GlobalConstants.APIVersion);
string apiDetails = await CallApiManagementAsync(azToken, requestUrl);
JObject oApiDetails = JObject.Parse(apiDetails);
APITemplateResource apiResource = JsonConvert.DeserializeObject<APITemplateResource>(apiDetails);
return apiResource.properties.serviceUrl;
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<JToken[]> GetAllAPIObjsAsync(string ApiManagementName, string ResourceGroupName)
{
JObject oApi = new JObject();
int numOfApis = 0;
List<JToken> apiObjs = new List<JToken>();
do
{
(string azToken, string azSubId) = await auth.GetAccessToken();
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/apis?$skip={4}&api-version={5}",
baseUrl, azSubId, ResourceGroupName, ApiManagementName, numOfApis, GlobalConstants.APIVersion);
numOfApis += GlobalConstants.NumOfRecords;
string apis = await CallApiManagementAsync(azToken, requestUrl);
oApi = JObject.Parse(apis);
foreach (var item in oApi["value"])
{
apiObjs.Add(item);
}
}
while (oApi["nextLink"] != null);
return apiObjs.ToArray();
}
19
View Source File : BackendExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<Tuple<Template,Dictionary<string,BackendApiParameters> > > GenerateBackendsARMTemplateAsync(string apimname, string resourceGroup, string singleApiName, List<TemplateResource> apiTemplateResources, List<TemplateResource> propertyResources, Extractor exc)
{
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting backends from service");
Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();
if (exc.paramBackend)
{
TemplateParameterProperties extractBackendParametersProperties = new TemplateParameterProperties()
{
type = "object"
};
armTemplate.parameters.Add(ParameterNames.BackendSettings, extractBackendParametersProperties);
}
List<TemplateResource> templateResources = new List<TemplateResource>();
// isolate api and operation policy resources in the case of a single api extraction, as they may reference backends
var policyResources = apiTemplateResources.Where(resource => (resource.type == ResourceTypeConstants.APIPolicy || resource.type == ResourceTypeConstants.APIOperationPolicy || resource.type == ResourceTypeConstants.ProductPolicy));
var namedValueResources = propertyResources.Where(resource => (resource.type == ResourceTypeConstants.Property));
// pull all backends for service
JObject oBackends = new JObject();
var oBackendParameters = new Dictionary<string, BackendApiParameters>();
int skipNumberOfBackends = 0;
do
{
string backends = await GetBackendsAsync(apimname, resourceGroup, skipNumberOfBackends);
oBackends = JObject.Parse(backends);
foreach (var item in oBackends["value"])
{
string backendName = ((JValue)item["name"]).Value.ToString();
string backend = await GetBackendDetailsAsync(apimname, resourceGroup, backendName);
// convert returned backend to template resource clreplaced
BackendTemplateResource backendTemplateResource = JsonConvert.DeserializeObject<BackendTemplateResource>(backend);
backendTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{backendName}')]";
backendTemplateResource.apiVersion = GlobalConstants.APIVersion;
bool includeBackend = false;
////only extract the backend if this is a full extraction, or in the case of a single api, if it is referenced by one of the policies
if (singleApiName == null)
{
// if the user is extracting all apis, extract all the backends
includeBackend = true;
}
else
{
// check extracted policies to see if the backend is referenced.
foreach (PolicyTemplateResource policyTemplateResource in policyResources)
{
string policyContent = ExtractorUtils.GetPolicyContent(exc, policyTemplateResource);
if (DoesPolicyReferenceBackend(policyContent, namedValueResources, backendName, backendTemplateResource))
{
// backend was used in policy, extract it
includeBackend = true;
// dont need to go through all policies if the back end has already been found
break;
}
}
}
if (includeBackend)
{
if (exc.paramBackend)
{
var apiToken = new BackendApiParameters();
string validApiParamName = ExtractorUtils.GenValidParamName(backendName, ParameterPrefix.Diagnostic).ToLower();
if (!string.IsNullOrEmpty(backendTemplateResource.properties.resourceId))
{
apiToken.resourceId = backendTemplateResource.properties.resourceId;
backendTemplateResource.properties.resourceId = $"[parameters('{ParameterNames.BackendSettings}').{validApiParamName}.resourceId]";
}
if (!string.IsNullOrEmpty(backendTemplateResource.properties.url))
{
apiToken.url = backendTemplateResource.properties.url;
backendTemplateResource.properties.url = $"[parameters('{ParameterNames.BackendSettings}').{validApiParamName}.url]";
}
if (!string.IsNullOrEmpty(backendTemplateResource.properties.protocol))
{
apiToken.protocol = backendTemplateResource.properties.protocol;
backendTemplateResource.properties.protocol = $"[parameters('{ParameterNames.BackendSettings}').{validApiParamName}.protocol]";
}
oBackendParameters.Add(validApiParamName, apiToken);
}
Console.WriteLine("'{0}' Backend found", backendName);
templateResources.Add(backendTemplateResource);
}
}
skipNumberOfBackends += GlobalConstants.NumOfRecords;
}
while (oBackends["nextLink"] != null);
armTemplate.resources = templateResources.ToArray();
return new Tuple<Template, Dictionary<string, BackendApiParameters>>(armTemplate, oBackendParameters);
}
19
View Source File : LoggerExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<Template> GenerateLoggerTemplateAsync(Extractor exc, string singleApiName, List<TemplateResource> apiTemplateResources, Dictionary<string, object> apiLoggerId)
{
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting loggers from service");
Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();
if (exc.paramLogResourceId)
{
TemplateParameterProperties loggerResourceIdParameterProperties = new TemplateParameterProperties()
{
type = "object"
};
armTemplate.parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdParameterProperties);
}
// isolate product api replacedociations in the case of a single api extraction
var policyResources = apiTemplateResources.Where(resource => (resource.type == ResourceTypeConstants.APIPolicy || resource.type == ResourceTypeConstants.APIOperationPolicy || resource.type == ResourceTypeConstants.ProductPolicy));
List<TemplateResource> templateResources = new List<TemplateResource>();
// pull all loggers for service
string loggers = await GetLoggersAsync(exc.sourceApimName, exc.resourceGroup);
JObject oLoggers = JObject.Parse(loggers);
foreach (var extractedLogger in oLoggers["value"])
{
string loggerName = ((JValue)extractedLogger["name"]).Value.ToString();
string fullLoggerResource = await GetLoggerDetailsAsync(exc.sourceApimName, exc.resourceGroup, loggerName);
// convert returned logger to template resource clreplaced
LoggerTemplateResource loggerResource = JsonConvert.DeserializeObject<LoggerTemplateResource>(fullLoggerResource);
loggerResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{loggerName}')]";
loggerResource.type = ResourceTypeConstants.Logger;
loggerResource.apiVersion = GlobalConstants.APIVersion;
loggerResource.scale = null;
if (singleApiName == null)
{
// if the user is extracting all apis, extract all the loggers
Console.WriteLine("'{0}' Logger found", loggerName);
templateResources.Add(loggerResource);
}
else
{
// if the user is extracting a single api, extract the loggers referenced by its diagnostics and api policies
bool isReferencedInPolicy = false;
bool isReferencedInDiagnostic = false;
foreach (PolicyTemplateResource policyTemplateResource in policyResources)
{
if (policyTemplateResource.properties.value.Contains(loggerName))
{
isReferencedInPolicy = true;
}
}
string validApiName = ExtractorUtils.GenValidParamName(singleApiName, ParameterPrefix.Api);
if (exc.paramApiLoggerId && apiLoggerId.ContainsKey(validApiName))
{
object diagnosticObj = apiLoggerId[validApiName];
if (diagnosticObj is Dictionary<string, string>)
{
Dictionary<string, string> curDiagnostic = (Dictionary<string, string>)diagnosticObj;
string validDName = ExtractorUtils.GenValidParamName(loggerResource.properties.loggerType, ParameterPrefix.Diagnostic).ToLower();
if (curDiagnostic.ContainsKey(validDName) && curDiagnostic[validDName].Contains(loggerName))
{
isReferencedInDiagnostic = true;
}
}
}
if (isReferencedInPolicy == true || isReferencedInDiagnostic == true)
{
// logger was used in policy or diagnostic, extract it
Console.WriteLine("'{0}' Logger found", loggerName);
templateResources.Add(loggerResource);
}
};
}
armTemplate.resources = templateResources.ToArray();
return armTemplate;
}
19
View Source File : PropertyExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<Template> GenerateNamedValuesTemplateAsync(string singleApiName, List<TemplateResource> apiTemplateResources, Extractor exc, BackendExtractor backendExtractor, List<TemplateResource> loggerTemplateResources)
{
Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();
if (exc.paramNamedValue)
{
TemplateParameterProperties namedValueParameterProperties = new TemplateParameterProperties()
{
type = "object"
};
armTemplate.parameters.Add(ParameterNames.NamedValues, namedValueParameterProperties);
}
if (exc.paramNamedValuesKeyVaultSecrets)
{
TemplateParameterProperties keyVaultNamedValueParameterProperties = new TemplateParameterProperties()
{
type = "object"
};
armTemplate.parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueParameterProperties);
}
if (exc.notIncludeNamedValue == true)
{
Console.WriteLine("------------------------------------------");
Console.WriteLine("Skipping extracting named values from service");
return armTemplate;
}
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting named values from service");
List<TemplateResource> templateResources = new List<TemplateResource>();
// pull all named values (properties) for service
string[] properties = await GetPropertiesAsync(exc.sourceApimName, exc.resourceGroup);
// isolate api and operation policy resources in the case of a single api extraction, as they may reference named value
var policyResources = apiTemplateResources.Where(resource => (resource.type == ResourceTypeConstants.APIPolicy || resource.type == ResourceTypeConstants.APIOperationPolicy || resource.type == ResourceTypeConstants.ProductPolicy));
foreach (var extractedProperty in properties)
{
JToken oProperty = JObject.Parse(extractedProperty);
string propertyName = ((JValue)oProperty["name"]).Value.ToString();
string fullPropertyResource = await GetPropertyDetailsAsync(exc.sourceApimName, exc.resourceGroup, propertyName);
// convert returned named value to template resource clreplaced
PropertyTemplateResource propertyTemplateResource = JsonConvert.DeserializeObject<PropertyTemplateResource>(fullPropertyResource);
propertyTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{propertyName}')]";
propertyTemplateResource.type = ResourceTypeConstants.Property;
propertyTemplateResource.apiVersion = GlobalConstants.APIVersion;
propertyTemplateResource.scale = null;
if (exc.paramNamedValue)
{
propertyTemplateResource.properties.value = $"[parameters('{ParameterNames.NamedValues}').{ExtractorUtils.GenValidParamName(propertyName, ParameterPrefix.Property)}]";
}
//Hide the value field if it is a keyvault named value
if (propertyTemplateResource.properties.keyVault != null)
{
propertyTemplateResource.properties.value = null;
}
if (propertyTemplateResource.properties.keyVault != null && exc.paramNamedValuesKeyVaultSecrets )
{
propertyTemplateResource.properties.keyVault.secretIdentifier = $"[parameters('{ParameterNames.NamedValueKeyVaultSecrets}').{ExtractorUtils.GenValidParamName(propertyName, ParameterPrefix.Property)}]";
}
if (singleApiName == null)
{
// if the user is executing a full extraction, extract all the loggers
Console.WriteLine("'{0}' Named value found", propertyName);
templateResources.Add(propertyTemplateResource);
}
else
{
// if the user is executing a single api, extract all the named values used in the template resources
bool foundInPolicy = DoesPolicyReferenceNamedValue(exc, policyResources, propertyName, propertyTemplateResource);
bool foundInBackEnd = await backendExtractor.IsNamedValueUsedInBackends(exc.sourceApimName, exc.resourceGroup, singleApiName, apiTemplateResources, exc, propertyName, propertyTemplateResource.properties.displayName);
bool foundInLogger = DoesLoggerReferenceNamedValue(loggerTemplateResources, propertyName, propertyTemplateResource);
// check if named value is referenced in a backend
if (foundInPolicy || foundInBackEnd || foundInLogger)
{
// named value was used in policy, extract it
Console.WriteLine("'{0}' Named value found", propertyName);
templateResources.Add(propertyTemplateResource);
}
}
}
armTemplate.resources = templateResources.ToArray();
return armTemplate;
}
19
View Source File : ExtractorUtils.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public static async Task GenerateSingleAPIWithRevisionsTemplates(ExtractorConfig exc, string apiName, FileNameGenerator fileNameGenerator, FileWriter fileWriter, FileNames fileNames)
{
Console.WriteLine("Extracting singleAPI {0} with revisions", apiName);
APIExtractor apiExtractor = new APIExtractor(fileWriter);
// Get all revisions for this api
string revisions = await apiExtractor.GetAPIRevisionsAsync(exc.sourceApimName, exc.resourceGroup, apiName);
JObject revs = JObject.Parse(revisions);
string currentRevision = null;
List<string> revList = new List<string>();
// Generate seperate folder for each API revision
for (int i = 0; i < ((JContainer)revs["value"]).Count; i++)
{
string apiID = ((JValue)revs["value"][i]["apiId"]).Value.ToString();
string singleApiName = apiID.Split("/")[2];
if (((JValue)revs["value"][i]["isCurrent"]).Value.ToString().Equals("True"))
{
currentRevision = singleApiName;
}
string revFileFolder = String.Concat(@exc.fileFolder, [email protected]"/{singleApiName}");
System.IO.Directory.CreateDirectory(revFileFolder);
await GenerateTemplates(new Extractor(exc, revFileFolder), singleApiName, null, fileNameGenerator, fileNames, fileWriter, null);
revList.Add(singleApiName);
}
if (currentRevision == null)
{
throw new Exception($"Revision {apiName} doesn't exist, something went wrong!");
}
// generate revisions master folder
string revMasterFolder = String.Concat(@exc.fileFolder, fileNames.revisionMasterFolder);
System.IO.Directory.CreateDirectory(revMasterFolder);
Extractor revExc = new Extractor(exc, revMasterFolder);
Template apiRevisionTemplate = await apiExtractor.GenerateAPIRevisionTemplateAsync(currentRevision, revList, apiName, revExc);
await GenerateTemplates(revExc, null, null, fileNameGenerator, fileNames, fileWriter, apiRevisionTemplate);
}
19
View Source File : ExtractorUtils.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public static async Task<Dictionary<string, object>> GetAllReferencedLoggers(List<string> apisToExtract, Extractor exc)
{
Dictionary<string, object> ApiLoggerId = new Dictionary<string, object>();
APIExtractor apiExc = new APIExtractor(new FileWriter());
string serviceDiagnostics = await apiExc.GetServiceDiagnosticsAsync(exc.sourceApimName, exc.resourceGroup);
JObject oServiceDiagnostics = JObject.Parse(serviceDiagnostics);
Dictionary<string, string> serviceloggerIds = new Dictionary<string, string>();
foreach (var serviceDiagnostic in oServiceDiagnostics["value"])
{
string diagnosticName = ((JValue)serviceDiagnostic["name"]).Value.ToString();
string loggerId = ((JValue)serviceDiagnostic["properties"]["loggerId"]).Value.ToString();
ApiLoggerId.Add(ExtractorUtils.GenValidParamName(diagnosticName, ParameterPrefix.Diagnostic), loggerId);
}
foreach (string curApiName in apisToExtract)
{
Dictionary<string, string> loggerIds = new Dictionary<string, string>();
string diagnostics = await apiExc.GetAPIDiagnosticsAsync(exc.sourceApimName, exc.resourceGroup, curApiName);
JObject oDiagnostics = JObject.Parse(diagnostics);
foreach (var diagnostic in oDiagnostics["value"])
{
string diagnosticName = ((JValue)diagnostic["name"]).Value.ToString();
string loggerId = ((JValue)diagnostic["properties"]["loggerId"]).Value.ToString();
loggerIds.Add(ExtractorUtils.GenValidParamName(diagnosticName, ParameterPrefix.Diagnostic), loggerId);
}
if (loggerIds.Count != 0)
{
ApiLoggerId.Add(ExtractorUtils.GenValidParamName(curApiName, ParameterPrefix.Api), loggerIds);
}
}
return ApiLoggerId;
}
19
View Source File : StringExtensions.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
internal static bool TryParseJson(this string str, out JToken result)
{
try
{
result = JToken.Parse(str);
return true;
}
catch (Exception)
{
}
result = null;
return false;
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<List<TemplateResource>> GenerateSingleAPIResourceAsync(string apiName, Extractor exc)
{
List<TemplateResource> templateResources = new List<TemplateResource>();
string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken;
string apiDetails = await GetAPIDetailsAsync(apimname, resourceGroup, apiName);
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting resources from {0} API:", apiName);
// convert returned api to template resource clreplaced
JObject oApiDetails = JObject.Parse(apiDetails);
APITemplateResource apiResource = JsonConvert.DeserializeObject<APITemplateResource>(apiDetails);
apiResource.type = ((JValue)oApiDetails["type"]).Value.ToString();
apiResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}')]";
apiResource.apiVersion = GlobalConstants.APIVersion;
apiResource.scale = null;
if (exc.paramServiceUrl)
{
apiResource.properties.serviceUrl = $"[parameters('{ParameterNames.ServiceUrl}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}]";
}
if (apiResource.properties.apiVersionSetId != null)
{
apiResource.dependsOn = new string[] { };
string versionSetName = apiResource.properties.apiVersionSetId;
int versionSetPosition = versionSetName.IndexOf("apiVersionSets/");
versionSetName = versionSetName.Substring(versionSetPosition, (versionSetName.Length - versionSetPosition));
apiResource.properties.apiVersionSetId = $"[concat(resourceId('Microsoft.ApiManagement/service', parameters('{ParameterNames.ApimServiceName}')), '/{versionSetName}')]";
}
else
{
apiResource.dependsOn = new string[] { };
}
templateResources.Add(apiResource);
templateResources.AddRange(await GetRelatedTemplateResourcesAsync(apiName, exc));
return templateResources;
}
19
View Source File : MasterTemplateExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<Template> CreateMasterTemplateParameterValues(List<string> apisToExtract, Extractor exc,
Dictionary<string, object> apiLoggerId,
Dictionary<string, string> loggerResourceIds,
Dictionary<string, BackendApiParameters> backendParams,
List<TemplateResource> propertyResources)
{
// used to create the parameter values for use in parameters file
// create empty template
Template masterTemplate = GenerateEmptyTemplate();
// add parameters with value property
Dictionary<string, TemplateParameterProperties> parameters = new Dictionary<string, TemplateParameterProperties>();
TemplateParameterProperties apimServiceNameProperties = new TemplateParameterProperties()
{
value = exc.destinationApimName
};
parameters.Add(ParameterNames.ApimServiceName, apimServiceNameProperties);
if (exc.linkedTemplatesBaseUrl != null)
{
TemplateParameterProperties linkedTemplatesBaseUrlProperties = new TemplateParameterProperties()
{
value = exc.linkedTemplatesBaseUrl
};
parameters.Add(ParameterNames.LinkedTemplatesBaseUrl, linkedTemplatesBaseUrlProperties);
// add linkedTemplatesSasToken parameter if provided and if the user has provided a linkedTemplatesBaseUrl
if (exc.linkedTemplatesSasToken != null)
{
TemplateParameterProperties linkedTemplatesSasTokenProperties = new TemplateParameterProperties()
{
value = exc.linkedTemplatesSasToken
};
parameters.Add(ParameterNames.LinkedTemplatesSasToken, linkedTemplatesSasTokenProperties);
}
// add linkedTemplatesUrlQueryString parameter if provided and if the user has provided a linkedTemplatesBaseUrl
if (exc.linkedTemplatesUrlQueryString != null)
{
TemplateParameterProperties linkedTemplatesUrlQueryStringProperties = new TemplateParameterProperties()
{
value = exc.linkedTemplatesUrlQueryString
};
parameters.Add(ParameterNames.LinkedTemplatesUrlQueryString, linkedTemplatesUrlQueryStringProperties);
}
}
if (exc.policyXMLBaseUrl != null)
{
TemplateParameterProperties policyTemplateBaseUrlProperties = new TemplateParameterProperties()
{
value = exc.policyXMLBaseUrl
};
parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlProperties);
// add policyXMLSasToken parameter if provided and if the user has provided a policyXMLBaseUrl
if (exc.policyXMLSasToken != null)
{
TemplateParameterProperties policyTemplateSasTokenProperties = new TemplateParameterProperties()
{
value = exc.policyXMLSasToken
};
parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenProperties);
}
}
if (exc.paramServiceUrl)
{
Dictionary<string, string> serviceUrls = new Dictionary<string, string>();
APIExtractor apiExc = new APIExtractor(new FileWriter());
foreach (string apiName in apisToExtract)
{
string validApiName = ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api);
string serviceUrl = exc.serviceUrlParameters != null ? GetApiServiceUrlFromParameters(apiName, exc.serviceUrlParameters) :
await apiExc.GetAPIServiceUrl(exc.sourceApimName, exc.resourceGroup, apiName);
serviceUrls.Add(validApiName, serviceUrl);
}
TemplateObjectParameterProperties serviceUrlProperties = new TemplateObjectParameterProperties()
{
value = serviceUrls
};
parameters.Add(ParameterNames.ServiceUrl, serviceUrlProperties);
}
if (exc.paramNamedValue)
{
Dictionary<string, string> namedValues = new Dictionary<string, string>();
PropertyExtractor pExc = new PropertyExtractor();
string[] properties = await pExc.GetPropertiesAsync(exc.sourceApimName, exc.resourceGroup);
foreach (var extractedProperty in properties)
{
JToken oProperty = JObject.Parse(extractedProperty);
string propertyName = ((JValue)oProperty["name"]).Value.ToString();
// check if the property has been extracted as it is being used in a policy or backend
if (propertyResources.Count(item => item.name.Contains(propertyName)) > 0)
{
string fullPropertyResource = await pExc.GetPropertyDetailsAsync(exc.sourceApimName, exc.resourceGroup, propertyName);
PropertyTemplateResource propertyTemplateResource = JsonConvert.DeserializeObject<PropertyTemplateResource>(fullPropertyResource);
//Only add the property if it is not controlled by keyvault
if (propertyTemplateResource?.properties.keyVault == null)
{
string propertyValue = propertyTemplateResource.properties.value;
string validPName = ExtractorUtils.GenValidParamName(propertyName, ParameterPrefix.Property);
namedValues.Add(validPName, propertyValue);
}
}
}
TemplateObjectParameterProperties namedValueProperties = new TemplateObjectParameterProperties()
{
value = namedValues
};
parameters.Add(ParameterNames.NamedValues, namedValueProperties);
}
if (exc.paramNamedValuesKeyVaultSecrets)
{
Dictionary<string, string> keyVaultNamedValues = new Dictionary<string, string>();
PropertyExtractor pExc = new PropertyExtractor();
string[] properties = await pExc.GetPropertiesAsync(exc.sourceApimName, exc.resourceGroup);
foreach (var extractedProperty in properties)
{
JToken oProperty = JObject.Parse(extractedProperty);
string propertyName = ((JValue)oProperty["name"]).Value.ToString();
// check if the property has been extracted as it is being used in a policy or backend
if (propertyResources.Count(item => item.name.Contains(propertyName)) > 0)
{
string fullPropertyResource = await pExc.GetPropertyDetailsAsync(exc.sourceApimName, exc.resourceGroup, propertyName);
PropertyTemplateResource propertyTemplateResource = JsonConvert.DeserializeObject<PropertyTemplateResource>(fullPropertyResource);
if (propertyTemplateResource?.properties.keyVault != null)
{
string propertyValue = propertyTemplateResource.properties.keyVault.secretIdentifier;
string validPName = ExtractorUtils.GenValidParamName(propertyName, ParameterPrefix.Property);
keyVaultNamedValues.Add(validPName, propertyValue);
}
}
}
TemplateObjectParameterProperties keyVaultNamedValueProperties = new TemplateObjectParameterProperties()
{
value = keyVaultNamedValues
};
parameters.Add(ParameterNames.NamedValueKeyVaultSecrets, keyVaultNamedValueProperties);
}
if (exc.paramApiLoggerId)
{
TemplateObjectParameterProperties loggerIdProperties = new TemplateObjectParameterProperties()
{
value = apiLoggerId
};
parameters.Add(ParameterNames.ApiLoggerId, loggerIdProperties);
}
if (exc.paramLogResourceId)
{
TemplateObjectParameterProperties loggerResourceIdProperties = new TemplateObjectParameterProperties()
{
value = loggerResourceIds
};
parameters.Add(ParameterNames.LoggerResourceId, loggerResourceIdProperties);
}
if(exc.paramBackend)
{
TemplateObjectParameterProperties backendProperties = new TemplateObjectParameterProperties()
{
value = backendParams
};
parameters.Add(ParameterNames.BackendSettings, backendProperties);
}
masterTemplate.parameters = parameters;
return masterTemplate;
}
19
View Source File : ProductExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<Template> GenerateProductsARMTemplateAsync(string apimname, string resourceGroup, string singleApiName, List<TemplateResource> apiTemplateResources, string fileFolder, Extractor exc)
{
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting products from service");
Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();
if (exc.policyXMLBaseUrl != null && exc.policyXMLSasToken != null)
{
TemplateParameterProperties policyTemplateSasTokenParameterProperties = new TemplateParameterProperties()
{
type = "string"
};
armTemplate.parameters.Add(ParameterNames.PolicyXMLSasToken, policyTemplateSasTokenParameterProperties);
}
if (exc.policyXMLBaseUrl != null)
{
TemplateParameterProperties policyTemplateBaseUrlParameterProperties = new TemplateParameterProperties()
{
type = "string"
};
armTemplate.parameters.Add(ParameterNames.PolicyXMLBaseUrl, policyTemplateBaseUrlParameterProperties);
}
// isolate product api replacedociations in the case of a single api extraction
var productAPIResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.ProductAPI);
List<TemplateResource> templateResources = new List<TemplateResource>();
// pull all products for service
string products = await GetProductsAsync(apimname, resourceGroup);
JObject oProducts = JObject.Parse(products);
foreach (var item in oProducts["value"])
{
string productName = ((JValue)item["name"]).Value.ToString();
string productDetails = await GetProductDetailsAsync(apimname, resourceGroup, productName);
// convert returned product to template resource clreplaced
JsonSerializerSettings settings = new JsonSerializerSettings
{
NullValueHandling = NullValueHandling.Ignore,
MissingMemberHandling = MissingMemberHandling.Ignore
};
ProductsTemplateResource productsTemplateResource = JsonConvert.DeserializeObject<ProductsTemplateResource>(productDetails, settings);
productsTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}')]";
productsTemplateResource.apiVersion = GlobalConstants.APIVersion;
string productGroupDetails = await GetProductGroupsAsync(apimname, resourceGroup, productName);
ProductGroupsTemplateResource productGroupsDetails = JsonConvert.DeserializeObject<ProductGroupsTemplateResource>(productGroupDetails, settings);
// only extract the product if this is a full extraction, or in the case of a single api, if it is found in products replacedociated with the api
if (singleApiName == null || productAPIResources.SingleOrDefault(p => p.name.Contains($"/{productName}/")) != null)
{
Console.WriteLine("'{0}' Product found", productName);
templateResources.Add(productsTemplateResource);
// add product policy resource to template
try
{
var productResourceId = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{productName}')]" };
foreach (ProductGroupsValue ProductGroup in productGroupsDetails.value)
{
ProductGroup.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}/{ProductGroup.name}')]";
ProductGroup.apiVersion = GlobalConstants.APIVersion;
ProductGroup.dependsOn = productResourceId;
templateResources.Add(ProductGroup);
}
string productPolicy = await GetProductPolicyAsync(apimname, resourceGroup, productName);
Console.WriteLine($" - Product policy found for {productName} product");
PolicyTemplateResource productPolicyResource = JsonConvert.DeserializeObject<PolicyTemplateResource>(productPolicy);
productPolicyResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}/policy')]";
productPolicyResource.apiVersion = GlobalConstants.APIVersion;
productPolicyResource.scale = null;
productPolicyResource.dependsOn = productResourceId;
// write policy xml content to file and point to it if policyXMLBaseUrl is provided
if (exc.policyXMLBaseUrl != null)
{
string policyXMLContent = productPolicyResource.properties.value;
string policyFolder = String.Concat(fileFolder, [email protected]"/policies");
string productPolicyFileName = [email protected]"/{productName}-productPolicy.xml";
this.fileWriter.CreateFolderIfNotExists(policyFolder);
this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, productPolicyFileName));
productPolicyResource.properties.format = "rawxml-link";
if (exc.policyXMLSasToken != null)
{
productPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{productPolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]";
}
else
{
productPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{productPolicyFileName}')]";
}
}
templateResources.Add(productPolicyResource);
}
catch (Exception) { }
// add tags replacedociated with the product to template
try
{
// pull tags replacedociated with the product
string productTags = await GetProductTagsAsync(apimname, resourceGroup, productName);
JObject oProductTags = JObject.Parse(productTags);
foreach (var tag in oProductTags["value"])
{
string productTagName = ((JValue)tag["name"]).Value.ToString();
Console.WriteLine(" - '{0}' Tag replacedociation found for {1} product", productTagName, productName);
// convert replacedociations between product and tags to template resource clreplaced
TagTemplateResource productTagResource = JsonConvert.DeserializeObject<TagTemplateResource>(tag.ToString());
productTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{productName}/{productTagName}')]";
productTagResource.apiVersion = GlobalConstants.APIVersion;
productTagResource.scale = null;
productTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/products', parameters('{ParameterNames.ApimServiceName}'), '{productName}')]" };
templateResources.Add(productTagResource);
}
}
catch (Exception) { }
}
}
armTemplate.resources = templateResources.ToArray();
return armTemplate;
}
19
View Source File : DiagnosticTranslatorService.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<List<string>> GetTranslations(List<string> textsToTranslate, string languageToTranslate, bool isHtmlTextType = false)
{
if (textsToTranslate == null)
{
return null;
}
// Always look for cache before calling translator API
Tuple<string, string> key = new Tuple<string, string>(languageToTranslate, string.Join("_", textsToTranslate));
if (this._translationCacheService.TryGetValue(key, out List<string> value))
{
return value;
}
string route = string.Format(_translatorApiURL, _apiVersion, languageToTranslate);
if (isHtmlTextType)
{
route += "&textType=html";
}
object[] body = textsToTranslate.Select((text) => { return new { Text = text }; }).ToArray();
var requestBody = JsonConvert.SerializeObject(body);
try
{
using (var request = new HttpRequestMessage())
{
request.Method = HttpMethod.Post;
request.RequestUri = new Uri(_translatorBaseURL + route);
request.Content = new StringContent(requestBody, Encoding.UTF8, "application/json");
HttpResponseMessage response = await _httpClient.SendAsync(request);
string result = await response.Content.ReadreplacedtringAsync();
JArray jObjectResponse = JArray.Parse(result);
List<string> translatedTexts = new List<string>();
foreach (var responseObject in jObjectResponse)
{
if (responseObject["translations"] != null && responseObject["translations"].Count() > 0)
{
translatedTexts.Add(responseObject["translations"][0]["text"].ToString());
}
}
// Update cache before returning translated texts array
this._translationCacheService.AddOrUpdate(key, translatedTexts);
return translatedTexts;
}
}
catch (Exception ex)
{
throw;
}
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<List<TemplateResource>> GenerateCurrentRevisionAPIResourceAsync(string apiName, Extractor exc)
{
List<TemplateResource> templateResources = new List<TemplateResource>();
string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken;
string apiDetails = await GetAPIDetailsAsync(apimname, resourceGroup, apiName);
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting resources from {0} API:", apiName);
// convert returned api to template resource clreplaced
JObject oApiDetails = JObject.Parse(apiDetails);
APITemplateResource apiResource = JsonConvert.DeserializeObject<APITemplateResource>(apiDetails);
apiResource.type = ((JValue)oApiDetails["type"]).Value.ToString();
apiResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}')]";
apiResource.apiVersion = GlobalConstants.APIVersion;
apiResource.scale = null;
apiResource.properties.isCurrent = null;
if (exc.paramServiceUrl)
{
apiResource.properties.serviceUrl = $"[parameters('{ParameterNames.ServiceUrl}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}]";
}
if (apiResource.properties.apiVersionSetId != null)
{
apiResource.dependsOn = new string[] { };
string versionSetName = apiResource.properties.apiVersionSetId;
int versionSetPosition = versionSetName.IndexOf("apiVersionSets/");
versionSetName = versionSetName.Substring(versionSetPosition, (versionSetName.Length - versionSetPosition));
apiResource.properties.apiVersionSetId = $"[concat(resourceId('Microsoft.ApiManagement/service', parameters('{ParameterNames.ApimServiceName}')), '/{versionSetName}')]";
}
else
{
apiResource.dependsOn = new string[] { };
}
templateResources.Add(apiResource);
templateResources.AddRange(await GetRelatedTemplateResourcesAsync(apiName, exc));
return templateResources;
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
private async Task<List<TemplateResource>> GenerateSchemasARMTemplate(string apimServiceName, string apiName, string resourceGroup, string fileFolder)
{
List<TemplateResource> templateResources = new List<TemplateResource>();
// pull all schemas from service
string schemas = await GetAPISchemasAsync(apimServiceName, resourceGroup, apiName);
JObject oSchemas = JObject.Parse(schemas);
foreach (var item in oSchemas["value"])
{
string schemaName = ((JValue)item["name"]).Value.ToString();
Console.WriteLine("'{0}' Operation schema found", schemaName);
string schemaDetails = await GetAPISchemaDetailsAsync(apimServiceName, resourceGroup, apiName, schemaName);
// pull returned schema and convert to template resource
RESTReturnedSchemaTemplate restReturnedSchemaTemplate = JsonConvert.DeserializeObject<RESTReturnedSchemaTemplate>(schemaDetails);
SchemaTemplateResource schemaDetailsResource = JsonConvert.DeserializeObject<SchemaTemplateResource>(schemaDetails);
schemaDetailsResource.properties.doreplacedent.value = GetSchemaValueBasedOnContentType(restReturnedSchemaTemplate.properties);
schemaDetailsResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{schemaName}')]";
schemaDetailsResource.apiVersion = GlobalConstants.APIVersion;
schemaDetailsResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
templateResources.Add(schemaDetailsResource);
}
return templateResources;
}
19
View Source File : APIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
private async Task<IEnumerable<TemplateResource>> GetRelatedTemplateResourcesAsync(string apiName, Extractor exc)
{
List<TemplateResource> templateResources = new List<TemplateResource>();
string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken;
#region Schemas
// add schema resources to api template
List<TemplateResource> schemaResources = await GenerateSchemasARMTemplate(apimname, apiName, resourceGroup, fileFolder);
templateResources.AddRange(schemaResources);
#endregion
#region Operations
// pull api operations for service
string[] operationNames = await GetAllOperationNames(apimname, resourceGroup, apiName);
int numBatches = 0;
// create empty array for the batch operation owners
List<string> batchOwners = new List<string>();
// if a batch size is specified
if (exc.operationBatchSize > 0)
{
// store the number of batches required based on exc.operationBatchSize
numBatches = (int)Math.Ceiling((double)operationNames.Length / (double)exc.operationBatchSize);
//Console.WriteLine ("Number of batches: {0}", numBatches);
}
foreach (string operationName in operationNames)
{
int opIndex = Array.IndexOf(operationNames, operationName);
//add batch owners into array
// ensure each owner is linked to the one before
if (exc.operationBatchSize > 0 && opIndex < numBatches)
{
batchOwners.Add(operationName);
//Console.WriteLine("Adding operation {0} to owner list", operationName);
}
string operationDetails = await GetAPIOperationDetailsAsync(apimname, resourceGroup, apiName, operationName);
Console.WriteLine("'{0}' Operation found", operationName);
// convert returned operation to template resource clreplaced
OperationTemplateResource operationResource = JsonConvert.DeserializeObject<OperationTemplateResource>(operationDetails);
string operationResourceName = operationResource.name;
operationResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}')]";
operationResource.apiVersion = GlobalConstants.APIVersion;
operationResource.scale = null;
// add operation dependencies and fix sample value if necessary
List<string> operationDependsOn = new List<string>() { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationResource.properties.request.representations)
{
AddSchemaDependencyToOperationIfNecessary(apiName, operationDependsOn, operationTemplateRepresentation);
ArmEscapeSampleValueIfNecessary(operationTemplateRepresentation);
}
foreach (OperationsTemplateResponse operationTemplateResponse in operationResource.properties.responses)
{
foreach (OperationTemplateRepresentation operationTemplateRepresentation in operationTemplateResponse.representations)
{
AddSchemaDependencyToOperationIfNecessary(apiName, operationDependsOn, operationTemplateRepresentation);
ArmEscapeSampleValueIfNecessary(operationTemplateRepresentation);
}
}
// add to batch if flagged
string batchdependsOn;
if (exc.operationBatchSize > 0 && opIndex > 0)
{
if (opIndex >= 1 && opIndex <= numBatches - 1)
{
// chain the owners to each other
batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[opIndex - 1]}')]";
//Console.WriteLine("Owner chaining: this request {0} to previous {1}", operationName, batchOwners[opIndex-1]);
}
else
{
// chain the operation to respective owner
int ownerIndex = (int)Math.Floor((opIndex - numBatches) / ((double)exc.operationBatchSize - 1));
batchdependsOn = $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{batchOwners[ownerIndex]}')]";
//Console.WriteLine("Operation {0} chained to owner {1}", operationName, batchOwners[ownerIndex]);
}
operationDependsOn.Add(batchdependsOn);
}
operationResource.dependsOn = operationDependsOn.ToArray();
templateResources.Add(operationResource);
// add operation policy resource to api template
try
{
string operationPolicy = await GetOperationPolicyAsync(apimname, resourceGroup, apiName, operationName);
Console.WriteLine($" - Operation policy found for {operationName} operation");
PolicyTemplateResource operationPolicyResource = JsonConvert.DeserializeObject<PolicyTemplateResource>(operationPolicy);
operationPolicyResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}/policy')]";
operationPolicyResource.apiVersion = GlobalConstants.APIVersion;
operationPolicyResource.scale = null;
operationPolicyResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{operationResourceName}')]" };
// write policy xml content to file and point to it if policyXMLBaseUrl is provided
if (policyXMLBaseUrl != null)
{
string policyXMLContent = operationPolicyResource.properties.value;
string policyFolder = String.Concat(fileFolder, [email protected]"/policies");
string operationPolicyFileName = [email protected]"/{apiName}-{operationName}-operationPolicy.xml";
this.fileWriter.CreateFolderIfNotExists(policyFolder);
this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, operationPolicyFileName));
operationPolicyResource.properties.format = "rawxml-link";
if (policyXMLSasToken != null)
{
operationPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{operationPolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]";
}
else
{
operationPolicyResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{operationPolicyFileName}')]";
}
}
templateResources.Add(operationPolicyResource);
}
catch (Exception) { }
// add tags replacedociated with the operation to template
try
{
// pull tags replacedociated with the operation
string apiOperationTags = await GetOperationTagsAsync(apimname, resourceGroup, apiName, operationName);
JObject oApiOperationTags = JObject.Parse(apiOperationTags);
foreach (var tag in oApiOperationTags["value"])
{
string apiOperationTagName = ((JValue)tag["name"]).Value.ToString();
Console.WriteLine(" - '{0}' Tag replacedociation found for {1} operation", apiOperationTagName, operationResourceName);
// convert operation tag replacedociation to template resource clreplaced
TagTemplateResource operationTagResource = JsonConvert.DeserializeObject<TagTemplateResource>(tag.ToString());
operationTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}/{apiOperationTagName}')]";
operationTagResource.apiVersion = GlobalConstants.APIVersion;
operationTagResource.scale = null;
operationTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{operationResourceName}')]" };
templateResources.Add(operationTagResource);
}
}
catch (Exception) { }
}
#endregion
#region API Policies
// add api policy resource to api template
try
{
string apiPolicies = await GetAPIPolicyAsync(apimname, resourceGroup, apiName);
Console.WriteLine("API policy found");
PolicyTemplateResource apiPoliciesResource = JsonConvert.DeserializeObject<PolicyTemplateResource>(apiPolicies);
apiPoliciesResource.apiVersion = GlobalConstants.APIVersion;
apiPoliciesResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{apiPoliciesResource.name}')]";
apiPoliciesResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
// write policy xml content to file and point to it if policyXMLBaseUrl is provided
if (policyXMLBaseUrl != null)
{
string policyXMLContent = apiPoliciesResource.properties.value;
string policyFolder = String.Concat(fileFolder, [email protected]"/policies");
string apiPolicyFileName = [email protected]"/{apiName}-apiPolicy.xml";
this.fileWriter.CreateFolderIfNotExists(policyFolder);
this.fileWriter.WriteXMLToFile(policyXMLContent, String.Concat(policyFolder, apiPolicyFileName));
apiPoliciesResource.properties.format = "rawxml-link";
if (policyXMLSasToken != null)
{
apiPoliciesResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{apiPolicyFileName}', parameters('{ParameterNames.PolicyXMLSasToken}'))]";
}
else
{
apiPoliciesResource.properties.value = $"[concat(parameters('{ParameterNames.PolicyXMLBaseUrl}'), '{apiPolicyFileName}')]";
}
}
templateResources.Add(apiPoliciesResource);
}
catch (Exception) { }
#endregion
#region API Tags
// add tags replacedociated with the api to template
try
{
// pull tags replacedociated with the api
string apiTags = await GetAPITagsAsync(apimname, resourceGroup, apiName);
JObject oApiTags = JObject.Parse(apiTags);
foreach (var tag in oApiTags["value"])
{
string apiTagName = ((JValue)tag["name"]).Value.ToString();
Console.WriteLine("'{0}' Tag replacedociation found", apiTagName);
// convert replacedociations between api and tags to template resource clreplaced
TagTemplateResource apiTagResource = JsonConvert.DeserializeObject<TagTemplateResource>(tag.ToString());
apiTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{apiTagName}')]";
apiTagResource.apiVersion = GlobalConstants.APIVersion;
apiTagResource.scale = null;
apiTagResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
templateResources.Add(apiTagResource);
}
}
catch (Exception) { }
#endregion
// add product api replacedociations to template
#region API Products
try
{
// pull product api replacedociations
string apiProducts = await GetAPIProductsAsync(apimname, resourceGroup, apiName);
JObject oApiProducts = JObject.Parse(apiProducts);
foreach (var item in oApiProducts["value"])
{
string apiProductName = ((JValue)item["name"]).Value.ToString();
Console.WriteLine("'{0}' Product replacedociation found", apiProductName);
// convert returned api product replacedociations to template resource clreplaced
ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject<ProductAPITemplateResource>(item.ToString());
productAPIResource.type = ResourceTypeConstants.ProductAPI;
productAPIResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiProductName}/{apiName}')]";
productAPIResource.apiVersion = GlobalConstants.APIVersion;
productAPIResource.scale = null;
productAPIResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
templateResources.Add(productAPIResource);
}
}
catch (Exception) { }
#endregion
#region Diagnostics
// add diagnostics to template
// pull diagnostics for api
string diagnostics = await GetAPIDiagnosticsAsync(apimname, resourceGroup, apiName);
JObject oDiagnostics = JObject.Parse(diagnostics);
foreach (var diagnostic in oDiagnostics["value"])
{
string diagnosticName = ((JValue)diagnostic["name"]).Value.ToString();
Console.WriteLine("'{0}' Diagnostic found", diagnosticName);
// convert returned diagnostic to template resource clreplaced
DiagnosticTemplateResource diagnosticResource = diagnostic.ToObject<DiagnosticTemplateResource>();
diagnosticResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{diagnosticName}')]";
diagnosticResource.type = ResourceTypeConstants.APIDiagnostic;
diagnosticResource.apiVersion = GlobalConstants.APIVersion;
diagnosticResource.scale = null;
diagnosticResource.dependsOn = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiName}')]" };
if (exc.paramApiLoggerId)
{
diagnosticResource.properties.loggerId = $"[parameters('{ParameterNames.ApiLoggerId}').{ExtractorUtils.GenValidParamName(apiName, ParameterPrefix.Api)}.{ExtractorUtils.GenValidParamName(diagnosticName, ParameterPrefix.Diagnostic)}]";
}
if (!diagnosticName.Contains("applicationinsights"))
{
// enableHttpCorrelationHeaders only works for application insights, causes errors otherwise
diagnosticResource.properties.enableHttpCorrelationHeaders = null;
}
templateResources.Add(diagnosticResource);
}
#endregion
return templateResources;
}
19
View Source File : BackendExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<bool> IsNamedValueUsedInBackends(string apimname, string resourceGroup, string singleApiName, List<TemplateResource> apiTemplateResources, Extractor exc, string propertyName, string propertyDisplayName)
{
// isolate api and operation policy resources in the case of a single api extraction, as they may reference backends
var policyResources = apiTemplateResources.Where(resource => (resource.type == ResourceTypeConstants.APIPolicy || resource.type == ResourceTypeConstants.APIOperationPolicy || resource.type == ResourceTypeConstants.ProductPolicy));
var emptyNamedValueResources = new List<TemplateResource>();
// pull all backends for service
JObject oBackends = new JObject();
int skipNumberOfBackends = 0;
do
{
string backends = await GetBackendsAsync(apimname, resourceGroup, skipNumberOfBackends);
oBackends = JObject.Parse(backends);
foreach (var item in oBackends["value"])
{
var content = item.ToString();
// check if backend references the named value, credentials for example
if (content.Contains(string.Concat("{{", propertyName, "}}")) || content.Contains(string.Concat("{{", propertyDisplayName, "}}")))
{
//only true if this is a full extraction, or in the case of a single api, if it is referenced by one of the API policies
if (singleApiName == null)
{
return true;
}
else
{
// is this backend related to the single api?
// is backend used in the extracted policies for this API
// if backend id is referenced in policy
// or a named value is referenced in policy to a backend, we have already checked the policy for named value.
// check if this backend is used by any of the policies extracted
string backendName = ((JValue)item["name"]).Value.ToString();
string backend = await GetBackendDetailsAsync(apimname, resourceGroup, backendName);
// convert returned backend to template resource clreplaced
BackendTemplateResource backendTemplateResource = JsonConvert.DeserializeObject<BackendTemplateResource>(backend);
// we have already checked if the named value is used in a policy, we just need to confirm if the backend is referenced by this single api within the policy file
// this is why an empty named values must be preplaceded to this method for validation
foreach (PolicyTemplateResource policyTemplateResource in policyResources)
{
string policyContent = ExtractorUtils.GetPolicyContent(exc, policyTemplateResource);
if (DoesPolicyReferenceBackend(policyContent, emptyNamedValueResources, backendName, backendTemplateResource))
{
// dont need to go through all policies and backends if the named values has already been found
return true;
}
}
}
}
}
skipNumberOfBackends += GlobalConstants.NumOfRecords;
}
while (oBackends["nextLink"] != null);
return false;
}
19
View Source File : ProductAPIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<List<TemplateResource>> GenerateSingleProductAPIResourceAsync(string apiName, Extractor exc, string[] dependsOn)
{
List<TemplateResource> templateResources = new List<TemplateResource>();
string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken;
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting products from {0} API:", apiName);
// add product api replacedociations to template
#region API Products
try
{
// pull product api replacedociations
string apiProducts = await GetAPIProductsAsync(apimname, resourceGroup, apiName);
JObject oApiProducts = JObject.Parse(apiProducts);
string lastProductAPIName = null;
foreach (var item in oApiProducts["value"])
{
string apiProductName = ((JValue)item["name"]).Value.ToString();
Console.WriteLine("'{0}' Product replacedociation found", apiProductName);
// convert returned api product replacedociations to template resource clreplaced
ProductAPITemplateResource productAPIResource = JsonConvert.DeserializeObject<ProductAPITemplateResource>(item.ToString());
productAPIResource.type = ResourceTypeConstants.ProductAPI;
productAPIResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiProductName}/{apiName}')]";
productAPIResource.apiVersion = GlobalConstants.APIVersion;
productAPIResource.scale = null;
productAPIResource.dependsOn = (lastProductAPIName != null) ? new string[] { lastProductAPIName } : dependsOn;
templateResources.Add(productAPIResource);
lastProductAPIName = $"[resourceId('Microsoft.ApiManagement/service/products/apis', parameters('{ParameterNames.ApimServiceName}'), '{apiProductName}', '{apiName}')]";
}
}
catch (Exception) { }
#endregion
return templateResources;
}
19
View Source File : PropertyExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<string[]> GetPropertiesAsync(string ApiManagementName, string ResourceGroupName)
{
JObject oProperty = new JObject();
int numOfProperties = 0;
List<string> propertyObjs = new List<string>();
do
{
(string azToken, string azSubId) = await auth.GetAccessToken();
string requestUrl = string.Format("{0}/subscriptions/{1}/resourceGroups/{2}/providers/Microsoft.ApiManagement/service/{3}/namedValues?$skip={4}&api-version={5}",
baseUrl, azSubId, ResourceGroupName, ApiManagementName, numOfProperties, GlobalConstants.APIVersion);
numOfProperties += GlobalConstants.NumOfRecords;
string properties = await CallApiManagementAsync(azToken, requestUrl);
oProperty = JObject.Parse(properties);
foreach (var item in oProperty["value"])
{
propertyObjs.Add(item.ToString());
}
}
while (oProperty["nextLink"] != null);
return propertyObjs.ToArray();
}
19
View Source File : TagAPIExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<List<TemplateResource>> GenerateSingleAPITagResourceAsync(string apiName, Extractor exc, string[] dependsOn)
{
List<TemplateResource> templateResources = new List<TemplateResource>();
string apimname = exc.sourceApimName, resourceGroup = exc.resourceGroup, fileFolder = exc.fileFolder, policyXMLBaseUrl = exc.policyXMLBaseUrl, policyXMLSasToken = exc.policyXMLSasToken;
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting tags from {0} API:", apiName);
string[] dependencyChain = dependsOn;
#region Operations
// pull api operations for service
string[] operationNames = await GetAllOperationNames(apimname, resourceGroup, apiName);
foreach (string operationName in operationNames)
{
string operationDetails = await GetAPIOperationDetailsAsync(apimname, resourceGroup, apiName, operationName);
Console.WriteLine("'{0}' Operation found", operationName);
// convert returned operation to template resource clreplaced
OperationTemplateResource operationResource = JsonConvert.DeserializeObject<OperationTemplateResource>(operationDetails);
string operationResourceName = operationResource.name;
// add tags replacedociated with the operation to template
try
{
// pull tags replacedociated with the operation
string apiOperationTags = await GetOperationTagsAsync(apimname, resourceGroup, apiName, operationName);
JObject oApiOperationTags = JObject.Parse(apiOperationTags);
foreach (var tag in oApiOperationTags["value"])
{
string apiOperationTagName = ((JValue)tag["name"]).Value.ToString();
Console.WriteLine(" - '{0}' Tag replacedociation found for {1} operation", apiOperationTagName, operationResourceName);
// convert operation tag replacedociation to template resource clreplaced
TagTemplateResource operationTagResource = JsonConvert.DeserializeObject<TagTemplateResource>(tag.ToString());
operationTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{operationResourceName}/{apiOperationTagName}')]";
operationTagResource.apiVersion = GlobalConstants.APIVersion;
operationTagResource.scale = null;
operationTagResource.dependsOn = dependencyChain;
templateResources.Add(operationTagResource);
dependencyChain = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/operations/tags', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{operationResourceName}', '{apiOperationTagName}')]" };
}
}
catch (Exception) { }
}
#endregion
#region Tags
// add tags replacedociated with the api to template
try
{
// pull tags replacedociated with the api
string apiTags = await GetAPITagsAsync(apimname, resourceGroup, apiName);
JObject oApiTags = JObject.Parse(apiTags);
foreach (var tag in oApiTags["value"])
{
string apiTagName = ((JValue)tag["name"]).Value.ToString();
Console.WriteLine("'{0}' Tag replacedociation found", apiTagName);
// convert replacedociations between api and tags to template resource clreplaced
TagTemplateResource apiTagResource = JsonConvert.DeserializeObject<TagTemplateResource>(tag.ToString());
apiTagResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{apiName}/{apiTagName}')]";
apiTagResource.apiVersion = GlobalConstants.APIVersion;
apiTagResource.scale = null;
apiTagResource.dependsOn = dependencyChain;
templateResources.Add(apiTagResource);
dependencyChain = new string[] { $"[resourceId('Microsoft.ApiManagement/service/apis/tags', parameters('{ParameterNames.ApimServiceName}'), '{apiName}', '{apiTagName}')]" };
}
}
catch (Exception) { }
#endregion
return templateResources;
}
19
View Source File : TagExtractor.cs
License : MIT License
Project Creator : Azure
License : MIT License
Project Creator : Azure
public async Task<Template> GenerateTagsTemplateAsync(string apimname, string resourceGroup, string singleApiName, List<TemplateResource> apiTemplateResources, List<TemplateResource> productTemplateResources, string policyXMLBaseUrl, string policyXMLSasToken)
{
Console.WriteLine("------------------------------------------");
Console.WriteLine("Extracting tags from service");
Template armTemplate = GenerateEmptyPropertyTemplateWithParameters();
// isolate tag and api operation replacedociations in the case of a single api extraction
var apiOperationTagResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.APIOperationTag);
// isolate tag and api replacedociations in the case of a single api extraction
var apiTagResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.APITag);
// isolate product api replacedociations in the case of a single api extraction
var productAPIResources = apiTemplateResources.Where(resource => resource.type == ResourceTypeConstants.ProductAPI);
// isolate tag and product replacedociations in the case of a single api extraction
var productTagResources = productTemplateResources.Where(resource => resource.type == ResourceTypeConstants.ProductTag);
List<TemplateResource> templateResources = new List<TemplateResource>();
// pull all named values (Tags) for service
JObject oTags = new JObject();
int skipNumOfTags = 0;
do
{
string Tags = await GetTagsAsync(apimname, resourceGroup, skipNumOfTags);
oTags = JObject.Parse(Tags);
foreach (var extractedTag in oTags["value"])
{
string TagName = ((JValue)extractedTag["name"]).Value.ToString();
// convert returned named value to template resource clreplaced
TagTemplateResource TagTemplateResource = JsonConvert.DeserializeObject<TagTemplateResource>(extractedTag.ToString());
TagTemplateResource.name = $"[concat(parameters('{ParameterNames.ApimServiceName}'), '/{TagName}')]";
TagTemplateResource.type = ResourceTypeConstants.Tag;
TagTemplateResource.apiVersion = GlobalConstants.APIVersion;
TagTemplateResource.scale = null;
// only extract the tag if this is a full extraction,
// or in the case of a single api, if it is found in tags replacedociated with the api operations
// or if it is found in tags replacedociated with the api
// or if it is found in tags replacedociated with the products replacedociated with the api
if (singleApiName == null
|| apiOperationTagResources.Any(t => t.name.Contains($"/{TagName}'"))
|| apiTagResources.Any(t => t.name.Contains($"/{TagName}'"))
|| (productAPIResources.Any(t => t.name.Contains($"/{singleApiName}"))
&& productTagResources.Any(t => t.name.Contains($"/{TagName}'"))))
{
Console.WriteLine("'{0}' Tag found", TagName);
templateResources.Add(TagTemplateResource);
}
}
skipNumOfTags += GlobalConstants.NumOfRecords;
}
while (oTags["nextLink"] != null);
armTemplate.resources = templateResources.ToArray();
return armTemplate;
}
See More Examples