Ejemplo n.º 1
0
 public bool IdentifyTarget(Option opt)
 {
     if (opt != null && opt.opt == 't')
     {
         Console.WriteLine(". connecting to target: {0}", opt.val);
         GetHAGroup(opt);
         RESTClient cn = new RESTClient(this);
         iCRresponse <iCRdevice> resp = cn.getQuery <iCRdevice>("/mgmt/tm/cm/device");
         if (resp == null)
         {
             return(false);
         }
         foreach (iCRdevice dev in resp.items)
         {
             if (dev.selfDevice)
             {
                 Console.WriteLine("  [name         ] : {0}", dev.hostname);
                 Console.WriteLine("  [software     ] : {0}.{1} {2}", dev.version, dev.build, dev.edition);
                 Console.WriteLine("  [failoverState] : {0}", dev.failoverState);
                 Console.WriteLine("  [mac address  ] : {0}", dev.baseMac);
                 Console.WriteLine("  [management ip] : {0}", dev.managementIp);
                 Console.WriteLine("  [cm group     ] : {0}", HAGroup);
                 return(true);
             }
         }
     }
     return(false);
 }
Ejemplo n.º 2
0
        public void ExecuteStep(string step)
        {
            switch (step)
            {
            case "LOAD_RULES": {
                upload_iRules();
                break;
            }

            case "SAVE": {
                RESTClient      cn   = new RESTClient(chnd);
                string          post = "{ \"command\": \"save\" }";
                iCRPostResponse resp = cn.postQuery <iCRPostResponse>(post, "/mgmt/tm/sys/config");
                Console.WriteLine("RESP: {0}", resp.kind);
                break;
            }

            case "SYNC": {
                RESTClient cn  = new RESTClient(chnd);
                string     grp = chnd.HAGroup;
                if (grp != null)
                {
                    string          post = "{ \"command\": \"run\", \"utilCmdArgs\": \"config-sync to-group " + grp + "\" }";
                    iCRPostResponse resp = cn.postQuery <iCRPostResponse>(post, "/mgmt/tm/cm");
                    Console.WriteLine("RESP: {0}", resp.kind);
                }
                break;
            }

            default: {
                Console.WriteLine(" !not implemented! ");
                break;
            }
            }
        }
Ejemplo n.º 3
0
 public void GetHAGroup(Option opt)
 {
     if (opt != null && opt.opt == 't')
     {
         RESTClient cn = new RESTClient(this);
         iCRresponse <iCRdevicegroup> resp = cn.getQuery <iCRdevicegroup>("/mgmt/tm/cm/device-group");
         if (resp == null)
         {
             return;
         }
         foreach (iCRdevicegroup dev in resp.items)
         {
             if (dev.type.Equals("sync-failover"))
             {
                 HAGroup = dev.name;
             }
         }
     }
 }
Ejemplo n.º 4
0
 public Target DescribeTarget(String tgt)
 {
     if (!tgt.Equals(""))
     {
         Console.WriteLine(". connecting to target: {0}", tgt);
         GetHAGroup(new Option('t', true, tgt));
         RESTClient cn = new RESTClient(this);
         iCRresponse <iCRdevice> resp = cn.getQuery <iCRdevice>("/mgmt/tm/cm/device");
         if (resp != null)
         {
             foreach (iCRdevice dev in resp.items)
             {
                 if (dev.selfDevice)
                 {
                     return(new Target(dev, HAGroup));
                 }
             }
         }
     }
     return(null);
 }
Ejemplo n.º 5
0
        public static int dumpRestTree(Config cfg, ExcelWorksheet ws, int nr, string section)
        {
            DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings {
                UseSimpleDictionaryFormat = true
            };
            var    serializer = new DataContractJsonSerializer(typeof(GenericAnswer), settings);
            string urn        = $"https://{cfg.Host}/mgmt/tm/{section}";
            Stream strm       = null;
            var    client     = new RESTClient(cfg);

            RESTClient.request(client.GetClient(), urn, "GET", "", (v) => { strm = v; return(true); }).Wait();
            if (strm != null)
            {
                try {
                    JObject resp    = JObject.Parse((new StreamReader(strm)).ReadToEnd());
                    String  pattern = $"https://.+?/{section}/(.+?)\\?.+";
                    if (resp["items"] != null)
                    {
                        foreach (JToken tk in resp["items"].Children())
                        {
                            if (tk["reference"] != null)
                            {
                                Console.WriteLine("  lnk {0}", tk["reference"]["link"]);
                                var ma = Regex.Match(tk["reference"]["link"].ToString(), pattern);
                                if (ma.Success)
                                {
                                    Console.WriteLine("++ matches: {0}", ma.Groups[1].Value);
                                    nr = dumpRestSection(cfg, ws, nr, $"{section}/{ma.Groups[1].Value}");
                                }
                            }
                        }
                    }
                } catch (Exception xx) {
                }
            }
            nr++;
            return(nr);
        }
