示例#1
0
        public async static void ExampleDoc()
        {
            // Authenticate
            var identity = new CloudIdentity
            {
                APIKey   = "{apikey}",
                Username = "******"
            };
            var authProvider = new CloudIdentityProvider(identity);
            var cdnService   = new ContentDeliveryNetworkService(authProvider, "{region}");

            // List Flavors
            IEnumerable <Flavor> flavors = await cdnService.ListFlavorsAsync();

            // Get Flavor
            Flavor flavor = await cdnService.GetFlavorAsync("{flavorId}");

            // Create Service
            ServiceDefinition serviceDefinition = new ServiceDefinition("example_site", "{flavorId}",
                                                                        domains: new[] { new ServiceDomain("www.example.com") },
                                                                        origins: new[] { new ServiceOrigin("example.com") });
            string serviceId = await cdnService.CreateServiceAsync(serviceDefinition);

            await cdnService.WaitForServiceDeployedAsync(serviceId);

            // List Services
            IPage <Service> services = await cdnService.ListServicesAsync();

            // Get Service
            Service service = await cdnService.GetServiceAsync("{serviceId}");

            // Purge All Service Assets
            await cdnService.PurgeCachedAssetsAsync("{serviceId}");

            // Purge a Specific Service Asset
            await cdnService.PurgeCachedAssetAsync("{serviceId}", "{relativeUrlOfAsset}");

            // Update Service
            var patch = new JsonPatchDocument <ServiceDefinition>();

            patch.Replace(svc => svc.Name, "newServiceName");
            await cdnService.UpdateServiceAsync("{serviceId}", patch);

            await cdnService.WaitForServiceDeployedAsync("{serviceId}");

            // Delete Service
            await cdnService.DeleteServiceAsync("{serviceId}");

            await cdnService.WaitForServiceDeletedAsync("{serviceId}");
        }
        public async void CreateAServiceOnAkamai_UsingDefaults()
        {
            Trace.WriteLine("Looking for a CDN flavor provided by Akamai...");
            var flavors = await _cdnService.ListFlavorsAsync();

            var flavor = flavors.FirstOrDefault(x => x.Providers.Any(p => string.Equals(p.Name, "Akamai", StringComparison.OrdinalIgnoreCase)));

            Assert.NotNull(flavor);
            var akamaiFlavor = flavor.Id;

            Trace.WriteLine(string.Format("Found the {0} flavor", akamaiFlavor));

            Trace.WriteLine("Creating a CDN service using defaults for anything I can omit...");
            var domains           = new[] { new ServiceDomain("mirror.example.com") };
            var origins           = new[] { new ServiceOrigin("example.com") };
            var serviceDefinition = new ServiceDefinition("ci-test", akamaiFlavor, domains, origins);
            var serviceId         = await _cdnService.CreateServiceAsync(serviceDefinition);

            Trace.WriteLine(string.Format("Service was created: {0}", serviceId));

            try
            {
                Trace.WriteLine("Waiting for the service to be deployed...");
                var service = await _cdnService.WaitForServiceDeployedAsync(serviceId, progress : new Progress <bool>(x => Trace.WriteLine("...")));

                Trace.WriteLine("Verifying service matches the requested definition...");
                Assert.Equal("ci-test", service.Name);
                Assert.Equal(serviceDefinition.FlavorId, service.FlavorId);

                Assert.Equal(serviceDefinition.Origins.Count, service.Origins.Count());
                Assert.Equal(serviceDefinition.Origins.First().Origin, service.Origins.First().Origin);

                Assert.Equal(serviceDefinition.Domains.Count, service.Domains.Count());
                Assert.Equal(serviceDefinition.Domains.First().Domain, service.Domains.First().Domain);

                Trace.WriteLine("Updating the service...");
                var patch = new JsonPatchDocument <ServiceDefinition>();
                patch.Replace(x => x.Name, "ci-test2");
                var intranetOnly = new ServiceRestriction("intranet", new[] { new ServiceRestrictionRule("intranet", "intranet.example.com") });
                patch.Add(x => x.Restrictions, intranetOnly, 0);
                await _cdnService.UpdateServiceAsync(serviceId, patch);

                Trace.WriteLine("Waiting for the service changes to be deployed...");
                service = await _cdnService.WaitForServiceDeployedAsync(serviceId, progress : new Progress <bool>(x => Trace.WriteLine("...")));

                Trace.WriteLine("Verifying service matches updated definition...");
                Assert.Equal("ci-test2", service.Name);
                Assert.Equal(JsonConvert.SerializeObject(intranetOnly), JsonConvert.SerializeObject(service.Restrictions.First()));

                Trace.WriteLine("Purging all assets on service");
                await _cdnService.PurgeCachedAssetsAsync(serviceId);
            }
            finally
            {
                Trace.WriteLine("Cleaning up any test data...");

                Trace.WriteLine("Removing the service...");
                _cdnService.DeleteService(serviceId);
                _cdnService.WaitForServiceDeleted(serviceId);
                Trace.WriteLine("The service was cleaned up sucessfully.");
            }
        }
