Compare commits

...

3 Commits

Author SHA1 Message Date
Mike Holloway
d13370f89e On branch golang-http-package
Changes to be committed:
	new file:   curl_droplets-by-tag
		dumb bash script to test API query
	new file:   do_2-node-cluster.tkn
		Add a token file - THIS SHOULD BE REGENERATED IN PRODUCTION
	modified:   main.go
2021-07-05 00:42:23 -04:00
Mike Holloway
e91a444354 On branch golang-master
Changes to be committed:
	modified:   main.go
		Adding nice comments
2021-07-04 03:10:22 -04:00
Mike Holloway
202814d46c On branch golang-master
Changes to be committed:
	new file:   main.go
		Basic url construction working
2021-07-04 03:03:13 -04:00
3 changed files with 68 additions and 0 deletions

1
curl_droplets-by-tag Executable file
View File

@ -0,0 +1 @@
curl -X GET -H "Content-Type: application/json" -H "Authorization: Bearer 72a0ec632d0834dd3e1b72ec096f4644f527853a747875ca387a0269a625597a" "https://api.digitalocean.com/v2/droplets?tag_name=pacemaker-project"

1
do_2-node-cluster.tkn Normal file
View File

@ -0,0 +1 @@
72a0ec632d0834dd3e1b72ec096f4644f527853a747875ca387a0269a625597a

66
main.go Normal file
View File

@ -0,0 +1,66 @@
package main
import (
"bytes"
"log"
"net/http"
"io/ioutil"
"strings"
"encoding/json"
/*
"fmt"
*/
)
func main() {
// Create a slice to insert the ID of the current droplet
url_slice := []string{"https://api.digitalocean.com/v2/droplets/", get_droplet_id("foo"), "/actions"}
// Construct the string from the slice
droplet_actions_url := strings.Join(url_slice, "")
/* ---- Old ------ Using http.Get
// Create the json object to be posted
post_body, _ := json.Marshal(map[string]string{"type": "power_off"})
// Open a buffer for the post response body
response_body := bytes.NewBuffer(post_body)
// instantiate the POST object
http_response, err := http.Post(droplet_actions_url, "application/json", response_body)
*/
do_client := &http.Client{
CheckRedirect: redirectPolicyFunc,
}
http_response, err := do_client.Get(droplet_actions_url))
if err != nil {
log.Fatalf("Responese Error: %v", err)
}
// Create an http.Request object to store the request data
// Is 'nil' the correct parameter? Check the docs
// How is the response body cached? Is the 'nil' parameter the actual POST body?
// Is the post_body to be fashioned with json.Marshal as above? Where is it
// used as a parameter?
http_request, err := http.Request("POST", droplet_actions_url, nil)
// Add the Authorization header with the OAuth token to the request data
http_request.Header.Add("Authorization", "Bearer 72a0ec632d0834dd3e1b72ec096f4644f527853a747875ca387a0269a625597a")
http_response, err := client.Post(droplet_actions_url, "application/json", response_body)
// Pause thread until response is read and http connection closes (I think?)
defer http_response.Body.Close()
body, err := ioutil.ReadAll(response_body)
if err != nil {
log.Fatalln(err)
}
// Turn the http response object into a string
response_body_string := string(body)
log.Printf(response_body_string)
}
func get_droplet_id(hostname string) string {
/*
Eventually, the goal is to use droplet tags to group the cluster,
use the droplet tags api to list all droplets, differentially
identify 'other' nodes with knowledge of our current droplet properties,
and finally return the droplet_id that corresponds to the supplied hostname
*/
// var droplet_id string = "253365345"
var droplet_id string = "253365347"
return droplet_id
}