summaryrefslogtreecommitdiff
path: root/examples/control-panel-digitalocean/main.go
blob: b1228f7a5ea2d8eac3cb87b430e15c2a20a7faae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package main

import (
	"context"
	"fmt"
	"os"

	"golang.org/x/oauth2"

	"go.wit.com/log"
	"go.wit.com/gui"
	"github.com/digitalocean/godo"
	"go.wit.com/control-panel-dns/digitalocean"
)

var title string = "Digital Ocean Control Panel"

/*
// createDroplet creates a new droplet in the specified region with the given name.
func createDroplet(token, name, region, size, image string) (*godo.Droplet, error) {
	// Create an OAuth2 token.
	tokenSource := &oauth2.Token{
		AccessToken: token,
	}

	// Create an OAuth2 client.
	oauthClient := oauth2.NewClient(context.Background(), tokenSource)

	// Create a DigitalOcean client with the OAuth2 client.
	client := godo.NewClient(oauthClient)

	// Define the create request.
	createRequest := &godo.DropletCreateRequest{
		Name:   name,
		Region: region,
		Size:   size,
		Image: godo.DropletCreateImage{
			Slug: image,
		},
	}

	// Create the droplet.
	ctx := context.TODO()
	newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
	if err != nil {
		return nil, err
	}

	return newDroplet, nil
}
*/

func main() {
	// Your personal API token from DigitalOcean.
	token := os.Getenv("DIGITALOCEAN_TOKEN")
	if token == "" {
		log.Fatal("Please set your DigitalOcean API token in the DIGITALOCEAN_TOKEN environment variable")
	}

	// List droplets and their IP addresses.
	err := digitalocean.ListDroplets(token)
	if err != nil {
		log.Fatalf("Error listing droplets: %s\n", err)
	}

	// initialize a new GO GUI instance
	myGui := gui.New().Default()

	// draw the cloudflare control panel window
	win := digitalocean.MakeWindow(myGui)
	win.SetText(title)

	// This is just a optional goroutine to watch that things are alive
	gui.Watchdog()
	gui.StandardExit()

	os.Exit(0)

	// Parameters for the droplet you wish to create.
	name := "ipv6.wit.com"
	region := "nyc1" // New York City region.
	size := "s-1vcpu-1gb" // Size of the droplet.
	image := "ubuntu-20-04-x64" // Image slug for Ubuntu 20.04 (LTS) x64.

	// Create a new droplet.
	droplet, err := createDropletNew(token, name, region, size, image)
	if err != nil {
		log.Fatalf("Something went wrong: %s\n", err)
	}

	fmt.Printf("Created droplet ID %d with name %s\n", droplet.ID, droplet.Name)
}

// createDroplet creates a new droplet in the specified region with the given name.
func createDropletNew(token, name, region, size, image string) (*godo.Droplet, error) {
	// Create an OAuth2 token.
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token})

	// Create an OAuth2 client.
	oauthClient := oauth2.NewClient(context.Background(), tokenSource)

	// Create a DigitalOcean client with the OAuth2 client.
	client := godo.NewClient(oauthClient)

	// Define the create request.
	createRequest := &godo.DropletCreateRequest{
		Name:   name,
		Region: region,
		Size:   size,
		Image: godo.DropletCreateImage{
			Slug: image,
		},
		IPv6: true, // Enable IPv6
	}

	// Create the droplet.
	ctx := context.TODO()
	newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
	if err != nil {
		return nil, err
	}

	return newDroplet, nil
}