public static ResourceOutput ResourceOutputFromResourceEntity(ResourceEntity resourceEntity)
 {
     return new ResourceOutput()
     {
         BillingETag = resourceEntity.BillingETag,
         CloudServiceSettings = new CloudServiceSettings()
         {
             GeoRegion = resourceEntity.CloudServiceGeoRegion
         },
         ETag = resourceEntity.ResourceETag,
         //IntrinsicSettings = resourceEntity.,
         Label = resourceEntity.Label,
         Name = resourceEntity.Name,
         OperationStatus = new OperationStatus()
         {
             Error = new ErrorData()
             {
                 ExtendedCode = String.Empty,
                 HttpCode = resourceEntity.LastOperationHttpCode,
                 Message = resourceEntity.LastOperationMessage,
             },
             Result = (OperationResult)Enum.Parse(typeof(OperationResult), resourceEntity.LastOperationResult)
         },
         OutputItems = DeserializeOutputItems(resourceEntity.OutputItems),
         Plan = resourceEntity.Plan,
         PromotionCode = resourceEntity.PromotionCode,
         SchemaVersion = resourceEntity.SchemaVersion,
         State = resourceEntity.State,
         SubState = resourceEntity.SubState,
         Type = resourceEntity.Type
     };
 }
        public ResourceOutput ProvisionOrUpdateResource(string subscriptionId, string cloudServiceName, string resourceType, string resourceName , ResourceInput resource)
        {
            //string theRawBody = Request.Content.ReadAsStringAsync().Result;
            //return null;

            if (String.IsNullOrEmpty(cloudServiceName) || String.IsNullOrEmpty(resourceType) || String.IsNullOrEmpty(resourceName) || (resource == null))
            {
                throw new HttpResponseException(HttpStatusCode.BadRequest);
            }

            ResourceEntity resourceEntity = WebApiApplication.Storage.ResourceStorage.GetResource(subscriptionId, cloudServiceName, resourceType, resourceName);
            if (resourceEntity == null)
            {
                                // Create a new resource
                resourceEntity = new ResourceEntity(subscriptionId, cloudServiceName, resourceType, resourceName)
                {
                    BillingETag = resource.BillingETag,
                    CloudServiceGeoRegion = resource.CloudServiceSettings.GeoRegion,
                    CloudServiceName = cloudServiceName,
                    ResourceETag = resource.ETag,
                    //IntrinsicSettings =
                    Label = resource.Label,
                    Name = resourceName,
                    OutputItems = null,
                    Plan = resource.Plan,
                    PromotionCode = resource.PromotionCode,
                    SchemaVersion = resource.SchemaVersion,

                    // Create always started
                    State = ResourceState.Started.ToString(),

                    SubscriptionId = subscriptionId,
                    SubState = String.Empty,

                    Type = resourceType,

                    LastOperation = "Create",
                    LastOperationHttpCode = 200,
                    LastOperationMessage = String.Empty,
                    LastOperationResult = OperationResult.Succeeded.ToString()
                };

                // Provision
                resourceEntity.OutputItems = SerializeOutputItems(ProvisionOnHashRedis(resourceEntity.PartitionKey + "-" + resourceEntity.RowKey));

                // Update resource on storage
                WebApiApplication.Storage.ResourceStorage.AddOrUpdateResource(resourceEntity);
            }
            else
            {
                // Only attempt if we have not yet already done so
                if (String.CompareOrdinal(resource.ETag, resourceEntity.ResourceETag) != 0)
                {
                    // Update an existing resource - Some things can never change for a resource
                    resourceEntity.BillingETag = resource.BillingETag;
                    resourceEntity.CloudServiceGeoRegion = resource.CloudServiceSettings.GeoRegion;
                    // Can never change -> resourceEntity.CloudServicename = cloudServiceName
                    resourceEntity.ResourceETag = resource.ETag;
                    //IntrinsicSettings = XXX
                    resourceEntity.Label = resource.Label;
                    // Can never change -> resourceEntity.Name = resourceName
                    //resourceEntity.OutputItems = SerializeOutputItems(resource.);
                    resourceEntity.Plan = resource.Plan;
                    resourceEntity.PromotionCode = resource.PromotionCode;
                    resourceEntity.SchemaVersion = resource.SchemaVersion;

                    // Upgrade always started
                    resourceEntity.State = ResourceState.Started.ToString();
                    // Can never change -> resourceEntity.SubscriptionId = subscriptionId

                    resourceEntity.SubState = String.Empty;
                    // Can never change -> resourceEntity.Type = resourceType

                    resourceEntity.LastOperation = "Update";
                    resourceEntity.LastOperationHttpCode = 200;
                    resourceEntity.LastOperationMessage = String.Empty;
                    resourceEntity.LastOperationResult = OperationResult.Succeeded.ToString();

                    // Update resource on storage
                    WebApiApplication.Storage.ResourceStorage.AddOrUpdateResource(resourceEntity);
                }
            }

            // Produce output for Azure Runtime
            ResourceOutput resourceToReturn = new ResourceOutput()
            {
                BillingETag = resourceEntity.BillingETag,
                CloudServiceSettings = new CloudServiceSettings()
                {
                    GeoRegion = resourceEntity.CloudServiceGeoRegion
                },
                ETag = resourceEntity.ResourceETag,
                //IntrinsicSettings = resourceEntity.XXX,
                Label = resourceEntity.Label,
                Name = resourceEntity.Name,
                OperationStatus = new OperationStatus()
                {
                    Error = new ErrorData()
                    {
                        ExtendedCode = String.Empty,
                        HttpCode = resourceEntity.LastOperationHttpCode,
                        Message = resourceEntity.LastOperationMessage
                    },
                    Result = (OperationResult)Enum.Parse(typeof(OperationResult), resourceEntity.LastOperationResult)
                },
                OutputItems = DeserializeOutputItems(resourceEntity.OutputItems),
                Plan = resourceEntity.Plan,
                PromotionCode = resourceEntity.PromotionCode,
                SchemaVersion = resourceEntity.SchemaVersion,
                State = resourceEntity.State,
                SubState = resourceEntity.SubState,
                Type = resourceEntity.Type
            };

            return resourceToReturn;

            //return DataModel.ProvisionOrUpdateResource(subscriptionId, cloudServiceName, resourceType, resourceName, resource);
        }
 public void AddOrUpdateResource(ResourceEntity resourceEntity)
 {
     TableOperation insertOrReplaceOperation = TableOperation.InsertOrReplace(resourceEntity);
     Table.Execute(insertOrReplaceOperation);
 }