Example #1
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;
            }
            }
        }
Example #2
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);
            }
        }