Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            var etcdClient = new EtcdClient("http://localhost:2379");

            etcdClient.DeleteRange("/foo");
            //Console.WriteLine(etcdClient.GetVal("/foo"));
            //Console.WriteLine(etcdClient.Get("foo").Kvs.FirstOrDefault().Value.ToStringUtf8());
            etcdClient.Put($"/foo/{Guid.NewGuid()}", Guid.NewGuid().ToString());
            etcdClient.Put(new PutRequest
            {
                Key   = ByteString.CopyFromUtf8("/time"),
                Value = ByteString.CopyFromUtf8(Guid.NewGuid().ToString()),
                Lease = etcdClient.LeaseGrant(new LeaseGrantRequest
                {
                    TTL = 20
                }).ID
            });
            var response = etcdClient.GetRange("/");

            foreach (var keyValue in response.Kvs)
            {
                Console.WriteLine($"{keyValue.Key.ToStringUtf8()}:{keyValue.Value.ToStringUtf8()}");
            }
            Console.ReadKey();
        }
        public async Task ShouldDeleteRangePrefix()
        {
            await etcdClient.Put("test1", string.Empty);

            await etcdClient.Put("test2", string.Empty);

            await etcdClient.Put("test22", string.Empty);

            await etcdClient.Put("test3", string.Empty);

            await etcdClient.Put("test4", string.Empty);

            var deletedCount1 = await etcdClient.DeleteRange("test2");

            var deletedCount2 = await etcdClient.DeleteRange("test4");

            Assert.AreEqual(2, deletedCount1);
            Assert.AreEqual(1, deletedCount2);
        }
Ejemplo n.º 3
0
        public bool Delete(string key)
        {
            if (key.IndexOf('/') != 0)
            {
                key = "/" + key;
            }
            var rep = etcdClient.DeleteRange(key).Result;

            return(true);
        }
        public async Task ShouldDeleteAllKeys()
        {
            var r = new Random();
            await etcdClient.Put("test1", r.Next().ToString());

            await etcdClient.Put("test2", r.Next().ToString());

            await etcdClient.Put("test3", r.Next().ToString());

            var deletedCount = await etcdClient.DeleteRange("test");

            var get = await etcdClient.GetRange("test");

            Assert.AreEqual(3, deletedCount);
            Assert.AreEqual(0, get.Values.Count);
        }
Ejemplo n.º 5
0
        public DeleteRangeResponse DeleteRange(string key)
        {
            var rsp = client.DeleteRange(key);

            return(rsp.FromProto());
        }
Ejemplo n.º 6
0
        public static void ImportToEtcd(KeyValueData data)
        {
            string etcdUrl = $"http://{data.EtcdHost}:{data.EtcdPort}";

            $"Import to etcd {etcdUrl}".OutUnderline();

            using (var client = new EtcdClient(etcdUrl))
            {
                "Replace key-value tree".OutUnderline();
                client.DeleteRange("cfg/");

                foreach (var kv in data.Tree)
                {
                    kv.Key.Out();
                    client.Put(kv.Key, kv.Value);
                }

                "Delete users & roles".OutUnderline();
                var users = client
                            .UserList(new Etcdserverpb.AuthUserListRequest())
                            .Users
                            .ToList();

                foreach (var name in users)
                {
                    if (name != "root")
                    {
                        var reqDelUsr = new Etcdserverpb.AuthUserDeleteRequest()
                        {
                            Name = name
                        };
                        client.UserDelete(reqDelUsr);
                        $"User {name} deleted".Out();
                    }
                }

                var roles = client
                            .RoleList(new Etcdserverpb.AuthRoleListRequest())
                            .Roles
                            .ToList();

                foreach (var name in roles)
                {
                    if (name != "root")
                    {
                        var reqDelRole = new Etcdserverpb.AuthRoleDeleteRequest()
                        {
                            Role = name
                        };
                        client.RoleDelete(reqDelRole);
                        $"Role {name} deleted".Out();
                    }
                }

                "Create roles".OutUnderline();

                int i        = 0;
                var rolesDic = new Dictionary <string, string>(); // access vs role_name

                data.Users
                .Select(x => x.Access)
                .Distinct()
                .ToList()
                .ForEach(x =>
                {
                    string roleName = $"role{++i}";

                    var reqRoleAdd = new Etcdserverpb.AuthRoleAddRequest()
                    {
                        Name = roleName
                    };
                    client.RoleAdd(reqRoleAdd);
                    $"Role {roleName} created".Out();

                    var reqAddPerm = new Etcdserverpb.AuthRoleGrantPermissionRequest()
                    {
                        Name = roleName,
                        Perm = new Authpb.Permission()
                        {
                            Key      = Google.Protobuf.ByteString.CopyFromUtf8($"cfg/{x}"),
                            RangeEnd = Google.Protobuf.ByteString.CopyFromUtf8($"cfg/{x}"),
                            PermType = Authpb.Permission.Types.Type.Read
                        }
                    };
                    client.RoleGrantPermission(reqAddPerm);
                    $"Readonly access to cfg/{x} granted".Out();

                    reqAddPerm = new Etcdserverpb.AuthRoleGrantPermissionRequest()
                    {
                        Name = roleName,
                        Perm = new Authpb.Permission()
                        {
                            Key      = Google.Protobuf.ByteString.CopyFromUtf8($"app/{x}"),
                            RangeEnd = Google.Protobuf.ByteString.CopyFromUtf8($"app/{x}"),
                            PermType = Authpb.Permission.Types.Type.Readwrite
                        }
                    };
                    client.RoleGrantPermission(reqAddPerm);
                    $"Readwrite access to app/{x} granted".Out();

                    rolesDic[x] = roleName;
                });

                "Create users and grant roles".OutUnderline();

                foreach (var user in data.Users)
                {
                    var reqAddUsr = new Etcdserverpb.AuthUserAddRequest()
                    {
                        Name     = user.Name,
                        Password = user.Password
                    };

                    client.UserAdd(reqAddUsr);
                    $"User {user.Name} created".Out();

                    var reqGrantRole = new Etcdserverpb.AuthUserGrantRoleRequest()
                    {
                        User = user.Name,
                        Role = rolesDic[user.Access]
                    };
                    client.UserGrantRole(reqGrantRole);
                    $"Access to {user.Access} granted ({rolesDic[user.Access]})".Out();
                }
            }
        }