Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #7 #49

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions client/authentication.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func (c *NsxtClient) Login(cred url.Values) error {
}

func (c *NsxtClient) Logout() {
if c.Token == "" {
return
}
target_url := c.BaseUrl + "/api/session/destroy"
req, _ := http.NewRequest("POST", target_url, nil)
req.Header.Set("X-Xsrf-Token", c.Token)
Expand Down
18 changes: 12 additions & 6 deletions client/fabric.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
func (c *NsxtClient) GetComputeManager() *[]structs.ComputeManager {
path := "/api/v1/fabric/compute-managers"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
res_cms := res.Body.(map[string]interface{})["results"]
cms := []structs.ComputeManager{}
for _, res_cm := range res_cms.([]interface{}) {
Expand Down Expand Up @@ -253,9 +256,6 @@ func (c *NsxtClient) GetTransportNodeTunnels(node_id string) structs.TransportNo
func (c *NsxtClient) GetTransportNodeStatus(id string) string {
path := "/api/v1/transport-nodes/" + id + "/status?source=realtime"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
return ""
}
return res.Body.(map[string]interface{})["status"].(string)
}

Expand All @@ -280,6 +280,9 @@ func (c *NsxtClient) GetTransportNodeProfile() *structs.TransportNodeProfiles {
func (c *NsxtClient) GetEdgeCluster() *[]structs.EdgeCluster {
path := "/api/v1/edge-clusters/"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
edgeClusters := []structs.EdgeCluster{}
for _, ec := range res.Body.(map[string]interface{})["results"].([]interface{}) {
str, _ := json.Marshal(ec)
Expand Down Expand Up @@ -318,14 +321,17 @@ func (c *NsxtClient) CreateEdge(name string, template_name string, address strin
}

path := "/api/v1/transport-nodes"
var resp *Response
resp = c.Request("POST", path, nil, jsonObj)
fmt.Println(resp)
var res *Response
res = c.Request("POST", path, nil, jsonObj)
fmt.Println(res)
}

func (c *NsxtClient) GetEdge() []structs.TransportNode {
path := "/api/v1/transport-nodes?node_types=EdgeNode"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
edges := []structs.TransportNode{}
for _, e := range res.Body.(map[string]interface{})["results"].([]interface{}) {
str, _ := json.Marshal(e)
Expand Down
6 changes: 6 additions & 0 deletions client/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ func (c *NsxtClient) GetTier0Gateway(gwId string) structs.Tier0Gateways {
path = path + "/" + gwId
}
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
var gateways interface{}
gws := structs.Tier0Gateways{}
if gwId != "" {
Expand Down Expand Up @@ -43,6 +46,9 @@ func (c *NsxtClient) GetTier1Gateway(gwId string) structs.Tier1Gateways {
path = path + "/" + gwId
}
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
var gateways interface{}
gws := structs.Tier1Gateways{}
if gwId != "" {
Expand Down
20 changes: 10 additions & 10 deletions client/infra.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,16 @@ package client

import (
"fmt"
"log"

"github.com/hichtakk/nsxctl/structs"
)

func (c *NsxtClient) GetSite() []string {
req := c.makeRequest("GET", "/policy/api/v1/infra/sites")
res, err := c.httpClient.Do(req)
if err != nil {
fmt.Println(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
fmt.Printf("StatusCode=%d\n", res.StatusCode)
return make([]string, 0, 0)
res := c.Request("GET", "/policy/api/v1/infra/sites", nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
//data := readResponseBody(res)
/*
gateways := data.(map[string]interface{})["results"]
for _, gateway := range gateways.([]interface{}) {
Expand All @@ -36,6 +30,9 @@ func (c *NsxtClient) GetEnforcementPoint(site_id string) *[]structs.EnforcementP
path := "/policy/api/v1/infra/sites/" + site_id + "/enforcement-points"
eps := []structs.EnforcementPoint{}
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
for _, ep := range res.Body.(map[string]interface{})["results"].([]interface{}) {
id := ep.(map[string]interface{})["id"].(string)
path := ep.(map[string]interface{})["path"].(string)
Expand Down Expand Up @@ -79,5 +76,8 @@ func (c *NsxtClient) GetEdgeClusterUnderEnforcementPoint(site_id string, ep_id s
func (c *NsxtClient) GetVersion() string {
path := "/api/v1/node/version"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
return res.Body.(map[string]interface{})["product_version"].(string)
}
7 changes: 7 additions & 0 deletions client/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"encoding/json"
"fmt"
"log"
"net/http"

"github.com/hichtakk/nsxctl/structs"
Expand All @@ -12,6 +13,9 @@ import (
func (c *NsxtClient) GetIpPool() structs.IpPools {
path := "/policy/api/v1/infra/ip-pools"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
ipps := structs.IpPools{}
str, _ := json.Marshal(res.Body.(map[string]interface{})["results"].([]interface{}))
json.Unmarshal(str, &ipps)
Expand All @@ -22,6 +26,9 @@ func (c *NsxtClient) GetIpPool() structs.IpPools {
func (c *NsxtClient) GetIpBlock() structs.IpBlocks {
path := "/policy/api/v1/infra/ip-blocks"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
ipbs := structs.IpBlocks{}
for _, b := range res.Body.(map[string]interface{})["results"].([]interface{}) {
str, _ := json.Marshal(b)
Expand Down
4 changes: 4 additions & 0 deletions client/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/sha256"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -74,6 +75,9 @@ func (c *NsxtClient) makeRequest(method string, path string) *http.Request {
}

func (c *NsxtClient) Request(method string, path string, query_param map[string]string, req_data []byte) *Response {
if c.Token == "" {
return &Response{nil, nil, errors.New("You must be logged in to the NSX-T Manager (Unauthorized)")}
}
// validate path
err := func() error {
var match bool
Expand Down
8 changes: 7 additions & 1 deletion client/security.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ import (
func (c *NsxtClient) GetDfwPolicies(domain string, name string) ([]structs.DfwPolicy, error) {
path := "/policy/api/v1/infra/domains/" + domain + "/security-policies?include_rule_count=true"
res := c.Request("GET", path, nil, nil)

if res.Error != nil {
log.Fatal(res.Error)
}

body, err := res.BodyBytes()
if err != nil {
return nil, err
Expand All @@ -37,6 +40,9 @@ func (c *NsxtClient) GetDfwRules(policy structs.DfwPolicy) []structs.DfwRule {
parent_path := "*security-policies*" + policy.Id
path := "/policy/api/v1/search/query?query=resource_type:Rule%20AND%20parent_path:" + url.PathEscape(parent_path)
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}

body, err := res.BodyBytes()
if err != nil {
Expand Down
3 changes: 3 additions & 0 deletions client/segment.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import (
func (c *NsxtClient) GetSegment() structs.Segments {
path := "/policy/api/v1/infra/segments"
res := c.Request("GET", path, nil, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
segments := []structs.Segment{}
body, _ := res.BodyBytes()
json.Unmarshal(body, &segments)
Expand Down
6 changes: 6 additions & 0 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func Login() error {
if err != nil {
return err
}
if site.Name == "" {
return nil
}
nsxtclient.BaseUrl = site.Endpoint
err = nsxtclient.Login(site.GetCredential())
if err != nil {
Expand All @@ -93,6 +96,9 @@ func LoginALB() error {
if err != nil {
return err
}
if albsite.Name == "" {
return nil
}
albclient.BaseUrl = albsite.Endpoint
err = albclient.Login(albsite.GetCredential())
if err != nil {
Expand Down
6 changes: 6 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ type NsxTConfig struct {
}

func (t *NsxTConfig) GetCurrentSite() (NsxTSite, error) {
if t.CurrentSite == "" {
return NsxTSite{}, nil
}
for _, s := range t.Sites {
if s.Name == t.CurrentSite {
return s, nil
Expand All @@ -41,6 +44,9 @@ type NsxAlbConfig struct {
}

func (a *NsxAlbConfig) GetCurrentSite() (NsxAlbSite, error) {
if a.CurrentSite == "" {
return NsxAlbSite{}, nil
}
for _, s := range a.Sites {
if s.Name == a.CurrentSite {
return s, nil
Expand Down
10 changes: 10 additions & 0 deletions nsxalb/gslb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,31 @@ package nsxalb

import (
"encoding/json"
"log"

"github.com/hichtakk/nsxctl/structs"
)

func (c *NsxAlbClient) GetGslbSites() structs.Gslb {
res := c.Request("GET", "/api/gslb", map[string]string{}, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
var results []structs.Gslb
str, _ := json.Marshal(res.Body.(map[string]interface{})["results"].([]interface{}))
json.Unmarshal(str, &results)

if len(results) == 0 {
return structs.Gslb{}
}
return results[0]
}

func (c *NsxAlbClient) GetGslbServices() structs.GslbServices {
res := c.Request("GET", "/api/gslbservice", map[string]string{}, nil)
if res.Error != nil {
log.Fatal(res.Error)
}
var results structs.GslbServices
str, _ := json.Marshal(res.Body.(map[string]interface{})["results"].([]interface{}))
json.Unmarshal(str, &results)
Expand Down
6 changes: 6 additions & 0 deletions nsxalb/infrastructure.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ import (

func (c *NsxAlbClient) ShowCloud() {
resp := c.Request("GET", "/api/cloud", map[string]string{}, nil)
if resp.Error != nil {
log.Fatal(resp.Error)
}
resByte, _ := resp.BodyBytes()
var res interface{}
json.Unmarshal(resByte, &res)
Expand Down Expand Up @@ -108,6 +111,9 @@ func (c *NsxAlbClient) DownloadSeImage() {
func (c *NsxAlbClient) GetServiceEngine() []structs.ServiceEngineInventory {
path := "/api/serviceengine-inventory/?&sort=name&include=config,faults,health_score,runtime&include_name=true"
resp := c.Request("GET", path, nil, nil)
if resp.Error != nil {
log.Fatal(resp.Error)
}
var results structs.SEResult
resByte, _ := resp.BodyBytes()
json.Unmarshal(resByte, &results)
Expand Down
4 changes: 4 additions & 0 deletions nsxalb/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"bytes"
"crypto/tls"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -41,6 +42,9 @@ func readResponseBody(res *http.Response) interface{} {
}

func (c *NsxAlbClient) Request(method string, path string, query_param map[string]string, req_data []byte) *client.Response {
if c.Token == "" {
return &client.Response{nil, nil, errors.New("You must be logged in to the ALB Controller (Unauthorized)")}
}
// validate path
err := func() error {
var match bool
Expand Down
10 changes: 10 additions & 0 deletions nsxalb/slb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package nsxalb

import (
"encoding/json"
"log"

"github.com/hichtakk/nsxctl/structs"
)

func (c *NsxAlbClient) ShowVirtualService() []structs.VirtualServiceInventory {
resp := c.Request("GET", "/api/virtualservice-inventory/?include_name=true", map[string]string{}, nil)
if resp.Error != nil {
log.Fatal(resp.Error)
}
var results structs.VSResult
resByte, _ := resp.BodyBytes()
json.Unmarshal(resByte, &results)
Expand All @@ -22,6 +26,9 @@ func (c *NsxAlbClient) ShowVirtualService() []structs.VirtualServiceInventory {
func (c *NsxAlbClient) GetPools() []structs.PoolInventory {
path := "/api/pool-inventory?include_name=true"
resp := c.Request("GET", path, nil, nil)
if resp.Error != nil {
log.Fatal(resp.Error)
}
var results structs.PoolResult
resByte, _ := resp.BodyBytes()
json.Unmarshal(resByte, &results)
Expand All @@ -36,6 +43,9 @@ func (c *NsxAlbClient) GetPools() []structs.PoolInventory {
func (c *NsxAlbClient) GetPool(id string) structs.Pool {
path := "/api/pool/" + id + "?include_name=true"
resp := c.Request("GET", path, nil, nil)
if resp.Error != nil {
log.Fatal(resp.Error)
}
var pool structs.Pool
resByte, _ := resp.BodyBytes()
json.Unmarshal(resByte, &pool)
Expand Down
4 changes: 4 additions & 0 deletions nsxalb/system.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ package nsxalb

import (
"encoding/json"
"log"

"github.com/hichtakk/nsxctl/structs"
)

func (c *NsxAlbClient) GetLicensingLedger() structs.LicensingLedger {
resp := c.Request("GET", "/api/licensing/ledger/details", map[string]string{}, nil)
if resp.Error != nil {
log.Fatal(resp.Error)
}
var result structs.LicensingLedger
resByte, _ := resp.BodyBytes()
json.Unmarshal(resByte, &result)
Expand Down