示例#3
0
        public async Task Run(string username, string apiKey)
        {
            // Authenticate
            var identity = new CloudIdentity
            {
                APIKey   = apiKey,
                Username = username
            };
            var authProvider = new CloudIdentityProvider(identity);
            var cdnService   = new ContentDeliveryNetworkService(authProvider, "DFW");

            // List Flavors
            Console.WriteLine();
            Console.WriteLine("List Flavors...");
            IEnumerable <Flavor> flavors = await cdnService.ListFlavorsAsync();

            foreach (Flavor f in flavors)
            {
                Console.WriteLine("{0}\t{1}", f.Id, f.Providers.First().Name);
            }

            // Get Flavor
            Console.WriteLine();
            Console.WriteLine("Get Flavor...");
            Flavor flavor = await cdnService.GetFlavorAsync(flavors.First().Id);

            Console.WriteLine("{0}\t{1}", flavor.Id, flavor.Providers.First().Name);

            // Create Service
            Console.WriteLine();
            Console.WriteLine("Create Service...");
            ServiceDefinition serviceDefinition = new ServiceDefinition("example_site", flavor.Id,
                                                                        domains: new[] { new ServiceDomain("www.example.com") },
                                                                        origins: new[] { new ServiceOrigin("example.com") });
            string serviceId = await cdnService.CreateServiceAsync(serviceDefinition);

            await cdnService.WaitForServiceDeployedAsync(serviceId);

            Console.WriteLine("Service Created: {0}", serviceId);

            // List Services
            Console.WriteLine();
            Console.WriteLine("List Services..");
            IPage <Service> services = await cdnService.ListServicesAsync();

            foreach (Service s in services)
            {
                Console.WriteLine("{0}\t{1}", s.Id, s.Name);
            }

            // Get Service
            Console.WriteLine();
            Console.WriteLine("Get Service");
            Service service = await cdnService.GetServiceAsync(serviceId);

            Console.WriteLine("{0}\t{1}", service.Id, service.Name);

            // Purge All Service Assets
            Console.WriteLine();
            Console.WriteLine("Purge All Service Assets...");
            await cdnService.PurgeCachedAssetsAsync(serviceId);

            // Purge a Specific Service Asset
            Console.WriteLine();
            Console.WriteLine("Purge a Specific Service Asset...");
            await cdnService.PurgeCachedAssetAsync(serviceId, "index.html");

            // Update Service
            Console.WriteLine();
            Console.WriteLine("Update Service...");
            var patch = new JsonPatchDocument <ServiceDefinition>();

            patch.Replace(svc => svc.Name, "newServiceName");
            await cdnService.UpdateServiceAsync(serviceId, patch);

            await cdnService.WaitForServiceDeployedAsync(serviceId);

            // Delete Service
            Console.WriteLine();
            Console.WriteLine("Delete Service...");
            await cdnService.DeleteServiceAsync(serviceId);

            await cdnService.WaitForServiceDeletedAsync(serviceId);
        }