cancel
Showing results for 
Search instead for 
Did you mean: 
Ryan_Hill_80313
Historic F5 Account

Problem this snippet solves:

This app enables and disables nodes. It installs and runs as a stand alone windows application. It connects to an LTM, collects the pools, collects pool members, and allows the user to toggle the enabled/disabled state of nodes.

This is my first iControl App. It is also my first run at C# and Visual Studio. It is pretty basic and a little rough around the edges. I am posting it because I did not find many examples of C# using Visual Studio. I plagiarized from other code examples found on DevCentral so you may see some similarities if you look around.

The entire project is attached as a .zip.

Code :

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public class poolmember {
            public string poolname { get; set; }
            public string address { get; set; }
            public Int64 port { get; set; }
            public iControl.LocalLBSessionStatus status { get; set; } 
            public iControl.CommonAddressPort addressport { get; set; }
        }
        List _poolmember = new List();
        //iControl.CommonAddressPort my_addressport() = "";
        iControl.Interfaces interfaces = new iControl.Interfaces();
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label6_Click(object sender, EventArgs e)
        {

        }

        private void label7_Click(object sender, EventArgs e)
        {

        }

        private void label5_Click(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            lblVersion.Text = "Connecting ...";
            lblFailover.Text = "";
            lblHostname.Text = "";
            if (interfaces.initialize(txtIPAddr.Text, txtAccount.Text, txtPasswd.Text))
            {
                lblVersion.Text = interfaces.SystemSystemInfo.get_version();
                lblFailover.Text = Convert.ToString(interfaces.SystemFailover.get_failover_state());
                lblHostname.Text = Convert.ToString(interfaces.ManagementDevice.get_local_device());
                string[] pools = interfaces.LocalLBPool.get_list();
                foreach (string pool in pools)
                {
                    lstBxPools.Items.Add(pool);
                }
            }
            else
            {
                lblVersion.Text = "Connection Failure";
            }
        }

        private void lstBxPools_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstBxPools.SelectedIndex >= 0)
            {
                btnGetMembers.Enabled = true;
            }
            else
            {
                btnGetMembers.Enabled = false;
            }
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            lstbxMembers.Items.Clear();
         //   List> members = new List>();
            string[] pool = new string[1] { lstBxPools.Text };
        //    lstbxMemUp.Items.Add(lstBxPools.Text);
            iControl.CommonAddressPort[][] memberlist = interfaces.LocalLBPool.get_member_v2(pool);
            foreach (iControl.CommonAddressPort member in memberlist[0])
            {

                poolmember node = new poolmember { poolname = lstBxPools.Text, address = member.address, port = member.port, addressport = member, }; 
                _poolmember.Add(node);
            }
            foreach (poolmember member in _poolmember)
            {
                //Method requires a multi-dimensional array for input. Add CommonAddressPort to new List 
                List convertedserverlist = new List { member.addressport };
                //Convert List to Array for input into the next method 
                iControl.CommonAddressPort[] targetarry = convertedserverlist.ToArray();
                //Call the BIG-IP - Inputs are an array of Pool Names and a multi-dimensional array of CommonAddressPorts 
                iControl.LocalLBSessionStatus[][] targetstatus = interfaces.LocalLBPool.get_member_session_status(new[] { member.poolname }, new[] { targetarry });

                //Only requested a single pool, so specify the first dimension and collect data for output. 
                foreach (iControl.LocalLBSessionStatus status in targetstatus[0])
                {
                    lstbxMembers.Items.Add(member.address + ":" + member.port + " " + status);
                    member.status = status;
                } 

            }
            _poolmember.Clear();
        }

        private void btnDown_Click(object sender, EventArgs e)
        {
            if (lstbxMembers.Text.Contains("SESSION_STATUS_ENABLED"))
            {
                label7.Text = "saw Session Status ENABLED";
             //   string[] poolname = { lstBxPools.Text };
             //   interfaces.LocalLBPool.set_member_session_enabled_state(poolname[0], my_addressport[1][1], iControl.CommonEnabledState.STATE_DISABLED);
                string node = lstbxMembers.Text.Split(':')[0];
                label7.Text = node;
                string[] node_a = {node};
                iControl.CommonEnabledState[] states = new iControl.CommonEnabledState[] { iControl.CommonEnabledState.STATE_DISABLED };
                interfaces.LocalLBNodeAddressV2.set_session_enabled_state(node_a, states);
            }
            else if (lstbxMembers.Text.Contains("SESSION_STATUS_ADDRESS_DISABLED"))
            {
                string node = lstbxMembers.Text.Split(':')[0];
                string[] node_a = { node };
                iControl.CommonEnabledState[] states = new iControl.CommonEnabledState[] { iControl.CommonEnabledState.STATE_ENABLED };
                interfaces.LocalLBNodeAddressV2.set_session_enabled_state(node_a, states);

            }
            else
            {
               
            }
            lstbxMembers.Items.Clear();
            //   List> members = new List>();
            string[] pool = new string[1] { lstBxPools.Text };
            //    lstbxMemUp.Items.Add(lstBxPools.Text);
            iControl.CommonAddressPort[][] memberlist = interfaces.LocalLBPool.get_member_v2(pool);
            foreach (iControl.CommonAddressPort member in memberlist[0])
            {

                poolmember node = new poolmember { poolname = lstBxPools.Text, address = member.address, port = member.port, addressport = member, };
                _poolmember.Add(node);
            }
            foreach (poolmember member in _poolmember)
            {
                //Method requires a multi-dimensional array for input. Add CommonAddressPort to new List 
                List convertedserverlist = new List { member.addressport };
                //Convert List to Array for input into the next method 
                iControl.CommonAddressPort[] targetarry = convertedserverlist.ToArray();
                //Call the BIG-IP - Inputs are an array of Pool Names and a multi-dimensional array of CommonAddressPorts 
                iControl.LocalLBSessionStatus[][] targetstatus = interfaces.LocalLBPool.get_member_session_status(new[] { member.poolname }, new[] { targetarry });

                //Only requested a single pool, so specify the first dimension and collect data for output. 
                foreach (iControl.LocalLBSessionStatus status in targetstatus[0])
                {
                    lstbxMembers.Items.Add(member.address + ":" + member.port + " " + status);
                    member.status = status;
                }

            }
            _poolmember.Clear();
        }
    }
}
Comments
sblanchard70_24
Nimbostratus
Nimbostratus

Hello,

 

Where is the zip file ? Thanks.

 

Sebastien

 

Version history
Last update:
‎09-Mar-2015 14:19
Updated by:
Contributors