summaryrefslogtreecommitdiff
path: root/poweron.go
blob: 51f8a24ab8674f2f70b1633d3f663b3162351d61 (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
package digitalocean

import (
	"context"

	"golang.org/x/oauth2"

	"github.com/digitalocean/godo"

	"go.wit.com/log"
)

func (d *DigitalOcean) PowerOn(dropletID int) error {
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
	oauthClient := oauth2.NewClient(context.Background(), tokenSource)
	client := godo.NewClient(oauthClient)

	ctx := context.TODO()

	// Create a request to power on the droplet.
	_, _, err := client.DropletActions.PowerOn(ctx, dropletID)
	if err != nil {
		return err
	}

	log.Printf("Power-on signal sent to droplet with ID: %d\n", dropletID)
	return nil
}

func (d *DigitalOcean) PowerOff(dropletID int) error {
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
	oauthClient := oauth2.NewClient(context.Background(), tokenSource)
	client := godo.NewClient(oauthClient)

	ctx := context.TODO()

	// Create a request to power on the droplet.
	_, _, err := client.DropletActions.PowerOff(ctx, dropletID)
	if err != nil {
		return err
	}

	log.Printf("Power-off signal sent to droplet with ID: %d\n", dropletID)
	return nil
}

/*
func (d *DigitalOcean) Destroy(dropletID int) error {
	tokenSource := oauth2.StaticTokenSource(&oauth2.Token{AccessToken: d.token})
	oauthClient := oauth2.NewClient(context.Background(), tokenSource)
	client := godo.NewClient(oauthClient)

	ctx := context.TODO()

	// Create a request to power on the droplet.
	_, _, err := client.DropletActions.Delete(ctx, dropletID)
	if err != nil {
		return err
	}

	log.Printf("Destroy sent to droplet with ID: %d\n", dropletID)
	return nil
}
*/

// createDroplet creates a new droplet in the specified region with the given name.
func (d *DigitalOcean) deleteDroplet(drop *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)

	ctx := context.TODO()
	log.Warn("deleteDroplet() going to delete ID =", drop.ID, "Name =", drop.GetName())
	response, err := client.Droplets.Delete(ctx, drop.ID)
	log.Warn(response)
	return err
}