Ejemplo n.º 6
0
        public async Task upload_iRule(string name)
        {
            var fname = bdef.options["rules_location"] + "/" + name + ".tcl";

            if (File.Exists(fname))
            {
                iRuleUpload iu = new iRuleUpload();
                iu.name      = name;
                iu.partition = "Common";
                iu.fullPath  = "/" + iu.partition + "/" + iu.name;
                using (var reader = File.OpenText(fname)) {
                    iu.apiAnonymous = await reader.ReadToEndAsync();
                }
                var serializer = new DataContractJsonSerializer(typeof(iRuleUpload));
                var stream     = new MemoryStream();
                serializer.WriteObject(stream, iu);
                stream.Position = 0;
                //Console.WriteLine("JSON: {0}", await (new StreamReader(stream)).ReadToEndAsync() );
                /* check whether given iRule exists */
                RESTClient            cn    = new RESTClient(chnd);
                bool                  isNew = true;
                iCRresponse <iCRitem> irs   = cn.getQuery <iCRitem>(
                    "/mgmt/tm/ltm/rule/~" + iu.partition + "~" + iu.name,     // + "?\\$select=name,kind,generation"
                    true
                    );
                if (irs != null && irs.kind != null)   // && irs.items[0].name.Equals(iu.name)) {
                {
                    isNew = false;
                }
                /* Upload iRule */
                if (isNew)
                {
                    iCRPostResponse resp = cn.postQuery <iCRPostResponse>(
                        await(new StreamReader(stream)).ReadToEndAsync(),
                        "/mgmt/tm/ltm/rule"
                        );
                    if (resp.apiError != null)
                    {
                        Console.WriteLine("install [FAILED]\n    {0}", resp.message);
                    }
                    else
                    {
                        Console.WriteLine("install [SUCCESS]");
                    }
                }
                else
                {
                    iCRPostResponse resp = cn.putQuery <iCRPostResponse>(
                        await(new StreamReader(stream)).ReadToEndAsync(),
                        "/mgmt/tm/ltm/rule/" + iu.name
                        );
                    if (resp.apiError != null)
                    {
                        Console.WriteLine("update [FAILED]\n    {0}", resp.message);
                    }
                    else
                    {
                        Console.WriteLine("update [SUCCESS]");
                    }
                }
            }
            else
            {
                throw new Exception("irule not found: " + fname);
            }
        }
Ejemplo n.º 7
0
        public static RFResult dumpRestRaw(Config cfg, string section, Func <Config, string, RFResult> callback = null, string[] cols = null)
        {
            var rfr = new RFResult();
            DataContractJsonSerializerSettings settings = new DataContractJsonSerializerSettings {
                UseSimpleDictionaryFormat = true
            };
            var    serializer = new DataContractJsonSerializer(typeof(GenericAnswer), settings);
            string urn        = $"https://{cfg.Host}/mgmt/tm/{section}";
            Stream strm       = null;
            var    client     = new RESTClient(cfg);

            RESTClient.request(client.GetClient(), urn, "GET", "", (v) => { strm = v; return(true); }).Wait();
            if (strm != null)
            {
                try {
                    JObject resp = JObject.Parse((new StreamReader(strm)).ReadToEnd());
                    Console.WriteLine("collection: {0}", resp["kind"]);
                    var globdict = new List <string>();
                    var list     = new List <RFRow>();
                    var banned   = new List <string>();
                    banned.Add("selfLink");
                    banned.Add("kind");
                    banned.Add("fullPath");
                    banned.Add("apiAnonymous");
                    if (resp["items"] != null)
                    {
                        foreach (JToken tk in resp["items"].Children())
                        {
                            var dict       = new Dictionary <string, string>();
                            var references = new Dictionary <string, string>();
                            //Console.WriteLine("  {0}", tk["name"]);
                            foreach (JToken ce in tk.Children())
                            {
                                if (ce.Type == JTokenType.Property)
                                {
                                    JProperty pr = ce.ToObject <JProperty>();
                                    //Console.WriteLine("    tp: {3} {0} {1} {2}", ce.Type, pr.Name, pr.Value, pr.Value.HasValues);
                                    if (!globdict.Contains(pr.Name))
                                    {
                                        if (pr.Name.EndsWith("Reference"))
                                        {
                                            references.Add(pr.Name, pr.Value["link"].ToString());
                                        }
                                        if (!banned.Contains(pr.Name) && !pr.Name.EndsWith("Reference"))
                                        {
                                            globdict.Add(pr.Name);
                                        }
                                    }
                                    if (pr.Value.HasValues)
                                    {
                                        if (pr.Value.Type == JTokenType.Array)
                                        {
                                            JArray ja = pr.Value.ToObject <JArray>();
                                            dict.Add(pr.Name, ja.ToString());
                                        }
                                        else
                                        {
                                            dict.Add(pr.Name, pr.Value.ToString());
                                        }
                                    }
                                    else
                                    {
                                        if (pr.Value.Type == JTokenType.String)
                                        {
                                            dict.Add(pr.Name, (string)pr.Value);
                                        }
                                        else
                                        {
                                            dict.Add(pr.Name, pr.Value.ToString());
                                        }
                                    }
                                }
                            }
                            var crow = new RFRow(dict);
                            if (cols != null)
                            {
                                foreach (var colref in cols)
                                {
                                    if (references[colref] != null)
                                    {
                                        crow.extra = callback(cfg, references[colref]);
                                    }
                                }
                            }
                            list.Add(crow);
                        }
                        rfr.header = globdict;
                        rfr.nodes  = list;
                    }
                } catch (Exception ex) {
                    Console.WriteLine("-- dumpRestRaw: {0}", ex.Message);
                }
            }
            return(rfr);
        }