-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #133 from aswinmprabhu/master
Add rackspace authentication package
- Loading branch information
Showing
6 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package rackspacecompute | ||
|
||
import "testing" | ||
|
||
func TestCreateNode(t *testing.T) { | ||
|
||
var rackspacecompute Rackspacecompute | ||
|
||
request := map[string]interface{}{ | ||
"Name": "testcloudserver", | ||
"ImageRef": "3afe97b2-26dc-49c5-a2cc-a2fc8d80c001", | ||
"FlavourRef": "2", | ||
"Region": "DFW", | ||
} | ||
_, err := rackspacecompute.CreateNode(request) | ||
|
||
if err != nil { | ||
t.Errorf("Test Fail") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
package rackspaceauth | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"io/ioutil" | ||
"log" | ||
"net/http" | ||
"os" | ||
) | ||
|
||
// TokenSource struct for representing RackSpace credentials. | ||
type TokenSource struct { | ||
RackSpaceTenantID string | ||
RackSpaceTenantName string | ||
RackSpaceAuthToken string | ||
RackSpaceAPIKey string | ||
RackSpaceUsername string | ||
Endpoints map[string][]Endpoint | ||
} | ||
|
||
// Endpoint represents the enpoint of a particular service | ||
type Endpoint struct { | ||
URL string `json:"publicURL"` | ||
Region string `json:"region"` | ||
} | ||
|
||
// Token is a variable of type TokenSource. | ||
var Token TokenSource | ||
|
||
// LoadConfigAndAuthenticate loads the RackSpace credentials. | ||
func LoadConfigAndAuthenticate() { | ||
|
||
// Read from file first. | ||
var home = os.Getenv("HOME") | ||
file, _ := os.Open(home + "/.gocloud" + "/rackspacecloudconfig.json") | ||
|
||
// Defer the closing of our file so that we can parse it later on. | ||
defer file.Close() | ||
|
||
// We initialize TokenSource struct. | ||
decoder := json.NewDecoder(file) | ||
Token = TokenSource{} | ||
_ = decoder.Decode(&Token) | ||
|
||
if Token.RackSpaceAPIKey == "" || Token.RackSpaceUsername == "" { | ||
// If digioceancloudconfig.json doesn't exist, look for credentials as environment variables. | ||
|
||
Token.RackSpaceAPIKey = os.Getenv("RackSpaceAPIKey") | ||
Token.RackSpaceUsername = os.Getenv("RackSpaceUsername") | ||
if Token.RackSpaceAPIKey == "" || Token.RackSpaceUsername == "" { | ||
log.Fatalln("Cannot get access token for RackSpace.") | ||
} | ||
} | ||
|
||
// prepare the authoriation request | ||
url := "https://identity.api.rackspacecloud.com/v2.0/tokens" | ||
authRequestData := map[string]interface{}{ | ||
"auth": map[string]interface{}{ | ||
"RAX-KSKEY:apiKeyCredentials": map[string]interface{}{ | ||
"username": Token.RackSpaceUsername, | ||
"apiKey": Token.RackSpaceAPIKey, | ||
}, | ||
}, | ||
} | ||
jsonStr, err := json.Marshal(authRequestData) | ||
if err != nil { | ||
log.Fatalln("Failed to marshal json") | ||
} | ||
req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonStr)) | ||
if err != nil { | ||
log.Fatalln("Failed to create the request") | ||
} | ||
req.Header.Set("Content-Type", "application/json") | ||
|
||
// make the authorization request | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
log.Fatalln("Failed to make the request") | ||
} | ||
defer resp.Body.Close() | ||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
log.Fatalln("Failed to read the response body") | ||
} | ||
|
||
// Extract the Auth Token and the tenant details | ||
respJSON := make(map[string]interface{}) | ||
err = json.Unmarshal(body, &respJSON) | ||
if err != nil { | ||
log.Fatalln("Failed to unmarshal response JSON") | ||
} | ||
accessJSON := respJSON["access"].(map[string]interface{}) | ||
tokenJSON := accessJSON["token"].(map[string]interface{}) | ||
serviceCatalogJSON := accessJSON["serviceCatalog"].([]map[string]interface{}) | ||
for _, service := range serviceCatalogJSON { | ||
endpointsBytes, _ := json.Marshal(service["endpoints"].([]map[string]interface{})) | ||
serviceName := service["name"].(string) | ||
json.Unmarshal(endpointsBytes, Token.Endpoints[serviceName]) | ||
} | ||
Token.RackSpaceAuthToken = tokenJSON["id"].(string) | ||
tenantJSON := tokenJSON["tenant"].(map[string]interface{}) | ||
Token.RackSpaceTenantID = tenantJSON["id"].(string) | ||
Token.RackSpaceTenantName = tenantJSON["name"].(string) | ||
} |