private ResourceOutput MapAzureStoreResourceToResourceOutput(AzureStoreResource resource, bool expandOutputItems)
        {
            return(new ResourceOutput
            {
                ETag = resource.nvc_ETag,
                Type = resource.nvc_ResourceType,
                Name = resource.nvc_ResourceName,
                Plan = resource.nvc_Plan,
                State = ResourceState.Started.ToString(),

                OutputItems = expandOutputItems ? this.GetOutputItemsForAzureStoreResource(resource) : null,
                UsageMeters = new UsageMeterList(this.GetUsageForAzureStoreResource(resource)),

                CloudServiceSettings = new CloudServiceSettings
                {
                    GeoRegion = resource.nvc_Region
                },

                OperationStatus = new OperationStatus()
                {
                    Result = OperationResult.Succeeded,
                    Error = new Error
                    {
                        HttpCode = (int)HttpStatusCode.OK,
                        Message = "OK"
                    },
                },
            });
        }
 private UsageMeter GetUsageForAzureStoreResource(AzureStoreResource resource)
 {
     return(new UsageMeter()
     {
         Name = "Servers",
         Unit = "generic",
         Used = this.GetUsedServersForAzureStoreResource(resource),
         Included = this.GetIncludedServersForAzureStoreResource(resource),
     });
 }
 private OutputItemList GetOutputItemsForAzureStoreResource(AzureStoreResource resource)
 {
     using (var provider = AccountDataProvider.Instance)
     {
         return(new OutputItemList(
                    new OutputItem[]
         {
             new OutputItem()
             {
                 Key = "ApplicationToken", Value = Utils.GenerateSecurityToken()
             },
             new OutputItem()
             {
                 Key = "ApplicationSecret", Value = Utils.GenerateSecurityToken()
             },
         }
                    ));
     }
 }
        private string GetIncludedServersForAzureStoreResource(AzureStoreResource resource)
        {
            if (resource.nvc_Plan.Equals("free", StringComparison.OrdinalIgnoreCase))
            {
                return("100");
            }
            if (resource.nvc_Plan.Equals("advanced10", StringComparison.OrdinalIgnoreCase))
            {
                return("10");
            }
            if (resource.nvc_Plan.Equals("advanced30", StringComparison.OrdinalIgnoreCase))
            {
                return("30");
            }
            if (resource.nvc_Plan.Equals("advanced80", StringComparison.OrdinalIgnoreCase))
            {
                return("80");
            }

            Logger.ErrorFormat("GetIncludedServersForAzureStoreResource: Unknown resource plan {0} encountered.", resource.nvc_Plan);
            return("100");
        }
 private string GetUsedServersForAzureStoreResource(AzureStoreResource resource)
 {
     return("42");
 }
        public ResourceOutput CreateOrUpdateResource(string subscriptionId, string cloudServiceName, string resourceType, string resourceName)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            ResourceInput resourceInput = null;

            try
            {
                resourceInput = Request.Content.ReadAsAsync <ResourceInput>().Result;

                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName) || String.IsNullOrEmpty(resourceType) || String.IsNullOrEmpty(resourceName) || (resourceInput == null))
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }

                    var eTag          = resourceInput.ETag;
                    var region        = resourceInput.CloudServiceSettings.GeoRegion;
                    var plan          = resourceInput.Plan;
                    var promotionCode = resourceInput.PromotionCode;

                    var resource = provider.GetAzureStoreResource(
                        subscriptionId: subscriptionId,
                        cloudServiceName: cloudServiceName,
                        resourceType: resourceType,
                        resourceName: resourceName
                        );

                    if (resource == null)
                    {
                        var subscription = provider.GetAzureStoreSubscriptionBySubscriptionId(subscriptionId);
                        if (subscription == null)
                        {
                            Logger.ErrorFormat("CreateOrUpdateResource: Unable to find Azure Store resource {1} for subscription {0}. Raw data:\n{2}\n", subscriptionId, resourceName, resourceInput.AsJson());
                            throw new HttpResponseException(HttpStatusCode.BadRequest);
                        }

                        resource = new AzureStoreResource
                        {
                            id_TenantId          = subscription.id_TenantId,
                            nvc_SubscriptionId   = subscriptionId,
                            nvc_CloudServiceName = cloudServiceName,
                            nvc_ResourceType     = resourceType,
                            nvc_ResourceName     = resourceName,
                        };
                    }

                    if (!eTag.Equals(resource.nvc_ETag, StringComparison.OrdinalIgnoreCase))
                    {
                        resource.nvc_ETag          = eTag;
                        resource.nvc_Region        = region ?? resource.nvc_Region;
                        resource.nvc_Plan          = plan ?? resource.nvc_Plan;
                        resource.nvc_PromotionCode = promotionCode ?? resource.nvc_PromotionCode;
                        resource.nvc_RawData       = resourceInput.AsJson();

                        provider.CreateOrUpdateAzureStoreResource(resource);
                    }

                    Logger.InfoFormat("CreateOrUpdateResource: Azure Store resource {1} for subscription {0} create or updated. Raw data:\n{2}\n", subscriptionId, resourceName, resourceInput.AsJson());
                    return(this.MapAzureStoreResourceToResourceOutput(resource, expandOutputItems: true));
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "CreateOrUpdateResource: Unable to provision or update resource {1} for subscription {0}. Raw data:\n{2}\n",
                        subscriptionId,
                        resourceName,
                        resourceInput != null ? resourceInput.AsJson() : "<null>"
                        ),
                    exception: ex
                    );

                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }