Exemplo n.º 1
0
 public static SupportTicketDetails ToSdkSupportTicket(this PSSupportTicket psSupportTicketDetails)
 {
     return(new SupportTicketDetails
     {
         Title = psSupportTicketDetails.Title,
         Description = psSupportTicketDetails.Description,
         SupportTicketId = psSupportTicketDetails.SupportTicketId,
         ServiceId = psSupportTicketDetails.ServiceId,
         ProblemClassificationId = psSupportTicketDetails.ProblemClassificationId,
         Severity = psSupportTicketDetails.Severity,
         Require24X7Response = psSupportTicketDetails.Require24X7Response,
         ProblemStartTime = psSupportTicketDetails.ProblemStartTime,
         ContactDetails = psSupportTicketDetails.ContactDetail.ToContactProfile(),
         TechnicalTicketDetails = !string.IsNullOrWhiteSpace(psSupportTicketDetails.TechnicalTicketResourceId) ?
                                  new TechnicalTicketDetails
         {
             ResourceId = psSupportTicketDetails.TechnicalTicketResourceId
         } :
         null,
         QuotaTicketDetails = psSupportTicketDetails.QuotaTicketDetail.ToQuotaTicketDetails()
     });
 }
        public override void ExecuteCmdlet()
        {
            try
            {
                if (this.IsParameterBound(c => this.ProblemStartTime))
                {
                    if (this.ProblemStartTime == DateTime.MinValue || this.ProblemStartTime > DateTime.Now)
                    {
                        throw new PSArgumentException(string.Format("ProblemStartTime {0} is not valid.", this.ProblemStartTime));
                    }
                }

                var problemClassificationResourceId = ResourceIdentifierHelper.BuildResourceIdentifier(this.ProblemClassificationId, ResourceType.ProblemClassifications);

                var checkNameAvailabilityInput = new CheckNameAvailabilityInput
                {
                    Type = Management.Support.Models.Type.MicrosoftSupportSupportTickets,
                    Name = this.Name
                };

                var checkNameAvailabilityResult = this.SupportClient.SupportTickets.CheckNameAvailability(checkNameAvailabilityInput);

                if (checkNameAvailabilityResult.NameAvailable.HasValue &&
                    !checkNameAvailabilityResult.NameAvailable.Value)
                {
                    throw new PSArgumentException(string.Format("A SupportTicket with name '{0}' cannot be created for the reason {1}.", this.Name, checkNameAvailabilityResult.Reason));
                }

                if (this.IsParameterBound(c => c.TechnicalTicketResourceId))
                {
                    var technicalResourceId = new ResourceIdentifier(this.TechnicalTicketResourceId);

                    if (!technicalResourceId.Subscription.Equals(this.SupportClient.SubscriptionId, StringComparison.OrdinalIgnoreCase))
                    {
                        throw new PSArgumentException(string.Format("TechnicalResourceId {0} does not belong in subscription {1}.", this.TechnicalTicketResourceId, this.SupportClient.SubscriptionId));
                    }

                    var resourceClient = AzureSession.Instance.ClientFactory.CreateArmClient <ResourceManagementClient>(
                        DefaultProfile.DefaultContext,
                        AzureEnvironment.Endpoint.ResourceManager);

                    var oDataQuery = new ODataQuery <GenericResourceFilter>($"resourceGroup eq '{technicalResourceId.ResourceGroupName}' and resourceType eq '{technicalResourceId.ResourceType}' and name eq '{technicalResourceId.ResourceName}'");

                    var result = resourceClient.Resources.List(oDataQuery);

                    if (result.Count() != 1)
                    {
                        throw new Exception(string.Format("TechnicalResourceId {0} was not found in subscription {1}.", this.TechnicalTicketResourceId, this.SupportClient.SubscriptionId));
                    }
                }

                var customHeaders = new Dictionary <string, List <string> >();
                if (!string.IsNullOrEmpty(this.CSPHomeTenantId))
                {
                    if (!Guid.TryParse(this.CSPHomeTenantId, out var result))
                    {
                        throw new PSArgumentException(string.Format("CSPHomeTenantId {0} is not a valid Guid.", this.CSPHomeTenantId));
                    }

                    var auxToken = AzureSession.Instance.AuthenticationFactory.Authenticate(this.DefaultContext.Account, this.DefaultContext.Environment, this.CSPHomeTenantId, null, "Never", null);
                    customHeaders.Add(AUX_HEADER_NAME, new List <string> {
                        $"{AUX_TOKEN_PREFIX} {auxToken.AccessToken}"
                    });
                }

                PSContactProfile contactObject = null;
                if (this.ParameterSetName.Equals(CreateSupportTicketWithContactObjectParameterSet) ||
                    this.ParameterSetName.Equals(CreateQuotaSupportTicketWithContactObjectParameterSet) ||
                    this.ParameterSetName.Equals(CreateTechnicalSupportTicketWithContactObjectParameterSet))
                {
                    contactObject = this.CustomerContactDetail;
                }
                else
                {
                    contactObject = new PSContactProfile
                    {
                        FirstName                = this.CustomerFirstName,
                        LastName                 = this.CustomerLastName,
                        PrimaryEmailAddress      = this.CustomerPrimaryEmailAddress,
                        PreferredTimeZone        = this.CustomerPreferredTimeZone,
                        PreferredSupportLanguage = this.CustomerPreferredSupportLanguage,
                        PhoneNumber              = this.CustomerPhoneNumber,
                        AdditionalEmailAddresses = this.AdditionalEmailAddress,
                        Country = this.CustomerCountry,
                        PreferredContactMethod = this.PreferredContactMethod.ToString()
                    };
                }

                var supportTicket = new PSSupportTicket
                {
                    Title                     = this.Title,
                    Description               = this.Description,
                    ServiceId                 = $"/providers/Microsoft.Support/services/{problemClassificationResourceId.ParentResource}",
                    ProblemClassificationId   = this.ProblemClassificationId,
                    Severity                  = this.Severity.ToString(),
                    Require24X7Response       = this.Require24X7Response.IsPresent ? true : (bool?)null,
                    ProblemStartTime          = this.IsParameterBound(c => this.ProblemStartTime) ? this.ProblemStartTime.ToUniversalTime() : (DateTime?)null,
                    ContactDetail             = contactObject,
                    TechnicalTicketResourceId = this.TechnicalTicketResourceId,
                    QuotaTicketDetail         = this.QuotaTicketDetail,
                };

                if (this.ShouldProcess(this.Name, string.Format("Creating a new SupportTicket with name '{0}'.", this.Name)))
                {
                    var sdkSupportTicket = supportTicket.ToSdkSupportTicket();
                    var result           = this.SupportClient.CreateSupportTicketForSubscription(this.Name, sdkSupportTicket, customHeaders);

                    this.WriteObject(result.ToPSSupportTicket());
                }
            }
            catch (ExceptionResponseException ex)
            {
                throw new PSArgumentException(string.Format("Error response received. Error Message: '{0}'",
                                                            ex.Response.Content));
            }
        }