Skip to content

Instantly share code, notes, and snippets.

@optik-aper
Created January 25, 2023 19:55
Show Gist options
  • Save optik-aper/e15664f290866a58acb1254da4653d19 to your computer and use it in GitHub Desktop.
Save optik-aper/e15664f290866a58acb1254da4653d19 to your computer and use it in GitHub Desktop.
Govultr vm spammer
package main
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/vultr/govultr/v2"
"golang.org/x/oauth2"
)
func main() {
vultrClient, ctx := testClient()
for i := 0; i < 550; i++ {
instCreate(ctx, vultrClient, fmt.Sprintf("inst-spam-%d", i))
time.Sleep(1 * time.Second)
}
}
func prodClient() (*govultr.Client, context.Context) {
apiKey := "<YOUR PROD API KEY>"
config := &oauth2.Config{}
ctx := context.Background()
ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: apiKey})
vultrClient := govultr.NewClient(oauth2.NewClient(ctx, ts))
return vultrClient, ctx
}
func testClient() (*govultr.Client, context.Context) {
apiKey := "<YOUR LOCAL API KEY>"
config := &oauth2.Config{}
ctx := context.Background()
ts := config.TokenSource(ctx, &oauth2.Token{AccessToken: apiKey})
vultrClient := govultr.NewClient(oauth2.NewClient(ctx, ts))
vultrClient.SetBaseURL("http://local.api.vultr.com")
return vultrClient, ctx
}
func instCreate(ctx context.Context, client *govultr.Client, label string) {
inst, err := client.Instance.Create(ctx, &govultr.InstanceCreateReq{
Region: "ewr",
Plan: "vc2-1c-1gb",
Label: label,
OsID: 1946,
Tags: []string{"govultr"},
})
if err != nil {
fmt.Println(err)
panic(err.Error())
}
printJson(inst)
}
func printJson(output interface{}) {
json, err := json.MarshalIndent(output, "", "\t")
if err != nil {
fmt.Println(err)
panic(err.Error())
}
fmt.Printf("%s", string(json))
}
@optik-aper
Copy link
Author

optik-aper commented Mar 1, 2023

func instList(client *govultr.Client, ctx context.Context) {
	insts, _, err := client.Instance.List(ctx, nil)

	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(insts)
}

func instGet(client *govultr.Client, ctx context.Context) {
	instId := "a78adc96-6c94-4ffd-ab0c-5d7fe5caba90"
	insts, err := client.Instance.Get(ctx, instId)

	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(insts)
}

func instDelete(client *govultr.Client, ctx context.Context) {
	instId := "2f51b3c2-917d-4856-bbf2-1e2e2a275120"
	err := client.Instance.Delete(ctx, instId)

	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}
}

func instCreate(client *govultr.Client, ctx context.Context) {
	inst, err := client.Instance.Create(ctx, &govultr.InstanceCreateReq{
		Region: "ord",
		Plan:   "vc2-1c-1gb",
		Label:  "inst-ip-reboot-test",
		OsID:   1946,
		Tags:   []string{"govultr"},
	})
	if err != nil {
		fmt.Println(err)
		panic(err.Error())
	}

	printJson(inst)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment