linode / linodego

Go client for Linode REST v4 API
MIT License
137 stars 81 forks source link

Add a list VPC IPs function for a specific VPC #488

Closed zliang-akamai closed 5 months ago

zliang-akamai commented 5 months ago

📝 Description

closes #483

✔️ How to Test

make ARGS="-run TestVPC_List" fixtures
import (
    "context"
    "encoding/json"
    "fmt"
    "os"

    "github.com/linode/linodego"
)

func main() {
    client := linodego.NewClient(nil)
    client.SetToken(os.Getenv("LINODE_TOKEN"))
    ctx := context.Background()
    region := "us-mia"
    vpc, err := client.CreateVPC(
        ctx, linodego.VPCCreateOptions{
            Label:  "test-vpc-ips",
            Region: region,
            Subnets: []linodego.VPCSubnetCreateOptions{
                {
                    Label: "test-vpc-ips-subnet",
                    IPv4:  "10.0.0.0/24",
                },
            },
        },
    )
    if err != nil {
        panic(err)
    }

    anyNAT11 := "any"
    instance, err := client.CreateInstance(
        ctx, linodego.InstanceCreateOptions{
            Region:   region,
            Image:    "linode/ubuntu22.04",
            Type:     "g6-nanode-1",
            RootPass: "this-is-an-UNSAFE-password",
            Interfaces: []linodego.InstanceConfigInterfaceCreateOptions{
                {
                    Purpose:  linodego.InterfacePurposeVPC,
                    SubnetID: &vpc.Subnets[0].ID,
                    IPv4: &linodego.VPCIPv4{
                        NAT1To1: &anyNAT11,
                    },
                },
            },
        },
    )
    if err != nil {
        panic(err)
    }

    VPCAllIPs, err := client.ListAllVPCIPAddresses(ctx, linodego.NewListOptions(0, ""))
    if err != nil {
        panic(err)
    }

    VPCIPs, err := client.ListVPCIPAddresses(ctx, vpc.ID, linodego.NewListOptions(0, ""))
    if err != nil {
        panic(err)
    }

    VPCAllIPsJson, err := json.Marshal(VPCAllIPs)
    if err != nil {
        panic(err)
    }

    VPCIPsJson, err := json.Marshal(VPCIPs)
    if err != nil {
        panic(err)
    }

    fmt.Println(string(VPCAllIPsJson))
    fmt.Println(string(VPCIPsJson))

    client.DeleteInstance(ctx, instance.ID)
    client.DeleteVPC(ctx, vpc.ID)
}