Beispiel #1
0
        public static void TryCreateNamespace(
            this NotificationHubsManagementClient client,
            string resourceGroupName,
            string namespaceName,
            string location,
            string scaleUnit = null)
        {
            var namespaceParameter = new NamespaceCreateOrUpdateParameters()
            {
                Location   = location,
                Properties = new NamespaceProperties
                {
                    NamespaceType = NamespaceType.NotificationHub
                }
            };

            if (!string.IsNullOrEmpty(scaleUnit))
            {
                namespaceParameter.Properties.ScaleUnit = scaleUnit;
            }

            client.Namespaces.CreateOrUpdate(
                resourceGroupName,
                namespaceName, namespaceParameter
                );

            var response = client.Namespaces.Get(resourceGroupName, namespaceName);

            ThrowIfTrue(!response.Value.Name.Equals(namespaceName), string.Format("Namespace name is not equal to {0}", namespaceName));
        }
Beispiel #2
0
        static async Task Main(string[] args)
        {
            var clientId = "1950a258-227b-4e31-a9cf-717495945fc2"; // "well-known" value: https://blogs.technet.microsoft.com/keithmayer/2014/12/30/leveraging-the-azure-service-management-rest-api-with-azure-active-directory-and-powershell-list-azure-administrators/
            var config   = LoadConfiguration(args);
            //var authorizationRule = "DefaultFullSharedAccessSignature";

            var creds = await UserTokenProvider.LoginByDeviceCodeAsync(clientId, (deviceCodeResult) =>
            {
                Console.WriteLine(deviceCodeResult.Message);
                return(true);
            });

            // Create resource group
            var resourceClient = new ResourceManagementClient(creds);

            resourceClient.SubscriptionId = config.SubscriptionId;
            await resourceClient.ResourceGroups.CreateOrUpdateAsync(config.ResourceGroupName, new ResourceGroup(config.Location));

            var nhManagemntClient = new NotificationHubsManagementClient(creds);

            nhManagemntClient.SubscriptionId = config.SubscriptionId;

            // Create namespace
            await nhManagemntClient.Namespaces.CreateOrUpdateAsync(config.ResourceGroupName, config.NamespaceName, new NamespaceCreateOrUpdateParameters(config.Location)
            {
                Sku = new Microsoft.Azure.Management.NotificationHubs.Models.Sku("standard")
            });

            // Create hub
            Microsoft.Azure.Management.NotificationHubs.Models.GcmCredential  gcmCreds  = null;
            Microsoft.Azure.Management.NotificationHubs.Models.ApnsCredential apnsCreds = null;
            if (config.GcmCreds != null)
            {
                gcmCreds = new Microsoft.Azure.Management.NotificationHubs.Models.GcmCredential
                {
                    GoogleApiKey = config.GcmCreds
                };
            }
            if (config.ApnsCreds != null)
            {
                var apnsCredsSplit = config.ApnsCreds.Replace("\\n", "\n").Split(";");
                apnsCreds = new Microsoft.Azure.Management.NotificationHubs.Models.ApnsCredential
                {
                    KeyId = apnsCredsSplit[0],
                    // Id
                    AppName = apnsCredsSplit[1],
                    // Prefix
                    AppId    = apnsCredsSplit[2],
                    Token    = apnsCredsSplit[3],
                    Endpoint = "https://api.development.push.apple.com:443/3/device"
                };
            }
            await nhManagemntClient.NotificationHubs.CreateOrUpdateAsync(config.ResourceGroupName, config.NamespaceName, config.HubName, new NotificationHubCreateOrUpdateParameters(config.Location)
            {
                GcmCredential  = gcmCreds,
                ApnsCredential = apnsCreds
            });

            Console.WriteLine($"Create NotificationHub {config.HubName}.");
        }
Beispiel #3
0
        public static NotificationHubsManagementClient GetNotificationHubsManagementClient(MockContext context, RecordedDelegatingHandler handler)
        {
            if (handler != null)
            {
                handler.IsPassThrough = true;
                NotificationHubsManagementClient nhManagementClient = context.GetServiceClient <NotificationHubsManagementClient>(handlers: handler);
                return(nhManagementClient);
            }

            return(null);
        }
Beispiel #4
0
        public static void RefreshAccessToken(this NotificationHubsManagementClient notificationHubsManagementClient)
        {
            if (HttpMockServer.Mode == HttpRecorderMode.Playback)
            {
                // if it's playback then do nothing
                return;
            }

            var testEnvironment = new CSMTestEnvironmentFactory().GetTestEnvironment();
            var context         = new AuthenticationContext(new Uri(testEnvironment.Endpoints.AADAuthUri, testEnvironment.Tenant).AbsoluteUri);

            var result   = context.AcquireToken("https://management.core.windows.net/", testEnvironment.ClientId, new Uri("urn:ietf:wg:oauth:2.0:oob"), PromptBehavior.Auto);
            var newToken = context.AcquireTokenByRefreshToken(result.RefreshToken, testEnvironment.ClientId, "https://management.core.windows.net/");

            ((TokenCloudCredentials)notificationHubsManagementClient.Credentials).Token = newToken.AccessToken;
        }
Beispiel #5
0
 protected void InitializeClients(MockContext context)
 {
     if (!m_initialized)
     {
         lock (m_lock)
         {
             if (!m_initialized)
             {
                 _resourceManagementClient = NotificationHubsManagementHelper.GetResourceManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
                 _notificationHubsManagementClient = NotificationHubsManagementHelper.GetNotificationHubsManagementClient(context, new RecordedDelegatingHandler {
                     StatusCodeToReturn = HttpStatusCode.OK
                 });
             }
         }
     }
 }