Esempio n. 1
0
 /// <summary>
 /// What’s the name of the network that contains the device with serial number “Q2CD-MJ68-SYFF”
 /// </summary>
 /// <param name="merakiDashboardClient"></param>
 /// <param name="organizationId"></param>
 /// <returns></returns>
 private async Task Exercise5(MerakiDashboardClient merakiDashboardClient, string organizationId)
 {
     const string   deviceSerial   = "Q2BV-B8VH-V2QH"; // "Q2CD-MJ68-SYFF" does not exist
     InventoryEntry inventoryEntry = merakiDashboardClient.GetOrganizationInventoryAsync(organizationId).Result
                                     .FirstOrDefault(ie => deviceSerial.Equals(ie.Serial, StringComparison.OrdinalIgnoreCase));
     await System.Console.Out.WriteLineAsync($"Network '{inventoryEntry?.NetworkId}' contains device with serial {deviceSerial}");
 }
Esempio n. 2
0
        public async void GetOrganizationAsync()
        {
            const string organizationId       = "myOrg";
            Organization expectedOrganization = new Organization
            {
                Name = "organization name",
                Id   = organizationId
            };

            Mock <MerakiHttpApiClient> apiClientMock = new Mock <MerakiHttpApiClient>(MockBehavior.Strict, "apiKey");

            apiClientMock.Setup(apiClient => apiClient.GetAsync <Organization>($"v0/organizations/{organizationId}"))
            .Returns(Task.FromResult(expectedOrganization));
            // apiClientMock.As<IDisposable>().Setup(apiClient => apiClient.Dispose());
            apiClientMock.Protected().Setup("Dispose", true);

            using (MerakiDashboardClient merakiDashboardClient = new MerakiDashboardClient(apiClientMock.Object))
            {
                Organization actualOrganization = await merakiDashboardClient.GetOrganizationAsync(organizationId);

                Assert.Equal(expectedOrganization, actualOrganization);
            }

            apiClientMock.VerifyAll();
        }
Esempio n. 3
0
        public async void GetOrganizationSnmpSettingsAsync()
        {
            const string    organizationId  = "myOrg";
            SnmpGetSettings snmpGetSettings = new SnmpGetSettings
            {
                V2cEnabled           = false,
                V3Enabled            = true,
                V3AuthenticationMode = SnmpAuthenticationMode.Sha,
                V3PrivacyMode        = SnmpPrivacyMode.Aes128,
                Hostname             = "snmp.meraki.com",
                Port = 162
            };

            Mock <MerakiHttpApiClient> apiClientMock = new Mock <MerakiHttpApiClient>(MockBehavior.Strict, "apiKey");

            apiClientMock.Setup(apiClient => apiClient.GetAsync <SnmpGetSettings>($"v0/organizations/{organizationId}/snmp"))
            .Returns(Task.FromResult(snmpGetSettings));
            // apiClientMock.As<IDisposable>().Setup(apiClient => apiClient.Dispose());
            apiClientMock.Protected().Setup("Dispose", true);

            using (MerakiDashboardClient merakiDashboardClient = new MerakiDashboardClient(apiClientMock.Object))
            {
                SnmpGetSettings actualSnmpGetSettings = await merakiDashboardClient.GetOrganizationSnmpSettingsAsync(organizationId);

                Assert.Equal(snmpGetSettings, actualSnmpGetSettings);
            }

            apiClientMock.VerifyAll();
        }
