public async Task <List <GraphGroupsViewModel> > GetGraphGroups() { var client = new RestApiClient(); var accessToken = await ServicePrincipal.GetGraphAccessToken(); var graphGroups = await client.CallGetListAsync <GraphGroupsViewModel>(UriConstants.GetGraphGroups, accessToken); return(graphGroups.Result.ToList()); }
public async Task <IEnumerable <ResourceGroup> > GetResourceGroups(string endpointUrl) { var client = new RestApiClient(); var result = await client.CallGetListAsync <ResourceGroup>(endpointUrl, await ServicePrincipal.GetAccessToken()); if (!result.Success) { return(new List <ResourceGroup>()); } return(result.Result); }
/// <summary> /// Retrieves the subscriptions /// </summary> /// <returns>Subscriptions list</returns> public async Task <List <Subscription> > GetSubscriptions() { var endpointUrl = string.Format(UriConstants.GetAllSubscriptionsUri); var client = new RestApiClient(); var result = await client.CallGetListAsync <Subscription>(endpointUrl, await ServicePrincipal.GetAccessToken()); if (result.Success) { return(result.Result.Where(s => s.State == SubscriptionState.Enabled).ToList()); } throw new ServiceCatalogException(result.Message); }
/// <summary> /// Retrieves the locations /// </summary> /// <returns>Locations <see cref="SelectListItem"/> list</returns> public async Task <List <SelectListItem> > GetLocations(string subscriptionId) { var endpointUrl = string.Format(UriConstants.GetAllLocationsUri, subscriptionId); var client = new RestApiClient(); var result = await client.CallGetListAsync <Location>(endpointUrl, await ServicePrincipal.GetAccessToken()); if (result.Success) { return(result.Result .Select(location => new SelectListItem() { Value = location.Name, Text = location.DisplayName }) .ToList()); } throw new ServiceCatalogException(result.Message); }
// GET: Deployments public async Task <ActionResult> DeploymentsView() { try { // Get all subscriptions for this tenant var subscriptions = await new SubscriptionController().GetSubscriptions(); var subscriptionId = subscriptions.FirstOrDefault()?.SubscriptionId; var token = await ServicePrincipal.GetAccessToken(); var client = new RestApiClient(); // Get all resource groups var resourceGroupUri = string.Format(UriConstants.GetAllResourceGroupsUri, Url.Encode(subscriptionId), ""); var resourceGroups = await client.CallGetListAsync <ResourceGroup>(resourceGroupUri, token); // Get all deployments var deployments = new List <DeploymentExtended>(); foreach (var resourceGroup in resourceGroups.Result) { var deploymentsUri = string.Format(UriConstants.GetDeploymentsByResourceGroup, subscriptionId, resourceGroup.Name); client = new RestApiClient(); var result = await client.CallGetListAsync <DeploymentExtended>(deploymentsUri, token); var deployment = result.Result; deployments.AddRange(deployment); } var email = ClaimsPrincipal.Current.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name").Value; var resultDeployments = new List <DeploymentViewModel>(); using (WebAppContext context = new WebAppContext()) { var localDeployments = await context.Deployments.ToListAsync(); foreach (var localDeployment in localDeployments) { foreach (var deployment in deployments) { if (localDeployment.DeploymentName != deployment?.Name) { continue; } if (UserRoleHelper.IsAdmin(email) || email == localDeployment.Owner) { var newDeployment = new DeploymentViewModel() { TemplateName = localDeployment.TemplateName, DeploymentId = localDeployment.DeploymentId, DeploymentName = localDeployment.DeploymentName, SubscriptionId = localDeployment.SubscriptionId, SubscriptionName = localDeployment.SubscriptionName, Owner = localDeployment.Owner, TemplateVersion = localDeployment.TemplateVersion, Timestamp = localDeployment.Timestamp, ProvisioningState = deployment?.Properties?.ProvisioningState, Outputs = deployment?.Properties?.Outputs?.ToString() }; resultDeployments.Add(newDeployment); } } } } var deploymentsList = resultDeployments.OrderByDescending(d => d.Timestamp).ToList(); ViewBag.FileLogName = $"{DateTime.Today:yyyy-MM-dd}.log"; return(View(deploymentsList)); } catch (Exception ex) { ViewBag.ErrorMessage = "Error"; ViewBag.ErrorDetails = ex.Message; return(View("Error")); } }
// GET: RunBooks public async Task <ActionResult> RunBooksView() { try { Log.Info("Start RunBooksController"); var subscriptions = await new SubscriptionController().GetSubscriptions(); var subscriptionId = subscriptions.FirstOrDefault()?.SubscriptionId; var token = await ServicePrincipal.GetAccessToken(); var automationAccountClient = new RestApiClient(); var automationAccountUri = string.Format(UriConstants.GetAutomationAccounts, Url.Encode(subscriptionId)); var automationAccounts = await automationAccountClient.CallGetListAsync <AutomationAccount>(automationAccountUri, token); var automationAccountsResult = automationAccounts.Result; var email = ClaimsPrincipal.Current.FindFirst("http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name").Value; Log.Info($"RunBooksController Owner: {email}"); List <Job> localJobs; using (var webAppContext = new WebAppContext()) { localJobs = await webAppContext.Jobs.ToListAsync(); } Log.Info($"LocalJobs: {TemplateHelper.ToJson(localJobs)}"); var jobList = new List <JobViewModel>(); foreach (var account in automationAccountsResult) { var jobAccountClient = new RestApiClient(); var jobsUrl = string.Format(UriConstants.GetJobs, account.Id); var jobs = await jobAccountClient.CallGetListAsync <JobViewModel>(jobsUrl, token); var jobsResult = jobs.Result; foreach (var job in jobsResult) { var isUserOwner = localJobs.Any(j => j.Id == job.Properties.JobId && j.Owner == email); Log.Info($"job status: {isUserOwner}"); if (UserRoleHelper.IsAdmin(email) || isUserOwner) { var jobOutputClient = new RestApiClient(); var jobOutputUrl = string.Format(UriConstants.GetJobOutput, job.Id); var jobOutput = await jobOutputClient.CallGetText(jobOutputUrl, token); var newJob = job; newJob.Outputs = jobOutput; jobList.Add(newJob); } } } return(View(jobList)); } catch (Exception ex) { ViewBag.ErrorMessage = "Error"; ViewBag.ErrorDetails = ex.Message; Log.Error(ex); return(View("Error")); } }