public override void ExecuteCmdlet() { var subscription = SubscriptionId ?? DefaultContext.Subscription.Id; try { switch (ParameterSetName) { case ParameterSetNames.BlueprintAssignmentsBySubscription: foreach (var assignment in BlueprintClient.ListBlueprintAssignments(Utils.GetScopeForSubscription(subscription))) { WriteObject(assignment, true); } break; case ParameterSetNames.BlueprintAssignmentByName: WriteObject(BlueprintClient.GetBlueprintAssignment(Utils.GetScopeForSubscription(subscription), Name)); break; default: throw new PSInvalidOperationException(); } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { try { Utils.ValidateName(Name); var bp = CreateBlueprint(GetValidatedFilePath(BlueprintFile)); RegisterBlueprintRp(SubscriptionId ?? DefaultContext.Subscription.Id); switch (ParameterSetName) { case ParameterSetNames.CreateBlueprintBySubscription: var subScope = Utils.GetScopeForSubscription(SubscriptionId ?? DefaultContext.Subscription.Id); ThrowIfBlueprintExits(subScope, Name); WriteObject(BlueprintClient.CreateOrUpdateBlueprint(subScope, Name, bp)); break; case ParameterSetNames.CreateBlueprintByManagementGroup: var mgScope = Utils.GetScopeForManagementGroup(ManagementGroupId); ThrowIfBlueprintExits(mgScope, Name); WriteObject(BlueprintClient.CreateOrUpdateBlueprint(mgScope, Name, bp)); break; } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { var scope = GetCurrentScope(); try { switch (ParameterSetName) { case ParameterSetNames.ManagementGroupScope: foreach (var bp in BlueprintClientWithVersion.ListBlueprints(scope)) { WriteObject(bp, true); } break; case ParameterSetNames.SubscriptionScope: var queryScopes = GetManagementGroupAncestorsForSubscription( SubscriptionId ?? DefaultContext.Subscription.Id) .Select(mg => FormatManagementGroupAncestorScope(mg)) .ToList(); //add current subscription scope to the list of MG scopes that we'll query queryScopes.Add(scope); foreach (var bp in BlueprintClientWithVersion.ListBlueprints(queryScopes)) { WriteObject(bp, true); } break; case ParameterSetNames.BySubscriptionAndName: case ParameterSetNames.ByManagementGroupAndName: WriteObject(BlueprintClientWithVersion.GetBlueprint(scope, Name)); break; case ParameterSetNames.BySubscriptionNameAndVersion: case ParameterSetNames.ByManagementGroupNameAndVersion: WriteObject(BlueprintClient.GetPublishedBlueprint(scope, Name, Version)); break; case ParameterSetNames.BySubscriptionNameAndLatestPublished: case ParameterSetNames.ByManagementGroupNameAndLatestPublished: WriteObject(BlueprintClient.GetLatestPublishedBlueprint(scope, Name)); break; default: throw new PSInvalidOperationException(); } } catch (Exception ex) { WriteExceptionError(ex); } }
/// <summary> /// Get Blueprint SPN object Id for this tenant /// </summary> /// <param name="scope"></param> /// <param name="assignmentName"></param> /// <returns>"spnObjectId"</returns> protected string GetBlueprintSpn(string scope, string assignmentName) { var response = BlueprintClient.GetBlueprintSpnObjectId(scope, assignmentName); if (response == null) { throw new KeyNotFoundException(Resources.BlueprintSpnObjectIdNotFound); } return response.ObjectId; }
public override void ExecuteCmdlet() { try { switch (ParameterSetName) { case ParameterSetNames.ByManagementGroupAndName: if (ShouldProcess(ManagementGroupId, string.Format(Resources.DeleteAssignmentShouldProcessString, Name))) { var deletedAssignment = BlueprintClient.DeleteBlueprintAssignment(Utils.GetScopeForManagementGroup(ManagementGroupId), Name); if (deletedAssignment != null && PassThru.IsPresent) { WriteObject(deletedAssignment); } } break; case ParameterSetNames.BySubscriptionAndName: var subscription = SubscriptionId ?? DefaultContext.Subscription.Id; if (ShouldProcess(subscription, string.Format(Resources.DeleteAssignmentShouldProcessString, Name))) { var deletedAssignment = BlueprintClient.DeleteBlueprintAssignment(Utils.GetScopeForSubscription(subscription), Name); if (deletedAssignment != null && PassThru.IsPresent) { WriteObject(deletedAssignment); } } break; case ParameterSetNames.DeleteBlueprintAssignmentByObject: if (ShouldProcess(InputObject.Scope, string.Format(Resources.DeleteAssignmentShouldProcessString, InputObject.Name))) { var deletedAssignment = BlueprintClient.DeleteBlueprintAssignment(InputObject.Scope, InputObject.Name); if (deletedAssignment != null && PassThru.IsPresent) { WriteObject(deletedAssignment); } } break; default: throw new PSInvalidOperationException(); } } catch (Exception ex) { WriteExceptionError(ex); } }
protected bool BlueprintExists(string scope, string blueprintName) { PSBlueprintBase blueprint = null; try { blueprint = BlueprintClient.GetBlueprint(scope, blueprintName); } catch (Exception ex) { if (ex is CloudException cex && cex.Response.StatusCode == System.Net.HttpStatusCode.NotFound) { // Blueprint doesn't exists. Ignore the exception and continue. }
public override void ExecuteCmdlet() { if (ShouldProcess(Utils.GetDefinitionLocationId(Blueprint.Scope), string.Format(Resources.PublishBlueprintShouldProcessString, Blueprint.Name))) { try { WriteObject(BlueprintClient.CreatePublishedBlueprint(Blueprint.Scope, Blueprint.Name, Version)); } catch (Exception ex) { WriteExceptionError(ex); } } }
public override void ExecuteCmdlet() { const string blueprintFileName = "Blueprint"; // Get serialized blueprint definition string serializedDefinition = BlueprintClient.GetBlueprintDefinitionJsonFromObject(Blueprint, Version); var resolvedPath = ResolveUserPath(OutputPath); var blueprintFolderPath = Path.Combine(resolvedPath, Blueprint.Name); // if directory exists, let's ask if we can clean contents of it. If directory doesn't exist, we'll create one. this.ConfirmAction( this.Force || !AzureSession.Instance.DataStore.DirectoryExists(blueprintFolderPath), string.Format(Resources.DeleteBlueprintFolderContentsProcessString, Blueprint.Name), Resources.DeleteBlueprintFolderContentsContinueMessage, blueprintFolderPath, () => CreateFolderIfNotExist(resolvedPath, Blueprint.Name) ); var blueprintJsonFilePath = Path.Combine(blueprintFolderPath, $"{blueprintFileName}.json"); AzureSession.Instance.DataStore.WriteFile(blueprintJsonFilePath, serializedDefinition); var artifacts = BlueprintClient.ListArtifacts(Blueprint.Scope, Blueprint.Name, Version); if (artifacts != null && artifacts.Any()) { // Get serialized artifacts from this blueprint and write them to disk var artifactsPath = CreateFolderIfNotExist(blueprintFolderPath, "Artifacts"); foreach (var artifact in artifacts) { string serializedArtifact = BlueprintClient.GetBlueprintArtifactJsonFromObject(Blueprint.Scope, Blueprint.Name, artifact, Version); var artifactFilePath = Path.Combine(artifactsPath, artifact.Name + ".json"); AzureSession.Instance.DataStore.WriteFile(artifactFilePath, serializedArtifact); } } if (PassThru.IsPresent) { WriteObject(true); } }
public override void ExecuteCmdlet() { try { var subscriptionsList = SubscriptionId ?? new[] { DefaultContext.Subscription.Id }; // TO-DO: Update should process string here. if (ShouldProcess(string.Join(",", subscriptionsList), string.Format(Resources.CreateAssignmentShouldProcessString, Name))) { var assignment = CreateAssignmentObject( this.IsParameterBound(c => c.UserAssignedIdentity) ? ManagedServiceIdentityType.UserAssigned : ManagedServiceIdentityType.SystemAssigned, this.IsParameterBound(c => c.UserAssignedIdentity) ? UserAssignedIdentity : null, Location, Blueprint.Id, Lock, Parameter, ResourceGroupParameter, SecureStringParameter); foreach (var subscription in subscriptionsList) { var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentExits(scope, Name); // Register Blueprint RP RegisterBlueprintRp(subscription); if (!this.IsParameterBound(c => c.UserAssignedIdentity)) { var spnObjectId = GetBlueprintSpn(scope, Name); AssignOwnerPermission(subscription, spnObjectId); } WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignment)); } } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { if (ShouldProcess(Utils.GetDefinitionLocationId(Blueprint.Scope), string.Format(Resources.PublishBlueprintShouldProcessString, Blueprint.Name))) { try { // parameters property is placeholder here, backend strips this information and publishes the latest master. It is needed in the payload since swagger calls it required. var publishedBlueprintObjForChangeNotes = new PublishedBlueprint( parameters: new Dictionary <string, ParameterDefinition>(), changeNotes: ChangeNote); WriteObject(BlueprintClient.CreatePublishedBlueprint(Blueprint.Scope, Blueprint.Name, Version, publishedBlueprintObjForChangeNotes)); } catch (Exception ex) { WriteExceptionError(ex); } } }
public override void ExecuteCmdlet() { var scope = Blueprint.Scope; try { if (this.IsParameterBound(c => c.Name)) { WriteObject(BlueprintClient.GetArtifact(scope, Blueprint.Name, Name, BlueprintVersion)); } else { WriteObject(BlueprintClient.ListArtifacts(scope, Blueprint.Name, BlueprintVersion)); } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { // Get serialized blueprint and write it to disk string serializedDefinition = BlueprintClient.GetBlueprintDefinitionJsonFromObject(Blueprint, Version); var blueprintFolderPath = CreateFolderIfNotExist(OutputPath, Blueprint.Name); var blueprintJsonFilePath = Path.Combine(blueprintFolderPath, $"{Blueprint.Name}.json"); this.ConfirmAction( this.Force || !AzureSession.Instance.DataStore.FileExists(blueprintJsonFilePath), string.Format(Resources.OverwriteExistingOutputFileProcessMessage, Blueprint.Name), Resources.OverwriteExistingOutputFileContinueMessage, blueprintJsonFilePath, () => AzureSession.Instance.DataStore.WriteFile(blueprintJsonFilePath, serializedDefinition) ); // Get serialized artifacts from this blueprint and write them to disk var artifactsPath = CreateFolderIfNotExist(blueprintFolderPath, "Artifacts"); var artifacts = BlueprintClient.ListArtifacts(Blueprint.Scope, Blueprint.Name, Version); foreach (var artifact in artifacts) { string serializedArtifact = BlueprintClient.GetBlueprintArtifactJsonFromObject(Blueprint.Scope, Blueprint.Name, artifact, Version); var artifactFilePath = Path.Combine(artifactsPath, artifact.Name + ".json"); this.ConfirmAction( this.Force || !AzureSession.Instance.DataStore.FileExists(artifactFilePath), string.Format(Resources.OverwriteExistingOutputFileProcessMessage, artifact.Name), Resources.OverwriteExistingOutputFileContinueMessage, artifactFilePath, () => AzureSession.Instance.DataStore.WriteFile(artifactFilePath, serializedArtifact) ); } if (PassThru.IsPresent) { WriteObject(true); } }
protected void ThrowIfAssignmentNotExist(string scope, string name) { PSBlueprintAssignment assignment = null; try { assignment = BlueprintClient.GetBlueprintAssignment(scope, name); } catch (Exception ex) { if (ex is CloudException cex && cex.Response.StatusCode != System.Net.HttpStatusCode.NotFound) { // if exception is for a reason other than .NotFound, pass it to the caller. throw; } } if (assignment == null) { throw new Exception(string.Format(Resources.AssignmentNotExist, name, scope)); } }
protected void ThrowIfArtifactExits(string scope, string blueprintName, string artifactName) { PSArtifact artifact = null; try { artifact = BlueprintClient.GetArtifact(scope, blueprintName, artifactName, null); } catch (Exception ex) { if (ex is CloudException cex && cex.Response.StatusCode != System.Net.HttpStatusCode.NotFound) { // if exception is for a reason other than .NotFound, pass it to the caller. throw; } } if (artifact != null) { throw new Exception(string.Format(Resources.ArtifactExists, artifactName, blueprintName)); } }
public override void ExecuteCmdlet() { try { var subscriptionsList = SubscriptionId ?? new[] { DefaultContext.Subscription.Id }; switch (ParameterSetName) { case ParameterSetNames.UpdateBlueprintAssignment: if (ShouldProcess(string.Join(",", subscriptionsList), string.Format(Resources.UpdateAssignmentShouldProcessString, Name))) { var assignment = CreateAssignmentObject( this.IsParameterBound(c => c.UserAssignedIdentity) ? ManagedServiceIdentityType.UserAssigned : ManagedServiceIdentityType.SystemAssigned, this.IsParameterBound(c => c.UserAssignedIdentity) ? UserAssignedIdentity : null, Location, Blueprint.Id, Lock, Parameter, ResourceGroupParameter, SecureStringParameter); foreach (var subscription in subscriptionsList) { var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentNotExist(scope, Name); // Register Blueprint RP RegisterBlueprintRp(subscription); if (!this.IsParameterBound(c => c.UserAssignedIdentity)) { var spnObjectId = GetBlueprintSpn(scope, Name); AssignOwnerPermission(subscription, spnObjectId); } WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignment)); } } break; case ParameterSetNames.UpdateBlueprintAssignmentByFile: if (ShouldProcess(string.Join(",", subscriptionsList), string.Format(Resources.UpdateAssignmentShouldProcessString, Name))) { var parametersFilePath = GetValidatedFilePath(AssignmentFile); foreach (var subscription in subscriptionsList) { Assignment assignmentObject; try { assignmentObject = JsonConvert.DeserializeObject <Assignment>( AzureSession.Instance.DataStore.ReadFileAsText(parametersFilePath), DefaultJsonSettings.DeserializerSettings); } catch (Exception ex) { throw new Exception("Can't deserialize the JSON file: " + parametersFilePath + ". " + ex.Message); } var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentNotExist(scope, Name); // Register Blueprint RP RegisterBlueprintRp(subscription); if (!this.IsParameterBound(c => c.UserAssignedIdentity)) { var spnObjectId = GetBlueprintSpn(scope, Name); AssignOwnerPermission(subscription, spnObjectId); } WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignmentObject)); } } break; default: throw new PSInvalidOperationException(); } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { try { var subscriptionsList = SubscriptionId ?? new[] { DefaultContext.Subscription.Id }; if (ShouldProcess(string.Join(",", subscriptionsList), string.Format(Resources.UpdateAssignmentShouldProcessString, Name))) { // If explicitly requested to use user assigned identity let's do that, otherwise let's default to system assigned if (this.IsParameterBound(c => c.UserAssignedIdentity)) { var userAssignedIdentity = new Dictionary <string, UserAssignedIdentity>() { { UserAssignedIdentity, new UserAssignedIdentity() } }; var assignment = CreateAssignmentObject(ManagedServiceIdentityType.UserAssigned, userAssignedIdentity, Location, Blueprint.Id, Lock, Parameter, ResourceGroupParameter); foreach (var subscription in subscriptionsList) { var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentNotExist(scope, Name); // Register Blueprint RP RegisterBlueprintRp(subscription); WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignment)); } } else { var assignment = CreateAssignmentObject(ManagedServiceIdentityType.SystemAssigned, null, Location, Blueprint.Id, Lock, Parameter, ResourceGroupParameter); foreach (var subscription in subscriptionsList) { var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentNotExist(scope, Name); // First Register Blueprint RP and grant owner permission to BP service principal RegisterBlueprintRp(subscription); var servicePrincipal = GetBlueprintSpn(); AssignOwnerPermission(subscription, servicePrincipal); WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignment)); } } } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { try { var scope = Blueprint.Scope; ThrowIfArtifactExits(scope, Blueprint.Name, Name); switch (ParameterSetName) { case ParameterSetNames.CreateArtifactByInputFile: if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.CreateArtifactShouldProcessString, Name))) { var artifact = JsonConvert.DeserializeObject <Artifact>( AzureSession.Instance.DataStore.ReadFileAsText(ResolveUserPath(ArtifactFile)), DefaultJsonSettings.DeserializerSettings); WriteObject(BlueprintClient.CreateArtifact(scope, Blueprint.Name, Name, artifact)); } break; case ParameterSetNames.CreateRoleAssignmentArtifact: if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.CreateArtifactShouldProcessString, Name))) { // Check if chosen -Type parameter matches with parameters set if (!Type.Equals(PSArtifactKind.RoleAssignmentArtifact)) { throw new PSInvalidOperationException("Artifact type mismatch."); } var roleAssignmentArtifact = new RoleAssignmentArtifact { DisplayName = Name, Description = Description, RoleDefinitionId = RoleDefinitionId, PrincipalIds = RoleDefinitionPrincipalId, ResourceGroup = ResourceGroupName, DependsOn = DependsOn }; WriteObject(BlueprintClient.CreateArtifact(scope, Blueprint.Name, Name, roleAssignmentArtifact)); } break; case ParameterSetNames.CreatePolicyAssignmentArtifact: if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.CreateArtifactShouldProcessString, Name))) { if (!Type.Equals(PSArtifactKind.PolicyAssignmentArtifact)) { throw new PSInvalidOperationException("Artifact type mismatch."); } var policyAssignmentParameters = GetPolicyAssignmentParameters(PolicyDefinitionParameter); var policyArtifact = new PolicyAssignmentArtifact { DisplayName = Name, Description = Description, PolicyDefinitionId = PolicyDefinitionId, Parameters = policyAssignmentParameters, DependsOn = DependsOn, ResourceGroup = ResourceGroupName }; WriteObject(BlueprintClient.CreateArtifact(scope, Blueprint.Name, Name, policyArtifact)); } break; case ParameterSetNames.CreateTemplateArtifact: if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.CreateArtifactShouldProcessString, Name))) { if (!Type.Equals(PSArtifactKind.TemplateArtifact)) { throw new PSInvalidOperationException("Artifact type mismatch."); } var parameters = GetTemplateParametersFromFile(ValidateAndReturnFilePath(TemplateParameterFile)); var templateArtifact = new TemplateArtifact { DisplayName = Name, Description = Description, ResourceGroup = ResourceGroupName, Parameters = parameters, Template = JObject.Parse(AzureSession.Instance.DataStore.ReadFileAsText(ValidateAndReturnFilePath(TemplateFile))), DependsOn = DependsOn }; WriteObject(BlueprintClient.CreateArtifact(scope, Blueprint.Name, Name, templateArtifact)); } break; default: throw new PSInvalidOperationException(); } } catch (Exception ex) { WriteExceptionError(ex); } }
public override void ExecuteCmdlet() { try { Utils.ValidateName(Name); var subscriptionsList = SubscriptionId ?? new[] { DefaultContext.Subscription.Id }; switch (ParameterSetName) { case ParameterSetNames.CreateBlueprintAssignment: if (ShouldProcess(string.Join(",", subscriptionsList), string.Format(Resources.CreateAssignmentShouldProcessString, Name))) { var assignment = CreateAssignmentObject( this.IsParameterBound(c => c.UserAssignedIdentity) ? ManagedServiceIdentityType.UserAssigned : ManagedServiceIdentityType.SystemAssigned, this.IsParameterBound(c => c.UserAssignedIdentity) ? UserAssignedIdentity : null, Location, Blueprint.Id, Lock, Parameter, ResourceGroupParameter, SecureStringParameter); foreach (var subscription in subscriptionsList) { var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentExists(scope, Name); // Register Blueprint RP RegisterBlueprintRp(subscription); if (!this.IsParameterBound(c => c.UserAssignedIdentity)) { var spnObjectId = GetBlueprintSpn(scope, Name); AssignOwnerPermission(subscription, spnObjectId); } WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignment)); } } break; case ParameterSetNames.CreateBlueprintAssignmentByFile: if (ShouldProcess(string.Join(",", subscriptionsList), string.Format(Resources.CreateAssignmentShouldProcessString, Name))) { var parametersFilePath = GetValidatedFilePath(AssignmentFile); foreach (var subscription in subscriptionsList) { Assignment assignmentObject; try { assignmentObject = JsonConvert.DeserializeObject <Assignment>( AzureSession.Instance.DataStore.ReadFileAsText(parametersFilePath), DefaultJsonSettings.DeserializerSettings); } catch (Exception ex) { throw new Exception(string.Format(Resources.CantDeserializeJson, parametersFilePath, ex.Message)); } var scope = Utils.GetScopeForSubscription(subscription); ThrowIfAssignmentExists(scope, Name); // Register Blueprint RP RegisterBlueprintRp(subscription); if (!IsUserAssignedIdentity(assignmentObject.Identity)) { // If user assigned identity is defined as the identity in the assignment // we consider the user assigned MSI, otherwise system assigned MSI. // // Assign owner permission to Blueprint SPN only if assignment is being done using // System assigned identity. // This is a no-op for user assigned identity. var spnObjectId = GetBlueprintSpn(scope, Name); AssignOwnerPermission(subscription, spnObjectId); } WriteObject(BlueprintClient.CreateOrUpdateBlueprintAssignment(scope, Name, assignmentObject)); } } break; } } catch (Exception ex) { WriteExceptionError(ex); } }