Esempio n. 4
0
        public async void PutOrganizationSnmpSettingsAsync()
        {
            const string    organizationId  = "myOrg";
            SnmpPutSettings snmpPutSettings = new SnmpPutSettings
            {
                V2cEnabled               = false,
                V3Enabled                = true,
                V3AuthenticationMode     = SnmpAuthenticationMode.Sha,
                V3AuthenticationPassword = "******",
                V3PrivacyMode            = SnmpPrivacyMode.Aes128,
                V3PrivacyPassword        = "******",
                PeerIps = new [] { IPAddress.Parse("192.168.0.1"), IPAddress.Parse("10.1.1.1") }
            };

            Mock <MerakiHttpApiClient> apiClientMock = new Mock <MerakiHttpApiClient>(MockBehavior.Strict, "apiKey");
            HttpStatusCode             statusCode    = HttpStatusCode.OK;

            apiClientMock.Setup(apiClient =>
                                apiClient.SendAsync(HttpMethod.Put, $"v0/organizations/{organizationId}/snmp", snmpPutSettings))
            .Returns(Task.FromResult(statusCode));
            // apiClientMock.As<IDisposable>().Setup(apiClient => apiClient.Dispose());
            apiClientMock.Protected().Setup("Dispose", true);

            using (MerakiDashboardClient merakiDashboardClient = new MerakiDashboardClient(apiClientMock.Object))
            {
                HttpStatusCode actualResponse =
                    await merakiDashboardClient.PutOrganizationSnmpSettingsAsync(organizationId, snmpPutSettings);

                Assert.Equal(statusCode, actualResponse);
            }

            apiClientMock.VerifyAll();
        }
Esempio n. 5
0
 /// <summary>
 /// What is the value for “claimedAt” for the device with serial number "Q2JD-W28X-FNEN"
 /// </summary>
 /// <param name="merakiDashboardClient"></param>
 /// <param name="organizationId"></param>
 /// <returns></returns>
 private async Task Exercise4(MerakiDashboardClient merakiDashboardClient, string organizationId)
 {
     const string   deviceSerial   = "Q2BV-B8VH-V2QH"; // "Q2JD-W28X-FNEN" does not exist
     InventoryEntry inventoryEntry = merakiDashboardClient.GetOrganizationInventoryAsync(organizationId).Result
                                     .FirstOrDefault(ie => deviceSerial.Equals(ie.Serial, StringComparison.OrdinalIgnoreCase));
     await System.Console.Out.WriteLineAsync($"Device {deviceSerial} claimed at '{inventoryEntry?.ClaimedAt}'");
 }
Esempio n. 6
0
        /// <summary>
        /// How many VLANs are configured on device “Q2QN-WPR6-UJPL”?
        /// </summary>
        /// <param name="merakiDashboardClient"></param>
        /// <param name="organizationId"></param>
        /// <returns></returns>
        private async Task Exercise8(MerakiDashboardClient merakiDashboardClient, string organizationId)
        {
            const string switchSerial = "Q2HP-DT5F-KMJE"; // "Q2QN-WPR6-UJPL" does not exist

            SwitchPort[] ports = await merakiDashboardClient.GetSwitchPortsAsync(switchSerial);

            await System.Console.Out.WriteLineAsync($"Device with serial {switchSerial} has '{ports.Select(sp => sp.Vlan).Distinct().Count()}' vlans");
        }
