/// <summary> /// Write the request to channel. /// </summary> /// <param name="requestModel">the fluent request model</param> /// <param name="cancellationToken">the task cancellation token</param> /// <returns>the request id</returns> public async Task <string> WriteAsync(FluentRequestModel requestModel, CancellationToken cancellationToken = default(CancellationToken)) { var requestId = Guid.NewGuid().ToString(); RequestEntity requestEntity = new RequestEntity(requestId, requestModel); await table.ExecuteAsync(TableOperation.InsertOrReplace(requestEntity)); await queue.AddMessageAsync(new CloudQueueMessage(requestId)); return(requestId); }
public async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { int i = 0; foreach (var model in this) { await model.ValidateAndResolveAsync(azure, fluentRequestModel, $"{propertyName}[{i}]", parentModel, cancellationToken); i++; } }
public override async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken) { await this.ValidateAndResolveResourceGroupAsync(azure, fluentRequestModel, propertyName, parentModel, cancellationToken); if (this.Linux != null) { this.Linux.Validate($"{propertyName}.linux"); } else if (this.Windows != null) { this.Windows.Validate($"{propertyName}.windows"); } else { throw new ArgumentException($"{propertyName}.linux or {propertyName}.windows should be specified"); } if (NewPrimaryNetwork != null) { NewPrimaryNetwork.Validate($"{propertyName}.newPrimaryNetwork"); NewPrimaryNetwork.ResolveInlineCreatable(azure, this); NewPrimaryNetwork.ResolveCreatableReference(azure, fluentRequestModel); } if (ExistingPrimaryNetwork != null) { ExistingPrimaryNetwork.Validate($"{propertyName}.existingPrimaryNetwork"); await ExistingPrimaryNetwork.ResolveResourceAsync(azure, cancellationToken); } if (NewPrimaryPublicIPAddress != null) { NewPrimaryPublicIPAddress.Validate($"{propertyName}.newPrimaryPublicIPAddress"); NewPrimaryPublicIPAddress.ResolveInlineCreatable(azure, this); NewPrimaryPublicIPAddress.ResolveCreatableReference(azure, fluentRequestModel); } if (ExistingPrimaryPublicIPAddress != null) { ExistingPrimaryPublicIPAddress.Validate($"{propertyName}.existingPrimaryPublicIPAddress"); await ExistingPrimaryPublicIPAddress.ResolveResourceAsync(azure, cancellationToken); } if (NewPrimaryNetworkInterface != null) { NewPrimaryNetworkInterface.Validate($"{propertyName}.newPrimaryNetworkInterface"); NewPrimaryNetworkInterface.ResolveInlineCreatable(azure, this); NewPrimaryNetworkInterface.ResolveCreatableReference(azure, fluentRequestModel); } if (ExistingPrimaryNetworkInterface != null) { ExistingPrimaryNetworkInterface.Validate($"{propertyName}.existingPrimaryNetworkInterface"); await ExistingPrimaryNetworkInterface.ResolveResourceAsync(azure, cancellationToken); } if (this.BootDiagnostics != null) { await this.BootDiagnostics.ValidateAndResolveAsync(azure, fluentRequestModel, $"{propertyName}.bootDiagnostics", this, cancellationToken); } }
public override async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { await base.ValidateAndResolveResourceGroupAsync(azure, fluentRequestModel, propertyName, parentModel, cancellationToken); if (this.AddressSpace != null && this.AddressSpace.Cidr == null && this.AddressSpace.Subnets != null) { throw new ArgumentException($"Specifying {propertyName}.addressSpace.subnets requires {propertyName}.addressSpace.cidr to present"); } }
public async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { foreach (ExistingResourceT item in this) { item.Validate(propertyName); } foreach (ExistingResourceT item in this) { await item.ResolveResourceAsync(azure, cancellationToken); } }
public Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { foreach (NewResourceT item in this) { item.Validate(propertyName); } foreach (NewResourceT item in this) { item.ResolveInlineCreatable(azure, parentModel); item.ResolveCreatableReference(azure, fluentRequestModel); } return(Task.CompletedTask); }
public override async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken) { await base.ValidateAndResolveResourceGroupAsync(azure, fluentRequestModel, propertyName, parentModel, cancellationToken); if (this.Rules != null) { int i = 0; foreach (var rule in Rules) { rule.Value.Validate($"{propertyName}.rules[{i}]"); i++; } } }
/// <summary> /// Creates RequestEntity to write channel. /// </summary> /// <param name="requestId">The request Id</param> /// <param name="requestPayload">The fluent request payload</param> public RequestEntity(string requestId, FluentRequestModel requestPayload) { this.PartitionKey = requestId ?? throw new ArgumentNullException("requestId"); if (requestPayload == null) { throw new ArgumentNullException("requestPayload"); } this.RowKey = RequestRowKey; this.Payload = JsonConvert.SerializeObject(requestPayload, Newtonsoft.Json.Formatting.None, new JsonSerializerSettings { NullValueHandling = NullValueHandling.Ignore }); }
public async Task <IActionResult> Post([FromBody] FluentRequestModel fluentRequestPayload) { if (fluentRequestPayload == null) { return(BadRequest(new Exception("Failed to parse the json payload, make sure it is valid"))); } var channel = await RequestChannel.CreateAsync(); var requestId = await channel.WriteAsync(fluentRequestPayload); var pollingUrl = Link($"GetCreateStatus/{requestId}"); HttpContext.Response.Headers["Location"] = pollingUrl; return(Ok(new BeginCreateResponse(pollingUrl))); }
protected async Task ValidateAndResolveResourceGroupAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { if (this.NewResourceGroup != null) { this.NewResourceGroup.Validate($"{propertyName}.newResourceGroup"); this.NewResourceGroup.ResolveInlineCreatable(azure, parentModel); this.NewResourceGroup.ResolveCreatableReference(azure, fluentRequestModel); // this.creatableResourceGroup = this.NewResourceGroup.GetCreatable(); } if (this.ExistingResourceGroup != null) { this.ExistingResourceGroup.Validate($"{propertyName}.existingResourceGroup"); await this.ExistingResourceGroup.ResolveResourceAsync(azure, cancellationToken); // this.resourceGroup = this.ExistingResourceGroup.GetResource(); } if (this.creatableResourceGroup == null && this.resourceGroup == null) { // if 'NewResourceGroup' and/or 'ExistingResourceGroup' is not set // in the request use parent model's resource group. // if (parentModel.CreatableResourceGroup() != null) { this.creatableResourceGroup = parentModel.CreatableResourceGroup(); } if (parentModel.ResourceGroup() != null) { this.resourceGroup = parentModel.ResourceGroup(); } if (this.creatableResourceGroup == null && this.resourceGroup == null) { throw new InvalidOperationException("Unable to derive resource group locally or from parent model"); } } if (this.Region == null) { this.Region = parentModel.Location(); if (this.Region == null) { throw new InvalidOperationException("Unable to derive region locally or from parent model"); } } }
public async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { if (this.Enabled == null) { throw new ArgumentException($"{propertyName} is specified then {propertyName}.enabled must be specified"); } if (this.NewStorageAccount != null) { NewStorageAccount.Validate($"{propertyName}.newStorageAccount"); NewStorageAccount.ResolveInlineCreatable(azure, parentModel); NewStorageAccount.ResolveCreatableReference(azure, fluentRequestModel); } if (this.ExistingStorageAccount != null) { ExistingStorageAccount.Validate($"{propertyName}.existingStorageAccount"); await ExistingStorageAccount.ResolveResourceAsync(azure, cancellationToken); } }
public override async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { await base.ValidateAndResolveResourceGroupAsync(azure, fluentRequestModel, propertyName, parentModel, cancellationToken); if (NewPrimaryNetwork != null) { NewPrimaryNetwork.Validate($"{propertyName}.newPrimaryNetwork"); NewPrimaryNetwork.ResolveInlineCreatable(azure, this); NewPrimaryNetwork.ResolveCreatableReference(azure, fluentRequestModel); } if (ExistingPrimaryNetwork != null) { ExistingPrimaryNetwork.Validate($"{propertyName}.existingPrimaryNetwork"); await ExistingPrimaryNetwork.ResolveResourceAsync(azure, cancellationToken); } if (NewPrimaryPublicIPAddress != null) { NewPrimaryPublicIPAddress.Validate($"{propertyName}.newPrimaryPublicIPAddress"); NewPrimaryPublicIPAddress.ResolveInlineCreatable(azure, this); NewPrimaryPublicIPAddress.ResolveCreatableReference(azure, fluentRequestModel); } if (ExistingPrimiaryPublicIPAddress != null) { ExistingPrimiaryPublicIPAddress.Validate($"{propertyName}.existingPrimiaryPublicIPAddress"); await ExistingPrimiaryPublicIPAddress.ResolveResourceAsync(azure, cancellationToken); } if (ExistingLoadBalancerBackends != null) { await ExistingLoadBalancerBackends.ValidateAndResolveAsync(azure, fluentRequestModel, $"{propertyName}.loadBalancerBackends", this, cancellationToken); } if (ExistingLoadBalancerInboundNatRules != null) { await ExistingLoadBalancerInboundNatRules.ValidateAndResolveAsync(azure, fluentRequestModel, $"{propertyName}.loadBalancerBackends", this, cancellationToken); } if (NewNetworkSecurityGroups != null) { await NewNetworkSecurityGroups.ValidateAndResolveAsync(azure, fluentRequestModel, $"{propertyName}.newPrimaryPublicIPAddress", this, cancellationToken); } if (ExistingNetworkSecurityGroups != null) { await ExistingNetworkSecurityGroups.ValidateAndResolveAsync(azure, fluentRequestModel, $"{propertyName}.existingNetworkSecurityGroups", this, cancellationToken); } }
public Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { if (this.Region == null) { if (parentModel.Location() != null) { this.Region = parentModel.Location().ToString(); } else { this.Region = Microsoft.Azure.Management.ResourceManager.Fluent.Core.Region.USEast2.ToString(); } } // // This model has no "New{Resource}" properties on which Validation and Resolution // needs to be run // return(Task.CompletedTask); }
public void ResolveCreatableReference(IAzure azure, FluentRequestModel fluentRequestModel) { if (this.creatable == null) { String refName = null; int refIndex = -1; try { Regex regex = new Regex(@"(\w+)\[([0-9]+)]"); Match match = regex.Match(this.Reference); if (match.Success && match.Groups.Count == 3) { refName = match.Groups[1].Value; refIndex = Int16.Parse(match.Groups[2].Value); } else { throw new ArgumentException($"Malformed reference {this.Reference}"); } } catch (Exception) { throw new ArgumentException($"Malformed reference {this.Reference}"); } if (!refName.Equals(ReferencePrefix(), StringComparison.OrdinalIgnoreCase)) { throw new ArgumentException($"Value of {ReferencePath()} should be {ReferencePrefix()}[index] but found {this.Reference}"); } var creatableModels = this.CreatableModels(fluentRequestModel); if (creatableModels.Count < refIndex) { throw new ArgumentException($"The index {refIndex} of the reference {this.Reference} is out of boundary"); } var creatableModel = creatableModels[refIndex]; this.creatable = creatableModel.ToCreatable(azure); } }
/// <summary> /// Gets the model collection in the request containing the creatable entry this new resource references. /// </summary> /// <param name="fluentRequestModel">The request instance in which property of this type belongs to</param> /// <returns>The model collection</returns> protected abstract CreatableModels <ModelT, FluentT> CreatableModels(FluentRequestModel fluentRequestModel);
public abstract Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken));
/// <summary> /// Creates RequestChannelData. /// </summary> /// <param name="requestId">The request id.</param> /// <param name="requestModel">The request payload.</param> internal RequestChannelData(string requestId, FluentRequestModel requestModel) { this.RequestId = requestId; this.RequestModel = requestModel; }
protected override CreatableModels <StorageAccountModel, IStorageAccount> CreatableModels(FluentRequestModel fluentRequestModel) { return(fluentRequestModel.StorageAccountModels); }
protected override CreatableModels <NetworkSecurityGroupModel, INetworkSecurityGroup> CreatableModels(FluentRequestModel fluentRequestModel) { return(fluentRequestModel.NetworkSecurityGroupModels); }
public override async Task ValidateAndResolveAsync(IAzure azure, FluentRequestModel fluentRequestModel, string propertyName, IGroupableModel parentModel, CancellationToken cancellationToken = default(CancellationToken)) { await base.ValidateAndResolveResourceGroupAsync(azure, fluentRequestModel, propertyName, parentModel); }
protected override CreatableModels <NetworkModel, INetwork> CreatableModels(FluentRequestModel fluentRequestModel) { return(fluentRequestModel.NetworkModels); }
protected override CreatableModels <PublicIPAddressModel, IPublicIPAddress> CreatableModels(FluentRequestModel fluentRequestModel) { return(fluentRequestModel.PublicIPAddressModels); }
/// <summary> /// Handle a fluent request from the REST endpoint. /// </summary> /// <param name="requestId">the request id</param> /// <param name="fluentRequestModel">the fluent request</param> /// <param name="cancellationToken">task cancellation token</param> /// <returns>task representing the request handling</returns> private async Task HandleRequestAsync(string requestId, FluentRequestModel fluentRequestModel, CancellationToken cancellationToken = default(CancellationToken)) { IAzure azure = GetAzure(); var progressReport = new ResourceCreateProgressReport(await ResourceCreateStatusesTable.CreateAsync(requestId, cancellationToken)); Dictionary <string, List <dynamic> > rootCreatablesDict = new Dictionary <string, List <dynamic> >(); try { await fluentRequestModel.ValidateAndResolveAsync(azure, fluentRequestModel, "", new DefaultGroupableModel(azure), cancellationToken); rootCreatablesDict = fluentRequestModel.ResolveRootCreatables(azure); } catch (Exception exception) { await progressReport.FailedAsync(FailureRowKey, null, "Error", exception, cancellationToken); return; } foreach (var rootCreatableDictEntry in rootCreatablesDict) { var resourceType = rootCreatableDictEntry.Key; foreach (var rootCreatable in rootCreatableDictEntry.Value) { switch (resourceType) { case ResourceCollectionType.ResourceGroups: await InvokeCreateIngoreErrorAsync((ICreatable <IResourceGroup>) rootCreatable, progressReport, cancellationToken); break; case ResourceCollectionType.PublicIPAddresses: await InvokeCreateIngoreErrorAsync((ICreatable <IPublicIPAddress>) rootCreatable, progressReport, cancellationToken); break; case ResourceCollectionType.Networks: await InvokeCreateIngoreErrorAsync((ICreatable <INetwork>) rootCreatable, progressReport, cancellationToken); break; case ResourceCollectionType.NetworkSecurityGroups: await InvokeCreateIngoreErrorAsync((ICreatable <INetworkSecurityGroup>) rootCreatable, progressReport, cancellationToken); break; case ResourceCollectionType.NetworkInterfaces: await InvokeCreateIngoreErrorAsync((ICreatable <INetworkInterface>) rootCreatable, progressReport, cancellationToken); break; case ResourceCollectionType.StorageAccounts: await InvokeCreateIngoreErrorAsync((ICreatable <IStorageAccount>) rootCreatable, progressReport, cancellationToken); break; case ResourceCollectionType.VirtualMachines: await InvokeCreateIngoreErrorAsync((ICreatable <IVirtualMachine>) rootCreatable, progressReport, cancellationToken); break; default: await progressReport.FailedAsync(FailureRowKey, null, "Error", new InvalidOperationException($"Unknown creatable type {resourceType}"), cancellationToken); break; } } } }
protected override CreatableModels <ResourceGroupModel, IResourceGroup> CreatableModels(FluentRequestModel fluentRequestModel) { return(fluentRequestModel.ResourceGroupModels); }