Example #1
0
        public async Task <IActionResult> Create([FromBody] CreateInputModel model,
                                                 [FromServices] ITenantRepository tenantRepository)
        {
            //データの入力チェック
            if (!ModelState.IsValid)
            {
                return(JsonBadRequest("Invalid inputs."));
            }

            //同じ名前のノードは登録できないので、確認する
            Node node = await nodeRepository.GetByNameAsync(model.Name);

            if (node != null)
            {
                return(JsonConflict($"Node {model.Name} already exists: ID = {node.Id}"));
            }

            node = new Node()
            {
                Name               = model.Name,
                Memo               = model.Memo,
                Partition          = model.Partition,
                AccessLevel        = model.AccessLevel == null ? NodeAccessLevel.Disabled : model.AccessLevel.Value,
                TensorBoardEnabled = model.TensorBoardEnabled
            };

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

                if (node.TensorBoardEnabled)
                {
                    await clusterManagementLogic.UpdateTensorBoardEnabledLabelAsync(node.Name, true);
                }

                if (node.AccessLevel == NodeAccessLevel.Private)
                {
                    //テナントをアサイン
                    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, true);
                    }
                }
            }

            nodeRepository.Add(node);
            unitOfWork.Commit();

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