Esempio n. 7
0
        /// <summary>
        /// To which port of “Q2HP-AJ22-UG72” is the client, “e0:55:3d:1f:a7:10” connected?
        /// </summary>
        /// <param name="merakiDashboardClient"></param>
        /// <param name="organizationId"></param>
        /// <returns></returns>
        private async Task Exercise7(MerakiDashboardClient merakiDashboardClient, string organizationId)
        {
            const string switchSerial = "Q2HP-DT5F-KMJE";    // "Q2HP-AJ22-UG72" does not exist
            const string deviceMac    = "e0:55:3d:4f:45:a9"; // "Q2AT-6CLF-RQFE", an MC7

            Client[] clients = await merakiDashboardClient.GetClientsAsync(switchSerial);

            await System.Console.Out.WriteLineAsync($"Switch {switchSerial} has device '{deviceMac} the on port '{clients.FirstOrDefault(c => deviceMac.Equals(c.Mac))?.SwitchPort}'");
        }
        public void Create_ApiKey()
        {
            const string apiKey = "000111222333444555666777888999000aaabbbbcccdddeee";

            using (MerakiDashboardClient merakiDashboardClient = MerakiDashboardClientFactory.Create(apiKey))
            {
                Assert.Equal(MerakiDashboardClientSettingsSetup.DefaultMerakiDashboardApiBaseAddress, merakiDashboardClient.Client.BaseAddress.AbsoluteUri);
                Assert.Equal(apiKey, merakiDashboardClient.Client.ApiKey);
            }
        }
        public async void TestMocking()
        {
            Organization organization = new Organization
            {
                Name = "White house",
                Id   = "Org Id"
            };
            Network network = new Network
            {
                Id             = "Network Id",
                Name           = "White House Network",
                NetworkType    = "Wireless",
                OrganizationId = organization.Id,
                Tags           = "tags",
                TimeZone       = "UTC-06:00"
            };
            Device device = new Device
            {
                Address      = "1600 Pennsylania Ave Washington DC",
                LanIpAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }),
                Lattitude    = 38.8977,
                Longitude    = 77.0365,
                Mac          = "00:00:00:00:00:00",
                Model        = "MR33",
                Name         = "Presidential AP",
                NetworkId    = network.Id,
                Serial       = "AAAA-BBBB-CCCC",
                Tags         = "Tags"
            };

            MerakiDashboardClientSettings merakiDashboardClientSettings = new MerakiDashboardClientSettings
            {
                ApiKey      = "api key",
                BaseAddress = new Uri("http://myapi.com")
            };

            Mock <MerakiDashboardClient> merakiDashboardlientMock = new Mock <MerakiDashboardClient>(MockBehavior.Strict, merakiDashboardClientSettings);

            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetOrganizationsAsync())
            .Returns(Task.FromResult(new [] { organization }));
            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetOrganizationNetworksAsync(organization))
            .Returns(Task.FromResult(new[] { network }));
            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetNetworkDevicesAsync(network))
            .Returns(Task.FromResult(new[] { device }));
            // Required by mocking framework
            merakiDashboardlientMock.Protected().Setup("Dispose", true);

            using (MerakiDashboardClient merakiDashboardClient = merakiDashboardlientMock.Object)
            {
                Assert.Equal(new[] { device }, await GetDevicesInAnOrganization(merakiDashboardClient));
            }
            merakiDashboardlientMock.VerifyAll();
        }
Esempio n. 10
0
        public void Options()
        {
            // Ensure this matches the appSettings.json file.
            const string apiKey = "8A7H65KJ098MHGI09";

            using (ServiceProvider serviceProvider = BuildServiceProvider())
            {
                MerakiDashboardClient merakiDashboardClient = serviceProvider.GetService <MerakiDashboardClient>();
                Assert.Equal(apiKey, merakiDashboardClient.Client.ApiKey);
                Assert.Equal(MerakiDashboardClientOptions.DefaultMerakiDashboardApiBaseAddress, merakiDashboardClient.Client.BaseAddress);
            }
        }
Esempio n. 11
0
        private static async Task PutSnmpSettings(string organizationId, MerakiDashboardClient merakiDashboardClient)
        {
            SnmpPutSettings snmpPutSettings = new SnmpPutSettings
            {
                V2cEnabled               = false,
                V3Enabled                = true,
                V3AuthenticationMode     = SnmpAuthenticationMode.Sha,
                V3AuthenticationPassword = "******",
                V3PrivacyMode            = SnmpPrivacyMode.Aes128,
                V3PrivacyPassword        = "******",
                PeerIps = new[] { IPAddress.Parse("8.8.8.8") }
            };

            System.Console.Out.WriteLine(await merakiDashboardClient.PutOrganizationSnmpSettingsAsync(organizationId, snmpPutSettings));
        }
        /// <summary>
        /// Get the devices in all networks in all organizations the current API key can access.
        /// </summary>
        /// <param name="merakiDashboardClient">
        /// The <see cref="MerakiDashboardClient"/> to access the device swith.
        /// </param>
        /// <returns>
        /// The <see cref="Device"/>s discovered.
        /// </returns>
        private async Task <IEnumerable <Device> > GetDevicesInAnOrganization(MerakiDashboardClient merakiDashboardClient)
        {
            List <Device> result = new List <Device>();

            foreach (Organization organization in await merakiDashboardClient.GetOrganizationsAsync())
            {
                foreach (Network network in await merakiDashboardClient.GetOrganizationNetworksAsync(organization))
                {
                    foreach (Device device in await merakiDashboardClient.GetNetworkDevicesAsync(network))
                    {
                        result.Add(device);
                    }
                }
            }
            return(result);
        }
