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);
            }
        }
Exemplo n.º 2
0
        public override void ExecuteCmdlet()
        {
            try
            {
                var scope = Blueprint.Scope;

                ThrowIfArtifactNotExist(scope, Blueprint.Name, Name);

                switch (ParameterSetName)
                {
                case ParameterSetNames.UpdateArtifactByInputFile:
                    if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.UpdateArtifactShouldProcessString, 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.UpdateRoleAssignmentArtifact:
                    if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.UpdateArtifactShouldProcessString, 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.UpdatePolicyAssignmentArtifact:
                    if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.UpdateArtifactShouldProcessString, 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.UpdateTemplateArtifact:
                    if (ShouldProcess(Utils.GetDefinitionLocationId(scope), string.Format(Resources.UpdateArtifactShouldProcessString, 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(BlueprintClientWithVersion.CreateArtifact(scope, Blueprint.Name, Name,
                                                                              templateArtifact));
                    }

                    break;

                default:
                    throw new PSInvalidOperationException();
                }
            }
            catch (Exception ex)
            {
                WriteExceptionError(ex);
            }
        }