diff --git a/decode.go b/decode.go index a26cb45..428f517 100644 --- a/decode.go +++ b/decode.go @@ -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") diff --git a/node.go b/node.go index e09d36c..50396e3 100644 --- a/node.go +++ b/node.go @@ -5,6 +5,7 @@ import ( "strings" ) +// Node contains information about each node type Node struct { Name string `ltm:"name"` Addr string `ltm:"addr"` @@ -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 { @@ -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.") { @@ -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 != "" { @@ -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 != "" { @@ -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 != "" { @@ -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 != "" { diff --git a/pool.go b/pool.go index 4b0c6ac..f63bbaa 100644 --- a/pool.go +++ b/pool.go @@ -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"` @@ -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"` @@ -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 { @@ -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.") { @@ -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 != "" { @@ -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 != "" { @@ -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 != "" { @@ -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 + "'" @@ -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 + " }" @@ -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 } }" @@ -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 } }" diff --git a/tmsh.go b/tmsh.go index 41a6f27..e004c35 100644 --- a/tmsh.go +++ b/tmsh.go @@ -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 { @@ -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 := "# " @@ -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 { @@ -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() } diff --git a/virtualserver.go b/virtualserver.go index 85f336e..db65d2b 100644 --- a/virtualserver.go +++ b/virtualserver.go @@ -6,6 +6,7 @@ import ( "strings" ) +// VirtualServer contains information about each virtual server type VirtualServer struct { Name string `ltm:"name"` Destination string `ltm:"destination"` @@ -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 { @@ -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.") { @@ -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 + " } }" @@ -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 != "" { @@ -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) @@ -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) @@ -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)