Esempio n. 13
0
        public async Task Run(string apiKey)
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(apiKey));
            }

            using (MerakiDashboardClient merakiDashboardClient =
                       MerakiDashboardClientFactory.Create(mcs => mcs.ApiKey = apiKey))
                using (new MerakiHttpApiDebugContext())
                {
                    string organizationId = merakiDashboardClient.GetOrganizationsAsync().Result.First().Id;
                    await GetSnmpSettings(organizationId, merakiDashboardClient);
                    await PutSnmpSettings(organizationId, merakiDashboardClient);
                }
        }
Esempio n. 14
0
        public void Ctor_MerakiDashboardClientSettings()
        {
            Uri          baseAddress = new Uri("http://www.myserver.com");
            const string apiKey      = "000111222333444555666777888999000aaabbbbcccdddeee";

            MerakiDashboardClientOptions merakiDashboardClientSettings = new MerakiDashboardClientOptions
            {
                BaseAddress = baseAddress,
                ApiKey      = apiKey
            };

            using (MerakiDashboardClient merakiDashboardClient = new MerakiDashboardClient(merakiDashboardClientSettings))
            {
                Assert.Equal(baseAddress, merakiDashboardClient.Client.BaseAddress);
                Assert.Equal(apiKey, merakiDashboardClient.Client.ApiKey);
            }
        }
Esempio n. 15
0
        public async Task Run(string apiKey)
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(apiKey));
            }

            MerakiDashboardClient merakiDashboardClient = MerakiDashboardClientFactory.Create(mcs => mcs.ApiKey = apiKey);

            const string organizationName = "Meraki Live Sandbox";
            string       organizationId   = MerakiDashboardHelper.GetOrganizationId(merakiDashboardClient, organizationName).Result;

            foreach (Func <MerakiDashboardClient, string, Task> exercise in
                     new Func <MerakiDashboardClient, string, Task>[] { })
            {
                await exercise(merakiDashboardClient, organizationId);
            }
        }
Esempio n. 16
0
        public async Task Run(string apiKey)
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(apiKey));
            }

            using (MerakiDashboardClient merakiDashboardClient = MerakiDashboardClientFactory.Create(apiKey))
            {
                const string organizationName = "Meraki Live Demo";
                string       organizationId   = MerakiDashboardHelper.GetOrganizationId(merakiDashboardClient, organizationName).Result;

                foreach (Func <MerakiDashboardClient, string, Task> exercise in
                         new Func <MerakiDashboardClient, string, Task>[]
                {
                    Exercise1, Exercise2, Exercise3, Exercise4, Exercise5, Exercise6, Exercise7, Exercise8
                })
                {
                    await exercise(merakiDashboardClient, organizationId);
                }
            }
        }
