Skip to main content
GCP SDK Fun
  1. Posts/

GCP SDK Fun

·3 mins
William Collins
Author
William Collins
Building at the intersection of cloud, automation, and AI. Host of The Cloud Gambit podcast.
Table of Contents

Sometimes, especially if you aren’t a developer by trade, you can get stuck on something small that will find you banging your forehead on your desk (figuratively, of course). Most of the time, it is easy enough to find an answer online or even from ChatGPT. Other times, you may not be so lucky. The other day, I fell victim to a TWE or time wasting event that I thought was worth writing about. Fasten your seatbelt!

Intro
Intro

The Problem
#

GCP SDK provides libraries and tools for interacting with Google’s cloud products and services. Pick your poison with whatever programming language you choose! On this particular Fun Day, I was writing a quick script to build VPCs and subnets from .yaml files and then expose a destroy call to wipe everything out when particular testing was completed. I’ve built something similar for AWS SDK for Go. On this particular day, I ran into the following error:

Error
Error

Digging into the Code
#

I knew I had to set AutoCreateSubnetworks: false, in the code as it disables GCP from automagically creating subnets for me. Of course, I want to do that myself! And this is where the confusion started – I did have this set, and I had omitted the ‘IPv4Range’ at the VPC level despite what the error said.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
func createVPCAndSubnets(ctx context.Context, service *compute.Service, vpcConfig *VPCConfig) {
    fmt.Printf("Creating VPC: %s\n", vpcConfig.Name)
    op, err := service.Networks.Insert(vpcConfig.Project, &compute.Network{
        Name:                  vpcConfig.Name,
        AutoCreateSubnetworks: false,
    }).Do()

    if err != nil {
        log.Fatalf("Failed to create VPC: %s, error: %s", vpcConfig.Name, err)
    }

Solving the Problem
#

I searched on DuckDuckGo, my search engine of choice, and came up empty. In my desperation, I turned to a bucket-of-bolts I don’t fully understand who’s supposed to know all the answers, ChatGPT. I pasted in the error message and code. The AI Overlord then patted me on the back (again figuratively) and told me to set AutoCreateSubnetworks: false and remove IPv4Range from my code since that is for legacy networks. The problem is that IPv4Range wasn’t defined anywhere in my code and AutoCreateSubnetworks was already set to false.

Google Wins
#

As I sat pondering reality, space, and time, I decided to just google search it and came across this excellent answer on Stack Overflow. Apparently, the AutoCreateSubnetworks field is not included in the request payload unless you define it explicitly with ForceSendFields: []string{“AutoCreateSubnetworks”},. All I needed was this extra line:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
func createVPCAndSubnets(ctx context.Context, service *compute.Service, vpcConfig *VPCConfig) {
    fmt.Printf("Creating VPC: %s\n", vpcConfig.Name)
    op, err := service.Networks.Insert(vpcConfig.Project, &compute.Network{
        Name:                  vpcConfig.Name,
        AutoCreateSubnetworks: false,
        ForceSendFields: []string{"AutoCreateSubnetworks"}, // I was missing!
    }).Do()

    if err != nil {
        log.Fatalf("Failed to create VPC: %s, error: %s", vpcConfig.Name, err)
    }

Conclusion
#

Artificial Intelligence is a society-altering technology. Is it coming for our jobs anytime soon? I don’t think so. Will it help enhance and augment our workflows? Yes, Absolutely! However, situations like this remind me that there is no substitute for people who possess deep skills and understanding in particular areas of technology. This holds especially TRUE when something fails or isn’t exhibiting the correct behavior. Happy coding!

Related

Configuring Systems With ChatGPT, Python, and Go

·5 mins
I recently wrote a blog about generating Terraform with ChatGPT. In that experiment, I was using GPT-3.5. It didn’t take much to trip up the galactic AI in many of the experiments I did with Terraform and beyond. According to OpenAI GPT-4 soars past its predecessor in its advanced reasoning capabilities. Let’s use this model to build code in Python and Go. How will the super titan fair in the Sysadmin arena? Let’s take on some tedious backend work head-on with the biggest brain around and see what happens.

Can ChatGPT Terraform Simple Networking In AWS?

Usually, when it comes to technology, my grandmother doesn’t know much because she doesn’t care. What is the cloud? How to install a new browser on her laptop? What is 2FA? I might be speaking French to her as I discuss these things. Yet, she knows what ChatGPT is. This shows the vast amount of publicity, hype, and polarization that has ensued since November 2022. I tend to avoid AI fear-mongering and focus more on, how could a tool like this help enhance my daily grind? Can ChatGPT write Terraform as elegantly as a poem written from the perspective of Samuel L. Jackson in Pulp Fiction? Let’s take it for a spin on AWS using infrastructure-as-code.

Getting Started With Alkira And Terraform (Part 2)

In Part 1, we started with a scalable foundation that can adapt over time as the business grows and adjusts to changing markets. With Alkira’s Network Cloud, we take a cloud native approach in enabling our customer’s transformation. No appliances need to be provisioned in remote VPCs or VNets, and no agents need to be installed on workloads. Getting started is as easy as kicking off a build pipeline. For Part 2, let’s connect some networks from AWS, Azure, and GCP.