public static void TryCreateApiService(
            this ApiManagementClient client,
            string resourceGroupName,
            string apiServiceName,
            string location,
            string skuType = SkuType.Developer)
        {
            client.ApiManagementService.CreateOrUpdate(
                resourceGroupName,
                apiServiceName,
                new ApiManagementServiceResource
            {
                Location = location,
                NotificationSenderEmail = "*****@*****.**",
                PublisherEmail          = "*****@*****.**",
                PublisherName           = "apimgmt",
                Sku = new ApiManagementServiceSkuProperties
                {
                    Capacity = 1,
                    Name     = skuType
                },
            });

            var response = client.ApiManagementService.Get(resourceGroupName, apiServiceName);

            ThrowIfTrue(!response.Name.Equals(apiServiceName), string.Format("ApiService name is not equal to {0}", apiServiceName));
        }
        public async override Task<AddServiceInstanceResult> AddServiceInstanceAsync(ConnectedServiceHandlerContext context, CancellationToken ct)
        {
            string token = (string)context.Args[Constants.CONNECTED_SERVICE_METADATA_KEY_FOR_ADAL_TOKEN];
            string subscriptionId = (string)context.Args[Constants.CONNECTED_SERVICE_METADATA_KEY_FOR_SUB_ID];
            string swaggerJson = (string)context.Args[Constants.CONNECTED_SERVICE_METADATA_KEY_FOR_SWAGGER];
            IEnumerable<ApiManagementProduct> products = (IEnumerable<ApiManagementProduct>)context.Args[Constants.CONNECTED_SERVICE_METADATA_KEY_FOR_PRODUCTS];
            ApiManagementInstance apimInstance = (ApiManagementInstance)context.Args[Constants.CONNECTED_SERVICE_METADATA_KEY_FOR_APIM_INSTANCE];
            ApiAppResource apiApp = (ApiAppResource)context.Args[Constants.CONNECTED_SERVICE_METADATA_KEY_FOR_API_APP];

            using (var apiManagementClient = new ApiManagementClient(
                new TokenCloudCredentials(subscriptionId, token)
                ))
            {
                await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information,
                    Resources.ProgressMessageImportingSwaggerTemplate,
                    apiApp.Name,
                    apimInstance.Name);

                // import the api
                using (MemoryStream stream = new MemoryStream())
                {
                    var writer = new StreamWriter(stream);
                    writer.Write(swaggerJson);
                    writer.Flush();
                    stream.Position = 0;

                    apiManagementClient.Apis.Import(
                        ResourceUtilities.GetResourceGroupFromResourceId(apimInstance.ResourceId),
                        apimInstance.Name,
                        apiApp.Name,
                        Constants.SWAGGER_CONTENT_TYPE,
                        stream,
                        apiApp.Name);
                }

                // add the api to products
                foreach (var product in products)
                {
                    await context.Logger.WriteMessageAsync(LoggerMessageCategory.Information,
                        Resources.ProgressMessageAddingApiToProductTemplate,
                        apiApp.Name,
                        product.Name);

                    apiManagementClient.ProductApis.Add(
                        ResourceUtilities.GetResourceGroupFromResourceId(apimInstance.ResourceId),
                        apimInstance.Name,
                        product.Id,
                        apiApp.Name);
                }
            }

            AddServiceInstanceResult result = new AddServiceInstanceResult(
               "AzureApiManagement",
               null);

            return result;
        }
        private async Task GetApiManagementProducts()
        {
            if (Wizard.Pages.First(x => x.Legend == Resources.Page2Legend).IsSelected)
            {
                using (Context.StartBusyIndicator(Resources.WaitMessageRetrievingProductList))
                {
                    IAzureRMUserAccountSubscriptionContext sub = _selectedSubscription;
                    if (sub == null)
                    {
                        return;
                    }

                    Token = await sub.GetAuthenticationHeaderAsync(true);
                    Token = Token.Substring("Bearer ".Length);

                    ApiManagementClient = new ApiManagementClient(
                        new TokenCloudCredentials(SelectedSubscription.Subscription.SubscriptionId, Token)
                    );

                    var productResponse = await ApiManagementClient.Products.ListAsync(
                            ResourceUtilities.GetResourceGroupFromResourceId(SelectedApiManagementInstance.ResourceId),
                            SelectedApiManagementInstance.Name,
                            new QueryParameters()
                            );

                    var products = new List<ApiManagementProduct>();

                    foreach (var product in productResponse.Result.Values)
                    {
                        products.Add(new ApiManagementProduct
                        {
                            Name = product.Name,
                            Id = product.Id
                        });
                    }

                    _dispatcher.Invoke(() =>
                    {
                        Products.Clear();
                        products.ForEach(p => Products.Add(p));
                    });
                }
            }
        }