/// <summary>
        /// Serializes the object to JSON.
        /// </summary>
        /// <param name="writer">The <see cref="T: Newtonsoft.Json.JsonWriter" /> to write to.</param>
        /// <param name="obj">The object to serialize to JSON.</param>
        internal static void Serialize(JsonWriter writer, PartitionBackupEntity obj)
        {
            // Required properties are always serialized, optional properties are serialized when not null.
            writer.WriteStartObject();
            writer.WriteProperty(obj.EntityKind, "EntityKind", BackupEntityKindConverter.Serialize);
            if (obj.ServiceName != null)
            {
                writer.WriteProperty(obj.ServiceName, "ServiceName", ServiceNameConverter.Serialize);
            }

            if (obj.PartitionId != null)
            {
                writer.WriteProperty(obj.PartitionId, "PartitionId", PartitionIdConverter.Serialize);
            }

            writer.WriteEndObject();
        }
        /// <summary>
        /// Reads JSON to create a particular Retention policy Type
        /// </summary>
        /// <returns>Retention Type Object created from JSON</returns>
        public override object ReadJson(JsonReader reader,
                                        Type objectType,
                                        object existingValue,
                                        JsonSerializer serializer)
        {
            BackupEntity backupEntity           = null;
            JObject      retentionPolicyJObject = JObject.Load(reader);

            if (retentionPolicyJObject["EntityKind"] == null)
            {
                var fabricError = new FabricError
                {
                    Message = string.Format("EntityKind is Required"),
                    Code    = NativeTypes.FABRIC_ERROR_CODE.E_INVALIDARG,
                };

                throw new HttpResponseException(new FabricErrorError(fabricError).ToHttpResponseMessage());
            }
            if ("Application".Equals(retentionPolicyJObject["EntityKind"].Value <string>()))
            {
                backupEntity = new ApplicationBackupEntity();
            }
            else if ("Service".Equals(retentionPolicyJObject["EntityKind"].Value <string>()))
            {
                backupEntity = new ServiceBackupEntity();
            }
            else if ("Partition".Equals(retentionPolicyJObject["EntityKind"].Value <string>()))
            {
                backupEntity = new PartitionBackupEntity();
            }
            else
            {
                var fabricError = new FabricError
                {
                    Message = string.Format("Invalid Backup Entitiy Type {0} ", retentionPolicyJObject["EntityKind"].Value <string>()),
                    Code    = NativeTypes.FABRIC_ERROR_CODE.E_INVALIDARG,
                };

                throw new HttpResponseException(new FabricErrorError(fabricError).ToHttpResponseMessage());
            }
            serializer.Populate(retentionPolicyJObject.CreateReader(), backupEntity);
            return(backupEntity);
        }
Example #3
0
        protected internal void PopulateApplicationServiceAndPartitionWithBackupEntity(BackupEntity backupEntity, TimeSpan timeout, CancellationToken cancellationToken)
        {
            switch (backupEntity.EntityKind)
            {
            case BackupEntityKind.Application:
                ApplicationBackupEntity applicationBackupEntity = (ApplicationBackupEntity)backupEntity;
                this.FabricUri = applicationBackupEntity.ApplicationName;
                break;

            case BackupEntityKind.Service:
                ServiceBackupEntity serviceBackupEntity = (ServiceBackupEntity)backupEntity;
                this.FabricUri = serviceBackupEntity.ServiceName;
                break;

            case BackupEntityKind.Partition:
                PartitionBackupEntity partitionBackupEntity = (PartitionBackupEntity)backupEntity;
                this.FabricUri = string.Format("{0}/{1}", partitionBackupEntity.ServiceName,
                                               partitionBackupEntity.PartitionId);
                break;
            }
            this.FabricBackupResourceType = UtilityHelper.GetApplicationAndServicePartitionName(this.FabricUri, out this.ApplicationName,
                                                                                                out this.ServiceName, out this.PartitionId);
        }
        /// <inheritdoc/>
        protected override void ProcessRecordInternal()
        {
            BackupStorageDescription backupStorageDescription = null;

            if (this.AzureBlobStore.IsPresent)
            {
                backupStorageDescription = new AzureBlobBackupStorageDescription(
                    connectionString: this.ConnectionString,
                    containerName: this.ContainerName,
                    friendlyName: this.FriendlyName);
            }
            else if (this.FileShare.IsPresent)
            {
                backupStorageDescription = new FileShareBackupStorageDescription(
                    path: this.Path,
                    friendlyName: this.FriendlyName,
                    primaryUserName: this.PrimaryUserName,
                    primaryPassword: this.PrimaryPassword,
                    secondaryUserName: this.SecondaryUserName,
                    secondaryPassword: this.SecondaryPassword);
            }
            else if (this.DsmsAzureBlobStore.IsPresent)
            {
                backupStorageDescription = new DsmsAzureBlobBackupStorageDescription(
                    storageCredentialsSourceLocation: this.StorageCredentialsSourceLocation,
                    containerName: this.ContainerName,
                    friendlyName: this.FriendlyName);
            }

            BackupEntity backupEntity = null;

            if (this.Application.IsPresent)
            {
                backupEntity = new ApplicationBackupEntity(
                    applicationName: this.ApplicationName);
            }
            else if (this.Service.IsPresent)
            {
                backupEntity = new ServiceBackupEntity(
                    serviceName: this.ServiceName);
            }
            else if (this.Partition.IsPresent)
            {
                backupEntity = new PartitionBackupEntity(
                    serviceName: this.ServiceName,
                    partitionId: this.PartitionId);
            }

            var getBackupByStorageQueryDescription = new GetBackupByStorageQueryDescription(
                storage: backupStorageDescription,
                backupEntity: backupEntity,
                startDateTimeFilter: this.StartDateTimeFilter,
                endDateTimeFilter: this.EndDateTimeFilter,
                latest: this.Latest);

            var continuationToken = default(ContinuationToken);

            do
            {
                var result = this.ServiceFabricClient.BackupRestore.GetBackupsFromBackupLocationAsync(
                    getBackupByStorageQueryDescription: getBackupByStorageQueryDescription,
                    serverTimeout: this.ServerTimeout,
                    continuationToken: continuationToken,
                    maxResults: this.MaxResults,
                    cancellationToken: this.CancellationToken).GetAwaiter().GetResult();

                if (result == null)
                {
                    break;
                }

                var count = 0;
                foreach (var item in result.Data)
                {
                    count++;
                    this.WriteObject(this.FormatOutput(item));
                }

                continuationToken = result.ContinuationToken;
                this.WriteDebug(string.Format(Resource.MsgCountAndContinuationToken, count, continuationToken));
            }while (continuationToken.Next);
        }