示例#1
0
        public async Task <IActionResult> Edit(long?id, [FromBody] CreateInputModel model,
                                               [FromServices] ITenantRepository tenantRepository) //EditとCreateで項目が同じなので、入力モデルを使いまわし
        {
            //データの入力チェック
            if (!ModelState.IsValid || !id.HasValue)
            {
                return(JsonBadRequest("Invalid inputs."));
            }
            //データの存在チェック
            var node = await nodeRepository.GetByIdAsync(id.Value);

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

            //同じ名前のノードは登録できないので、確認する
            if (await nodeRepository.ExistsAsync(n => n.Id != node.Id && n.Name == model.Name))
            {
                return(JsonConflict($"Node {model.Name} already exists: ID = {node.Id}"));
            }

            //NodeはCLIではなく画面から変更されるので、常にすべての値を入れ替える
            node.Name               = model.Name;
            node.Memo               = model.Memo;
            node.Partition          = model.Partition;
            node.TensorBoardEnabled = model.TensorBoardEnabled;
            node.NotebookEnabled    = model.NotebookEnabled;
            node.AccessLevel        = model.AccessLevel.Value;

            if (node.AccessLevel != NodeAccessLevel.Disabled)
            {
                //アクセスレベルがDisable以外であれば、k8sとの同期を行う
                //ノードが存在しない場合を考慮し、もし失敗しても気にせず更新処理を続ける
                //代わりに、前回成功している確証がないので、値が変更前と同じでも更新処理は行う
                await clusterManagementLogic.UpdatePartitionLabelAsync(node.Name, node.Partition);

                await clusterManagementLogic.UpdateTensorBoardEnabledLabelAsync(node.Name, node.TensorBoardEnabled);

                await clusterManagementLogic.UpdateNotebookEnabledLabelAsync(node.Name, node.NotebookEnabled);

                if (node.AccessLevel == NodeAccessLevel.Private)
                {
                    //テナントをアサイン

                    //まずは全てのアサイン情報を削除する
                    nodeRepository.ResetAssinedTenants(node.Id);

                    //アサイン
                    if (model.AssignedTenantIds != null)
                    {
                        foreach (long tenantId in model.AssignedTenantIds)
                        {
                            if (tenantRepository.Get(tenantId) == null)
                            {
                                return(JsonNotFound($"Tenant ID {tenantId} is not found."));
                            }
                        }
                        nodeRepository.AssignTenants(node, model.AssignedTenantIds, false);
                    }
                }
            }

            unitOfWork.Commit();

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