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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
|
package digitalocean
import (
"context"
"golang.org/x/oauth2"
"github.com/digitalocean/godo"
"go.wit.com/log"
"go.wit.com/gui/gadgets"
// "go.wit.com/gui"
)
/*
// 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 (d *DigitalOcean) Create(name string) {
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 := d.createDropletNew(name, region, size, image)
if err != nil {
log.Fatalf("Something went wrong: %s\n", err)
}
log.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 (d *DigitalOcean) createDropletNew(name, region, size, image string) (*godo.Droplet, error) {
// Create an OAuth2 token.
tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
// Create an OAuth2 client.
oauthClient := oauth2.NewClient(context.Background(), tokenSource)
// Create a DigitalOcean client with the OAuth2 client.
client := godo.NewClient(oauthClient)
var sshKeys []godo.DropletCreateSSHKey
// Find the key by name.
for i, key := range d.sshKeys {
log.Info("found ssh i =", i, key.Name)
log.Verbose("found ssh key.Name =", key.Name)
log.Verbose("found ssh key.Fingerprint =", key.Fingerprint)
log.Verbose("found ssh key:", key)
/*
sshKeys = []godo.DropletCreateSSHKey{
{ID: key.ID},
}
*/
sshKeys = append(sshKeys, godo.DropletCreateSSHKey{ID: key.ID})
}
// Define the create request.
createRequest := &godo.DropletCreateRequest{
Name: name,
Region: region,
Size: size,
Image: godo.DropletCreateImage{
Slug: image,
},
IPv6: true, // Enable IPv6
SSHKeys: sshKeys, // Add SSH key IDs here
}
// Create the droplet.
ctx := context.TODO()
newDroplet, _, err := client.Droplets.Create(ctx, createRequest)
if err != nil {
return nil, err
}
return newDroplet, nil
}
var myCreate *windowCreate
// This is initializes the main DO object
// You can only have one of these
func InitCreateWindow() *windowCreate {
if ! myDo.Ready() {return nil}
if myCreate != nil {return myCreate}
myCreate = new(windowCreate)
myCreate.ready = false
myCreate.window = myDo.parent.NewWindow("Create Droplet")
// make a group label and a grid
myCreate.group = myCreate.window.NewGroup("droplets:").Pad()
myCreate.grid = myCreate.group.NewGrid("grid", 2, 1).Pad()
myCreate.name = gadgets.NewBasicEntry(myCreate.grid, "Name").Set("test.wit.com")
myCreate.grid.NewLabel("makes a new droplet")
myCreate.grid.NewButton("Create", func () {
myDo.Create(myCreate.name.Get())
})
myCreate.ready = true
myDo.create = myCreate
return myCreate
}
// Returns true if the status is valid
func (d *windowCreate) Ready() bool {
if d == nil {return false}
return d.ready
}
func (d *windowCreate) Show() {
if ! d.Ready() {return}
log.Info("digitalocean.Show() window")
if d.hidden {
d.window.Show()
}
d.hidden = false
}
func (d *windowCreate) Hide() {
if ! d.Ready() {return}
log.Info("digitalocean.Hide() window")
if ! d.hidden {
d.window.Hide()
}
d.hidden = true
}
|