Beispiel #1
0
        /// <summary>Snippet for CreateInstance</summary>
        public void CreateInstance()
        {
            // Snippet: CreateInstance(ProjectName,InstanceName,Instance,CallSettings)
            // Create client
            InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create();
            // Initialize request argument(s)
            ProjectName  parent     = new ProjectName("[PROJECT]");
            InstanceName instanceId = new InstanceName("[PROJECT]", "[INSTANCE]");
            Instance     instance   = new Instance();
            // Make the request
            Operation <Instance, CreateInstanceMetadata> response =
                instanceAdminClient.CreateInstance(parent, instanceId, instance);

            // Poll until the returned long-running operation is complete
            Operation <Instance, CreateInstanceMetadata> completedResponse =
                response.PollUntilCompleted();
            // Retrieve the operation result
            Instance result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <Instance, CreateInstanceMetadata> retrievedResponse =
                instanceAdminClient.PollOnceCreateInstance(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                Instance retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }
        async void InitializeInstance()
        {
            InstanceAdminClient instanceAdminClient = await InstanceAdminClient.CreateAsync();

            try
            {
                string   name     = $"projects/{_fixture.ProjectId}/instances/{_fixture.InstanceId}";
                Instance response = await instanceAdminClient.GetInstanceAsync(name);
            }
            catch (RpcException ex) when(ex.Status.StatusCode == StatusCode.NotFound)
            {
                string   parent   = $"projects/{_fixture.ProjectId}";
                Instance instance = new Instance
                {
                    DisplayName = _fixture.InstanceId,
                    Name        = $"projects/{_fixture.ProjectId}/instances/{_fixture.InstanceId}",
                    NodeCount   = 1,
                    Config      = $"projects/{_fixture.ProjectId}/instanceConfigs/regional-us-central1"
                };

                // Make the CreateInstance request
                var response = instanceAdminClient.CreateInstance(parent, _fixture.InstanceId, instance);

                // Poll until the returned long-running operation is complete
                response.PollUntilCompleted();
            }
        }
    public Instance CreateInstanceWithProcessingUnits(string projectId, string instanceId)
    {
        // Create the InstanceAdminClient instance.
        InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create();

        InstanceName instanceName = InstanceName.FromProjectInstance(projectId, instanceId);

        // Initialize request parameters.
        Instance instance = new Instance
        {
            InstanceName = instanceName,
            ConfigAsInstanceConfigName = InstanceConfigName.FromProjectInstanceConfig(projectId, "regional-us-central1"),
            DisplayName     = "This is a display name.",
            ProcessingUnits = 500,
            Labels          =
            {
                { "cloud_spanner_samples", "true" },
            }
        };
        ProjectName projectName = ProjectName.FromProject(projectId);

        // Make the CreateInstance request.
        Operation <Instance, CreateInstanceMetadata> response = instanceAdminClient.CreateInstance(projectName, instanceId, instance);

        Console.WriteLine("Waiting for the operation to finish.");

        // Poll until the returned long-running operation is complete.
        Operation <Instance, CreateInstanceMetadata> completedResponse = response.PollUntilCompleted();

        if (completedResponse.IsFaulted)
        {
            Console.WriteLine($"Error while creating instance: {completedResponse.Exception}");
            throw completedResponse.Exception;
        }

        Console.WriteLine($"Instance created successfully.");

        // GetInstance to get the processing units using a field mask.
        GetInstanceRequest getInstanceRequest = new GetInstanceRequest
        {
            FieldMask = new FieldMask
            {
                Paths = { "processing_units" }
            },
            InstanceName = instanceName
        };

        instance = instanceAdminClient.GetInstance(getInstanceRequest);
        Console.WriteLine($"Instance {instanceId} has {instance.ProcessingUnits} processing units.");
        return(instance);
    }
    public static Instance SpannerCreateInstance(string projectId, string instanceId)
    {
        // Create the InstanceAdminClient instance.
        InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create();

        // Initialize request parameters.
        Instance instance = new Instance
        {
            InstanceName = InstanceName.FromProjectInstance(projectId, instanceId),
            ConfigAsInstanceConfigName =
                InstanceConfigName.FromProjectInstanceConfig(projectId, "regional-us-central1"),
            DisplayName = "This is a display name.",
            NodeCount   = 1,
            Labels      =
            {
                { "cloud_spanner_samples", "true" },
            }
        };
        ProjectName parentAsProjectName = ProjectName.FromProject(projectId);

        // Make the CreateInstance request.
        Operation <Instance, CreateInstanceMetadata> response =
            instanceAdminClient.CreateInstance(parentAsProjectName, instanceId, instance);

        Console.WriteLine("Waiting for the operation to finish.");

        // Poll until the returned long-running operation is complete.
        Operation <Instance, CreateInstanceMetadata> completedResponse =
            response.PollUntilCompleted();

        if (completedResponse.IsFaulted)
        {
            Console.WriteLine($"Error while creating instance: {completedResponse.Exception}");
            throw completedResponse.Exception;
        }

        Console.WriteLine($"Instance created successfully.");

        return(completedResponse.Result);
    }
Beispiel #5
0
        public void CreateInstance_RequestObject()
        {
            // Snippet: CreateInstance(CreateInstanceRequest,CallSettings)
            // Create client
            InstanceAdminClient instanceAdminClient = InstanceAdminClient.Create();
            // Initialize request argument(s)
            CreateInstanceRequest request = new CreateInstanceRequest
            {
                Parent     = new ProjectName("[PROJECT]").ToString(),
                InstanceId = "",
                Instance   = new Instance(),
            };
            // Make the request
            Operation <Instance> response =
                instanceAdminClient.CreateInstance(request);

            // Poll until the returned long-running operation is complete
            Operation <Instance> completedResponse =
                response.PollUntilCompleted();
            // Retrieve the operation result
            Instance result = completedResponse.Result;

            // Or get the name of the operation
            string operationName = response.Name;
            // This name can be stored, then the long-running operation retrieved later by name
            Operation <Instance> retrievedResponse =
                instanceAdminClient.PollOnceCreateInstance(operationName);

            // Check if the retrieved long-running operation has completed
            if (retrievedResponse.IsCompleted)
            {
                // If it has completed, then access the result
                Instance retrievedResult = retrievedResponse.Result;
            }
            // End snippet
        }