You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@trafficcontrol.apache.org by mi...@apache.org on 2017/05/17 19:21:54 UTC

[1/2] incubator-trafficcontrol git commit: created folder for cdn api scripts

Repository: incubator-trafficcontrol
Updated Branches:
  refs/heads/master 8d645a678 -> 77cd3adca


created folder for cdn api scripts


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/8145b4f0
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/8145b4f0
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/8145b4f0

Branch: refs/heads/master
Commit: 8145b4f051ac51bde95ca26150ef657ce9fda12d
Parents: 8d645a6
Author: Ashish Timilsina <as...@comcast.com>
Authored: Wed May 17 15:09:32 2017 -0400
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Wed May 17 13:21:07 2017 -0600

----------------------------------------------------------------------
 traffic_ops/experimental/api_scripts/README.md  | 10 +++
 .../experimental/api_scripts/cdn_api_mojokey.go | 69 ++++++++++++++++++++
 2 files changed, 79 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/8145b4f0/traffic_ops/experimental/api_scripts/README.md
----------------------------------------------------------------------
diff --git a/traffic_ops/experimental/api_scripts/README.md b/traffic_ops/experimental/api_scripts/README.md
new file mode 100644
index 0000000..39a55cb
--- /dev/null
+++ b/traffic_ops/experimental/api_scripts/README.md
@@ -0,0 +1,10 @@
+# Overview
+
+
+## How to run go scripts:
+1. Create a folder called /Downloads/scripts
+2. Store your CDN_API_Credentials.txt in this folder (user, pw on separate lines)
+
+## To run cdn_api_mojokey.go:
+1. go run cdn_api_mojokey.go
+2. It will return a mojokey that you can use to authenticate all other API requests

http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/blob/8145b4f0/traffic_ops/experimental/api_scripts/cdn_api_mojokey.go
----------------------------------------------------------------------
diff --git a/traffic_ops/experimental/api_scripts/cdn_api_mojokey.go b/traffic_ops/experimental/api_scripts/cdn_api_mojokey.go
new file mode 100644
index 0000000..ab88f71
--- /dev/null
+++ b/traffic_ops/experimental/api_scripts/cdn_api_mojokey.go
@@ -0,0 +1,69 @@
+package main
+
+//to run-> go run cdn_api_mojokey.go
+
+import (
+	"bytes"
+	"encoding/json"
+	"fmt"
+	"io/ioutil"
+	"log"
+	"net/http"
+	"os"
+	"regexp"
+	"strings"
+)
+
+//ERROR HANDLER -------------------------------------------------------------
+func checkError(message string, err error) {
+	if err != nil {
+		log.Fatal(message, err)
+	}
+}
+
+//READ IN CREDENTIALS FROM FILE--------------------------------------------------------
+func read_creds_file() (user, pw string) {
+	path := os.Getenv("HOME") + "/Downloads/scripts/"
+	file, err := os.Open(path + "CDN_API_Credentials.txt")
+	checkError("Failed to open creds file", err)
+	contents, err := ioutil.ReadAll(file) //read file to bytes
+	checkError("Failed to read creds file", err)
+	creds := strings.Split(string(contents), "\n") //create a slice of the contents, must convert from bytes to string
+	file.Close()                                   //closes file
+	user = creds[0]                                // extract user from file strings
+	pw = creds[1]
+
+	return
+}
+
+//API Request for Mojolicious Key-------------------------------------------------------------
+func api_request(user, pw string) (mojo_key string) {
+	user_pw := map[string]string{"p": pw, "u": user}                                // create a map of user and pw for inclusion in request
+	user_pw_json, _ := json.Marshal(user_pw)                                        // convert user/password map to json structure for inclusion in request
+	key_url := "https://cdnportal.comcast.net/api/1.2/user/login"                   // add key to url
+	key_req, err := http.NewRequest("POST", key_url, bytes.NewBuffer(user_pw_json)) // post request to get mojolicious cookie
+	key_req.Header.Add("Accept", "application/json")
+	key_client := &http.Client{}
+	key_resp, err := key_client.Do(key_req)
+	checkError("Failed key http request: ", err)
+	key_resp.Body.Close()
+
+	//Extract Mojolicious cookie from response headers map
+	pattern := regexp.MustCompile(`mojolicious=([A-Za-z0-9\-\_]+);`) //compile regex to extract cookie from response
+	mojo_cookie := key_resp.Header["Set-Cookie"]                     // get cookie header from http response
+	mojo_key = pattern.FindStringSubmatch(mojo_cookie[0])[1]         //extract cookie from response
+	return
+}
+
+func main() {
+
+	//GET CREDENTIALS FROM FILE IF NOT ENTERED ON COMMAND LINE
+	fmt.Println("Getting user credentials from file...")
+	user, pw := read_creds_file()
+	fmt.Println("USER =", user)
+	//GET MOJOLICIOUS KEY FROM API
+	fmt.Println("Sending API call to get mojolicious key...")
+	mojo_key := api_request(user, pw)
+	fmt.Println("MOJO KEY =", mojo_key)
+
+}


[2/2] incubator-trafficcontrol git commit: This closes #585

Posted by mi...@apache.org.
This closes #585


Project: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/commit/77cd3adc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/tree/77cd3adc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-trafficcontrol/diff/77cd3adc

Branch: refs/heads/master
Commit: 77cd3adca32d9495267eb2b2717c8be934bd4c56
Parents: 8145b4f
Author: Jeremy Mitchell <mi...@gmail.com>
Authored: Wed May 17 13:21:45 2017 -0600
Committer: Jeremy Mitchell <mi...@gmail.com>
Committed: Wed May 17 13:21:45 2017 -0600

----------------------------------------------------------------------

----------------------------------------------------------------------