Exemplo n.º 1
0
        public IActionResult EditRegistryToken([FromBody] RegistryCredentialInputModel model,
                                               [FromServices] IRegistryRepository registryRepository,
                                               [FromServices] IClusterManagementLogic clusterManagementLogic)
        {
            //入力値チェック
            if (!ModelState.IsValid)
            {
                return(JsonBadRequest("Invalid inputs."));
            }

            var tenantId = CurrentUserInfo.SelectedTenant.Id;

            //まずは今の情報を取得
            var userRegistryMap = registryRepository.GetUserTenantRegistryMap(CurrentUserInfo.Id, tenantId, model.Id);

            if (userRegistryMap == null)
            {
                //今の値がない場合、紐づけられていない情報を変更しようとしているとみなす。
                return(JsonBadRequest("Couldn't map the registry to the current user & tenant. Please contact a user administrator."));
            }

            //変更
            userRegistryMap.RegistryUserName = model.UserName;
            userRegistryMap.RegistryPassword = model.Password;

            //シークレットを登録する。
            clusterManagementLogic.RegistRegistryToTenantAsync(CurrentUserInfo.SelectedTenant.Name, userRegistryMap);

            unitOfWork.Commit();

            RegistryCredentialOutputModel result = new RegistryCredentialOutputModel(userRegistryMap);

            return(JsonOK(result));
        }
