Skip to content

Commit 070905b

Browse files
authored
Merge pull request #63 from plumgrid/miscellaneous
miscellaneous scripts added
2 parents f4f1dff + 759a93d commit 070905b

3 files changed

Lines changed: 299 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package main
2+
3+
import (
4+
"encoding/json"
5+
"flag"
6+
"fmt"
7+
"os"
8+
"strconv"
9+
10+
pg_rest "github.com/msagheer/miscellaneous/rest"
11+
)
12+
13+
func main() {
14+
15+
var net, lb string
16+
var del bool
17+
flag.StringVar(&net, "network", "", "Network ID")
18+
flag.StringVar(&lb, "lb", "", "loab balancer name")
19+
flag.BoolVar(&del, "delete", false, "delete the loab balancer")
20+
21+
flag.Parse()
22+
23+
pg_ip := os.Getenv("PG_API_IP")
24+
pg_port := os.Getenv("PG_API_PORT")
25+
pg_username := os.Getenv("PG_USERNAME")
26+
pg_password := os.Getenv("PG_PASSWORD")
27+
28+
i, err := strconv.Atoi(pg_port)
29+
rest_handle := pg_rest.CreatePGRestClient(pg_ip, i, pg_username, pg_password)
30+
if err := pg_rest.AttemptLogin(rest_handle); err != nil {
31+
fmt.Printf("Login Failed: %v", err)
32+
}
33+
34+
domain, netID := FindDomainFromNetwork(rest_handle, net)
35+
36+
url := pg_rest.GetRestPath(rest_handle) + "/v0/vd/" + domain + "/lbaas/" + lb
37+
38+
if !del {
39+
fmt.Printf("Creating Load Balancer (%v) in domain (%v) against bridge (%v)\n", lb, domain, netID)
40+
interface1 := make([]interface{}, 0)
41+
interface1 = append(interface1, map[string]interface{}{
42+
"name": "vip",
43+
"vip": "0.0.0.0",
44+
"attachment": netID,
45+
"port_id": "123-456-7890"})
46+
47+
data := map[string]interface{}{
48+
"origin": "openstack",
49+
"display_name": lb,
50+
"interfaces": interface1}
51+
52+
a, _ := json.Marshal(data)
53+
err, status_code, _ := pg_rest.RestPut(rest_handle, url, string(a))
54+
if err != nil {
55+
fmt.Printf("Error while posting VD : %v", err)
56+
} else if status_code != 200 {
57+
fmt.Printf("Failed to put VND domain: %s", status_code)
58+
}
59+
} else if del {
60+
fmt.Printf("Deleting Load Balancer (%v) in domain (%v) against bridge (%v)\n", lb, domain, netID)
61+
err, status_code, _ := pg_rest.RestDelete(rest_handle, url)
62+
if err != nil {
63+
fmt.Printf("Error while deleting VD : %v", err)
64+
} else if status_code != 200 {
65+
fmt.Printf("Failed to delete VND domain: %s", status_code)
66+
}
67+
68+
}
69+
70+
if err = pg_rest.AttemptLogout(rest_handle); err != nil {
71+
fmt.Println("Logout Failed: %v", err)
72+
}
73+
}
74+
75+
func FindDomainFromNetwork(rest_handle *pg_rest.PgRestHandle, ID string) (domainid string, netid string) {
76+
url := pg_rest.GetRestPath(rest_handle) + "/0/connectivity/domain?configonly=true&level=3"
77+
78+
err, status_code, body := pg_rest.RestGet(rest_handle, url)
79+
if err != nil {
80+
fmt.Printf("Error while getting VD : %v", err)
81+
} else if status_code != 200 {
82+
fmt.Printf("Failed to get VND domain: %s", status_code)
83+
}
84+
85+
var domain_data map[string]interface{}
86+
err = json.Unmarshal([]byte(body), &domain_data)
87+
if err != nil {
88+
panic(err)
89+
}
90+
for domains, domain_val := range domain_data {
91+
if nes, ok := domain_val.(map[string]interface{})["ne"]; ok {
92+
for ne, data := range nes.(map[string]interface{}) {
93+
if data.(map[string]interface{})["metadata"] == ID {
94+
domainid = domains
95+
netid = ne
96+
break
97+
}
98+
}
99+
}
100+
}
101+
return
102+
}

