Example #1
0
        public static async Task MainWaiter()
        {
            // Accepts profile name and creates a auth provider based on config file
            var    provider          = new ConfigFileAuthenticationDetailsProvider(OciConfigSourceProfileName);
            string bootVolumeBackup  = Environment.GetEnvironmentVariable("OCI_BOOT_VOLUME_BACKUP_ID");
            string destinationRegion = Environment.GetEnvironmentVariable("OCI_DESTINATION_REGION");

            // Create clients for the service to enable using its APIs
            var blockStorageClient = new BlockstorageClient(provider, new ClientConfiguration());

            try
            {
                // Copy Boot Volume backup
                var backupCopyId = await CopyBootVolumeBackup(blockStorageClient, bootVolumeBackup, destinationRegion);

                logger.Info($"Create backup copy with OCID:{backupCopyId}");

                // Create new client for making a retrying call for new backup copy in destination region
                var destinationProvider = new ConfigFileAuthenticationDetailsProvider(OciConfigDestinationProfileName);
                blockStorageClient = new BlockstorageClient(destinationProvider, new ClientConfiguration());
                var state = GetBootVolumeBackup(blockStorageClient, backupCopyId);

                logger.Info($"Successfully create backup copy with Lifecyle State:{state}");

                // Delete the backup copy for cleanup
                await DeleteBootVolumeBackup(blockStorageClient, backupCopyId);
            }
            catch (Exception e)
            {
                logger.Info($"Received exception due to {e.Message}");
            }
            finally
            {
                blockStorageClient.Dispose();
            }
        }
        public static async Task MainInstance()
        {
            logger.Info("Starting example");

            var provider      = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
            var compartmentId = Environment.GetEnvironmentVariable("OCI_COMPARTMENT_ID");

            var identityClient       = new IdentityClient(provider);
            var computeClient        = new ComputeClient(provider, new ClientConfiguration());
            var virtualNetworkClient = new VirtualNetworkClient(provider);
            var blockStorageClient   = new BlockstorageClient(provider);
            var networkCidrBlock     = "10.0.1.0/24";

            LaunchInstanceDetails launchInstanceDetails = null;
            Instance          instance               = null;
            BootVolume        bootVolume             = null;
            Instance          instanceFromBootVolume = null;
            Vcn               vcn               = null;
            Subnet            subnet            = null;
            CreateVnicDetails createVnicDetails = null;
            InternetGateway   internetGateway   = null;

            AvailabilityDomain availablityDomain = await getAvailabilityDomains(identityClient, compartmentId);

            logger.Info($"availability domain is {availablityDomain.Name}");

            Shape shape = await getShape(computeClient, compartmentId, availablityDomain);

            if (shape == null)
            {
                logger.Error($"No Shapes available in the availability domain: {availablityDomain.Name}");
                return;
            }
            logger.Info($"shape is {shape.ShapeProp}");
            Image image = await getImage(computeClient, compartmentId, shape);

            try
            {
                vcn = await createVcn(virtualNetworkClient, compartmentId, networkCidrBlock);

                // The Internet Gateway with updated Route Rules will enable the instance to connect to the public
                // internet. If it is not desired, remove the following two lines below that create an internet
                // gateway and add that internet gateway to the VCN route table.
                internetGateway = await createInternalGateway(virtualNetworkClient, compartmentId, vcn);
                await addInternetGatewayToDefaultRouteTable(virtualNetworkClient, vcn, internetGateway);

                subnet = await createSubnet(virtualNetworkClient, compartmentId, availablityDomain, networkCidrBlock, vcn);

                createVnicDetails = new CreateVnicDetails {
                    SubnetId = subnet.Id
                };

                launchInstanceDetails = new LaunchInstanceDetails
                {
                    AvailabilityDomain = availablityDomain.Name,
                    CompartmentId      = compartmentId,
                    Shape             = shape.ShapeProp,
                    CreateVnicDetails = createVnicDetails,
                    ImageId           = image.Id
                };

                instance = await createInstance(computeClient, launchInstanceDetails);
                await printInstance(computeClient, virtualNetworkClient, instance);

                logger.Info("Instance is being created via boot volume ...");
                // This boot volume is created based on the boot volume of previous instance which needs to be running
                bootVolume = await createBootVolume(blockStorageClient, compartmentId, availablityDomain, image);

                launchInstanceDetails  = createLaunchInstanceDetailsFromBootVolume(launchInstanceDetails, bootVolume);
                instanceFromBootVolume = await createInstance(computeClient, launchInstanceDetails);
                await printInstance(computeClient, virtualNetworkClient, instanceFromBootVolume);
            }
            catch (Exception e)
            {
                logger.Error($"Failed to call LaunchInstance API: {e.Message}");
            }
            finally
            {
                logger.Info("cleaning up resources");
                if (instanceFromBootVolume != null)
                {
                    await terminateInstance(computeClient, instanceFromBootVolume);
                }

                if (instance != null)
                {
                    await terminateInstance(computeClient, instance);
                }

                if (internetGateway != null)
                {
                    await clearRouteRulesFromDefaultRouteTable(virtualNetworkClient, vcn);
                    await deleteInternetGateway(virtualNetworkClient, internetGateway);
                }

                if (subnet != null)
                {
                    await deleteSubnet(virtualNetworkClient, subnet);
                }

                if (vcn != null)
                {
                    await deleteVcn(virtualNetworkClient, vcn);
                }

                identityClient.Dispose();
                computeClient.Dispose();
                virtualNetworkClient.Dispose();
                blockStorageClient.Dispose();

                logger.Info("End example");
            }
        }