Exemplo n.º 2
0
        /// <summary>
        /// アクアリウム用レジストリシークレットをk8sに登録する
        /// </summary>
        /// <returns></returns>
        private async Task <string> RegistRegistryToTenantAsync(Registry registry, string token)
        {
            var registryMap        = registryLogic.GetCurrentRegistryMap(registry.Id);
            var tokenKey           = $"aqiarium-registry-{registry.Name}-{CurrentUserInfo.Id}";
            var url                = registry.RegistryUrl;
            var selectedTenantName = CurrentUserInfo.SelectedTenant.Name;
            var userName           = "";
            var password           = token ?? registryMap?.RegistryPassword ?? "";

            var result = await clusterManagementLogic.RegistRegistryToTenantAsync(tokenKey, url, registry, selectedTenantName,
                                                                                  userName, password);

            return(result ? tokenKey : null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// 指定したユーザをテナントに新規登録する。
        /// 途中でエラーが発生した場合、そのエラー結果が返る。NULLなら成功。
        /// </summary>
        private async Task <IActionResult> AddTenantAsync(User user, Tenant tenant, IEnumerable <long> tenantRoleIds, bool isCreate)
        {
            //ロールについての存在&入力チェック
            var roles = new List <Role>();

            if (tenantRoleIds != null)
            {
                foreach (long roleId in tenantRoleIds)
                {
                    var role = await roleRepository.GetRoleAsync(roleId);

                    if (role == null)
                    {
                        //ロールがない
                        return(JsonNotFound($"Role ID {roleId} is not found."));
                    }
                    if (role.IsSystemRole)
                    {
                        //システムロールをテナントロールとして追加しようとしている
                        return(JsonBadRequest($"The system role {role.Name} is not assigned to a user as a tenant role."));
                    }
                    roles.Add(role);
                }
            }

            var maps = userRepository.AttachTenant(user, tenant.Id, roles, isCreate);

            if (maps != null)
            {
                foreach (var map in maps)
                {
                    //レジストリを登録
                    var registryResult = await clusterManagementLogic.RegistRegistryToTenantAsync(tenant.Name, map);

                    if (registryResult == false)
                    {
                        return(JsonError(HttpStatusCode.ServiceUnavailable, "Couldn't map the tenant and the registry in a cluster management service. Please contact a user administrator."));
                    }
                }
            }

            return(null);
        }
Exemplo n.º 4
0
        /// <summary>
        /// 指定したユーザをテナントに新規登録する。
        /// 途中でエラーが発生した場合、そのエラー結果が返る。NULLなら成功。
        /// </summary>
        private async Task <IActionResult> AddTenantAsync(User user, Tenant tenant, IEnumerable <long> tenantRoleIds)
        {
            //ロールについての存在&入力チェック
            var roles = new List <Role>();

            if (tenantRoleIds != null)
            {
                foreach (long roleId in tenantRoleIds)
                {
                    var role = await roleRepository.GetRoleAsync(roleId);

                    if (role == null)
                    {
                        //ロールがない
                        return(JsonNotFound($"Role ID {roleId} is not found."));
                    }
                    if (role.IsSystemRole)
                    {
                        //システムロールをテナントロールとして追加しようとしている
                        return(JsonBadRequest($"The system role {role.Name} is not assigned to a user as a tenant role."));
                    }
                    roles.Add(role);
                }
            }

            var maps = userRepository.AttachTenant(user, tenant.Id, roles);

            if (maps != null)
            {
                foreach (var map in maps)
                {
                    //レジストリを登録
                    await clusterManagementLogic.RegistRegistryToTenantAsync(tenant.Name, map);
                }
            }

            return(null);
        }
Exemplo n.º 5
0
        public async Task <IActionResult> Edit(long?id, [FromBody] EditInputModel model)
        {
            //データの入力チェック
            if (!ModelState.IsValid || !id.HasValue)
            {
                return(JsonBadRequest("Invalid inputs."));
            }
            //データの存在チェック
            var tenant = await tenantRepository.GetTenantWithStorageForUpdateAsync(id.Value);

            if (tenant == null)
            {
                return(JsonNotFound($"Tenant ID {id.Value} is not found."));
            }

            if (model.DefaultGitId != null && model.GitIds.Contains(model.DefaultGitId.Value) == false)
            {
                //デフォルトGitがGit一覧の中になかったらエラー
                return(JsonConflict($"Default Git ID {model.DefaultGitId.Value} does NOT exist in selected gits."));
            }
            if (model.DefaultRegistryId != null && model.RegistryIds.Contains(model.DefaultRegistryId.Value) == false)
            {
                //デフォルトレジストリがレジストリ一覧の中になかったらエラー
                return(JsonConflict($"Default Registry ID {model.DefaultRegistryId.Value} does NOT exist in selected registries."));
            }
            if (model.StorageId != null)
            {
                //データの存在チェック
                var storage = tenantRepository.GetStorage(model.StorageId.Value);
                if (storage == null)
                {
                    return(JsonNotFound($"The selected storage ID {model.StorageId.Value} is not found."));
                }

                //バケットを作成する
                await storageLogic.CreateBucketAsync(tenant, storage);
            }

            tenant.DisplayName = model.DisplayName;
            tenant.StorageId   = model.StorageId;

            //コンテナ管理サービス作業
            //テナントを登録
            var tenantResult = await clusterManagementLogic.RegistTenantAsync(tenant.Name);

            if (tenantResult == false)
            {
                return(JsonError(HttpStatusCode.ServiceUnavailable, "Couldn't create cluster master namespace. Please check the configuration to the connect cluster manager service."));
            }

            //テナントとGitを紐づけ
            //まずは現状のGitを取得して、そこから増減を判断する
            var currentGits = gitRepository.GetGitAll(tenant.Id).ToList();

            if (model.GitIds != null && model.GitIds.Count() > 0)
            {
                //デフォルトGitの設定(無ければ一個目)
                tenant.DefaultGitId = model.DefaultGitId == null?
                                      model.GitIds.ElementAt(0) : model.DefaultGitId.Value;

                foreach (long gitId in model.GitIds)
                {
                    Git currentGit = currentGits.FirstOrDefault(r => r.Id == gitId);
                    if (currentGit != null)
                    {
                        //以前も紐づいていたので、無視。
                        currentGits.Remove(currentGit);
                        continue;
                    }

                    //データの存在チェック
                    Git git = await gitRepository.GetByIdAsync(gitId);

                    if (git == null)
                    {
                        return(JsonNotFound($"The selected git ID {gitId} is not found."));
                    }

                    await gitRepository.AttachGitToTenantAsync(tenant, git, false);
                }
            }
            //残っているのは削除された紐づけなので、消す
            foreach (var removedGit in currentGits)
            {
                gitRepository.DetachGitFromTenant(tenant, removedGit);
            }

            //テナントとレジストリを紐づけ
            //まずは現状のレジストリを取得して、そこから増減を判断する
            var currentRegistries = registryRepository.GetRegistryAll(tenant.Id).ToList();

            if (model.RegistryIds != null && model.RegistryIds.Count() > 0)
            {
                //デフォルトレジストリの設定(無ければ一個目)
                tenant.DefaultRegistryId = model.DefaultRegistryId == null?
                                           model.RegistryIds.ElementAt(0) : model.DefaultRegistryId.Value;

                foreach (long registryId in model.RegistryIds)
                {
                    Registry currentRegistry = currentRegistries.FirstOrDefault(r => r.Id == registryId);
                    if (currentRegistry != null)
                    {
                        //以前も紐づいていたので、無視。
                        currentRegistries.Remove(currentRegistry);
                        continue;
                    }

                    //データの存在チェック
                    Registry registry = await registryRepository.GetByIdAsync(registryId);

                    if (registry == null)
                    {
                        return(JsonNotFound($"The selected registry ID {registryId} is not found."));
                    }

                    var maps = await registryRepository.AttachRegistryToTenantAsync(tenant, registry, false);

                    if (maps != null)
                    {
                        foreach (var map in maps)
                        {
                            //レジストリを登録
                            var registryResult = await clusterManagementLogic.RegistRegistryToTenantAsync(tenant.Name, map);

                            if (registryResult == false)
                            {
                                return(JsonError(HttpStatusCode.ServiceUnavailable, "Couldn't map the tenant and the registry in a cluster management service. Please check the configuration to the connect cluster manager service."));
                            }
                        }
                    }
                }
            }
            //残っているのは削除された紐づけなので、消す
            foreach (var removedRegistry in currentRegistries)
            {
                registryRepository.DetachRegistryFromTenant(tenant, removedRegistry);
            }

            // 関連するクラスタトークンをリセット
            tenantRepository.DeleteClusterToken(tenant.Id);

            tenantRepository.Update(tenant, unitOfWork);

            return(JsonOK(new IndexOutputModel(tenant)));
        }