func scaleUpInstance()

in infrastructure/alloydb-scale-function/go-sample/alloydbscale.go [95:164]


func scaleUpInstance(ctx context.Context, project, location, cluster, instance string) error {
	// Scale Up an instance
	fmt.Println("Starting scaling up")

	// Create a new authenticated HTTP client using the application default credentials.
	client, err := google.DefaultClient(ctx, "https://www.googleapis.com/auth/cloud-platform")
	if err != nil {
		log.Fatal(err)
	}

	// Construct the URL to fetch the instance details
	instancesURL := apiURL + "/" + "projects/" + project + "/locations/" + location + "/clusters/" + cluster + "/instances/" + instance
	fmt.Println(instancesURL)
	instancesReq, err := http.NewRequest("GET", instancesURL, nil)
	if err != nil {
		log.Fatal(err)
	}
	resp, err := client.Do(instancesReq)
	if err != nil {

		log.Fatal(err)
	}
	fmt.Println(resp.Status)

	instanceBody, err := io.ReadAll(resp.Body)
	if err != nil {
		log.Fatal(err)
	}

	var instance_json Instance
	err = json.Unmarshal(instanceBody, &instance_json)
	if err != nil {
		log.Println(err)
	}

	fmt.Println(instance_json.MachineConfig.CpuCount)

	// Double CPU count if it's less than 64
	if instance_json.MachineConfig.CpuCount < 64 {
		var instance_patch Instance_patch
		instance_patch.MachineConfig.CpuCount = instance_json.MachineConfig.CpuCount * 2
		instance_patch.InstanceType = instance_json.InstanceType

		fmt.Println(instance_patch.MachineConfig.CpuCount)
		instance_payload, err := json.Marshal(instance_patch)
		if err != nil {
			log.Fatal(err)
		}

		fmt.Println(instance_patch)
		fmt.Println(string(instance_payload))
		// Construct the URL for the PATCH request, including update mask.
		instancesURL = apiURL + "/" + "projects/" + project + "/locations/" + location + "/clusters/" + cluster + "/instances/" + instance + "?updateMask=machineConfig.cpuCount"
		fmt.Println(instancesURL)
		// Create PATCH request to update the instance.
		instancesReq, err = http.NewRequest("PATCH", instancesURL, bytes.NewBuffer(instance_payload))
		if err != nil {
			log.Fatal(err)
		}

		resp, err = client.Do(instancesReq)
		if err != nil {

			log.Fatal(err)
		}
		fmt.Println(resp)
	}

	return nil
}