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);
        }
Example #2
0
        public static ResourceOutput ProvisionOrUpdateResource(string subscriptionId, string cloudServiceName, string resourceType, string resourceName, ResourceInput resource)
        {
            ResourceOutput output;

            lock (theMassiveLock)
            {
                Subscription subscription;
                if (!allSubscriptions.TryGetValue(subscriptionId, out subscription))
                {
                    subscription = new Subscription() { Id = subscriptionId };
                    allSubscriptions[subscriptionId] = subscription;
                }

                CloudService theMatchingCloudService = subscription.CloudServices.SingleOrDefault<CloudService>(cs => String.CompareOrdinal(cs.Name, cloudServiceName) == 0);

                if (theMatchingCloudService == null)
                {
                    theMatchingCloudService = new CloudService() { Name = cloudServiceName };
                    subscription.CloudServices.Add(theMatchingCloudService);
                }

                ResourceOutput theMatchingResource = theMatchingCloudService.Resources.FirstOrDefault(r => String.Compare(r.Name, resourceName) == 0);

                if (theMatchingResource != null)
                {
                    // We can be called to provision / update a resource several time - Ignore the request if we have a record of the resource with the same incarnation id
                    if (theMatchingResource.ETag != resource.ETag)
                    {
                        theMatchingResource.CloudServiceSettings = resource.CloudServiceSettings;
                        theMatchingResource.ETag = resource.ETag;
                        theMatchingResource.IntrinsicSettings = resource.IntrinsicSettings;
                        theMatchingResource.Name = resourceName;
                        theMatchingResource.OperationStatus = new OperationStatus()
                        {
                            Error = new ErrorData() { HttpCode = 200, Message="OK" },
                            Result = OperationResult.Succeeded
                        };
                        theMatchingResource.Plan = resource.Plan;
                        theMatchingResource.SchemaVersion = resource.SchemaVersion;
                        theMatchingResource.State = ResourceState.Started.ToString();
                        theMatchingResource.SubState = "";
                        theMatchingResource.Type = resource.Type;
                        theMatchingResource.UsageMeters = GenerateUsageMeters();
                    }

                    output = theMatchingResource;

                    //UpgradeRequest upgradeParams = new UpgradeRequest() { heroku_id = theMatchingCloudService.Name + "-" + theMatchingResource.Type + "-" + theMatchingResource.Name, plan = output.Plan};
                  //  UpgradeResponse upgradeResponse = EnsureHerokuClient().Upgrade(/* the id form the mapped table*/, upgradeParams);
                }
                else
                {
                    output = new ResourceOutput()
                    {
                        CloudServiceSettings = resource.CloudServiceSettings,
                        ETag = resource.ETag,
                        IntrinsicSettings = resource.IntrinsicSettings,
                        Name = resourceName,
                        OperationStatus = new OperationStatus()
                        {
                            Error = new ErrorData() { HttpCode = 200, Message="OK" },
                            Result = OperationResult.Succeeded
                        },
                        OutputItems = GenerateOutputItems(),
                        Plan = resource.Plan,
                        SchemaVersion = resource.SchemaVersion,
                        State = ResourceState.Started.ToString(),
                        SubState = "",
                        Type = resource.Type,
                        UsageMeters = GenerateUsageMeters()
                    };

                    theMatchingCloudService.Resources.Add(output);

                    //ProvisionRequest provisionParams = new ProvisionRequest() { heroku_id = theMatchingCloudService.Name + "-" + theMatchingResource.Type + "-" + theMatchingResource.Name, plan = output.Plan };
                    //ProvisionResponse provisionResponse = EnsureHerokuClient().Provision(provisionParams);

                    // Get ID out of response, map back to resource table
                }
            }

            return output;
        }