Skip to content

Commit

Permalink
Add godoc
Browse files Browse the repository at this point in the history
  • Loading branch information
yukirii committed Feb 14, 2018
1 parent 0e2b864 commit 3a2c8e7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions decode.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
)

// Unmarshal parses the 'field-fmt' formatted data and stores the result in the value pointed to by out.
func Unmarshal(data string, out interface{}) error {
data = strings.Trim(data, "\n")

Expand Down
7 changes: 7 additions & 0 deletions node.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"strings"
)

// Node contains information about each node
type Node struct {
Name string `ltm:"name"`
Addr string `ltm:"addr"`
Expand All @@ -13,6 +14,7 @@ type Node struct {
EnabledState string `ltm:"status.enabled-state"`
}

// GetAllNodes returns a list of all nodes.
func (bigip *BigIP) GetAllNodes() ([]Node, error) {
ret, err := bigip.ExecuteCommand("show ltm node field-fmt")
if err != nil {
Expand All @@ -31,6 +33,7 @@ func (bigip *BigIP) GetAllNodes() ([]Node, error) {
return nodes, nil
}

// GetNode gets a node by name. Return nil if the node does not exist.
func (bigip *BigIP) GetNode(name string) (*Node, error) {
ret, _ := bigip.ExecuteCommand("show ltm node " + name + " field-fmt")
if strings.Contains(ret, "was not found.") {
Expand All @@ -45,6 +48,7 @@ func (bigip *BigIP) GetNode(name string) (*Node, error) {
return &node, nil
}

// CreateNode creates a new node.
func (bigip *BigIP) CreateNode(name, ipaddr string) error {
ret, _ := bigip.ExecuteCommand("create ltm node " + name + " address " + ipaddr)
if ret != "" {
Expand All @@ -53,6 +57,7 @@ func (bigip *BigIP) CreateNode(name, ipaddr string) error {
return nil
}

// DeleteNode removes a node.
func (bigip *BigIP) DeleteNode(name string) error {
ret, _ := bigip.ExecuteCommand("delete ltm node " + name)
if ret != "" {
Expand All @@ -61,6 +66,7 @@ func (bigip *BigIP) DeleteNode(name string) error {
return nil
}

// EnableNode changes the status of a node to enable.
func (bigip *BigIP) EnableNode(name string) error {
ret, _ := bigip.ExecuteCommand("modify ltm node " + name + " session user-enabled")
if ret != "" {
Expand All @@ -69,6 +75,7 @@ func (bigip *BigIP) EnableNode(name string) error {
return nil
}

// DisableNode changes the status of a node to disable.
func (bigip *BigIP) DisableNode(name string) error {
ret, _ := bigip.ExecuteCommand("modify ltm node " + name + " session user-disabled")
if ret != "" {
Expand Down
11 changes: 11 additions & 0 deletions pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
)

// Pool contains information about each Pool
type Pool struct {
ActiveMemberCount int `ltm:"active-member-cnt"`
Name string `ltm:"name"`
Expand All @@ -16,6 +17,7 @@ type Pool struct {
PoolMembers []PoolMember `ltm:"members"`
}

// Pool contains information about each pool member in a pool
type PoolMember struct {
Name string `ltm:"node-name"`
Addr string `ltm:"addr"`
Expand All @@ -27,6 +29,7 @@ type PoolMember struct {
StatusReason string `ltm:"status.status-reason"`
}

// GetAllPools returns a list of all pools
func (bigip *BigIP) GetAllPools() ([]Pool, error) {
ret, err := bigip.ExecuteCommand("show ltm pool members field-fmt")
if err != nil {
Expand All @@ -45,6 +48,7 @@ func (bigip *BigIP) GetAllPools() ([]Pool, error) {
return pools, nil
}

// GetPool gets a pool by name. Return nil if the pool does not exist.
func (bigip *BigIP) GetPool(name string) (*Pool, error) {
ret, _ := bigip.ExecuteCommand("show ltm pool " + name + " members field-fmt")
if strings.Contains(ret, "was not found.") {
Expand All @@ -59,6 +63,7 @@ func (bigip *BigIP) GetPool(name string) (*Pool, error) {
return &pool, nil
}

// CreatePool creates a new pool.
func (bigip *BigIP) CreatePool(name string) error {
ret, _ := bigip.ExecuteCommand("create ltm pool " + name)
if ret != "" {
Expand All @@ -67,6 +72,7 @@ func (bigip *BigIP) CreatePool(name string) error {
return nil
}

// DeletePool removes a pool.
func (bigip *BigIP) DeletePool(name string) error {
ret, _ := bigip.ExecuteCommand("delete ltm pool " + name)
if ret != "" {
Expand All @@ -75,6 +81,7 @@ func (bigip *BigIP) DeletePool(name string) error {
return nil
}

// DeletePoolMember adds a monitor to pool.
func (bigip *BigIP) AddMonitorToPool(poolName, monitorName string) error {
ret, _ := bigip.ExecuteCommand("modify ltm pool " + poolName + " monitor '" + monitorName + "'")
if ret != "" {
Expand All @@ -83,6 +90,7 @@ func (bigip *BigIP) AddMonitorToPool(poolName, monitorName string) error {
return nil
}

// DeletePoolMember adds a new pool member.
func (bigip *BigIP) AddPoolMember(poolName, nodeName, monitorName string, port int) error {
member := nodeName + ":" + strconv.Itoa(port)
cmd := "modify ltm pool " + poolName + " members add { " + member + " } monitor '" + monitorName + "'"
Expand All @@ -93,6 +101,7 @@ func (bigip *BigIP) AddPoolMember(poolName, nodeName, monitorName string, port i
return nil
}

// DeletePoolMember removes a pool member.
func (bigip *BigIP) DeletePoolMember(poolName, nodeName string, port int) error {
member := nodeName + ":" + strconv.Itoa(port)
cmd := "modify ltm pool " + poolName + " members delete { " + member + " }"
Expand All @@ -103,6 +112,7 @@ func (bigip *BigIP) DeletePoolMember(poolName, nodeName string, port int) error
return nil
}

// EnablePoolMember changes the status of pool member to enable.
func (bigip *BigIP) EnablePoolMember(poolName, nodeName string, port int) error {
member := nodeName + ":" + strconv.Itoa(port)
cmd := "modify ltm pool " + poolName + " members modify { " + member + " { session user-enabled } }"
Expand All @@ -113,6 +123,7 @@ func (bigip *BigIP) EnablePoolMember(poolName, nodeName string, port int) error
return nil
}

// DisablePoolMember changes the status of pool member to disable.
func (bigip *BigIP) DisablePoolMember(poolName, nodeName string, port int) error {
member := nodeName + ":" + strconv.Itoa(port)
cmd := "modify ltm pool " + poolName + " members modify { " + member + " { session user-disabled } }"
Expand Down
5 changes: 5 additions & 0 deletions tmsh.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import (
"strings"
)

// BigIP is a struct for session state
type BigIP struct {
host string
user string
sshconn SSH
}

// NewSession sets up new SSH session to BIG-IP TMSH
func NewSession(host, port, user, password string) (*BigIP, error) {
sshconn, err := newSSHConnection(host+":"+port, user, password)
if err != nil {
Expand Down Expand Up @@ -46,6 +48,7 @@ func NewSession(host, port, user, password string) (*BigIP, error) {
return bigip, nil
}

// ExecuteCommand is used to execute any TMSH commands
func (bigip *BigIP) ExecuteCommand(cmd string) (string, error) {
promptSuffix := "# "

Expand Down Expand Up @@ -81,6 +84,7 @@ func (bigip *BigIP) ExecuteCommand(cmd string) (string, error) {
return strings.Join(lines, "\n"), nil
}

// Save is used to execute 'save /sys config' command
func (bigip *BigIP) Save() error {
ret, err := bigip.ExecuteCommand("save /sys config current-partition")
if err != nil {
Expand All @@ -101,6 +105,7 @@ func (bigip *BigIP) Save() error {
return nil
}

// Close is used to close SSH session
func (bigip *BigIP) Close() {
bigip.sshconn.Close()
}
8 changes: 8 additions & 0 deletions virtualserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"strings"
)

// VirtualServer contains information about each virtual server
type VirtualServer struct {
Name string `ltm:"name"`
Destination string `ltm:"destination"`
Expand All @@ -20,6 +21,7 @@ type Profile struct {
Context string `ltm:"context"`
}

// GetAllVirtualServers returns a list of all virtual servers
func (bigip *BigIP) GetAllVirtualServers() ([]VirtualServer, error) {
ret, err := bigip.ExecuteCommand("list ltm virtual all-properties")
if err != nil {
Expand All @@ -38,6 +40,7 @@ func (bigip *BigIP) GetAllVirtualServers() ([]VirtualServer, error) {
return vss, nil
}

// GetVirtualServer gets a virtual server by name. Rerutn nil if the virtual server does not found.
func (bigip *BigIP) GetVirtualServer(name string) (*VirtualServer, error) {
ret, _ := bigip.ExecuteCommand("list ltm virtual " + name)
if strings.Contains(ret, "was not found.") {
Expand All @@ -52,6 +55,7 @@ func (bigip *BigIP) GetVirtualServer(name string) (*VirtualServer, error) {
return &vs, nil
}

// CreateVirtualServer creates a virtual server.
func (bigip *BigIP) CreateVirtualServer(vsName, poolName, targetVIP, defaultProfileName string, targetPort int) error {
destination := targetVIP + ":" + strconv.Itoa(targetPort)
cmd := "create ltm virtual " + vsName + " { destination " + destination + " ip-protocol tcp mask 255.255.255.255 pool " + poolName + " profiles add { " + defaultProfileName + " } }"
Expand All @@ -62,6 +66,7 @@ func (bigip *BigIP) CreateVirtualServer(vsName, poolName, targetVIP, defaultProf
return nil
}

// DeleteVirtualServer removes a virtual server.
func (bigip *BigIP) DeleteVirtualServer(vsName string) error {
ret, _ := bigip.ExecuteCommand("delete ltm virtual " + vsName)
if ret != "" {
Expand All @@ -70,6 +75,7 @@ func (bigip *BigIP) DeleteVirtualServer(vsName string) error {
return nil
}

// AddVirtualServerProfile adds a profile to a virtual server.
func (bigip *BigIP) AddVirtualServerProfile(vsName, profileName, context string) error {
cmd := "modify ltm virtual " + vsName + " profiles add { " + profileName + " { context " + context + " } }"
ret, _ := bigip.ExecuteCommand(cmd)
Expand All @@ -79,6 +85,7 @@ func (bigip *BigIP) AddVirtualServerProfile(vsName, profileName, context string)
return nil
}

// DeleteVirtualServerProfile removes a profile from a virtual server.
func (bigip *BigIP) DeleteVirtualServerProfile(vsName, profileName, context string) error {
cmd := "modify ltm virtual " + vsName + " profiles delete { " + profileName + " }"
ret, _ := bigip.ExecuteCommand(cmd)
Expand All @@ -88,6 +95,7 @@ func (bigip *BigIP) DeleteVirtualServerProfile(vsName, profileName, context stri
return nil
}

// ApplyPolicyToVirtualServer applies a policy to a virtual server.
func (bigip *BigIP) ApplyPolicyToVirtualServer(vsName, policyName string) error {
cmd := "modify ltm virtual " + vsName + " fw-enforced-policy " + policyName
ret, _ := bigip.ExecuteCommand(cmd)
Expand Down

0 comments on commit 3a2c8e7

Please sign in to comment.