Esempio n. 17
0
        public async Task Run(string apiKey)
        {
            if (string.IsNullOrWhiteSpace(apiKey))
            {
                throw new ArgumentException("Cannot be null, empty or whitespace", nameof(apiKey));
            }

            using (MerakiDashboardClient merakiDashboardClient =
                       MerakiDashboardClientFactory.Create(mcs => mcs.ApiKey = apiKey))
            {
                System.Console.WriteLine("By organization by network:");
                foreach (Organization organization in await merakiDashboardClient.GetOrganizationsAsync())
                {
                    System.Console.WriteLine($"{organization.Name} ({organization.Id}):");
                    foreach (Network network in await merakiDashboardClient.GetOrganizationNetworksAsync(organization))
                    {
                        System.Console.WriteLine($"  {network.Name} ({network.Id}):");
                        foreach (Device device in await merakiDashboardClient.GetNetworkDevicesAsync(network))
                        {
                            System.Console.WriteLine($"    {device.Serial} ({device.Model}, {device.Mac})");
                        }
                    }
                }

                System.Console.WriteLine();
                System.Console.WriteLine("By organization inventory:");
                foreach (Organization organization in await merakiDashboardClient.GetOrganizationsAsync())
                {
                    System.Console.WriteLine($"{organization.Name} ({organization.Id}):");
                    foreach (InventoryEntry inventory in await merakiDashboardClient.GetOrganizationInventoryAsync(
                                 organization))
                    {
                        System.Console.WriteLine(
                            $"    {inventory.Serial} ({inventory.Model}) claimed at {inventory.ClaimedAt}");
                    }
                }
            }
        }
        public async void TestMocking()
        {
            Organization organization = new Organization
            {
                Name = "White house",
                Id   = "Org Id"
            };
            Network network = new Network
            {
                Id             = "Network Id",
                Name           = "White House Network",
                NetworkType    = "Wireless",
                OrganizationId = organization.Id,
                Tags           = "tags",
                TimeZone       = "UTC-06:00"
            };
            Device device = new Device
            {
                Address      = "1600 Pennsylania Ave Washington DC",
                LanIpAddress = new IPAddress(new byte[] { 127, 0, 0, 1 }),
                Lattitude    = 38.8977,
                Longitude    = 77.0365,
                Mac          = "00:00:00:00:00:00",
                Model        = "MR33",
                Name         = "Presidential AP",
                NetworkId    = network.Id,
                Serial       = "AAAA-BBBB-CCCC",
                Tags         = "Tags"
            };

            DeviceClient dclient = new DeviceClient
            {
                Id           = Guid.NewGuid().ToString(),
                Description  = "description",
                IPAddress    = "127.0.0.1",
                MacAddress   = "80:c5:f2:79:be:e1",
                MDnsName     = "Test",
                DhcpHostName = "Test",
                SwitchPort   = null,
                User         = "******",
                VLan         = "10"
            };

            MerakiDashboardClientOptions merakiDashboardClientSettings = new MerakiDashboardClientOptions
            {
                ApiKey      = "api key",
                BaseAddress = new Uri("http://myapi.com")
            };

            Mock <MerakiDashboardClient> merakiDashboardlientMock = new Mock <MerakiDashboardClient>(MockBehavior.Strict, merakiDashboardClientSettings);

            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetOrganizationsAsync())
            .Returns(Task.FromResult(new[] { organization }));
            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetOrganizationNetworksAsync(organization))
            .Returns(Task.FromResult(new[] { network }));
            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetNetworkDevicesAsync(network))
            .Returns(Task.FromResult(new[] { device }));

            merakiDashboardlientMock.Setup(merakiDashboardClient => merakiDashboardClient.GetDeviceClientsAsync(device.Serial, new TimeSpan(24, 0, 0)))
            .Returns(Task.FromResult(new[] { dclient }));

            // Required by mocking framework
            merakiDashboardlientMock.Protected().Setup("Dispose", true);

            using (MerakiDashboardClient merakiDashboardClient = merakiDashboardlientMock.Object)
            {
                Assert.Equal(new[] { device }, await GetDevicesInAnOrganization(merakiDashboardClient));
                var deviceClients = await merakiDashboardClient.GetDeviceClientsAsync(device.Serial, new TimeSpan(24, 0, 0));

                Assert.NotNull(deviceClients);
                Assert.Single(deviceClients);
                Assert.Equal(dclient.MacAddress, deviceClients.Single().MacAddress);
            }
            merakiDashboardlientMock.VerifyAll();
        }