miscellaneous/rest/rest.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package rest
2+
3+
import (
4+
"bytes"
5+
"crypto/tls"
6+
"fmt"
7+
"io/ioutil"
8+
"net/http"
9+
"net/http/cookiejar"
10+
)
11+
12+
type PgRestHandle struct {
13+
Ip_or_host string
14+
Port int
15+
User string
16+
Password string
17+
18+
client_cookies *cookiejar.Jar
19+
httpClient *http.Client
20+
}
21+
22+
func CreatePGRestClient(rest_ip string, rest_Port int, Username string, Password string) *PgRestHandle {
23+
24+
cookies, _ := cookiejar.New(nil)
25+
26+
return &PgRestHandle{
27+
Ip_or_host: rest_ip,
28+
Port: rest_Port,
29+
User: Username,
30+
Password: Password,
31+
32+
client_cookies: cookies,
33+
httpClient: &http.Client{
34+
Transport: &http.Transport{
35+
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
36+
},
37+
Jar: cookies,
38+
},
39+
}
40+
}
41+
42+
func RestGet(handle *PgRestHandle, path string) (error, int, []byte) {
43+
44+
req, _ := http.NewRequest("GET", path, nil)
45+
46+
req.Header.Set("Accept", "application/json")
47+
48+
res, err := handle.httpClient.Do(req)
49+
if err != nil {
50+
return fmt.Errorf("Failed to login on PG plat: %v", err), 0, nil
51+
}
52+
53+
// Read body data
54+
body_data, err := ioutil.ReadAll(res.Body)
55+
defer res.Body.Close()
56+
57+
if err != nil {
58+
return fmt.Errorf("Error reading body data %v", err), 0, nil
59+
}
60+
61+
return nil, res.StatusCode, body_data
62+
}
63+
64+
func RestPost(handle *PgRestHandle, path string, data string) (error, int, []byte) {
65+
66+
req, _ := http.NewRequest("POST", path, bytes.NewBufferString(data))
67+
68+
req.Header.Set("Content-Type", "application/json")
69+
req.Header.Set("Accept", "application/json")
70+
71+
res, err := handle.httpClient.Do(req)
72+
if err != nil {
73+
return fmt.Errorf("Faild to POST: %v", err), 0, nil
74+
}
75+
76+
// Read body data
77+
body_data, err := ioutil.ReadAll(res.Body)
78+
defer res.Body.Close()
79+
80+
if err != nil {
81+
return fmt.Errorf("Error reading body data %v", err), 0, nil
82+
}
83+
84+
return nil, res.StatusCode, body_data
85+
}
86+
87+
func RestPut(handle *PgRestHandle, path string, data string) (error, int, []byte) {
88+
89+
req, _ := http.NewRequest("PUT", path, bytes.NewBufferString(data))
90+
91+
req.Header.Set("Content-Type", "application/json")
92+
req.Header.Set("Accept", "application/json")
93+
94+
res, err := handle.httpClient.Do(req)
95+
if err != nil {
96+
return fmt.Errorf("Faild to PUT: %v", err), 0, nil
97+
}
98+
99+
// Read body data
100+
body_data, err := ioutil.ReadAll(res.Body)
101+
defer res.Body.Close()
102+
103+
if err != nil {
104+
return fmt.Errorf("Error reading body data %v", err), 0, nil
105+
}
106+
107+
return nil, res.StatusCode, body_data
108+
}
109+
110+
func RestDelete(handle *PgRestHandle, path string) (error, int, []byte) {
111+
112+
req, _ := http.NewRequest("DELETE", path, nil)
113+
114+
req.Header.Set("Accept", "application/json")
115+
116+
res, err := handle.httpClient.Do(req)
117+
if err != nil {
118+
return fmt.Errorf("Failed to DELETE: %v", err), 0, nil
119+
}
120+
121+
// Read body data
122+
body_data, err := ioutil.ReadAll(res.Body)
123+
defer res.Body.Close()
124+
125+
if err != nil {
126+
return fmt.Errorf("Error reading body data %v", err), 0, nil
127+
}
128+
129+
return nil, res.StatusCode, body_data
130+
}

miscellaneous/rest/rest_utils.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package rest
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
// Login to platform. Cookies are taken care of from the underlay
8+
func AttemptLogin(handle *PgRestHandle) error {
9+
login_path := getLoginPath(handle)
10+
login_data := getLoginData(handle)
11+
12+
err, status_code, _ := RestPost(handle, login_path, login_data)
13+
if err != nil {
14+
return fmt.Errorf("Error while logging in: %v", err)
15+
} else if status_code != 200 {
16+
return fmt.Errorf("Failed to login: %d", status_code)
17+
}
18+
19+
// Set-cookie header should have been captured already by the cookiejar
20+
// Client will remain open with the cookie set (no keep-alive header though)
21+
22+
return nil
23+
}
24+
25+
// Logout from platform
26+
func AttemptLogout(handle *PgRestHandle) error {
27+
logout_path := getLogoutPath(handle)
28+
logout_data := getLogoutData(handle)
29+
30+
err, status_code, _ := RestPost(handle, logout_path, logout_data)
31+
if err != nil {
32+
return fmt.Errorf("Error while logging out: %v", err)
33+
} else if status_code != 200 {
34+
return fmt.Errorf("Failed to logout: %d", status_code)
35+
}
36+
37+
return nil
38+
}
39+
40+
// PG Rest Path
41+
func GetRestPath(handle *PgRestHandle) string {
42+
if handle.Port != 0 {
43+
return fmt.Sprintf("https://%s:%d", handle.Ip_or_host, handle.Port)
44+
} else {
45+
return fmt.Sprintf("https://%s", handle.Ip_or_host)
46+
}
47+
}
48+
49+
// Login Path
50+
func getLoginPath(handle *PgRestHandle) string {
51+
return GetRestPath(handle) + "/0/login"
52+
}
53+
54+
// Get login data
55+
func getLoginData(handle *PgRestHandle) string {
56+
return fmt.Sprintf("{'userName': '%s', 'password': '%s'}", handle.User, handle.Password)
57+
}
58+
59+
// Logout Path
60+
func getLogoutPath(handle *PgRestHandle) string {
61+
return GetRestPath(handle) + "/0/logout"
62+
}
63+
64+
// Get logout data
65+
func getLogoutData(handle *PgRestHandle) string {
66+
return fmt.Sprintf("{}")
67+
}

0 commit comments

Comments
 (0)