Esempio n. 19
0
        private static async Task GetSnmpSettings(string organizationId, MerakiDashboardClient merakiDashboardClient)
        {
            SnmpGetSettings snmpGetSettings = await merakiDashboardClient.GetOrganizationSnmpSettingsAsync(organizationId);

            System.Console.Out.WriteLine(JsonConvert.SerializeObject(snmpGetSettings));
        }
Esempio n. 20
0
 /// <summary>
 /// Get the <see cref="Device"/> for the given serial number.
 /// </summary>
 /// <param name="merakiDashboardClient"></param>
 /// <param name="organizationId"></param>
 /// <param name="serial"></param>
 /// <returns>
 /// The <see cref="Device"/> or null, if no device exists.
 /// </returns>
 private Device GetDevice(MerakiDashboardClient merakiDashboardClient, string organizationId, string serial)
 {
     return(merakiDashboardClient.GetOrganizationNetworksAsync(organizationId).Result
            .SelectMany(n => merakiDashboardClient.GetNetworkDevicesAsync(n.Id).Result)
            .FirstOrDefault(d => serial.Equals(d.Serial, StringComparison.OrdinalIgnoreCase)));
 }
Esempio n. 21
0
 /// <summary>
 /// Determine the Organization ID for the given organization.
 /// </summary>
 /// <param name="merakiDashboardClient"></param>
 /// <param name="organizationId"></param>
 /// <returns></returns>
 private async Task Exercise1(MerakiDashboardClient merakiDashboardClient, string organizationId)
 {
     await System.Console.Out.WriteLineAsync($"ID {organizationId}");
 }
Esempio n. 22
0
        /// <summary>
        /// Find out through the API when the license of this organization expires.
        /// </summary>
        /// <param name="merakiDashboardClient"></param>
        /// <param name="organizationId"></param>
        /// <returns></returns>
        private async Task Exercise2(MerakiDashboardClient merakiDashboardClient, string organizationId)
        {
            LicenceState licenseState = await merakiDashboardClient.GetOrganizationLicenseStateAsync(organizationId);

            await System.Console.Out.WriteLineAsync($"The license expires on {licenseState.ExpirationDate}");
        }
Esempio n. 23
0
        /// <summary>
        /// Is SNMP enabled on this Organization?
        /// </summary>
        /// <param name="merakiDashboardClient"></param>
        /// <param name="organizationId"></param>
        /// <returns></returns>
        private async Task Exercise3(MerakiDashboardClient merakiDashboardClient, string organizationId)
        {
            SnmpGetSettings snmpSettings = await merakiDashboardClient.GetOrganizationSnmpSettingsAsync(organizationId);

            await System.Console.Out.WriteLineAsync($"SNMP v2c enabled: {snmpSettings.V2cEnabled},  v3 enabled: {snmpSettings.V3Enabled}");
        }
        public static async Task <string> GetOrganizationId(MerakiDashboardClient merakiDashboardClient, string organizationName)
        {
            Organization[] organizations = await merakiDashboardClient.GetOrganizationsAsync();

            return(organizations.First(o => o.Name == organizationName).Id);
        }
Esempio n. 25
0
 /// <summary>
 /// What tags are applied to device “Q2HP-AJ22-UG72”?
 /// </summary>
 /// <param name="merakiDashboardClient"></param>
 /// <param name="organizationId"></param>
 /// <returns></returns>
 private async Task Exercise6(MerakiDashboardClient merakiDashboardClient, string organizationId)
 {
     const string deviceSerial = "Q2BV-B8VH-V2QH"; // "Q2HP-AJ22-UG72" does not exist
     Device       device       = GetDevice(merakiDashboardClient, organizationId, deviceSerial);
     await System.Console.Out.WriteLineAsync($"Device with serial {deviceSerial} has the tags '{device?.Tags}'");
 }